Any2Quiz
CBSE / 9 / Computer Science / Programming with Python

Control Structures (If-Else and Loops) Quiz - CBSE Class 9 Computer Science

Free practice quiz: Control Structures (If-Else and Loops) (CBSE Class 9 Computer Science). 20 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 20 questions free

Sample questions

  1. What is the default else block in an if-else structure executed?

    1. When the if condition is true
    2. When the if condition is false
    3. Always
    4. Never
    Show answer

    Answer: B. When the if condition is false

    The else block is executed only when the if condition evaluates to false.

  2. What is the output when 'x = 5; if x == 5: print('Yes') else: print('No')' is executed?

    1. Yes
    2. No
    3. 5
    4. Error
    Show answer

    Answer: A. Yes

    Since x equals 5, the if condition is true and 'Yes' is printed.

  3. In an 'if' statement block in Python, what indicates that a block of code belongs to that statement?

    1. Curly braces {}
    2. Indentation
    3. Parentheses ()
    4. Square brackets []
    Show answer

    Answer: B. Indentation

    Python uses indentation to define blocks of code, particularly for control structures like if statements.

  4. What is the output of: 'for i in range(5, 1, -1): print(i)'?

    1. 5 4 3 2
    2. 5 4 3 2 1
    3. 4 3 2 1
    4. Error
    Show answer

    Answer: A. 5 4 3 2

    The 'range(5, 1, -1)' generates numbers from 5 to 2 (step -1), printing 5, 4, 3, 2 on new lines.

  5. In Python, what will happen if the indentation is incorrect in a loop?

    1. Nothing, Python ignores indentation
    2. Syntax error
    3. Logical error
    4. Performance delay
    Show answer

    Answer: B. Syntax error

    Indentation is crucial in Python since it defines the level of code hierarchy. Incorrect indentation will lead to a SyntaxError.

Take the full quiz

More quizzes

Browse all Programming with Python quizzes