What is the result of the following program compilation and execution?

public class Test {
    public static void main(String[] args) {
        int i = 1;
        long j = 1;
        if (i == 1) {
            int k = 1;
            if (i == k) {
                System.out.println("i equals k");
        } else if (j == k) {
            System.out.println("j equals k");
Variable k is declared inside if block, thus it is only visible inside if block of if-else statement. We will have a compilation error in the line
 } else if (j == k) {
that says compiler is not able to find symbol k.
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback