Skip to content

UGC NET CS 2019Programming Languages and Computer Graphics › Object Oriented ProgrammingVerified

UGC NET CS 2019 PYQ: What is the output of the following JAVA program? public class Good { private int m; public Good (int…

What is the output of the following JAVA program?

public class Good {
    private int m;
    public Good (int m){this.m=m;}
    public Boolean equals (Good n) {return n.m==m;}
    public static void main (string args[]){
        Good m1 = new Good(22);
        Good m2 = new Good(22);
        Object s1 = new Good(22);
        Object s2 = new Good(22);
        System.out.println(m1.equals(m2));
        System.out.println(s1.equals(s2));
        System.out.println(m1.equals(s2));
        System.out.println(s1.equals(m2));
    }
}
  1. A
    True, True, False, False
  2. B
    True, False, True, False
  3. C
    True, True, False, True
  4. D
    True, False, False, False
    Correct answer

Answer

(D) True, False, False, False

Approach

Check which overload of equals is invoked at compile time based on the static type of the reference.

Reviewed by UGC IQ Editorial Team.