NEW
Font size
WorksheetsMastering Control Flow and Exceptions
Total questions: 10
Worksheet time: 5mins
What is the purpose of an if statement in programming?
The purpose of an if statement is to control the flow of execution based on conditions.
The if statement is used to declare variables in programming.
The if statement serves to create loops in programming.
The if statement is meant for defining functions in code.
How do you create a loop that runs 10 times in Python?
for i in range(10): pass
while i < 10: pass
repeat 10 times: pass
for i in range(1, 11): pass
What is the difference between a while loop and a for loop?
A while loop requires a counter variable, while a for loop does not.
A for loop can only iterate over arrays, while a while loop cannot.
A while loop is always faster than a for loop.
The main difference is that a while loop checks the condition before each iteration, while a for loop is used for a specific number of iterations or over a collection.
How can you handle exceptions in Python?
Use if-else statements to manage exceptions in Python.
Employ loops to catch exceptions in Python.
Use try-except blocks to handle exceptions in Python.
Utilize switch-case constructs for exception handling in Python.
What will be the output of the following code: if 5 > 3: print('Hello')?
Hi there
Goodbye
Greetings
Hello
Explain the use of 'break' and 'continue' in loops.
'break' exits the loop; 'continue' skips to the next iteration.
'break' continues the loop; 'continue' exits the loop.
'break' skips to the next iteration; 'continue' exits the loop.
'break' pauses the loop; 'continue' restarts the loop.
What is a try-except block and how is it used?
A try-except block is a method for optimizing code performance.
A try-except block is a construct used to handle exceptions in code execution.
A try-except block is a way to declare variables in Java.
A try-except block is used to define functions in Python.
How do you check if a variable is equal to a specific value using an if statement?
if (variable = value) { // execute code }
if (variable == value) { // run code }
if (variable != value) { // perform action }
if (variable === value) { // code to execute }
What happens if an exception is not caught in a program?
The program continues running without issues.
The program displays a warning message.
The program automatically fixes the error.
The program terminates with an error.
Can you nest if statements? Provide an example.
Example: if (x > 0) { if (x % 2 == 0) { console.log('x is positive and even'); } }
if (x == 0) { if (x % 3 == 0) { console.log('x is zero and divisible by 3'); } }
if (x < 0) { console.log('x is negative'); }
if (x > 10) { if (x < 5) { console.log('x is greater than 10 and less than 5'); } }
