Open In App

Output of Java Programs | Set 40 (for loop)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Loops in Java

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




public
class 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?




public
class 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?




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");
    }
}


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?




public
class 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?




public
class 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.



Last Updated : 27 Sep, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads