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 elementsB. 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. iterateB. 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 loopB. 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 loopB. 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 executedB. 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. updateB. increment
C. modify
D. None of the above
Q. Can you nest for loops in Java?
A. Yes, but only one level deepB. 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 differenceB. 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 4B.
1 2 3 4 5C.
2D.
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]); }