What is the result?

Given:

What is the result?
A. Compilation fails at line n3 and line n4.
B. Compilation fails at line n1 and line n2,
C. Welcome Visit Count:1 Welcome Visit Count: 2
D. Welcome Visit Count:1 Welcome Visit Count: 2

Download Printable PDF. VALID exam to help you PASS.

4 thoughts on “What is the result?

  1. B :
    # javac Test36.java
    Test36.java:4: error: non-static variable count cannot be referenced from a static context
    count++;
    ^
    Test36.java:5: error: non-static variable count cannot be referenced from a static context
    System.out.println(“Welcome ” + “Visit Count: “+ count);
    ^
    2 errors

    ———————————-
    per suggestion from the compiler, it is needed to declare as “static int count”, then it will be possible to compile the program and get such result:
    #javac Test36.java
    #java Test36
    Welcome Visit Count: 1
    Welcome Visit Count: 2

  2. If we declare a variable outside a static method and use it inside the method, we must make it static and we are allowed to not initialize it. However, if we declare a variable inside a static method we cannot make it static and we have to initialize it. So the answer is B

    3
    1

Leave a Reply

Your email address will not be published.