Question
class A {
int x;
int y;
void display() {
System.out.print(x + " " + y);
}
}
class Output {
public static void main(String args[]) {
A obj1 = new A();
A obj2 = new A();
obj1.x = 1;
obj1.y = 2;
obj2 = obj1.clone();
obj1.display();
obj2.display();
}
}
What is the output of this program?
class A {
int x;
int y;
void display() {
System.out.print(x + " " + y);
}
}
class Output {
public static void main(String args[]) {
A obj1 = new A();
A obj2 = new A();
obj1.x = 1;
obj1.y = 2;
obj2 = obj1.clone();
obj1.display();
obj2.display();
}
}
Answer: Option B
Was this answer helpful ?
clone() method of object class is used to generate duplicate copy of the object on which it
is called. Copy of obj1 is generated and stored in obj2.
Output:
$ javac Output.java
$ java Output
1 2 1 2
Was this answer helpful ?
More Questions on This Topic :
Question 6.
Which of these methods must be made static?
....
Submit Solution