Python Code Snippets MCQ

OticRose avatar
OticRose
·
·
Download

Start Quiz

Study Flashcards

30 Questions

What is the output of $23+(5+6)(1+1)?$

17

What will be the data type of 'var' after the following code snippet?

str and int

What will be the output of the following code snippet?

a = [1, 2, 3] 
a = tuple(a) 
a = 2 
print(a) 

Error.

What is the output of $5/2$ and $5//2$?

float and int

What will be the output of the following code snippet?

a = [1, 2, 3, 4, 5] 
sum = 0 
for ele in a: 
    sum += ele 
print(sum) 

15

What will be the output of the following code snippet?

a=3 
b=1 
print(a, b) 
a, b = b, a 
print(a, b) 

(13, 31)

Which of the following is an invalid variable name in Python?

1st_string

What is the result of $22 ext{ % }3$ in Python?

1

Which of the following CANNOT be a valid variable name in Python?

in

What is the correct operator for power($x^y$) in Python?

$X**y$

Which of the following statements is invalid in Python?

a b c = 1000 2000 3000

Which operator has the highest precedence in a Python expression?

Exponential

What data type is the object L = [1, 23, 'hello', 1]?

list

What is the output of the following Python code? i=1 while True: if i%3 == 0: break print(i) i+=1

1 2 3

Which of the following Python code snippets results in a SyntaxError?

'”That’s okay”'

What will be the average value of the following Python code snippet? >>>grade1 = 80 >>>grade2 = 90 >>>average = (grade1 + grade2) / 2

85.0

What will be the output of the following Python code? i=1 while True: if i%2 == 0: break print(i) i += 2

1

What will be the output of the following Python code? i=1 while False: if i%2 == 0: break print(i) i += 2

1

What will be the output of the following Python code? print('xyyzxyzxzxyy'.count('xy'))

4

What will be the output of the following Python code? print('abcdef'.index('f'))

5

What will be the output of the following Python code? print('123abc'.isdigit())

False

What will be the output of the following Python code? print('Hello'.replace('l', 'x'))

'Hexxo'

What will be the output of the following Python code? print('HeLLoWoRLd'.swapcase())

'hEllOwOrlD'

What will be the output of the following Python code? print('Python is fun'.split())

['Python', 'is', 'fun']

What is the output of len(list1) if list1 is ['apple', 'banana', 'cherry']?

3

In list1 = [2, 4, 6, 8, 10], what does list1[2:4] return?

[6, 8]

If list1 = [1, 2, 3] and list2 = [4, 5], what is the result of list1 + list2?

[1, 2, 3, 4, 5]

How can an element be removed by value from a list in Python?

list1.remove(5)

What is the correct way to copy the contents of list1 into a new list called list2?

list2 = list1.copy()

If list1 = [10, 20, 30] and list2 = list1, what happens if we modify list2[0] to 5?

list1 becomes [5, 20, 30]

Study Notes

Python Basics

  • In Python, 2**3 + (5 + 6)**(1 + 1) evaluates to 129
  • print(type(var)) will output str or int depending on the type of var
  • a = [1, 2, 3]; a = tuple(a) converts a list to a tuple
  • print(type(5 / 2)) outputs float and print(type(5 // 2)) outputs int
  • a = [1, 2, 3, 4, 5]; sum = 0; for ele in a: sum += ele; print(sum) outputs 15
  • a, b = b, a swaps the values of a and b

Loops and Control Structures

  • while True: creates an infinite loop
  • break statement exits a loop
  • if i%3 == 0: break exits a loop when i is a multiple of 3
  • if i%2 == 0: break exits a loop when i is even

Data Types and Operations

  • L = [1, 23, 'hello', 1] is a list
  • dict is used to store values in key-value pairs
  • average = (grade1 + grade2) / 2 calculates the average of two numbers
  • str.endswith("xyy") checks if a string ends with "xyy"
  • str.find("cd") finds the index of the first occurrence of "cd" in a string
  • str.format is used to format strings

Functions and Operators

  • % is the modulus operator
  • ** is the exponentiation operator
  • int(x) converts a value to an integer
  • str() converts a value to a string
  • print("xyyzxyzxzxyy".endswith("xyy")) checks if a string ends with "xyy"
  • print("abcdef".find("cd")) finds the index of the first occurrence of "cd" in a string

Lists and Tuples

  • list[index] accesses an element in a list
  • list[index:] slices a list from index to the end
  • list1 * 2 concatenates a list with itself
  • list1.append(5) adds an element to the end of a list
  • list1.insert(2, 5) inserts an element at a specific position in a list

Test your knowledge of Python with these multiple-choice questions covering topics such as arithmetic operations, data types, and data structures. See if you can predict the output of various code snippets and identify the data types of variables.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser