Java Programming - Flow Control - Discussion

Discussion :: Flow Control - Finding the output (Q.No.11)

11. 

What will be the output of the program?

for (int i = 0; i < 4; i += 2) 
{ 
    System.out.print(i + " "); 
} 
System.out.println(i); /* Line 5 */

[A]. 0 2 4
[B]. 0 2 4 5
[C]. 0 1 2 3 4
[D]. Compilation fails.

Answer: Option D

Explanation:

Compilation fails on the line 5 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop.


Mohan said: (Jan 9, 2012)  
Compilation fails on the line 5 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop.

Archit Rai Saxena said: (Mar 27, 2014)  
if i has declared within the loop. then what will be the output.

for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
System.out.println(i);
}

Raman said: (Sep 26, 2015)  
If I has declared within the loop. Then what will be the output.

for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
System.out.println(i);
}

O/p = 0 0.

2 2.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.