Prerequisite : Loops in Java
1. what will be the output of the following program?
publicclass Test { public static void main(String[] args) { for (int i = 0; i < 10; i++) int x = 10; } } |
Options:
1. No Output
2. 10
3. Compile time error
4. 10 (10 times)
The answer is option (3)
Explanation: Curly braces are optional and without curly braces we can take only one statement under for loop which should not be declarative statement. Here we are declaring a variable that’s why we will get compile time error saying error: variable declaration not allowed here.
2. what will be the output of the following program?
publicclass Test { public static void main(String[] args) { for (int i = 0, String = "GFG"; i < 2; i++) System.out.println("HELLO GEEKS"); } } |
Options:
1. HELLO GEEKS
2. Compile time error
3. HELLO GEEKS
HELLO GEEKS
HELLO GEEKS
4. No Output
The answer is option (2)
Explanation: Initialization part of the for loop will be executed only once in the for loop life cycle. Here we can declare any number of variables but should be of same type. By mistake if we are trying to declare different data types variables then we will get compile time error saying error: incompatible types: String cannot be converted to int.
3. what will be the output of the following program?
publicclass Test { public static void main(String[] args) { int i = 0; for (System.out.println("HI"); i < 1; i++) System.out.println("HELLO GEEKS"); } } |
Output:
1. HI
HELLO GEEKS
2. No Output
3. Compile time error
4. HELLO GEEKS
The answer is option (1)
Explanation:I n the initialization section we can take any valid java statement including System.out.println(). In the for loop initialization section is executed only once that’s why here it will print first HI and after that HELLO GEEKS
4. what will be the output of the following program?
publicclass Test { public static void main(String[] args) { for (int i = 0;; i++) System.out.println("HELLO GEEKS"); } } |
Options:
1. Compile time error
2. HELLO GEEKS
3. HELLO GEEKS (Infinitely)
4. Run-time Exception
The answer is option (3)
Explanation: In the conditional check we can take any valid java statement but should be of type Boolean. If we did not give any statement then it always returns true.
5. what will be the output of the following program?
publicclass Test { public static void main(String[] args) { for (int i = 0; i < 1; System.out.println("WELCOME")) System.out.println("GEEKS"); } } |
Options:
1.GEEKS
WELCOME
GEEKS
WELCOME
2.No Output
3.Compile time error
4.GEEKS WELCOME(Infinitely)
Output:
The answer is option (4)
Explanation: In increment-decrement section we can take any valid java statement including System.out.println(). Here in the increment/decrement section, a statement is there, which result the program to go to infinite loop.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Output of Java Programs | Set 36 (do-while loop)
- Output of C programs | Set 56 (While loop)
- Output of C programs | Set 57 (for loop)
- Output of Java Programs | Set 12
- Output of Java programs | Set 10 (Garbage Collection)
- Output of Java programs | Set 13 (Collections)
- Output of Java Programs | Set 14 (Constructors)
- Output of Java Programs | Set 21 (Type Conversions)
- Output of Java programs | Set 24 (Final Modifier)
- Output of Java programs | Set 29
- Output of Java Programs | Set 30
- Output of Java Programs | Set 31
- Output of Java Programs | Set 32
- Output of Java Programs | Set 34 (Collections)
- Output of Java Programs | Set 33 (Collections)
- Output of Java Programs | Set 35 (Decision Making)
- Output of Java Programs | Set 37 (If-else)
- Output of Java Programs | Set 38 (Arrays)
- Output of Java Programs | Set 39 (throw keyword)
- Output of Java Programs | Set 41 (try-catch)

