Menu
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();
        result.setVal(val);
        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);
        myClass.setTest(test.clone());
        return myClass;
    }
    protected MyClass copy() {
        MyClass myClass = new MyClass(name, id);
        myClass.setTest(test);
        return myClass;
    }
    public void setName(String name) {this.name = name;}
    public void setId(Integer id) { this.id = id; }
    public boolean equals(Object arg0) {
        return ((MyClass) arg0).name.equals(name) && ((MyClass) arg0).id.equals(id);
    }
    public MyClass(String name, Integer id) {
        this.name = name;
        this.id = id;
        setTest(new CloneTest());
    }
}
public class CopyTest {
    public static void main(String... str) {
        MyClass myClass1 = new MyClass("Mahi", 575);
        myClass1.setId(555);
        MyClass clone = myClass1.clone();
        MyClass copy = clone.copy();
        myClass1.setName("Nani");
        MyClass classCopy = myClass1.copy();
        myClass1.setId(255);
        CloneTest cloneTest = new CloneTest();
        cloneTest.setVal(122);
        myClass1.setTest(cloneTest);
        System.out.print(copy.equals(clone) + " " + (copy == clone) + " " + (copy == classCopy) + " " + copy.equals(classCopy) + " ");
        System.out.print(clone.getTest().val + " ");
        System.out.println(classCopy.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


A
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]

EVERY OBJECT IS INDEPENDENT
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