Output of Java Programs | Set 40 (for loop)

Prerequisite : Loops in Java

1. what will be the output of the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        for (int i = 0; i < 10; i++)
            int x = 10;
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        for (int i = 0, String = "GFG"; i < 2; i++)
            System.out.println("HELLO GEEKS");
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        int i = 0;
        for (System.out.println("HI"); i < 1; i++)
            System.out.println("HELLO GEEKS");
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        for (int i = 0;; i++)
            System.out.println("HELLO GEEKS");
    }
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

public
class Test {
public
    static void main(String[] args)
    {
        for (int i = 0; i < 1; System.out.println("WELCOME"))
            System.out.println("GEEKS");
    }
}

chevron_right


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.



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.