NEW
Font size
WorksheetsMastering Control Structures
Total questions: 20
Worksheet time: 10mins
What is the purpose of an if statement?
The purpose of an if statement is to declare variables in a program.
The purpose of an if statement is to define functions in a program.
The purpose of an if statement is to create loops in a program.
The purpose of an if statement is to control the flow of execution in a program based on conditions.
How does a switch statement differ from an if statement?
A switch statement evaluates multiple boolean conditions, while an if statement matches a single value.
A switch statement can only handle numeric values, whereas an if statement can handle any data type.
A switch statement is used for sequential execution, while an if statement is for branching logic.
A switch statement is used for matching a single value against multiple cases, while an if statement evaluates boolean conditions.
What is the syntax for a for loop in JavaScript?
for (initialization; condition; increment) { // code to be executed }
for (start; test; step) { // execute code }
for (let i = 0; i < n; i++) // loop body
for (var x in object) { // iterate properties }
Explain the use of break in loops.
The 'break' statement pauses a loop until a condition is met.
The 'break' statement exits a loop when a specific condition is satisfied.
The 'break' statement skips the current iteration of the loop.
The 'break' statement continues the loop until it is manually stopped.
What is the output of the following code: if (5 > 3) { console.log('A'); } else { console.log('B'); }?
A
B
C
D
How can you create a while loop in Python?
for condition in iterable:
if condition: # code to execute
do while condition: # code to execute
while condition: # code to execute
What is the difference between a do-while loop and a while loop?
A do-while loop can only execute if the condition is true, while a while loop executes regardless of the condition.
The main difference is that a do-while loop guarantees at least one execution of the loop body, while a while loop may not execute at all if the condition is false.
Both loops execute the body at least once, but a do-while loop checks the condition first.
A while loop is used for infinite iterations, while a do-while loop is for finite iterations.
In which scenario would you prefer a switch statement over multiple if statements?
When needing to execute the same code for different cases.
When checking a range of values for a variable.
When dealing with multiple discrete values for a single variable.
When evaluating a single condition with multiple outcomes.
What will be the output of the following code: switch (2) { case 1: console.log('One'); break; case 2: console.log('Two'); break; default: console.log('Default'); }?
Zero
One
Two
Three
How do you handle multiple conditions in an if statement?
Use logical operators like '&&' for 'and' and '||' for 'or' to combine conditions.
Check conditions sequentially without logical operators.
Use nested if statements for each condition separately.
Combine conditions using only 'and' without operators.
What is the role of the else if clause?
The 'else if' clause terminates the execution of the program immediately.
The 'else if' clause is used to define a loop structure in the code.
The 'else if' clause serves as a default case when all conditions fail.
The 'else if' clause provides an additional condition to check if the preceding 'if' condition is false.
Can you nest if statements? Provide an example.
if (num == 0) { console.log('Zero'); }
if (num % 2 != 0) { console.log('Odd number'); }
if (num < 0) { console.log('Negative number'); }
Example: if (num > 0) { if (num % 2 == 0) { console.log('Positive even number'); } }
What is the purpose of the continue statement in loops?
The continue statement ends the loop immediately.
The continue statement skips the current iteration of a loop.
The continue statement pauses the loop for a specified time.
The continue statement restarts the loop from the beginning.
How do you iterate over an array using a for loop?
for (i in array) { console.log(array[i]); }
for (let i = 1; i <= array.length; i++) { console.log(array[i]); }
for (let i = 0; i < array.length; i++) { print(array[i]); }
for (let i = 0; i < array.length; i++) { console.log(array[i]); }
What is the difference between a for loop and a forEach method in JavaScript?
A for loop is more flexible and allows index control, while forEach is simpler and specifically for arrays.
A for loop requires a callback function, while forEach does not need one.
A for loop iterates over all data types, while forEach is limited to numbers.
A for loop is used for objects, while forEach is only for strings.
How can you exit a loop prematurely?
Implement a 'stop' function.
Use the 'break' statement.
Utilize a 'return' command.
Use the 'continue' statement.
What is the output of the following code: for (let i = 0; i < 3; i++) { console.log(i); }?
undefined
0 1 2
3
-1
Explain the concept of infinite loops and how to avoid them.
An infinite loop is a loop that executes only once. To avoid them, always check the loop variable before starting.
An infinite loop is a loop that never ends due to a failure in its exit condition. To avoid them, ensure exit conditions are met and implement safeguards.
An infinite loop is a loop that runs for a fixed number of iterations. To avoid them, use a timer to limit execution.
An infinite loop is a loop that runs in reverse order. To avoid them, use a different programming language.
What is the significance of the default case in a switch statement?
The default case allows for multiple matches in a switch statement.
The default case is used to define the first case in a switch statement.
The default case is mandatory for every switch statement to function properly.
The default case provides a fallback option for unmatched cases in a switch statement.
How can you use logical operators in if statements?
You can only use '&&' in if statements to check conditions.
You should avoid using any operators in if statements.
Logical operators are not applicable in if statements.
You can use logical operators like '&&', '||', and '!' in if statements to combine multiple conditions.
