Question 41

Which constructor will be called by the following lines of code.?

Which constructor will be called by the following lines of code.? (i) Student S1; (ii) Student S2 = S1
  1. AFirst copy constructor, then default constructor.
  2. BFirst default constructor, then copy constructor.
  3. CDefault constructors for both lines of code.
  4. DCopy constructors for both lines of code.
Show answerHide answer

Correct answer: (B) First default constructor, then copy constructor.

Explanation

Student S1; creates an object with no arguments, so the default constructor is called. Student S2 = S1; initialises a new object from an existing one, so the copy constructor is called.

From DSSSB TGT Computer Science Practice Set 1

This is question 41 of DSSSB TGT Computer Science Practice Set 1. See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.