Search Tutorials


Java For Loop MCQ - Multiple Choice Questions And Answers | JavaInUse

Java For Loop MCQ - Multiple Choice Questions And Answers

Q. What is the purpose of a for loop in Java?

A. To iterate over a collection of elements
B. To filter elements based on a condition
C. To transform values
D. To create objects

Q. Which keyword is used to start a for loop in Java?

A. iterate
B. for
C. loop
D. start

Q. What is the syntax for a basic "for" loop in Java?

A. for(int i = 0; i < 10; i++)
B. for(int i = 0; i < 10)
C. for(int i = 0, i < 10, i++)
D. int i = 0; i < 10; i++

Q. What does the initialization statement in a for loop do?

A. It starts the loop
B. It sets the initial value of the loop control variable
C. It determines when the loop should end
D. It increments the loop control variable

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

A. It starts the loop
B. It sets the initial value of the loop control variable
C. It determines when the loop should end
D. It increments the loop control variable

Q. What happens during each iteration of a for loop?

A. The initialization statement is executed
B. The condition statement is executed
C. The update statement is executed
D. All of the above

Q. What statement is used to increment the loop control variable in a for loop?

A. update
B. increment
C. modify
D. None of the above

Q. Can you nest for loops in Java?

A. Yes, but only one level deep
B. No, it is not allowed
C. Yes, you can nest multiple levels deep
D. Only if the loops have the same initialization values

Q. What is the syntax for iterating over an array using a for loop in Java?

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

Q. What is the difference between a for loop and a while loop in Java?

A. There is no difference
B. A for loop can only be used with arrays
C. A for loop has a predefined number of iterations, while a while loop relies on a condition
D. A for loop is faster than a while loop

Q. What is the correct syntax for a basic for loop in Java?

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

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

int[] numbers = {1, 2, 3, 4, 5};

for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] % 2 == 0) {
        System.out.println(numbers[i]);
    }
}
A.
2
4
B.
1
2
3
4
5
C.
2
D.
1
3
5

Q. How do you iterate over an array using an enhanced for loop in Java?

A.
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
    System.out.println(num);
}
B.
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
    System.out.println(arr[i]);
}
C.
int[] arr = {1, 2, 3, 4, 5};
for (int i = arr.length-1; i >= 0; i--) {
    System.out.println(arr[i]);
}




D.
int[] arr = {1, 2, 3, 4, 5};
for (int i = 1; i <= arr.length; i++) {
    System.out.println(arr[i]);
}

Q. What is the output of the following for loop?

for (int i = 0; i <= 10; i = i + 2) {
    System.out.println(i);
}
A.
0
2
4
6
8
10
B.
0
2
4
8
C.
1
3
5
7
9
D.
0
1
2
3
4
5
6
7
8
9
10

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

A.
The continue statement is used to exit the loop and skip to the next iteration.
B.
The continue statement is used to immediately stop the program execution.
C.
The continue statement is used to restart the loop from the beginning.
D.
The continue statement is used to break out of the loop entirely.

Q. How can you iterate over a list using a for-each loop in Java?

A.
List<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
for (int num : myList) {
    System.out.println(num);
}
B.
List<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
for (int i = 0; i < myList.size(); i++) {
    System.out.println(myList.get(i));
}
C.
List<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
for (int i = myList.size()-1; i >= 0; i--) {
    System.out.println(myList.get(i));
}
D.
List<Integer> myList = new ArrayList<>();
myList.add(1);
myList.add(2);
for (int i = 1; i <= myList.size(); i++) {
    System.out.println(myList.get(i));
}

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

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        System.out.println(i + j);
    }
}
A.
0
1
1
2
2
3
B.
0
1
2
3
4
5
C.
0
1
0
1
0
1
D.
1
2
3
2
3
4

Q. Which statement is true about the initialization expression in a for loop?

A.
The initialization expression is executed after each iteration of the loop.
B.
The initialization expression is executed before the loop starts.
C.
The initialization expression is executed only once at the beginning of the loop.
D.
The initialization expression is not required in a for loop.

Q. What happens if the update expression in a for loop is missing?

A.
The loop will still execute normally.
B.
The loop will throw a compilation error.
C.
The loop will run indefinitely.
D.
The loop will exit immediately.

Q. Which of the following statements about the loop variable scope in a for loop is true?

A.
The loop variable is accessible only within the loop block.
B.
The loop variable is available globally throughout the program.
C.
The loop variable can be accessed outside the loop if declared as a class variable.
D.
The loop variable has block-level scope within the loop block.