Editor

Python basics MCQ questions with answers

Variables, data types, type conversion and print: pick an answer to see why it is right.

0 of 20 answered

  1. Question 1 · Easy

    What is the output of this code?

    x = 5
    y = "5"
    print(type(x), type(y))
    Show the answer

    Answer: B

    type() returns a class, which prints as <class 'int'>. The quotes make y a string, so its type is str. The <type ...> form is how Python 2 printed types.

    Run this code in the editor
  2. Question 2 · Easy

    What is the output of this code?

    print("A", "B", "C", sep="-", end="|")
    print("D")
    Show the answer

    Answer: A

    sep goes between the values and end replaces the usual newline, so the first call prints A-B-C| without a line break. The second print continues on the same line.

    Run this code in the editor
  3. Question 3 · Easy

    What is the output of this code?

    print(int(7.9), int(-7.9))
    Show the answer

    Answer: C

    int() drops the fractional part, which truncates toward zero: 7.9 becomes 7 and -7.9 becomes -7. It does not round, and it does not floor -7.9 down to -8.

    Run this code in the editor
  4. Question 4 · Easy

    What is the output of this code?

    a = "3"
    b = "4"
    print(a + b)
    print(int(a) + int(b))
    Show the answer

    Answer: D

    + on two strings joins them, giving 34. After int() converts both values, + adds numbers and gives 7.

    Run this code in the editor
  5. Question 5 · Easy

    What is the output of this code?

    price = 49.5
    qty = 3
    print(f"Total: {price * qty:.2f}")
    Show the answer

    Answer: B

    The f-string evaluates price * qty (148.5), and the format spec .2f shows exactly two decimal places, so it prints 148.50. Without the f prefix the braces would print as typed.

    Run this code in the editor
  6. Question 6 · Medium

    What is the output of this code?

    print(bool("False"), bool(0.0), bool(" "))
    Show the answer

    Answer: C

    Any non-empty string is truthy, even "False" and a single space; only the empty string "" is falsy. 0.0 is zero, so bool(0.0) is False.

    Run this code in the editor
  7. Question 7 · Medium

    What is the output of this code?

    a, b = 10, 20
    a, b = b, a + b
    print(a, b)
    Show the answer

    Answer: D

    The right-hand side is evaluated in full before anything is assigned, so a + b uses the old values (10 + 20). Doing the two assignments one after the other would give 20 40.

    Run this code in the editor
  8. Question 8 · Medium

    What is the output of this code?

    items = ["pen", "ink", "nib"]
    print(*items, sep=", ")
    print(items)
    Show the answer

    Answer: A

    *items unpacks the list into three separate arguments, so sep is placed between them. The second print receives the list itself and shows it with brackets and quotes.

    Run this code in the editor
  9. Question 9 · Hard

    What is the output of this code?

    name = "Asha"
    score = 7
    print(f"{name=}, {score * 2 = }")
    Show the answer

    Answer: B

    A trailing = in an f-string field prints the expression text and then its value using repr(), so the string keeps its quotes. Spaces around the = are kept exactly as written, which is why the second field shows score * 2 = 14.

    Run this code in the editor
  10. Question 10 · Hard

    What is the output of this code?

    print(round(2.5), round(3.5), round(-0.5))
    Show the answer

    Answer: C

    round() with no digits argument sends an exact half to the nearest even integer (banker's rounding): 2.5 becomes 2, 3.5 becomes 4 and -0.5 becomes 0. Halves are not always rounded up.

    Run this code in the editor
  11. Question 11 · Medium

    What is the output of this code?

    a = b = [1, 2]
    b.append(3)
    print(a)
    Show the answer

    Answer: A

    Chained assignment binds both names to the same list object; it does not make a copy. Appending through b changes that one list, so a shows the 3 as well.

    Run this code in the editor
  12. Question 12 · Hard

    What is the output of this code?

    print(True + True * 3)
    print(int("101", 2) + int(True))
    Show the answer

    Answer: D

    bool is a subclass of int, so True counts as 1, and * runs before +: 1 + 3 = 4. int("101", 2) reads the text in base 2, which is 5, and int(True) is 1, giving 6.

    Run this code in the editor
  13. Question 13 · Easy

    Which error does this code raise?

    age = 16
    print("Age: " + age)
    Show the answer

    Answer: C, TypeError: can only concatenate str (not "int") to str

    + cannot join a str and an int, so Python raises TypeError. Write "Age: " + str(age), use an f-string, or pass both values to print separated by a comma.

    Run this code in the editor
  14. Question 14 · Medium

    Which error does this code raise?

    value = int("3.5")
    print(value)
    Show the answer

    Answer: B, ValueError: invalid literal for int() with base 10: '3.5'

    int() accepts a string only if it looks like a whole number. "3.5" has a decimal point, so it raises ValueError instead of truncating. int(float("3.5")) gives 3.

    Run this code in the editor
  15. Question 15 · Medium

    Which error does this code raise?

    2nd_place = "Ravi"
    print(2nd_place)
    Show the answer

    Answer: A, SyntaxError: invalid decimal literal

    A variable name cannot start with a digit, so the program is rejected before any line runs. Python reports a SyntaxError, not a NameError, because 2nd_place is not a valid name at all.

    Run this code in the editor
  16. Question 16 · Easy

    A program runs age = input("Age: ") and the user types 25. What is the type of age?

    Show the answer

    Answer: D, str

    input() always returns a string, whatever the user types. Convert it with int(age) before doing arithmetic with it.

  17. Question 17 · Easy

    Which of these is a valid Python variable name?

    Show the answer

    Answer: B, _score2

    Names may contain letters, digits and underscores but cannot start with a digit, so _score2 is valid. my-score reads as a subtraction, and class is a reserved keyword.

  18. Question 18 · Medium

    Which of these built-in types is immutable, so its items cannot be changed after it is created?

    Show the answer

    Answer: C, tuple

    A tuple cannot be changed in place: assigning to t[0] raises TypeError. Lists, dicts and sets all have methods that change them, such as append, update and add.

  19. Question 19 · Medium

    Which line prints exactly 5 + 3 = 8?

    Show the answer

    Answer: D

    print puts a space between its arguments, and 5 + 3 is worked out before printing, giving 5 + 3 = 8. Joining str(5) + str(3) makes 53, and adding an int to a str raises TypeError.

  20. Question 20 · Hard

    Given pi = 3.14159, which line prints 3.14?

    Show the answer

    Answer: A

    .2f means two digits after the decimal point. .2 without the f means two significant digits, giving 3.1, and 2f sets a minimum width of 2 with the default six decimals, giving 3.141590.

Keep going