Test Your Knowledge (Ch 10)

The following questions are based on what we have learned so far in Chapter 10 – Loops in C. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

Which of the following loop structure execute at least once?

for loop
while loop
do-while loop
None of the above
explanation

The do-while loop executes at least once because the condition is checked after execution of the loop body. This is not the case with other loops.

Question 2

Which of the following loops is best suited for the situation when the number of iterations is known in advance?

for loop
do-while loop
while loop
None of the above
explanation

The for loop is best suited when the number of iterations is known in advance because the initialisation, condition checking, and increment/decrement all are done in the same line.

Question 3

In a for loop, which part is executed only once?

Initialization
Update
Condition
None of the above
explanation

A variable is initialized only once within the for loop. The update and condition is often evaluated multiple times. 

Question 4

Determine the output of the following C code:

int main() {
    int i = 10;
    while (i > 0) {
        i -= 3;
        printf("%d ", i);
    }
    return 0;
}
7 4 1
7 4 2
7 4 1 -2
None of the above
explanation

Iteration 1: i = 7; print 7
Iteration 2: i = 4; print 4
Iteration 3: i = 1; print 1
Iteration 4: i = -2; print -2
Stop.

Question 5

What is the output of the following code?

#include <stdio.h>
int main() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            if (i == j)
                continue;
            printf("%d %d ", i, j);
        }
    }
    return 0;
}
0 1 1 0 0 0 0 0
0 1 1 0 2 0 2 1
0 1 1 0 2 0 2 2
None of the above
explanation

Without the condition check, the two loops would have printed the following:

0 0 0 1 1 0 1 1 2 0 2 1

Remove all the combinations where i and j are the same. We will get

0 1 1 0 2 0 2 1

And hence, option (b) is correct.

Question 6

Determine the output of the following C program:

#include <stdio.h>
int main() {
    int i = 0;
    while (i < 20) {
        switch (i) {
            case 0:
                i += 5;
                break;
            case 5:
                i += 10;
                break;
            case 15:
                i += 15;
                break;
            default:
                i += 1;    
        }
        printf("%d ", i);
    }
    return 0;
}
5 10 15 19
5 10 15
5 15 30
None of the above
explanation

Let’s evaluate each iteration of the while loop: 

Iteration 1: 
– Variable i is 0 and the while condition is satisfied. 
– Case 0 will execute and hence, i is incremented by 5. 
– The new value of i is 5.
– The printf function displays 5.

Iteration 2:
– Variable i is 5 and the while loop condition is satisfied. 
– Case 5 will execute and hence, i is incremented by 10. 
– The new value of i is 15. 
– The printf function displays 15.

Iteration 3:
– Variable i is 15 and the while loop condition is satisfied. 
– Case 15 will execute and hence, i is incremented by 15. 
– The new value of i is 30. 
– The printf function displays 30.

Therefore, the output is 5 15 30.

Question 7

What is the output of the following program?

#include <stdio.h>
int main() {
    int i = 0;
    for (i) printf("PUB");
    return 0;
}
PUB
Compilation error
Runtime error
The loop will run infinite times
explanation

for (i) is not the correct way of writing the for loop as it doesn’t make sense whether i represents the initialisation, condition or update expression to the compiler. Hence, it is the compilation error. 

Following are the correct ways to write the for loop:
– for (i;;) represents the for loop with i treated as the initialisation statement because i is placed in the first position. The two semicolons after i are the null statements taking place of the conditional and update expressions. 
– for (;i;) represents the for loop with i treated as the conditional statement as it is placed between the two semicolons. Always remember that the first position in the for loop is for the initialisation statement, second for the conditional statement, and third for the update statement. 
– for (;;i) is also valid. The i here represents the update statement. 

Hence, it is clear that the for loop given in the questions does not make sense to the compiler and therefore, it is the compilation error.

Question 8

What’s the output?

#include <stdio.h>
int main() {
    int i = 0;
    for (;;) printf("PUB");
    return 0;
}
PUB
Compilation error
Runtime error
The loop will run infinite times
explanation

The loop will run indefinitely because the condition is not specified and hence, there is no condition for which the loop will terminate.

Question 9

Determine the output of the following program:

#include <stdio.h>
int main() {
    int i = 4;
    for (; i == printf("PUB "); i++) 
        printf("PUB ");
    return 0;
}
PUB
PUB PUB
PUB PUB PUB
PUB PUB PUB PUB
explanation

The variable i is initialized before the loop and the loop is valid. 

Iteration 1:
– Variable i is compared with the printf function. The printf function will execute and displays “PUB “. The printf function also returns the number of characters displayed so the printf function in the conditional statement can be replaced by value 4. As variable i is also 4, therefore the condition is satisfied. 
– The printf function in the body of the loop will execute and hence the string “PUB “ will display (without double quotes) for the second time. 
– Then, i is incremented by 1 making i = 2. 
– One more time variable i is compared with the printf function. The printf function will execute and displays “PUB “ for the third time. The printf function also returns the number of characters displayed so the printf function in the conditional statement can be replaced by value 4. 
This time variable i is holding value 5, therefore the condition is not satisfied, and hence the loop will terminate. 

Therefore, the string “PUB “ is displayed a total of 3 times and hence, the correct option is (c). 

Question 10

Predict the output of the following program:

#include <stdio.h>
int main() {
    for (int i = 65; i <= 70; i++) 
        printf("%c ", i);
    return 0;
}
65 66 67 68 69 70
A B C D E F
Compilation error
Runtime error
explanation

The format specifier is %c in the printf function. Therefore, according to the following ASCII table, the corresponding character of the ASCII code will be displayed.

Your score is out of



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.