WorksheetsMastering Control Structures in C
Total questions: 20
Worksheet time: 10mins
What is the purpose of an if statement in C?
To declare variables in a program.
To create loops for repeated execution.
To define a function in C programming.
To execute code conditionally based on a boolean expression.
How does a switch statement differ from an if statement?
A switch statement is used for matching a single value against multiple cases, while an if statement evaluates boolean conditions.
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.
What is the syntax for a for loop in C?
for(initialization: condition: increment/decrement) { // code to be executed }
for(initialization; condition; increment/decrement) { // code to be executed }
for(initialization; increment/decrement; condition) { // code to be executed }
for(condition; initialization; increment/decrement) { // code to be executed }
Explain the use of the break statement in loops.
The break statement allows a loop to run indefinitely without stopping.
The break statement continues to the next iteration of the loop.
The break statement allows a loop to be exited prematurely.
The break statement pauses the loop for a specified time.
What will be the output of the following code: if(5 > 3) printf('True'); else printf('False');?
True
Incorrect
Maybe
False
How can you create an infinite loop in C?
for(1) {}
while(1) {} or for(;;) {}
do {} while(0);
while(0) {}
What is the role of the default case in a switch statement?
The default case is used to define the variable scope in a switch statement.
The default case is a mandatory part of every switch statement for proper execution.
The default case executes when the first case matches in a switch statement.
The default case provides a fallback action when no case matches in a switch statement.
Describe the difference between while and do-while loops.
The main difference is that a while loop may not execute at all if the condition is false, while a do-while loop always executes at least once.
Both loops execute the same number of times regardless of the condition.
A while loop checks the condition after executing, while a do-while loop checks it before.
A while loop executes at least once, while a do-while loop may not execute at all.
What happens if the condition in an if statement is false?
The code inside the if statement is not executed.
The program terminates immediately without executing further.
The code inside the if statement runs without any checks.
The if statement is executed regardless of the condition.
How can you nest if statements in C?
if (condition1) { /* code */ } else { if (condition2) { /* code */ } }
if (condition1) { if (condition2) { /* other code */ } else { /* code */ } }
if (condition1) { if (condition2) { /* code */ } }
if (condition1 && condition2) { /* code */ }
What is the output of the following code: switch(2) { case 1: printf('One'); break; case 2: printf('Two'); break; default: printf('Default'); }?
One
Default
Two
Three
How do you use logical operators in if statements?
Use logical operators like '&&' for AND, '||' for OR, and '!' for NOT in if statements to evaluate multiple conditions.
Use logical operators like '??' for null coalescing, '??=' for null assignment, and '===' for strict equality in if statements.
Use logical operators such as '++' for increment, '--' for decrement, and '==' for comparison in if statements.
Use logical operators like '==' for equality, '!=' for inequality, and '>' for greater than in if statements.
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 restarts the loop from the beginning.
The continue statement pauses the loop for a specified time.
How can you implement a countdown using a for loop?
for (int i = 1; i <= 5; i++) { System.out.println(i); }
for (int i = 5; i > 0; i--) { System.out.println(i); }
for (int i = 5; i >= 0; i--) { System.out.println(i); }
for (int i = 0; i < 5; i++) { System.out.println(i); }
What is the result of the following code: for(int i = 0; i < 5; i++) { if(i == 3) break; printf('%d ', i); }?
1 2 3 4
0 1 2 3
0 1 4
0 1 2
Explain how to use multiple conditions in an if statement.
Utilize 'and' and 'or' keywords directly in the if statement.
Combine conditions with '++' for 'and' and '--' for 'or'.
Use '&&' for 'and' conditions and '||' for 'or' conditions in an if statement.
Use '==' for 'and' conditions and '!=' for 'or' conditions.
What is the significance of the return statement in a function?
The return statement is significant because it allows a function to output a value and terminate its execution.
The return statement is used to define a function's parameters and its scope.
The return statement is important for declaring global variables within a function.
The return statement serves to log messages to the console during function execution.
How can you use a switch statement to handle multiple cases?
Utilize a ternary operator to evaluate conditions and return results.
Use a switch statement with 'case' for each value and 'break' to separate cases.
Use an if-else statement for each condition and return values directly.
Implement a loop to iterate through cases and execute actions sequentially.
What is the output of the following code: int x = 10; if(x < 5) printf('Low'); else if(x < 15) printf('Medium'); else printf('High');?
Very High
High
Medium
Low
How do you properly format a nested loop in C?
for (int i = 0; i < outer_limit; i++) { for (int j = 0; j < inner_limit; j++) { // inner loop code } }
for (int i = 0; i < outer_limit; i++) { while (j < inner_limit) { // inner loop } }
for (int i = 1; i <= outer_limit; i++) { for (int j = 1; j <= inner_limit; j++) { // inner code } }
for (int i = 0; i < limit; i++) { for (int j = 0; j < count; j++) { // loop code } }
