Home Tutorial Java Scjp Part5 SCJP Module-5 Question-6

 
 

SCJP Module-5 Question-6
Posted on: July 12, 2010 at 12:00 AM
This program will test your understanding of while loop in Java and is helpful for SCJP exam.

Given a sample code:

1    public class Sample {
2    static int j=5;
3    public static void main(String args[]) {
4    while (j > 0) {
5    j--;
6    }
7    System.out.println(j);
}}

What will be the result of above code ?

(1) 1
(2) 0
(3) 4
(4) The program compilation fails because of line number 2.

Answer:

(2)

Explanation:

The variable j gets initialized to zero and therefore while loop does not get executed.

Related Tags for SCJP Module-5 Question-6: