Unit 3: Control Statements and Decision Making - Practice Quiz

CAP1008 — C Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is the main purpose of control statements in C?

introduction of control statement Easy
A. To allocate memory dynamically at runtime by requesting blocks from the operating system
B. To control the flow of execution of a program
C. To include header files
D. To declare variables

2 Which of the following is NOT a category of control statements in C?

introduction of control statement Easy
A. Iteration statements
B. Declaration statements
C. Jump statements
D. Selection statements

3 Conditional statements in C execute a block of code based on the result of a:

condition statements Easy
A. Preprocessor directive
B. Boolean (true/false) condition
C. Function prototype
D. Variable declaration

4 In an if statement, the block of code executes only when the condition is:

if Easy
A. False (zero)
B. True (non-zero)
C. Always, regardless of condition
D. Equal to a string

5 What is the output of the following code?
c
int x = 5;
if (x > 3)
printf("Yes");

if Easy
A. No
B. Nothing is printed
C. Compilation error
D. Yes

6 In an if-else statement, the else block executes when the condition is:

if-else Easy
A. False
B. True
C. Both true and false
D. Undefined

7 What is the output of the following code?
c
int a = 2;
if (a == 5)
printf("Equal");
else
printf("Not Equal");

if-else Easy
A. Compilation error
B. Not Equal
C. Both are printed
D. Equal

8 A nested if statement means:

nested if and switch statement Easy
A. An if without any condition
B. An if statement inside another if statement
C. An if used only inside a loop
D. Two if statements written side by side

9 Which keyword is used to define the default case in a switch statement?

nested if and switch statement Easy
A. else
B. otherwise
C. default
D. final

10 What happens if break is omitted after a matching case in a switch statement?

nested if and switch statement Easy
A. The switch restarts from the beginning and re-evaluates every case value one by one
B. Execution falls through to the following cases
C. A compilation error occurs
D. The program stops immediately

11 The expression used in a switch statement must evaluate to which type of value?

nested if and switch statement Easy
A. An integer or character constant
B. A floating-point number
C. A pointer address
D. A string

12 In a while loop, when is the condition checked?

looping statements-while and do-while loop Easy
A. Before each iteration of the loop body
B. Only once at the start of the program
C. Never, the loop runs infinitely by design
D. After each iteration of the loop body

13 How many times is the body of a do-while loop guaranteed to execute at minimum?

looping statements-while and do-while loop Easy
A. Zero times
B. Depends on the header file
C. Exactly twice
D. At least once

14 Which loop checks its condition at the end (bottom) of the loop?

looping statements-while and do-while loop Easy
A. while loop
B. nested if
C. for loop
D. do-while loop

15 What is the output of the following code?
c
int i = 1;
while (i <= 3) {
printf("%d", i);
i++;
}

looping statements-while and do-while loop Easy
A. 123
B. 111
C. 12
D. 1234

16 In a for loop, which part is executed only once, at the very beginning?

for loop Easy
A. Condition
B. The loop body
C. Increment/Decrement
D. Initialization

17 How many times will the following loop print Hi?
c
for (int i = 0; i < 4; i++)
printf("Hi");

for loop Easy
A. 4 times
B. Infinite times
C. 3 times
D. 5 times

18 What does the break statement do inside a loop?

break and continue statements Easy
A. Skips to the next iteration
B. Terminates the loop immediately
C. Restarts the loop from the beginning
D. Pauses the loop for one second

19 What does the continue statement do inside a loop?

break and continue statements Easy
A. Skips the rest of the current iteration and moves to the next
B. Exits the entire program
C. Prints the current value of the loop variable and then stops the loop
D. Ends the loop completely

20 The goto statement transfers control to a:

goto statement Easy
A. Function return value
B. Labeled statement in the program
C. Header file
D. Random line number

21 Which category of control statements is used to repeatedly execute a block of code based on a condition?

introduction of control statement Medium
A. Iteration statements
B. Declaration statements
C. Jump statements
D. Selection statements

22 What is the value of the expression 5 > 3 && 2 < 1 in C?

condition statements Medium
A.
B. Compilation error
C.
D.

23 What does the following code print?

C
int x = 10;
if (x = 5)
    printf("Yes");
else
    printf("No");

if Medium
A. Compilation error
B. Yes
C. Nothing
D. No

24 What is the output of the following code?

C
int a = 7;
if (a % 2 == 0)
    printf("Even");
else
    printf("Odd");

if-else Medium
A. Odd
B. Even
C. EvenOdd
D. No output

25 In the code below, which else does the inner if associate with?

C
if (a > 0)
    if (b > 0)
        printf("A");
else
    printf("B");

nested if and switch statement Medium
A. Both if statements
B. The outer if (a > 0)
C. Neither, it is a syntax error
D. The inner if (b > 0)

26 What is the output of the following switch statement?

C
int n = 2;
switch (n) {
    case 1: printf("One");
    case 2: printf("Two");
    case 3: printf("Three");
    default: printf("None");
}

nested if and switch statement Medium
A. TwoThree
B. TwoThreeNone
C. Two
D. None

27 Which data type is NOT allowed as a switch expression in C?

nested if and switch statement Medium
A. char
B. float
C. int
D. enum

28 How many times does the body of this loop execute?

C
int i = 5;
while (i < 5) {
    printf("%d", i);
    i++;
}

looping statements-while and do-while loop Medium
A. times
B. Infinite times
C. times
D. time

29 How many times does the body of this loop execute?

C
int i = 5;
do {
    printf("%d", i);
    i++;
} while (i < 5);

looping statements-while and do-while loop Medium
A. time
B. times
C. Infinite times
D. times

30 What is the final value printed by this code?

C
int sum = 0, i = 1;
while (i <= 4) {
    sum += i;
    i++;
}
printf("%d", sum);

looping statements-while and do-while loop Medium
A.
B.
C.
D.

31 What is the output of the following code?

C
for (int i = 0; i < 3; i++)
    printf("%d ", i * i);

for loop Medium
A. 1 2 3
B. 0 1 4
C. 0 1 2
D. 1 4 9

32 How many times does the following loop execute?

C
for (int i = 10; i > 0; i -= 3)
    printf("*");

for loop Medium
A. times
B. times
C. Infinite times
D. times

33 What happens when all three expressions of a for loop are omitted, as in for(;;)?

for loop Medium
A. It causes a compilation error
B. It runs exactly once
C. It skips the loop body
D. It creates an infinite loop

34 What is the output of the following code?

C
for (int i = 1; i <= 5; i++) {
    if (i == 3)
        break;
    printf("%d ", i);
}

break and continue statements Medium
A. 1 2 4 5
B. 1 2
C. 3 4 5
D. 1 2 3

35 What is the output of the following code?

C
for (int i = 1; i <= 5; i++) {
    if (i % 2 == 0)
        continue;
    printf("%d ", i);
}

break and continue statements Medium
A. 2 4
B. 1 2 3 4 5
C. 1 3 5
D. 1 3

36 In nested loops, what does a break statement inside the inner loop terminate?

break and continue statements Medium
A. The entire program
B. Both the inner and outer loops
C. Only the outer loop
D. Only the inner loop

37 What is the output of the following code?

C
int i = 0;
start:
printf("%d ", i);
i++;
if (i < 3)
    goto start;

goto statement Medium
A. 0 1 2
B. 0 1 2 3
C. 0 0 0
D. 1 2 3

38 Which statement about the goto statement in C is correct?

goto statement Medium
A. It automatically ends the current loop only
B. It can jump to a label in any other function
C. It transfers control to a labeled statement within the same function
D. It requires a matching break to work

39 What is the output of this ternary-based code?

C
int a = 4, b = 9;
int max = (a > b) ? a : b;
printf("%d", max);

if-else Medium
A.
B.
C.
D.

40 What is the output of the following nested loop?

C
for (int i = 1; i <= 2; i++)
    for (int j = 1; j <= 2; j++)
        printf("%d%d ", i, j);

for loop Medium
A. 11 12 13 21
B. 11 22
C. 11 12 21 22
D. 12 21

41 What is the output of the following code?

C
#include <stdio.h>
int main() {
    int a = 5, b = 10;
    if (a > b)
        if (a > 0)
            printf("X");
    else
        printf("Y");
    return 0;
}

if-else Hard
A. Prints Y
B. Compilation error
C. Prints X
D. Prints nothing

42 What does this program print?

C
#include <stdio.h>
int main() {
    int x = 2;
    switch (x) {
        case 1: printf("1");
        case 2: printf("2");
        case 3: printf("3"); break;
        default: printf("D");
    }
    return 0;
}

switch statement Hard
A. 23D
B. 23
C. 233
D. 2

43 What is the output of the following code?

C
#include <stdio.h>
int main() {
    int i, sum = 0;
    for (i = 1; i <= 10; i++) {
        if (i % 2 == 0) continue;
        if (i > 7) break;
        sum += i;
    }
    printf("%d", sum);
    return 0;
}

for loop Hard
A. 25
B. 24
C. 16
D. 9

44 What is printed by this code?

C
#include <stdio.h>
int main() {
    int i = 5;
    do {
        printf("%d ", i);
        i -= 2;
    } while (i > 0);
    return 0;
}

looping statements-while and do-while loop Hard
A. 5 3 1
B. 5 3
C. 3 1
D. 5 3 1 -1

45 What is the output?

C
#include <stdio.h>
int main() {
    int i;
    for (i = 0; i < 5; i++);
        printf("%d", i);
    return 0;
}

for loop Hard
A. Nothing is printed
B. 01234
C. 0
D. 5

46 For which value of n does this print Match?

C
if (n > 0) {
    if (n % 3 == 0) {
        if (n % 2 != 0)
            printf("Match");
    }
}

nested if Hard
A. n = 6
B. n = -9
C. n = 12
D. n = 9

47 What is the output of this code?

C
#include <stdio.h>
int main() {
    int i = 0, sum = 0;
loop:
    i++;
    if (i == 3) goto skip;
    sum += i;
skip:
    if (i < 5) goto loop;
    printf("%d", sum);
    return 0;
}

goto statement Hard
A. 10
B. 9
C. 12
D. 15

48 What is the output of the nested loop?

C
#include <stdio.h>
int main() {
    int i, j, c = 0;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            if (j == 2) break;
            c++;
        }
    }
    printf("%d", c);
    return 0;
}

break and continue statements Hard
A. 9
B. 1
C. 6
D. 3

49 What does the following print?

C
#include <stdio.h>
int main() {
    int x = 1, y = 0;
    switch (x) {
        case 1: y += 1;
        case 2: y += 2;
        default: y += 10;
        case 3: y += 3;
    }
    printf("%d", y);
    return 0;
}

switch statement Hard
A. 16
B. 1
C. 13
D. 6

50 What is the output?

C
#include <stdio.h>
int main() {
    int i, j;
    for (i = 0, j = 10; i < j; i++, j--)
        ;
    printf("%d %d", i, j);
    return 0;
}

for loop Hard
A. 5 5
B. 6 4
C. 5 6
D. 4 6

51 What is the output of this program?

C
#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 100) {
        i *= 2;
    }
    printf("%d", i);
    return 0;
}

looping statements-while and do-while loop Hard
A. 128
B. 64
C. 256
D. 100

52 What does this print for the assignment shown?

C
#include <stdio.h>
int main() {
    int a = 3, b = 5;
    int max = (a > b) ? a : (b > 10 ? b : a + b);
    printf("%d", max);
    return 0;
}

if-else Hard
A. 5
B. 3
C. 8
D. 10

53 What is the output?

C
#include <stdio.h>
int main() {
    int i = 0;
    while (i < 5) {
        i++;
        if (i == 3) continue;
        printf("%d", i);
    }
    return 0;
}

break and continue statements Hard
A. 124
B. 12345
C. 1235
D. 1245

54 Which statement about this code is correct?

C
int x = 2;
switch (x) {
    case 1.5: printf("A"); break;
    case 2: printf("B"); break;
}

switch statement Hard
A. Prints nothing at runtime
B. Prints A
C. Prints B
D. Compilation error: case label is not an integer constant

55 What is the output of this code?

C
#include <stdio.h>
int main() {
    int count = 0;
    for (int i = 1; i <= 3; i++)
        for (int j = i; j <= 3; j++)
            count++;
    printf("%d", count);
    return 0;
}

for loop Hard
A. 5
B. 3
C. 6
D. 9

56 What is printed?

C
#include <stdio.h>
int main() {
    int a = 0;
    if (a = 5)
        if (a == 5)
            printf("P");
        else
            printf("Q");
    else
        printf("R");
    return 0;
}

nested if Hard
A. P
B. Nothing
C. Q
D. R

57 How many times does the body execute?

C
int i = 10;
while (i-- > 0) {
    // body
}

looping statements-while and do-while loop Hard
A. 11
B. 10
C. Infinite
D. 9

58 What does this code output?

C
#include <stdio.h>
int main() {
    int i = 1;
    start:
    if (i <= 3) {
        printf("%d", i);
        i++;
        goto start;
    }
    printf("End");
    return 0;
}

goto statement Hard
A. 1End
B. 123End
C. End
D. 123

59 What is the output?

C
#include <stdio.h>
int main() {
    int x = 5, y = 5, z;
    z = x++ || ++y;
    printf("%d %d %d", x, y, z);
    return 0;
}

condition statements Hard
A. 6 6 5
B. 6 6 1
C. 5 6 1
D. 6 5 1

60 Which classification correctly groups these C control statements?

1. if, if-else, switch
2. while, do-while, for
3. break, continue, goto, return

introduction of control statement Hard
A. Group 1: iteration, Group 2: selection, Group 3: jump
B. Group 1: selection, Group 2: iteration, Group 3: jump
C. Group 1: jump, Group 2: iteration, Group 3: selection
D. Group 1: selection, Group 2: jump, Group 3: iteration