Output of Java Programs | Set 40 (for loop)

27 Sep, 2017
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 Output2. 103. Compile time error4. 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 GEEKS2. Compile time error3. HELLO GEEKSHELLO GEEKSHELLO GEEKS4. 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. HIHELLO GEEKS2. No Output3. Compile time error4. 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 error2. HELLO GEEKS3. 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.GEEKSWELCOMEGEEKSWELCOME2.No Output3.Compile time error4.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.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.