Languages-C & C Plus Plus [TCS Placement]: Sample Questions 47 - 48 of 119

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 47

C & C Plus Plus

Question

MCQ▾

Which of the following is an abstract data type?

Choices

Choice (4)

a.

double

b.

class

c.

int

d.

string

Answer

b.

Explanation

  • Class is an abstract data type.
  • Classes are the most important feature of C ++ that leads to Object Oriented programming.
  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • Class name must start with an uppercase letter (although not mandatory) .
  • If class name is made of more than one word, then first letter of each word must be in uppercase. Example, class Study, class StudyTonight etc
  • Classes contain data members and member functions, and the access of these data members and variable depends on the access specifiers (discussed in next section) .
  • Classes in c ++ are similar to structure in c- but class defaults to private access control, whereas structure defaults to public.
Given the Image is Define the Class in C ++

Question 48

C & C Plus Plus

Question

MCQ▾

Which constructor function is designed to copy objects of the same class type?

Choices

Choice (4)

a.

Object constructor

b.

Dynamic constructor

c.

Copy constructor

d.

Create constructor

Answer

c.

Explanation

  • Copy constructor copies objects of the same class type
  • A copy constructor is a member function which initializes an object using another object of the same class.
  • Copy constructor is called when a new object is created from an existing object, as a copy of the existing object.
  • Assignment operator is called when an already initialized object is assigned a new value from another existing object.
  • Copy constructor has the following general prototype:

    ClassName (const ClassName &old_obj) ;

  • Example of copy constructor
  1. #include<iostream>
  2. usingnamespacestd;
  3. classPoint
  4. {
  5.     private:
  6.     intx, y;
  7.     public:
  8.     Point(intx1, inty1) { x =x1; y =y1; }
  9.     // Copy constructor
  10.     Point(constPoint &p2) {x =p2.x; y =p2.y; }
  11.     intgetX() { returnx; }
  12.     intgetY() { returny; }
  13. };
  14. intmain()
  15. {
  16.     Pointp1(20, 78); // Normal constructor
  17.     Pointp2 =p1; // Copy constructor
  18.     cout ≪ "p1.x =" ≪ p1.getX() ≪ ", p1.y =" ≪ p1.getY();
  19.     cout ≪ "p2.x =" ≪ p2.getX() ≪ ", p2.y =" ≪ p2.getY();
  20.     return0;
  21. }
  • Output:

p1. x = 20 , p1. y = 15

p1. x = 20 , p1. y = 15

Developed by: