SCJP Module-5 Question-6


 

SCJP Module-5 Question-6

This program will test your understanding of while loop in Java and is helpful for SCJP exam.

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.

Ads