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 freeSample questions
What is the default else block in an if-else structure executed?
- When the if condition is true
- When the if condition is false
- Always
- Never
Show answer
Answer: B. When the if condition is false
The else block is executed only when the if condition evaluates to false.
What is the output when 'x = 5; if x == 5: print('Yes') else: print('No')' is executed?
- Yes
- No
- 5
- Error
Show answer
Answer: A. Yes
Since x equals 5, the if condition is true and 'Yes' is printed.
In an 'if' statement block in Python, what indicates that a block of code belongs to that statement?
- Curly braces {}
- Indentation
- Parentheses ()
- Square brackets []
Show answer
Answer: B. Indentation
Python uses indentation to define blocks of code, particularly for control structures like if statements.
What is the output of: 'for i in range(5, 1, -1): print(i)'?
- 5 4 3 2
- 5 4 3 2 1
- 4 3 2 1
- 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.
In Python, what will happen if the indentation is incorrect in a loop?
- Nothing, Python ignores indentation
- Syntax error
- Logical error
- 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.