Object Oriented Programming using Java Questions and Answers – Copy Constructors

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Copy Constructors”.

1. What will be the output of the following Java code?

public class Main
{
    String s;
    Main(String s)
    {
        this.s = s;
    }
    Main(Main a)
    {
        s = a.s;
    }
    void Print()
    {
        System.out.println(s);
    }
    public static void main(String[] args)
    {
        Main a = new Main("Sanfoundry");
        Main b = new Main(a);
        b.Print();
    }
}

a) Compilation Error
b) Runtime Error
c) a
d) Sanfoundry
View Answer

Answer: d
Explanation: The copy constructor takes an object of the same class as an argument and then copies the value of the string variable s of the object passed to that of the object veing created.
Output:

$ javac Main.java
$ java Main
Sanfoundry

2. What will be the output of the following Java code?

advertisement
advertisement
public class Main
{
    int a;
    Main(int a)
    {
        this.a = a;
    }
    Main(Main s)
    {
        a = 10;
    }
    void disp()
    {
        System.out.println(a);
    }
    public static void main(String[] args)
    {
        Main a = new Main(20);
        Main b = new Main(a);
        a.disp();
        b.disp();
    }
}

a)

10
10

b)

advertisement
20
20

c)

20
10

d)

advertisement
10
20
View Answer
Answer: c
Explanation: For the object a, the value of the variable a is set to 20. When b is created, the copy constructor is called, but the value here is being set to 10.
Output:

$ javac Main.java
$ java Main
20
10

3. What will be the output of the following Java code?

public class Main
{
    int a;
    int b;
    Main(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
    Main(Main s)
    {
        a = s.a;
        b = s.b;
    }
    void Set()
    {
        a = 10;
        b = 20;
    }
    void calc()
    {
        System.out.println(a*b);
    }
    public static void main(String[] args)
    {
        Main a = new Main(20, 30);
        Main b = new Main(a);
        a.calc();
        a.Set();
        b.calc();
    }
}

a)

600
200

b)

200
200

c)

600
600

d)

200
600
View Answer
Answer: c
Explanation: Since a copy constructor is being used instead of equating the objects, they do not correspond to the same object, therefore changing one object doesn’t affect the other.
Output:

$ javac Main.java
$ java Main
600
600

4. What will be the output of the following Java code?

public class Main
{
    String s;
    Main(String s)
    {
        this.s = s;
    }
    Main(Main a)
    {
        s = a.s;
    }
    void pos(int a)
    {
        System.out.println(s.charAt(a));
    }
    public static void main(String[] args)
    {
        Main a = new Main("Copy Constructor");
        Main b = new Main(a);
        b.pos(6);
    }
}

a) Compilation Error
b) C
c) o
d) a
View Answer

Answer: c
Explanation: The charAt function returns the character at the index passed to it in the string calling it. As indexing starts at 0, the 6th index corresponds to the 7th position in the string.
Output:

$ javac Main.java
$ java Main
o

5. What will be the output of the following Java code?

public class Main
{
    String s;
    Main(String s)
    {
        this.s = s;
    }
    Main(Main a)
    {
        s = a.s;
    }
    void print()
    {
        System.out.println(this);
    }
    public static void main(String[] args)
    {
        Main a = new Main("Copy Constructor");
        Main b = new Main(a);
        a.print();
        b.print();
    }
}

a) Compilation Error
b)

Copy Constructor
Copy Constructor

c)

Main@2a139a55
Main@15db9742

d)

Main@15db9742
Main@15db9742
View Answer
Answer: c
Explanation: When an object is printed, it is printed in the format ClassName@HashCode. Since a copy constructor is used here, they do not correspond to the same object, hence have different hashcodes.
Output:

$ javac Main.java
$ java Main
Main@2a139a55
Main@15db9742

6. What will be the output of the following Java code?

public class Main
{
    String s;
    Main(String s)
    {
        this.s = s;
    }
    void print()
    {
        System.out.println(this);
    }
    public static void main(String[] args)
    {
        Main a = new Main("Copy Constructor");
        Main b = a;
        a.print();
        b.print();
    }
}

a) Compilation Error
b)

Copy Constructor
Copy Constructor

c)

Main@2a139a55
Main@2a139a55

d)

Main@2a139a55
Main@15db9742
View Answer
Answer: c
Explanation: The object b is equated to b, hence linking it to the same object. When an object is printed, it is printed in the format ClassName@HashCode. Since both of them correspond to the same object, they have the same hashcode.
Output:

$ javac Main.java
$ java Main
Main@2a139a55
Main@2a139a55

7. What will be the output of the following Java code?

public class Main
{
    int a;
    Main(int a)
    {
        this.a = a;
    }
    Main(Main s)
    {
        a = s.a;
    }
    void set()
    {
        a = 40;
    }
    void display()
    {
        System.out.println(a);
    }
    public static void main(String[] args)
    {
        Main a = new Main(10);
        Main b = new Main(a);
        Main c = a;
        a.display();
        a.set();
        b.display();
        c.display();
    }
}

a)

10
10
10

b)

10
40
40

c)

10
10
40

d) Compilation Error
View Answer

Answer: c
Explanation: When an object is copied using the copy constructor, it creates a new reference for that object, thus changing it won’t affect the other object. Equating an object links both the object to the same reference, hence changing any one affects the other.
Output:

$ javac Main.java
$ java Main
10
10
40

8. What will be the output of the following Java code?

public class Main
{
    char s;
    Main(char s)
    {
        this.s = s;
    }
    Main(Main a)
    {
        s = 's';
    }
    void Char()
    {
        System.out.println((int)s);
    }
    public static void main(String[] args)
    {
        Main a = new Main('S');
        Main b = new Main(a);
        a.Char();
        b.Char();
    }
}

a)

83
83

b) Compilation Error
c)

115
83

d)

83
115
View Answer
Answer: d
Explanation: The copy constructor changes the value of String s to the character ‘s’. The ascii value of ‘S’ is 83 and ‘s’ is 115.
Output:

$ javac Main.java
$ java Main
83
115

9. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    LinkedList l = new LinkedList();
    Main(int i)
    {
        l.add(i);
    }
    Main(Main a)
    {
        l = (LinkedList) a.l.clone();
    }
    void push(int a)
    {
        l.add(a);
    }
    void size()
    {
        System.out.println(l.size());
    }
    public static void main(String[] args)
    {
        Main a = new Main(5);
        Main b = new Main(a);
        a.push(10);
        b.size();
        a.size();
    }
}

a)

0
1

b)

1
2

c)

2
2

d)

1
1
View Answer
Answer: b
Explanation: The copy constructor creates a clone of the linkedlist, hence they do not correspond to the same linked list, hence changing a doesn’t change b.
Output:

$ javac Main.java
$ java Main
1
2

10. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    LinkedList l = new LinkedList();
    Main(int i)
    {
        l.add(i);
    }
    Main(Main a)
    {
        l = (LinkedList) a.l.clone();
    }
    void push(int a)
    {
        l.add(a);
    }
    void size()
    {
        System.out.println(l.size());
    }
    public static void main(String[] args)
    {
        Main a = new Main(10);
        Main b = a;
        a.push(20);
        b.size();
        a.size();
    }
}

a)

2
2

b)

1
2

c)

2
2

d)

1
1
View Answer
Answer: c
Explanation: The object b is equated to a, which links both the objects to the same reference, therefore a change in a results a change in b.
Output:

$ javac Main.java
$ java Main
2
2

11. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    LinkedList l = new LinkedList();
    Main(int i)
    {
        l.add(i);
    }
    Main(Main a)
    {
        l = (LinkedList) a.l.clone();
    }
    public static void main(String[] args)
    {
        Main a = new Main(5);
        Main b = new Main(a);
        System.out.println(b.getClass());
        System.out.println(a.getClass().getSuperclass());
    }
}

a)

class Main
class Object

b)

class Main
class java.io.Object

c)

class Main
class java.lang.Object

d) Compilation Error
View Answer

Answer: c
Explanation: A class is an object of the superclass Object of the lang package of Java. Therefore when the superclass of Main is called, it returns java.lang.Object.
Output:

$ javac Main.java
$ java Main
class Main
class java.lang.Object

12. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    LinkedList l = new LinkedList();
    Main(int i)
    {
        l.add(i);
    }
    Main(Main a)
    {
        l = (LinkedList) a.l.clone();
    }
    void className()
    {
        System.out.println(l.getClass());
        System.out.println(l.getClass().getSuperclass());
    }
    public static void main(String[] args)
    {
        Main a = new Main(5);
        Main b = a;
        b.className();
    }
}

a)

class java.lang.LinkedList
class java.lang.AbstractSequentialList

b)

class java.io.LinkedList
class java.io.AbstractSequentialList

c)

class java.util.LinkedList
class java.util.AbstractSequentialList

d) Compilation Error
View Answer

Answer: c
Explanation: The LinkedList is a subclass of the class AbstractSequentialList in the util package of java. The classname() function therefore executes the getClass() method which returns the paths of Linked list and Sequential LinkedList respectively.
Output:

$ javac Main.java
$ java Main
class java.util.LinkedList
class java.util.AbstractSequentialList

13. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    LinkedList l = new LinkedList();
    Main(int i)
    {
        l.add(i);
    }
    Main(Main a)
    {
        l = (LinkedList) a.l.clone();
    }
    void push(int a)
    {
        l.add(a);
    }
    void print()
    {
        System.out.println(l);
    }
    public static void main(String[] args)
    {
        Main a = new Main(5);
        Main b = new Main(a);
        a.push(10);
        a.push(20);
        b.push(30);
        b.push(40);
        b.print();
        a.print();
    }
}

a)

{30, 40}
{5, 10, 20} 

b)

{5, 30, 40}
{5, 10, 20} 

c)

[30, 40]
[5, 10, 20]

d)

[5, 30, 40]
[5, 10, 20]
View Answer
Answer: d
Explanation: Linked lists are displayed with elements inside square brackets[] separated by commas. Here, the object b is copied from a, hence it retains the 5 that was initially passed to a.
Output:

$ javac Main.java
$ java Main
[5, 30, 40]
[5, 10, 20]

14. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    int a,b;
    Main(int i, int j)
    {
        a=i;
        b=j;
    }
    Main(Main m)
    {
        a=m.a;
        b=m.b;
    }
    void change(int i, int j)
    {
        a=i;
        b=j;
    }
    public static void main(String[] args)
    {
        Main a = new Main(5, 10);
        Main b = new Main(a);
        System.out.println(a.a*a.b);
        System.out.println(b.a*b.b);
        a.change(30,40);
        System.out.println(b.a*b.b);
    }
}

a)

50
50
50

b)

50
50
1200

c)

50
1200
1200

d)

50
1200
50
View Answer
Answer: a
Explanation: The object b is copied from object a using a copy constructor, hence they aren’t linked to the same reference. Therefore changing a doesn’t change b.
Output:

$ javac Main.java
$ java Main
50
50
50

15. What will be the output of the following Java code?

import java.util.*;
public class Main
{
    int a,b;
    Main(int i, int j)
    {
        a=i;
        b=j;
    }
    Main(Main m)
    {
        a=m.a;
        b=m.b;
    }
    void push()
    {
        System.out.println(a<<b);
    }
    public static void main(String[] args)
    {
        Main a = new Main(27, 2);
        Main b = new Main(a);
        b.push();
 
    }
}

a) 108
b) 27
c) 216
d) Compilation Error
View Answer

Answer: a
Explanation: The left shift operator shifts the bits of the operand to the left followed by 0s. Here the bits of a are pushed to the left 2 times, hence increasing the value to 4 times the original value.
Output:

$ javac Main.java
$ java Main
108

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.