C For Loop MCQ

1. What is the primary purpose of a 'for' loop in C?

a) To execute a block of code a fixed number of times
b) To execute a block of code based on a condition
c) To create an infinite loop
d) To iterate over arrays only

Answer:

a) To execute a block of code a fixed number of times

Explanation:

A 'for' loop is typically used in C to execute a block of code a specific number of times, with the number of iterations usually determined by the loop's initialization and condition.

2. What are the three parts of a 'for' loop declaration in C?

a) Initialization, condition, update
b) Condition, update, execution
c) Start, end, step
d) Declaration, execution, termination

Answer:

a) Initialization, condition, update

Explanation:

A 'for' loop in C consists of three parts: initialization (executed once before the loop starts), condition (checked before each iteration), and update (executed after each iteration).

3. Which part of the 'for' loop is optional?

a) Initialization
b) Condition
c) Update
d) All of the above

Answer:

d) All of the above

Explanation:

All three parts of the 'for' loop (initialization, condition, update) are optional in C. Omitting the condition creates an infinite loop.

4. How is a 'for' loop commonly used in array processing?

a) To search for an element
b) To iterate over each element
c) To modify array size
d) To reverse the array

Answer:

b) To iterate over each element

Explanation:

'For' loops are commonly used to iterate over each element in an array, typically by using an index variable that is incremented in each iteration.

5. What is the scope of a variable declared in the initialization part of a 'for' loop in C?

a) It is global
b) It is local to the loop
c) It is local to the function containing the loop
d) It is local to the entire program

Answer:

b) It is local to the loop

Explanation:

A variable declared in the initialization part of a 'for' loop is local to the loop and cannot be accessed outside the loop.

6. What will be the output of the following C code snippet?

for (int i = 0; i < 3; i++) {
   printf("%d", i);
}
a) 012
b) 123
c) 0123
d) 3

Answer:

a) 012

Explanation:

The loop iterates three times with 'i' taking values 0, 1, and 2, resulting in the output "012".

7. How do you create an infinite 'for' loop in C?

a) for ( ; ; ) { }
b) for (int i = 0; true; i++) { }
c) for (1) { }
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

An infinite 'for' loop can be created by leaving the condition empty (for ( ; ; ) { }) or by using a condition that always evaluates to true.

8. What does the 'break' statement do inside a 'for' loop?

a) Pauses the loop
b) Terminates the loop immediately
c) Skips to the next iteration
d) Exits the program

Answer:

b) Terminates the loop immediately

Explanation:

The 'break' statement is used to exit the loop immediately, regardless of the loop condition.

9. What is the use of the 'continue' statement in a 'for' loop?

a) To stop the loop
b) To skip the rest of the current loop iteration and continue with the next one
c) To repeat the loop from the beginning
d) To pause the loop execution

Answer:

b) To skip the rest of the current loop iteration and continue with the next one

Explanation:

The 'continue' statement skips the remaining part of the loop body for the current iteration and proceeds with the next iteration of the loop.

10. What will happen if you omit all three parts of the 'for' loop declaration?

a) The loop will not compile
b) The loop will execute once
c) The loop will become an infinite loop
d) The loop will be skipped

Answer:

c) The loop will become an infinite loop

Explanation:

Omitting all three parts (initialization, condition, and update) of the 'for' loop creates an infinite loop because there is no condition to terminate the loop.

11. Can the initialization part of the 'for' loop contain multiple statements in C?

a) Yes, separated by commas
b) No, it can contain only one statement
c) Only if they are of the same type
d) Only if they are assignments

Answer:

a) Yes, separated by commas

Explanation:

The initialization part of a 'for' loop can contain multiple statements, separated by commas.

12. Is it possible to declare more than one variable in the initialization part of a 'for' loop in C?

a) Yes, but only of the same type
b) Yes, of any type
c) No, only one variable is allowed
d) Only if they are pointers

Answer:

a) Yes, but only of the same type

Explanation:

Multiple variables can be declared in the initialization part of a 'for' loop, but they must all be of the same type.

13. What will be the output of the following C code snippet?

for (int i = 10; i >= 0; i -= 2) {
   printf("%d ", i);
}
a) 10 8 6 4 2 0
b) 10 9 8 7 6 5 4 3 2 1 0
c) 9 7 5 3 1
d) No output

Answer:

a) 10 8 6 4 2 0

Explanation:

The loop starts at 10 and decrements 'i' by 2 in each iteration, printing the even numbers down to 0.

14. What is the correct way to write a nested 'for' loop in C?

a) for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { } }
b) for (int i = 0; i < n; i++), for (int j = 0; j < m; j++) { }
c) for (int i = 0, j = 0; i < n, j < m; i++, j++) { }
d) for int i = 0; i < n; i++ { for int j = 0; j < m; j++ { } }

Answer:

a) for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { } }

Explanation:

Nested 'for' loops are written by placing one 'for' loop inside the body of another, with each loop having its own control variables and conditions.

15. What is the role of the update part of the 'for' loop in C?

a) To initialize loop variables
b) To check the loop condition
c) To modify the loop variable(s) after each iteration
d) To define the loop body

Answer:

c) To modify the loop variable(s) after each iteration

Explanation:

The update part of the 'for' loop is executed after each iteration of the loop body and is typically used to modify the loop variable(s), such as incrementing or decrementing a counter.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top