Which constructor will be called by the following lines of code? (i) Student…
2013
Which constructor will be called by the following lines of code? (i) Student S1; (ii) Student S2 = S1
- A.
First copy constructor, then default constructor.
- B.
First default constructor, then copy constructor.
- C.
Default constructors for both lines of code.
- D.
Copy constructors for both lines of code.
Attempted by 172 students.
Show answer & explanation
Correct answer: B
Answer: The constructors called for each line are explained below.
Student S1; — Default constructor is called because a new object is created without using any existing object.
Student S2 = S1; — Copy constructor is called because a new object (S2) is initialized using an existing object (S1).
Therefore, the correct result is: default constructor for the first line and copy constructor for the second line.