Python MCQs - Data Types, Operators and Type Conversion (Set 1)

Python MCQs - Data Types, Operators and Type Conversion (Set 1) - CodingHumans

This set tests the foundations every Python program rests on: built-in data types, variables and identifiers, arithmetic, logical and bitwise operators, precedence rules, implicit and explicit type conversion, and the behaviour of input() and print(). Many questions ask you to predict exact output, the format used in university practicals, campus placement tests and technical interviews. Work through them to catch the small surprises, such as floor division with negatives, before they cost you marks.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.

Python MCQ series: Set 1 | Set 2 | Set 3 | Set 4 | Set 5 | Set 6

Topics covered in this set

  • Built-in data types and truthiness
  • Variables and valid identifiers
  • Arithmetic, floor division and modulo
  • Operator precedence and associativity
  • Logical, comparison and bitwise operators
  • Type conversion with int(), float(), bool() and str()
  • input() and print() with sep and end

Python MCQs with answers and explanations

Q1. What is the output of the following code?

print(7 // -2, -7 % 3)

a) -4 2

b) -3 2

c) -4 -1

d) -3 -1

Q2. What is the output of the following code?

print(2 ** 3 ** 2)

a) 64

b) 512

c) 36

d) SyntaxError

Q3. What is the output of the following code?

print(True + True * 3)

a) 6

b) True

c) TypeError

d) 4

Q4. What is the output of the following code?

x = "5"
y = 2
print(x * y)

a) 10

b) 55

c) TypeError

d) 52

Q5. What is the output of the following code?

print(int("0x1A", 16))

a) ValueError

b) 1A

c) 26

d) 16

Q6. What is the output of the following code?

print(bool("False"), bool(0.0), bool([0]))

a) False False False

b) True False True

c) False False True

d) True False False

Q7. What is the output of the following code?

print(round(2.5), round(3.5), round(-0.5))

a) 3 4 -1

b) 2 4 0

c) 3 4 0

d) 2 3 -1

Q8. What is the output of the following code?

print(0.1 + 0.2 == 0.3, round(0.1 + 0.2, 2) == 0.3)

a) True True

b) False False

c) True False

d) False True

Q9. What is the output of the following code?

print(not 1 or 0 and 5)

a) 0

b) 5

c) True

d) False

Q10. What is the output of the following code?

print(3 > 2 > 1, (3 > 2) > 1)

a) True False

b) True True

c) False False

d) False True

Q11. What is the output of the following code?

print(type(10 / 2).__name__, 10 // 2.0)

a) int 5

b) float 5.0

c) float 5

d) int 5.0

Q12. What is the output of the following code?

print("a", "b", "c", sep="-", end="!")
print("d")

a) a b c!d

b) a-b-c-!d

c) a-b-c! d

d) a-b-c!d

Q13. What is the type of the value returned by input() when the user types 42 and presses Enter?

a) str

b) int

c) float

d) It depends on what the user types

Q14. Which of the following is NOT a valid Python identifier?

a) _value2

b) value_2nd

c) 2nd_value

d) __value__

Q15. Which of these built-in types is immutable?

a) tuple

b) list

c) dict

d) set

Q16. What is the output of the following code?

print(int(-3.9), int("  42 "), divmod(-17, 5))

a) -4 42 (-3, -2)

b) -3 42 (-3, -2)

c) -3 42 (-4, 3)

d) -4 ValueError (-4, 3)

Q17. What is the output of the following code?

print(5 & 3, 5 | 3, 5 ^ 3, ~5)

a) 1 7 6 -5

b) 1 7 2 -6

c) 0 7 6 6

d) 1 7 6 -6

Q18. What is the output of the following code?

x = -2 ** 2
y = (-2) ** 2
print(x, y)

a) 4 4

b) -4 -4

c) -4 4

d) 4 -4

Quick revision notes

  • // is floor division: it rounds towards negative infinity, so 7 // -2 is -4.
  • The sign of a % b follows the divisor b, and a == (a // b) * b + a % b always holds.
  • ** is right-associative and binds tighter than unary minus: -2 ** 2 is -4.
  • / always returns a float in Python 3, even when both operands are int.
  • round() uses banker's rounding: halves go to the nearest even integer.
  • Falsy values: 0, 0.0, '', [], (), {}, set(), None and False; everything else is truthy.
  • and / or return one of their operands, not necessarily True or False.
  • input() always returns a str; convert it explicitly with int() or float().

Frequently asked questions

Why does 7 // -2 give -4 in Python?

Python's floor division rounds the true quotient (-3.5) down towards negative infinity, giving -4. Languages like C truncate towards zero instead, which is why the result surprises many learners.

Is Python dynamically typed or strongly typed?

Both. Types are attached to objects and checked at run time (dynamic typing), but Python will not silently mix incompatible types such as '5' + 2, raising a TypeError instead (strong typing).

Why is 0.1 + 0.2 not equal to 0.3 in Python?

Floats are stored in binary (IEEE 754), and 0.1 and 0.2 have no exact binary representation, so their sum is 0.30000000000000004. Use math.isclose() or the decimal module when exact comparison matters.

Comments

Answered 0/0Correct 0