Welcome to Java MCQ (Mulitple Choice Question) series. In this post, we will see one more quiz question on Java Loops.
B. Program will print all odd numbers between 0 to 10
C. Program gives a compilation error
D. None of the above
Java
Java-Quiz
MCQ
What is the output of the following program?
public class Test{
public static void main(String []args){
for(int i = 0; i < 10; i++){
if(i % 2 == 0){
continue;
}
System.out.println(i);
}
}
}
A. Program will print all even numbers between 0 to 10B. Program will print all odd numbers between 0 to 10
C. Program gives a compilation error
D. None of the above
Answer:
B. Program will print all odd numbers between 0 to 10Explanation:
Option B is the correct choice. For loop starts with 0 and goes up to 9 after that the condition becomes false. Inside the loop, if the condition checks if the current value of variable i is divisible by 2 by checking the remainder. If it is 0, the current iteration is skipped using the continue statement. If not, the number is odd (not divisible by 2) and the value is printed.
Comments
Post a Comment