Sail E0 Webinar
Question


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();
}
}
Options:
A .  1 2 0 0
B .  1 2 1 2
C .  0 0 0 0
D .  System Dependent
Answer: Option B

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 ?
Next Question

Submit Solution

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

Latest Videos

Latest Test Papers