Topics Index

Creating Class with multiple methods1675

If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Class with multiple methods1675
What will be the output of the following program?
class CloneTest extends Object {
    int val = 50;
    protected CloneTest clone() {
        CloneTest result = new CloneTest();
        return result;
    public void setVal(int val) {this.val = val;}
class MyClass {
    String name;
    Integer id;
    CloneTest test;
    public CloneTest getTest() { return test; }
    public void setTest(CloneTest test) {this.test = test;}
    protected MyClass clone() {
        MyClass myClass = new MyClass(name, id);
        return myClass;
    protected MyClass copy() {
        MyClass myClass = new MyClass(name, id);
        return myClass;
    public void setName(String name) { = name;}
    public void setId(Integer id) { = id; }
    public boolean equals(Object arg0) {
        return ((MyClass) arg0).name.equals(name) && ((MyClass) arg0).id.equals(id);
    public MyClass(String name, Integer id) { = name; = id;
        setTest(new CloneTest());
public class CopyTest {
    public static void main(String... str) {
        MyClass myClass1 = new MyClass("Mahi", 575);
        MyClass clone = myClass1.clone();
        MyClass copy = clone.copy();
        MyClass classCopy = myClass1.copy();
        CloneTest cloneTest = new CloneTest();
        System.out.print(copy.equals(clone) + " " + (copy == clone) + " " + (copy == classCopy) + " " + copy.equals(classCopy) + " ");
        System.out.print(clone.getTest().val + " ");

A. true false false false 50 50
B. true false false true 50 50
C. true false false false 50 122
D. true false false true 50 122
E. Some other output
F. Compilation Error or Runtime Error
Topic: Object As A Super Class In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans a; but i don't know that concept

Posted by Meghana Trikovala    2014-07-23 12:51:20

ans a
copy.equals(clone) gives true, othrs giv false.
both clonegettest,clascopy .gettest val returns 50

Posted by Maheshwari Natarajan    2014-07-23 12:53:29

ans is a..== wil check whtr 2 refrnce pointg the sam obj r not and equls method wil compare the content

Posted by Uday Kumar    2014-07-23 12:55:45

Answer A.
equals() compare char imside string object.
so copy.equals(clone)  true
== compare 2 objct refernc .

clone n classcopy getTest().val returns 50

Posted by NagaRaj Nataraj    2014-07-23 13:02:05

1. Myclass overrides Object's clone method to perform deep copy i.e CloneTest reference will be separate for cloned object
2. Copy simply creates new Object of Myclass type with same name and id (so it is independent of object that is copied)
3 == checks reference equality .equals of myclass checks content equality (name and id field)
contents of class
myClass1[nahi,122,clonetest->val 122]
clone[mahi,555,clonetest->val 50]
copy[mahi,555,clonetest->val 50]
classcopy[nahi,555,clonetest->val 50]

copy.equals(clone) is true rest all false

Posted by Tushar Ghude    2014-07-23 14:15:48

Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-24 04:48:38

© meritcampus 2019

All Rights Reserved.

Open In App