wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering C Control Structures

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What is the purpose of an if statement in C?

a)

An if statement in C is for declaring variables.

b)

The purpose of an if statement in C is to execute code conditionally based on a boolean expression.

c)

The if statement in C is used to define functions.

d)

The purpose of an if statement in C is to loop through code.

2.

How do you write a nested if statement in C?

a)

if (condition1) { if (condition2) // code to execute }

b)

if (condition1) { if (condition2) { // execute code } }

c)

if (condition1) { if (condition2) { // code to execute if both conditions are true } }

d)

if (condition1) { // code to execute }

3.

What is the syntax for a switch case in C?

a)

switch(expression) { case constant1: // statements; break; case constant2: // statements; break; default: // statements; }

b)

switch { case constant1: // statements; break; }

c)

switch(expression) { if(condition) // statements; }

d)

case constant1: // statements; switch(expression) }

4.

How does a for loop differ from a while loop?

a)

A for loop runs indefinitely, while a while loop stops after a set number of iterations.

b)

A for loop is used for conditional statements, while a while loop is used for repetitive tasks.

c)

A for loop iterates a known number of times, while a while loop continues until a condition is false.

d)

A for loop checks a condition before each iteration, while a while loop does so after each iteration.

5.

What is the output of the following code: int x = 5; if (x > 3) { printf("Greater"); } else { printf("Lesser"); }?

a)

Smaller

b)

Greater

c)

Equal

d)

Lesser

6.

Explain the mechanics of a while loop in C.

a)

A while loop in C executes only once regardless of the condition.

b)

A while loop in C runs indefinitely without any condition.

c)

A while loop in C executes a block of code as long as a specified condition is true.

d)

A while loop in C requires a return statement to function.

7.

What is the structure of a do-while loop?

a)

do { /* code block */ } if (condition);

b)

while (condition) { /* code block */ } do;

c)

do { /* code block */ } until (condition);

d)

do { /* code block */ } while (condition);

8.

How can you use a switch statement to handle multiple cases?

a)

Use 'if' statements for each case separately.

b)

Combine cases using 'case value1, value2:' syntax.

c)

Use 'case value1:', 'case value2:', etc., followed by the code block and 'break;' to handle multiple cases.

d)

Utilize a loop to iterate through cases instead.

9.

What happens if no case matches in a switch statement?

a)

The switch statement will throw an error if no case matches.

b)

The switch statement will execute the 'default' case if it exists; otherwise, it will do nothing.

c)

The switch statement will execute the first case regardless of the match.

d)

The switch statement will skip to the next function call.

10.

Can you nest switch statements in C?

a)

Switch statements can only be used at the top level in C.

b)

No, switch statements cannot be nested in C.

c)

Yes, you can nest switch statements in C.

d)

You can only use one switch statement in C.

11.

What is the difference between pre-increment and post-increment in a for loop?

a)

Pre-increment and post-increment are identical in function and usage.

b)

Pre-increment updates the variable before use; post-increment updates it after use.

c)

Pre-increment updates the variable after use; post-increment updates it before use.

d)

Pre-increment is used only in while loops; post-increment is used in for loops.

12.

How do you break out of a loop in C?

a)

Use the 'continue' statement.

b)

Utilize the 'exit' function.

c)

Use the 'break' statement.

d)

Implement a 'return' command.

13.

What is the output of the following code: for (int i = 0; i < 3; i++) { printf("%d ", i); }?

a)

0 1 1

b)

1 2 3

c)

2 1 0

d)

0 1 2

14.

How can you use a while loop to create an infinite loop?

a)

Use 'while (x < 10) {' to create an infinite loop.

b)

Use 'while (1) {' to create an infinite loop.

c)

Use 'while (false) {' to create an infinite loop.

d)

Use 'while (true) {' to create an infinite loop.

15.

What is the role of the default case in a switch statement?

a)

The default case allows for multiple cases to be grouped together in a switch statement.

b)

The default case executes when the first case is true in a switch statement.

c)

The default case is used to define the variable scope in a switch statement.

d)

The default case provides a fallback action when no case matches in a switch statement.

16.

How do you initialize multiple variables in a for loop?

a)

Initialize variables after the loop ends.

b)

Use a single declaration outside the loop.

c)

Declare variables in a separate function.

d)

Use destructuring assignment or separate declarations in the loop.

17.

What is the difference between a while loop and a do-while loop?

a)

A while loop executes at least once, while a do-while loop may not execute at all.

b)

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.

c)

Both loops execute the same number of times regardless of the condition.

d)

A while loop checks the condition after executing, while a do-while loop checks it before.

18.

How can you use a switch statement to replace multiple if-else statements?

a)

Use a switch statement to compare two variables and return a boolean result.

b)

Utilize a switch statement to handle exceptions and manage error states in the code.

c)

Use a switch statement to evaluate a variable and define actions for each case, replacing multiple if-else statements.

d)

Implement a switch statement to loop through an array of values and print each one.

19.

What is the output of the following code: int i = 0; while (i < 5) { printf("%d ", i++); }?

a)

0 1 2 3 4 5

b)

0 1 2 3 4

c)

1 2 3 4 5

d)

0 1 2 3

20.

How do you use the continue statement in loops?

a)

The continue statement pauses the loop for a specified time.

b)

Use the continue statement to end the loop immediately.

c)

Use the continue statement to repeat the current iteration.

d)

Use the continue statement in loops to skip the current iteration and proceed to the next one.