What is the result?

Given:

What is the result?

A. Option A
B. Option B
C. Option C
D. Option D

Download Printable PDF. VALID exam to help you PASS.

3 thoughts on “What is the result?

  1. B
    public class Q118 {
    int ns;
    static int s;
    Q118(int ns){
    if(s<ns){
    s=ns;
    this.ns=ns;
    }
    }
    void doPrint(){
    System.out.println("ns "+ns+" "+ " s "+s);
    }
    public static void main(String[] args) {
    Q118 ref1= new Q118(50);
    Q118 ref2= new Q118(125);
    Q118 ref3= new Q118S(100);//it is not true in if 100<125 not true so ns =0 s 125
    ref1.doPrint();
    ref2.doPrint();
    ref3.doPrint();
    }
    }

  2. Answer B:

    The reason is because all the three instances got initialized BEFORE calling the doPrint() methods. In addition to the fact that the variable “s” is static. Meaning that it retains its last updated value. Therefore, from the last initialization (i.e. of ref3) the value of “s” became 125. After that, the doPrint() methods got called, resulting in printing the latest “s” value to all of the objects, while printing the instance specific value “ns” of each object.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.