Debugging and Fixing Common Code Errors Quiz Quiz

  1. Syntax Error Detection

    Which line in the following Python code causes a syntax error? nnn1. for i in range(5):n2. print(i)n3. print('Done')nnSelect the problematic line.

    1. Line 1
    2. Line 2
    3. Line 3
    4. All lines are correct
    5. None of the abvoe
  2. Logical Error Identification

    What is the main issue with this code if the goal is to sum all numbers from 1 to 5? nnntotal = 0nfor i in range(1, 5):n total += innWhich is the correct revision?

    1. Change range(1, 5) to range(1, 6)
    2. Change total = 0 to total = 1
    3. Replace += with =+
    4. Remove total = 0
    5. Use range(0, 5) instead
  3. Variable Name Typo

    What will happen when the following code is executed?nnncount = 10nprint(cout)n

    1. A NameError will occur
    2. cout will print as 'cout'
    3. 10 will be printed
    4. The program runs but prints nothing
    5. SyntaxError will be rased
  4. Index Out of Range

    Given the list `nums = [2, 4, 6, 8]`, what happens if you run `print(nums[4])`?

    1. An IndexError is raised
    2. It prints 8
    3. It prints 2
    4. It prints None
    5. It prints 'nums[4]'
  5. Indentation Error

    Which change will fix the following Python code?nnndef greet():nprint('Hello!')n

    1. Indent the print statement
    2. Add parentheses to 'def greet:'
    3. Change 'def' to 'function'
    4. Remove the colon from line 1
    5. Capitalize 'print'
  6. Infinite Loop Debugging

    What is the main bug in this code?nnni = 0nwhile i u003C 3:n print(i)n

    1. i is never incremented, causing an infinite loop
    2. i starts at 1 instead of 0
    3. Indentation is missing
    4. Print statement should be outisde the loop
    5. The colon is not needed after while
  7. Print Statement Fix

    What is wrong with this code in Python 3?nnnprint 'Hello World!'n

    1. Parentheses are missing in print()
    2. Single quotes are not allowed
    3. print should be Print
    4. Hello World should be in double quotes
    5. There is a comma missing at the end
  8. Type Error Identification

    What error occurs when running this code?nnnx = '10' + 2n

    1. TypeError
    2. ValueError
    3. KeyError
    4. SyntaxError
    5. No error, output is '102'
  9. Off-by-One Error

    In the following code, what will be printed?nnnfor i in range(3):n print(i)n

    1. 0, 1, 2
    2. 1, 2, 3
    3. 0, 1, 2, 3
    4. 1, 2
    5. 3, 2, 1
  10. Function Return Bug

    Consider this code:nnndef multiply(x, y):n result = x * ynprint(multiply(2, 4))nnWhat does it print?

    1. None
    2. 8
    3. Error: multiply is not defined
    4. 2, 4
    5. multiply(2, 4)