Cloning and Copy Constructors: past questions

9 questions · 44 marks · 9 papers. Try each one before checking.

Solve them and check answers →Read the notes first

2023 Aug

Question 4 MCQ · 3 marks

Consider the code given below.

interface Shape {
    public abstract int area();
}
class Square implements Shape, Cloneable{
    int side;
    public Square(int s){
        side = s;
    }
    public int area(){
        return side * side;
    }
    public Square clone() throws CloneNotSupportedException{
        return (Square)super.clone();
    }
}
public class Test{
    public static void main(String[] args){
        Square s1 = new Square(5);
        try{
            Square s2 = s1.clone();
            s1.side = 3;
            System.out.print(s1.area() + s2.area());
        }
        catch(CloneNotSupportedException e){
            System.out.println("Cloning not supported");
        }
    }
}

What will the output be?

  1. 18

  2. 50

  3. 34

  4. Cloning not supported

2023 Dec

Question 5 MCQ · 3 marks

Consider the Java code given below.

class Person {
    String name;
    // Constructor
    public String toString(){
        return name;
    }
}
class Instructor extends Person implements Cloneable {
    int salary;
    // Constructor
    public Instructor clone() throws CloneNotSupportedException{
        return (Instructor)super.clone();
    }
    public String toString(){
        return (super.toString() + ": " + salary);
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException{
        Instructor i1 = new Instructor("Thangarajan", 150000);
        Instructor i2 = i1.clone();
        i2.name = "Monika";
        i2.salary = 240000;
        System.out.println(i1 + "\n" + i2);
    }
}

What will the output be?

  1. Thangarajan: 240000
    Monika: 240000
  2. Thangarajan: 240000
    Monika: 150000
  3. Thangarajan: 150000
    Monika: 150000
  4. Thangarajan: 150000
    Monika: 240000

2024 Mar 24

Question 8 MCQ · 6 marks

Consider the Java code given below.

class Student implements Cloneable {
    String studentName;
    public Student(String n) {
        studentName = n;
    }
    public Student clone() throws CloneNotSupportedException {
        return (Student) super.clone();
    }
}
class Game implements Cloneable {
    String gameName;
    Student student1;
    Student student2;
    public Game(String gN, Student s1, Student s2) {
        gameName = gN;
        student1 = s1;
        student2 = s2;
    }
    public Game clone() throws CloneNotSupportedException {
        Game g = (Game) super.clone();
        g.student1 = g.student1.clone();
        g.student2 = g.student2.clone();
        return g;
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student s1 = new Student("Ramesh");
        Student s2 = new Student("Jogesh");
        Game obj1 = new Game("ABC", s1, s2);
        Game obj2 = obj1.clone();
        obj2.student1.studentName = "Shubham";
        obj2.gameName = "XYZ";
        System.out.println(obj1.gameName + " : " + obj1.student1.studentName);
        System.out.println(obj2.gameName + " : " + obj2.student1.studentName);
    }
}

What will the output be?

  1. ABC : Shubham
    ABC : Shubham
  2. XYZ : Shubham
    XYZ : Shubham
  3. ABC : Ramesh
    XYZ : Shubham
  4. ABC : Shubham
    XYZ : Shubham

2024 Aug 04

Question 7 MCQ · 6 marks

Consider the Java code given below.

import java.util.*;
class Book implements Cloneable {
    String title;
    String author;
    public Book(String t, String a) {
        this.title = t;
        this.author = a;
    }
    public Book clone() throws CloneNotSupportedException {
        return (Book) super.clone();
    }
    public String toString() {
        return title + ":" + author;
    }
}
class Library implements Cloneable {
    String libraryName;
    List<Book> books;
    public Library(String l, List<Book> b) {
        this.libraryName = l;
        this.books = b;
    }
    public Library clone() throws CloneNotSupportedException {
        Library clonedLibrary = (Library) super.clone();
        clonedLibrary.books = new ArrayList<>();
        for (Book book : this.books) {
            clonedLibrary.books.add(book.clone());
        }
        return clonedLibrary;
    }
}
public class TestCloning {
    public static void main(String[] args) throws CloneNotSupportedException {
        List<Book> books = new ArrayList<>();
        books.add(new Book("1984", "George Orwell"));
        books.add(new Book("Outline", "Rachel Cusk"));
        Library library1 = new Library("Central Library", books);
        Library library2 = library1.clone();
        library2.books.get(0).title = "Hanging";
        System.out.println(library1.books);
        System.out.println(library2.books);
    }
}

What will the output be?

  1. [Hanging:George Orwell, Outline:Rachel Cusk]
    [Hanging:George Orwell, Outline:Rachel Cusk]
  2. [1984:George Orwell, Outline:Rachel Cusk]
    [1984:George Orwell, Outline:Rachel Cusk]
  3. [1984:George Orwell, Outline:Rachel Cusk]
    [Hanging:George Orwell, Hanging:Rachel Cusk]
  4. [1984:George Orwell, Outline:Rachel Cusk]
    [Hanging:George Orwell, Outline:Rachel Cusk]

2025 Mar 16

Question 5 MCQ · 6 marks

Consider the code given below.

interface Vehicle {
    public abstract int getMileage();
}
class Car implements Vehicle, Cloneable {
    int mileage;
    public Car(int m) {
        mileage = m;
    }
    public int getMileage() {
        return mileage;
    }
    public Car clone() throws CloneNotSupportedException {
        return (Car) super.clone();
    }
}
public class Test {
    public static void main(String[] args) {
        Car car1 = new Car(25);
        try {
            Car car2 = car1.clone();
            car1.mileage = 30;
            System.out.print(car1.getMileage() + car2.getMileage());
        } catch (CloneNotSupportedException e) {
            System.out.println("Cloning not supported");
        }
    }
}

What will the output be?

  1. 55

  2. 50

  3. 60

  4. Cloning not supported

2026 Apr 06

Question 12 MCQ · 7 marks

Consider the code given below about an aircraft maintenance record.

class Aircraft implements Cloneable {
    String airline;
    int[] flightHours;
    public Aircraft(String m, int[] h) {
        airline = m;
        flightHours = h;
    }
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        int[] hrs = {50, 60, 70};
        Aircraft a1 = new Aircraft("Air India", hrs);
        Aircraft a2 = (Aircraft) a1.clone();
        Aircraft a3 = a1;
        a2.flightHours[1] = 90;
        a3.airline = "IndiGo";
        System.out.println(a1.airline + " " + a1.flightHours[1]);
        System.out.println(a2.airline + " " + a2.flightHours[1]);
        System.out.println(a3.airline + " " + a3.flightHours[1]);
    }
}

What will the output be?

  1. IndiGo 90
    Air India 90
    IndiGo 90
  2. IndiGo 60
    Air India 90
    IndiGo 60
  3. Air India 60
    Air India 90
    IndiGo 60
  4. Air India 60
    Air India 60
    IndiGo 60

2026 Jul 16

Question 9 MCQ · 3 marks

Consider the following Java code.

class Dataset{
    String name;
    String[] features;

    public Dataset(String n, String[] f){
        name = n;
        features = f;
    }
    public Dataset(Dataset d){
        this.name = d.name;
        this.features = d.features;
    }
}
public class Test{
    public static void main(String[] args) {
        String[] f = {"Age", "Income", "Score"};
        Dataset d1 = new Dataset("CustomerData", f);
        Dataset d2 = new Dataset(d1);
        Dataset d3 = d1;
        d2.name = "SalesData";
        d2.features[0] = "Salary";
        System.out.println(d1.name + ", " + d1.features[0]);
        System.out.println(d2.name + ", " + d2.features[0]);
        System.out.println(d3.name + ", " + d3.features[0]);
    }
}

What will the output be?

  1. CustomerData, Salary
    SalesData, Salary
    CustomerData, Salary
  2. CustomerData, Age
    SalesData, Salary
    CustomerData, Age
  3. SalesData, Age
    SalesData, Salary
    CustomerData, Age
  4. SalesData, Age
    SalesData, Salary
    SalesData, Age

2025 Aug 03

Question 11 MCQ · 7 marks

Consider the code given below.

interface Shape {
    public abstract double getArea();
}
class Rectangle implements Shape, Cloneable {
    protected double width;
    protected double height;
    public Rectangle(double w, double h) {
        width = w;
        height = h;
    }
    public double getArea() {
        return width * height;
    }
    public Rectangle clone() throws CloneNotSupportedException {
        return (Rectangle) super.clone();
    }
}
class Square extends Rectangle {
    public Square(double side) {
        super(side, side);
    }
    public Square clone() throws CloneNotSupportedException {
        return (Square) super.clone();
    }
}
public class ShapeTest {
    public static void main(String[] args) {
        try {
            Square s1 = new Square(5);
            Square s2 = s1.clone();
            s1.width = 10;
            System.out.print(s1.getArea() + s2.getArea());
        } catch (CloneNotSupportedException e) {
            System.out.println("Cloning not supported");
        }
    }
}

What will the output be?

  1. 125.0

  2. 75.0

  3. 50.0

  4. Cloning not supported

2022 July

Question 2 MCQ · 3 marks

Consider the Java code given below.

import java.util.*;
class Address implements Cloneable{
    private String city;
    private int pinCode;
    // constructor here to instantiate city, pinCode
    public void setCity(String c){
        city = c;
    }
}
class Temple implements Cloneable{
    private Address adr;
    private String tempName;
    // constructor here to instantiate adr, tempName
    public Address getAddress(){
        return adr;
    }
    public void setTempName(String t){
        tempName = t;
    }
    public Temple clone() throws CloneNotSupportedException{
        Temple t = (Temple) super.clone();
        return t;
    }
    //Overrides the method toString()
    // to return tempName + ":"+ city + ":" + pinCode;
}
public class Main{
    public static void main(String[] args){
        Temple t1 = new Temple(new Address("Madurai", 625001), "Meenakshi temple");
        try{
            Temple t2 = t1.clone();
            t2.setTempName("Golden temple");
            t2.getAddress().setCity("Amritsar");
            System.out.println(t1);
            System.out.println(t2);
        }
        catch(CloneNotSupportedException e){
            System.out.println("Cloning not supported");
        }
    }
}

What will the output be?

  1. Meenakshi temple:Madurai:625001
    Golden temple:Amritsar:625001
  2. Meenakshi temple:Amritsar:625001
    Golden temple:Amritsar:625001
  3. Golden temple:Amritsar:625001
    Golden temple:Amritsar:625001
  4. Meenakshi temple:Madurai:625001
    Golden temple:Madurai:625001

Check your answers →

Weeks covering this topic

Other Java topics