Search Tutorials


C Loops MCQ Questions and Answers | JavaInUse

C Loops MCQ Questions and Answers

Q. What is the type of loop that is used to execute a block of code as long as a specific condition is true?

A. while loop
B. do-while loop
C. for loop
D. All of the above

Q. What is the output of the following code snippet?

int i = 1;
    while (i <= 5) {
        System.out.print(i);
        i++;
    }
    
A. 12345
B. 1234
C. 123456
D. None of the above

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

A. while loop checks the condition before executing the loop body, do-while loop checks the condition after executing the loop body.
B. while loop continues until the condition is false, do-while loop continues until the condition is true.
C. There is no difference between them.
D. do-while loop is faster than while loop.

Q. What is the purpose of the break statement in a loop?

A. To exit the loop immediately
B. To skip the rest of the current iteration and continue with the next iteration
C. To start a new iteration
D. To exit the program

Q. What is the output of the following code snippet?

int i = 1;
    while (true) {
        System.out.print(i);
        if (i == 3) {
            break;
        }
        i++;
    }
    
A. 123
B. 12
C. 1234
D. An infinite loop

Q. What is the purpose of the continue statement in a loop?

A. To exit the loop immediately
B. To skip the rest of the current iteration and continue with the next iteration
C. To start a new iteration
D. To exit the program

Q. What is the output of the following code snippet?

for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;
        }
        System.out.print(i);
    }
    
A. 1245
B. 12345
C. 125
D. An error

Q. What is the purpose of the for loop?

A. To execute a block of code a fixed number of times
B. To execute a block of code as long as a condition is true
C. To iterate over a collection of items
D. All of the above

Q. What is the output of the following code snippet?

for (int i = 1; i <= 5; i++) {
        System.out.print(i);
    }
    
A. 12345
B. 1
C. 1234
D. 5

Q. How can you iterate over the elements of an array using a for loop?

A. for (int i = 0; i < array.length; i++) {
B. for (int i = 1; i <= array.length; i++) {
C. for (int i = array.length; i > 0; i--) {
D. for (int i = 0; i <= array.length; i++) {





Q. What is the purpose of the enhanced for loop (for-each loop) in Java?

A. To iterate over a collection of items and perform an action on each item
B. To execute a block of code a fixed number of times
C. To iterate over the elements of an array and perform calculations
D. To execute a block of code as long as a condition is true

Q. What is the output of the following code snippet?

int[] numbers = {1, 2, 3, 4, 5};
    for (int num : numbers) {
        System.out.print(num);
    }
    
A. 12345
B. 54321
C. 1
D. 5

Q. What is the purpose of the label in a labeled loop?

A. To give a name to the loop
B. To provide a way to break out of multiple nested loops
C. To improve the performance of the loop
D. To allow for multiple iterations of the loop

Q. How can you use a labeled break statement to break out of a labeled loop?

outerLoop:
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            if (i == 1 && j == 1) {
                break outerLoop;
            }
            System.out.print("i = " + i + ", j = " + j);
        }
    }
    
A. The code will print "i = 0, j = 0" and "i = 0, j = 1"
B. The code will print "i = 0, j = 0", "i = 0, j = 1", and "i = 1, j = 0"
C. The code will print "i = 0, j = 0", "i = 0, j = 1", "i = 1, j = 0", and "i = 1, j = 1"
D. The code will print "i = 0, j = 0", "i = 1, j = 0", and "i = 2, j = 0"

Q. What is the purpose of the "for (;;)" loop?

A. To create an infinite loop
B. To execute a block of code a fixed number of times
C. To iterate over a collection of items
D. To perform a specific action until a condition is met

Q. What will be the output of the following code snippet?

int i;
for (i = 1; i <= 5; i++) {
    if (i % 2 == 0) {
        break;
    }
}

System.out.println(i);
A.
2
B.
6
C.
1
D.
The code will result in an error.

Q. Which of the following code snippets demonstrates the correct usage of a do-while loop to calculate the sum of numbers from 1 to 10?

A.
int sum = 0;
int i = 1;
do {
    sum += i;
    i++;
} while (i <= 10);

System.out.println(sum);
B.
int sum = 0;
int i = 1;

do {
    sum += i;
} while (i++ <= 10);

System.out.println(sum);
C.
int sum = 0, i = 1;
do {
    sum += i++;
} while (i <= 10);

System.out.println(sum);
D.
int sum = 0;
int i = 10;
do {
    sum += i;
    i--;
} while (i >= 1);

System.out.println(sum);

Q. What will be the output of the following code snippet?

int i = 1;
while (i <= 5) {
    System.out.print(i);
    i++;
}
A.
12345
B.
1234
C.
1
D.
An infinite loop will occur.

Q. Which of the following code snippets demonstrates the correct usage of a for loop to calculate the factorial of a number?

A.
int factorial = 1;
int number = 5;
for (int i = 1; i <= number; i++) {
    factorial *= i;
}

System.out.println(factorial);
B.
int factorial = 1;
int number = 5;

for (int i = number; i > 0; i--) {
    factorial *= i;
}

System.out.println(factorial);
C.
int factorial = 5;
int number = 5;

for (int i = 1; i <= number; i++) {
    factorial = factorial * i;
}

System.out.println(factorial);
D.
int factorial = 1;
int number = 5;

for (int i = 2; i <= number; i++) {
    factorial = factorial * i;
}

System.out.println(factorial);

Q. What will be the output of the following code snippet?

int i;
for (i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.print(i);
}
A.
1245
B.
125
C.
12345
D.
An error will occur.

Q. Which of the following code snippets demonstrates the correct usage of a nested loop to print a pattern like this:

*
**
***
****
*****
A.
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
}
B.
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 5; j++) {
        System.out.print("*");
    }
    System.out.println();
}
C.
for (int i = 1; i <= 5; i++) {
    for (int j = i; j <= 5; j++) {
        System.out.print("*");
    }
}
D.
for (int i = 1; i <= 5; i++) {
    int j = 1;
    while (j <= i) {
        System.out.print("*");
        j++;
    }
    System.out.println();
}

Q. What will be the output of the following code snippet?

int i, j;
for (i = 1; i <= 3; i++) {
    for (j = 1; j <= 2; j++) {
        System.out.print(i * j + " ");
    }
    System.out.println();
}
A.
2 4 
4 8 
6 12 
B.
1 2 
3 6 
5 10 
C.
1 2 
2 4 
3 6 
D.
2 
4 
6 

Q. Which of the following code snippets demonstrates the correct usage of a while loop to find the sum of squares of the first 5 positive integers?

A.
int sum = 0, i = 1, num;
while (i <= 5) {
    num = i * i;
    sum += num;
    i++;
}

System.out.println(sum);
B.
int sum = 0, i = 1;
while (i <= 5) {
    sum += i * i;
    i++;
}

System.out.println(sum);
C.
int sum = 0, i = 1;
while (i <= 5) {
    sum = sum + i * i;
    i = i + 1;
}

System.out.println(sum);
D.
int sum = 0, i = 1;
do {
    sum += i * i;
    i++;
} while (i <= 5);

System.out.println(sum);

Q. What will be the output of the following code snippet?

int i = 1;
while (i <= 5) {
    if (i == 3) {
        i += 2;
    } else {
        System.out.print(i);
    }
    i++;
}
A.
125
B.
1245
C.
1235
D.
12345