NCERT Solutions Class 12 Computer Science Constructor and Destructor

NCERT Solutions Class 12 Computer Science Constructor and Destructor have been provided below and is also available in Pdf for free download. The NCERT solutions for Class 12 Computer Science have been prepared as per the latest syllabus, NCERT books and examination pattern suggested in Class 12 by CBSE, NCERT and KVS. Questions given in NCERT book for Class 12 Computer Science are an important part of exams for Class 12 Computer Science and if answered properly can help you to get higher marks. Refer to more Chapter-wise answers for NCERT Class 12 Computer Science and also download more latest study material for all subjects. Constructor and Destructor is an important topic in Class 12, please refer to answers provided below to help you score better in exams

Constructor and Destructor Class 12 Computer Science NCERT Solutions

Class 12 Computer Science students should refer to the following NCERT questions with answers for Constructor and Destructor in Class 12. These NCERT Solutions with answers for Class 12 Computer Science will come in exams and help you to score good marks

Constructor and Destructor NCERT Solutions Class 12 Computer Science

Question 1: Write four characteristics of constructor function used in a class. 
Answer:
(i) A constructor function has no return type.
(ii) Name of constructor is same as that of class.
(iii) A constructor can be called while creating objects.
(iv) If there is no constructor declared in class then default constructor is invoked. (1/2 mark for each characteristic).

 

Question 2: What is a copy constructor ? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it. 
Answer:
A copy constructor is an overloaded constructor in which an object of the same class is passed as reference parameter,
class point
{
int x ;
public :
point ( ) {x = 0 ; } point (Point &p) // copy
constructor
{x = p . x;}
};
void main ( )
{
point p1;
point p2 (p1) ; // copy constructor is called here
//
OR
point p3 = p1 ; //Copy constructor is called here
}

Question 3: Answer the questions (i) and (ii) after going through the following class : 
class Exam
{
int Rollno;
char Cname [25];
float Marks;
public :
Exam( ) //Function 1
Rollno = 0;
strcpy (Cname = " " );
Marks = 0.0;
}
Exam (int Rno, char candnam [ ] //Function 2
{
Rollno = Rno;
strcpy(Cname, candname);
}
~Exam ( ) //Function 3
{
cout<<"Result will be intimated shortly'' <<endl;
}
void Display ( ) //Function 4
{
cout<<"Roll no :"<<Rollno;
cout<<"Name :"<<Cname;
cout<<"Marks :"<<Marks;
}
} ;

(i) Which OOP concept does Function 1 and Function 2 implement. Explain ?
Answer: Constructor Overloading/Polymorphism, as multiple definitions for Constructors are given in the same scope. Function 1 is a Default constructor and function 2 is a Parameterized constructor.

What is Function 3 called? When will it be invoked?
Answer: (ii) Function 3 is a Destructor which is invoked when the object goes out of scope.
 

Question 4: Answer the questions (i) and (ii) after going through the following class :
class planet
{
char name[20];
char distance[20]
public:
planet() //Function 1
{
strcpy(name, "Venus");
strcpy(distance,"38 million km");
}
void display(char na[],char d[]) //Function 2
{
cout<<na<<"has"<<d<<" distance from Earth"<<endl ;
}
planet (char na[],char d[]) //Funciton 3
strcpy(name,na);
strcpy(distance,d);
}
~Planet() //Function 4
{
cout<<"Planetarium time over !!! "«endl;
}
};

(i) What is Function 1 referred as ? When will it be executed ?
(ii) Write suitable C++ statement to invoke Function 2.
Answer:
(i) Constructor
It will be excuted at the time of object creation.
(1 1/2 Mark for each correct answer)
(ii) planet p;
p.display(“Pluto”,”7.5 Billion Km”);
(1/2 Mark for each correct answer)


Question 5: Observe the following C++ code and answer the question (i) and (ii): 
class passenger
{
long PNR;
char Name [20] ;
public :
passenger ( ) //Function 1
{cout<<"Ready"<<endl; }
void Book (long P, char N[ ])
//Function 2

{ PNR = P; strcpy (Name, N) . ; } void print ( ) //Function 3
{ cout<<PNR << Name << endl; } -Passenger ( ) //Function 4
{ cout<<"Booking cancelled ! "<<endl
}
} ;

(i) Fill in the statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code:
void main ( )
{
Passenger P;
__________ //Line 1
__________ //Line 2
} // Ends here
Answer:
(i) P. Book (1234567, “Ravi”); //Line 1
P. Frint (); //Line 2
(ii) Which function will be executed at } // Ends here ? What is this function referred as ?
(ii) Function 4
OR
~ Passenger ()
It is a Destructor function.
(1/2 Mark for writing Function 4 OR ~Passenger ())
It is a Destructor function.
(1/2 Mark for referring Destructor)


Question 6: Answer the questions (i) and (ii) after going through the following C+ + class : 
class Stream
{
int StreamCode ;
char Streamname [20]; float fees;
public:
Stream( ) //Function 1
{
StreamCode=1;
strcpy (Streamname,"DELHI");
fees=1000;
}
void display(float C) //Function 2
{
cout<<StreamCode<<":"<<Streamname <<fees<<endl;
}
~Stream( ) //Function 3
{
cout<<"End of Stream Object"<<endl;
}
Stream (int SC,char S[ ],float F) ;
//Function 4
} ;

(i) In Object Oriented Programming, what are Function 1 and Function 4 combined together referred as? Write the definition of function 4.
(ii) What is the difference between the following statements?
Stream S(11,”Science”,8700);
Stream S=Stream(11,”Science”,8700);
Answer:
(i) Constructor Overloading
Stream (int Sc, char S[], float F)
{
StreamCode = Sc;
strcpy(Streamname, S);
fees = F;
} [1]
(ii) The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method.
Stream S(ll,” Science,8700); – In this statement constructor is called implicitly.
Stream S = Stream(ll,”Science,8700); – Here constructor is called explicitly. 

Question 7: Answer the question (i) and (ii) after going through the following class:
class Book
{
int BookNo; char BookTitle[20];
public:
Book ();
//Function 1
Book (Book &); //Function 2
Book (int, char [ ] );//Function 3
void Buy (); //Function 4
void Sell (); //Function 5
:
:
}

(i) Name the feature of Object Oriented Programming demonstrated by Function 1,Function 2 and Function 3.
(ii) Write statements in C + + to execute function 3 & Function 4 inside the main () function.
Answer:
(i) Constructor overloading is demonstrated. [1] (ii) void main ()
{
Book B;
B. Book (5,'Jay');
B. Buy();
} [1]

Question 8: Answer the questions (i) and (ii) after going through the following class:
class Race
{
int CarNo, Track;
public:
Race ( ) ; //Function 1
Race (int CN); //Function 2
Race (Race &R); //Function 3
void Register! ); //Function 4
void Drive( ); //Function 5
};
void main( )
{
Race R;
}
(i) Out of the following, which of the options is correct way for calling Function 2?
Option 1—Race T(30);
Option 2—Race U(R);
(ii) Name the feature of Object Oriented
Programming, which is illustrated by Function 1, Function 2, and Function 3 combined
Together. 
Answer:
(i) Option 1: Race T(30) is the correct way for calling Function 2.
(ii) Constructor Overloading, i.e., Polymorphism. 

Question 9: Answer the questions (i) and (ii) after going through the following class:
class Motor
{
int MotorNo, Track;
public:
Motor (); //Function 1
Motor (int MN); //Function 2
Motor (Motor & M) ; //Function 3

void Allocate(); //Function 4
void Move(); //Function 5
} ;
void main()
{
MotorM;
}
(i) Out of the following, which of the options is correct way for calling Function 2?
Option 1—Motor N(M);
Option 2—Motor P(10);
(ii) Name the feature of Object Oriented Programming, which is illustrated by Function 1,
Function 2 and Function 3 combined Together. 
Answer:
(i) Option 2: Motor P(10) is the correct way for calling Function 2. 
(ii) Constructor Overloading, i.e., Polymorphism. 

Question 10: What is constructor overloading ? Give an example to illustrate the same. 
Answer:
Declaring constructors with different parameters in a program is called Constructor
overloading. [1]
Example:
class Circle
{
int radius, area; public:
Circle() //Constructor
{
radius=0;
}
Circle(int r)//Constructor Overloaded
{
radius=r;
}
}; [1]

Question 11: What is a Copy Constructor ? Give a suitable example of Copy Constructor using a class in C++ . 
Answer:
Copy Constructor
• A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.
• A copy constructor is a constructor of the form classname (classname &). The compiler will use the copy constructors
• whenever you initialize an instance using the values of another instance of the same type.

• Copying of objects is achieved by the use of a copy constructor and a assignment operator.
Example:
class Sample {int i, j ;
public:
Sample(int a, int b) // constructor {i=a;j =b;}
Sample (Sample & s)
//copy constructor
{j=s.j ; i=s.j;
cout << "\n Copy constructor
working \n";
}
void print (void)
{cout<<i<<j<<"\n";
}
};
Note : The argument to a copy constructor is passed by reference, the reason being that when an argument is passed by value, a copy of it is constructed. But the copy constructor is creating a copy of the object for itself, thus, it calls itself. Again the called copy constructor requires another copy, so again it is called. In fact it calls itself again and again until the compiler runs out of the memory so, in the copy constructor, the argument must be passed by reference. 

Question 12: Observe the following C++ code and answer the questions (i) and (ii) Assume all necessary files are included : 
class COMIC
{
long CCode;
char CTitle [20];
float CPrice;
public :
COMIC () //Member Function 1
{
cout<<"got..."<<endl;
CCode=100;strcpy(CTitle, "Unkown"); CPrice=100;
}
COMIC(int C, char T[],float p) //Member Function 2
CCode=C;
strapy(CTitle, T);
CPrice=P;
}
void Raise(float P)
//Member Function 3

{
void Disp()
//Member Function 4
{
cout<<CCode<<";"<<CTitle<< " ; "<<CPrice<<endl;
}
-COMIC() //Member Function 5
{
cout<<"COMIC discarded! "«endl;
}
} ;
void main() //Line 1
{ //Line 2
COMIC Cl,C2(1001,"Tom and Jerry",75) //Line 3
for (int 1=0;I<4;I++) / //Line 4
{ //Line 5
Cl.Raise(20);C2,Raise(15); //Line 6
Cl.DispO ;C2.Disp() ; //Line 7
} //Line 8
} //Line 9
(i) Which specific concept of object oriented programming out of the following is illustrated by Member Function 1 and Member Function 2 combined together?
• Data Encapsulation
• Data Hiding
• Polymorphism
• Inheritance
(ii) How many times the massage “COMIC Discard!” will be displayed after executing the above C++ code ? Out of Line 1 to Line 9, which line(s) is responsible to display the message
“COMIC Discarded!”
Answer:
(i) Polymorphism
(1 Mark for mentioning the correct option name)
(ii) 2 times Line 9
(1/2 Mark for writing correct number of times)
(1/2 Mark for writing correct line numbers)


Short Answer Type Questions-II 

Question 1: Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute? 
Answer:
Constructor is a member function which creates an object.
Destructor is a member function which deallocates the memory occupied by an object
class ABC

 {
int a, b;
public
ABC () //Constructor
{a=0;b=0;}
~ ABC () //Destructor
{
}
};

Question 2: Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included. 
class FICTION
{
long FCode;
char FTitle [20];
float FPrice;
Public:
FICTION()
//Member Function 1
{
cout < <"Bought"< < endl;
FCode=100;strcpy(FTitle,"Noname ");FPrice=50;
}
FICTION(int C, char T[], float P) //Member Function 2
{
FCode=C;
strcpy(FTitle,T);
FPrice=P;
}
void Increase(float P) //Member Function 3
{
FPrice+=P;
}
void Show()
//Member Function 4
{
cout<<Fcode<< " : "<<FTitle<<":"<<FPrice<<endl;
}
~FICTION ( ) //Member Function 5
}
};
void main() //Line 1
{ //Line 2
FICTION FI, F2(101,"Dare",75); // Line 3

 for (int 1=0;I<4;I++) //Line 4
{ //Line 5
F1.Increase(2 0);F2.
Increase(15); //Line 6
FI.Show(); //Line 7
F2.Show() //Line 8
} //Line 9

(i) Which specific concept of object oriented programming out of the following is illustrated by Member Function 1 and Member Function 2 combined together?
> Data Encapsulation
> Data Hiding
> Polymorphism
> Inheritance

(ii) How many times the message “Fiction removed!” will be display ed after executing the above C + + code ? Out of Line 1 to Line 9, which line is responsible to display the message “Fiction removed! ” ?
Answer:
(i) Polymorphism 1
(ii) 2 times 1
Line 9. 1

 

Topic-II
Destructors

Short Answer Type Questions-I 

Question 1: Write any two similarities between Constructors and Destructors. Write the Function headers for constructor and destructor of a class Flight. 
Answer: Similarities between Constructor and Destructor:
(i) Both have same name as of the class name.
(ii) Both handle the objects of a class. [1]
Function Headers:
Constructor:
Flight ( )
Destructor:
~ Flight ( ) [1]

Question 2: Write any two differences between Constructors and Destructors. Write the function headers for constructor and destructor of a class member. [Delhi, 2013]
Answer:
            Constructor                                        Destructor

(i) Invoked every time when the object is        Invoked every time when object goes out
created                                                         of scope.
(ii) Used to construct and initialize object         Used to destroy objects
values. 
[1]
Function Headers:
Constructor:
Member ( )
Destructor:
~ Member ( ) [1]

Question 3: Answer the following questions (i) and (ii) aftergoing through the following class :
class Tour
{
int LocationCode; char Location[20]; float Charges;
public:
Tour() //Function 1
{
LocationCode=1;
strcpy (Location,"PURI");
Charges=1200;
}
void TourPlan(float C) //Function 2
{
cout<<PlaceCode<<":"<<LPlace<<":" <<Charges<<endl;
Charges+=100;
Tour (int LC, char L[ ], float C)
//Function 3
{
LocationCode=LC;
strcpy(Location, L);
Charges=C;
~Tour( ) //Function 4
{
cout<<"TourPlan cancelled" <<endl;
}
};
(i) In object oriented programming, what are Function 1 and Function 3 combined together known as?
(ii) In object oriented programming, which concept is illustrated by Function 4? When is this function called/invoked?
Answer:
(i) Function 1 and 3 are together referred as Constructor overloading.
(ii) Function 4 illustrates Destructor. 
It is invoked when object goes out of scope. 

Question 4: Answer the following questions (i) and (ii) after going through the following class:
class Travel
int PlaceCode;
char Place [20];
float Charges;
public:
Travel( ) //Function 1
{
PlaceCode=l;
strcpy (Place,"DELHI");
Charges=l000;
}
void TravelPlan( float C) //Function 2
{
cout<<PlaceCode<<":"<<Place<<":"« Charges<<endl;
Charges+=100;
}
~Travel ( ) //Function 3
{
cout<<"Travel Plan cancelled"<<endl;
}
Travel (int PC, charP [ ], float C)
//Function 4
{
PlaceCode=PC;
strcpy(Place, P);
Charges=C;
}
};
(i) In object oriented programming, what are Function 1 and Function 4 combined together known as?
(ii) In object oriented programming, which concept is illustrated by Function 3? When is this function called/invoked?
Answer:
(i) Function 1 and 4 are together referred as Constructor overloading. 
(ii) Function 3 illustrates Destructor. 
It is invoked when object of the class goes out of scope.

Question 5: Answer the questions (i) and (ii) after going through the following:
class schoolbag
{
int pockets;
public:
schoolbag() //Function 1
{ pockets=30;
cout<<"The bag has pockets"<<endl;
}
void company() //Function 2
{
cout<<"The company of the Bag is ABC" «endl ;
}
schoolbag(int D) //Function 3
{
pockets=D;
cout<<"Now the Bag has
pockets"<<pockets <<endl;
}
-schoolbag() //Function 4
{
cout<<" Thanks " < < endl ;
}
};
(i) In Object Oriented Programming, what is Function 4 referred as and when does it get invoked/called ?
(ii) In Object Oriented Programming, which concept is illustrated by Function 1 and Function 3 together ?
Answer:
(i) Function 4 is referred as Destructor, and this function is called / invoked as soon as the scope of the object gets over. 
(ii) Here constructor overloading concept is illustrated by function 1 and function 3 together.

 

Default constructor, Constructor with arguments, default initializing. Overloading constructor.

Question. What is constructor?
Answer: A constructor is a Member function that automatically called, when the object is created of that class. It has the same name as that of the class name and its primary job is to initialise the object to a legal value for the class.

Question. Why do we need a constructor as a class member? Answer: Constructor is used create an instance of of a class, This can be also called creating an object. Question. Why does a constructor should be define as public?
Answer: A constructor should be define in public section of a class, so that its objects can be created in any function.

Question. Explain default constructor?
Answer: The constructor that accepts no parameter is called the default constructor. If we do not explicitly define a constructor for a class., then java creates a default constructor for the class. The default constructor is often sufficient for simple class but not for sophisticated classes.
Example:
class ant
{
int i;
public static
void main() ant nc=new ant(); } the line new ant() creates an object and calls the default constructor, without it we have no method to call to build our objects. once you create a constructor with argument the default constructor becomes hidden.

Question. Explain the Parameterised constructor? Answer: If we want to initialise objects with our desired value, we can use parameters with constructor and initialise the data members based on the arguments passed to it . Constructor that can take arguments are called Parameterised constructor.
Answer:

Example:
public class result
{
int per;
int tot;
public result (int percentage)
{ per=percentage; tot=0;
}
}

Question. Give a syntax/example of constructor overloading. Define a class, which accept roll number and marks of a student. Write constructor for the class, which accepts parameter to initialise the data member.
Answer:
Also take care of the case where the student has not appeared for the test where just the roll number is passed as argument. Answer: class student
{
int roll;
float marks;
student(int r, float m) // constructor with two argument.
{
roll=r;
marks=m;
}
student(int r) // constructor with one argument
{ roll=r; marks=0;
}
student() // default constructor
{ roll=0; marks=0;

}
}

Question. Mention some characteristics of constructors.
Answer: The special characteristics of constructors are:
(i) Constructors should be declared in the public section of the class. (ii) They are invoked automatically when an object of the class is created. (iii) They do not have
any return type and cannot return any values. (iv) Like any other function, they can accept arguments. (v) A class can have
more than one constructor. (vi) Default constructor do not accept parameters. (vii) If no constructor is present in the class the compiler provides a default constructor.

Question. State the difference between Constructor and Method.
Answer: The function has a return type like int. but the constructor has no return type. The function must be called in programs where as constructor automatically called when the object of that class is created.

Question. Enter any two variables through constructor parameters and write a program to swap and print the values. class swap
Answer:
{
int a,b;
swap(int x,int y)
{
a=x;
b=y;
}
public void main(String args[])
{
int t=a;
a=b;
b=t;
System.out.println(“the value of a and b after swaping : “+a+” “+b);
}
}

Question. What are the types of Constructors used in a class?
Answer: The different types of constructors are as follows:
i. Default Constructors.
ii. Parameterized Constructor. iii. Copy Constructors.

Question. Define Copy constructors.
Answer: A copy constructors initializes the instant variables of an object by copying
the initial value of the instant variables from another objects. e.g.
class xyz
{
int a,b;
xyz(int x,int z)
{
a=x;
b=y;
}
xyz(xyz p)
{
a=p.x;

b=p.y;
}
}

More Study Material

NCERT Solutions Class 12 Computer Science Constructor and Destructor

NCERT Solutions Class 12 Computer Science Constructor and Destructor is available on our website www.studiestoday.com for free download in Pdf. You can read the solutions to all questions given in your Class 12 Computer Science textbook online or you can easily download them in pdf.

Constructor and Destructor Class 12 Computer Science NCERT Solutions

The Class 12 Computer Science NCERT Solutions Constructor and Destructor are designed in a way that will help to improve the overall understanding of students. The answers to each question in Constructor and Destructor of Computer Science Class 12 has been designed based on the latest syllabus released for the current year. We have also provided detailed explanations for all difficult topics in Constructor and Destructor Class 12 chapter of Computer Science so that it can be easier for students to understand all answers.

NCERT Solutions Constructor and Destructor Class 12 Computer Science

Class 12 Computer Science NCERT Solutions Constructor and Destructor is a really good source using which the students can get more marks in exams. The same questions will be coming in your Class 12 Computer Science exam. Learn the Constructor and Destructor questions and answers daily to get a higher score. Constructor and Destructor of your Computer Science textbook has a lot of questions at the end of chapter to test the students understanding of the concepts taught in the chapter. Students have to solve the questions and refer to the step-by-step solutions provided by Computer Science teachers on studiestoday to get better problem-solving skills.

Constructor and Destructor Class 12 NCERT Solution Computer Science

These solutions of Constructor and Destructor NCERT Questions given in your textbook for Class 12 Computer Science have been designed to help students understand the difficult topics of Computer Science in an easy manner. These will also help to build a strong foundation in the Computer Science. There is a combination of theoretical and practical questions relating to all chapters in Computer Science to check the overall learning of the students of Class 12.

Class 12 NCERT Solution Computer Science Constructor and Destructor

NCERT Solutions Class 12 Computer Science Constructor and Destructor detailed answers are given with the objective of helping students compare their answers with the example. NCERT solutions for Class 12 Computer Science provide a strong foundation for every chapter. They ensure a smooth and easy knowledge of Revision notes for Class 12 Computer Science. As suggested by the HRD ministry, they will perform a major role in JEE. Students can easily download these solutions and use them to prepare for upcoming exams and also go through the Question Papers for Class 12 Computer Science to clarify all doubts

Where can I download latest NCERT Solutions for Class 12 Computer Science Constructor and Destructor

You can download the NCERT Solutions for Class 12 Computer Science Constructor and Destructor for latest session from StudiesToday.com

Can I download the NCERT Solutions of Class 12 Computer Science Constructor and Destructor in Pdf

Yes, you can click on the link above and download NCERT Solutions in PDFs for Class 12 for Computer Science Constructor and Destructor

Are the Class 12 Computer Science Constructor and Destructor NCERT Solutions available for the latest session

Yes, the NCERT Solutions issued for Class 12 Computer Science Constructor and Destructor have been made available here for latest academic session

How can I download the Constructor and Destructor Class 12 Computer Science NCERT Solutions

You can easily access the links above and download the Constructor and Destructor Class 12 NCERT Solutions Computer Science for each chapter

Is there any charge for the NCERT Solutions for Class 12 Computer Science Constructor and Destructor

There is no charge for the NCERT Solutions for Class 12 Computer Science Constructor and Destructor you can download everything free

How can I improve my scores by reading NCERT Solutions in Class 12 Computer Science Constructor and Destructor

Regular revision of NCERT Solutions given on studiestoday for Class 12 subject Computer Science Constructor and Destructor can help you to score better marks in exams

Are there any websites that offer free NCERT solutions for Constructor and Destructor Class 12 Computer Science

Yes, studiestoday.com provides all latest NCERT Constructor and Destructor Class 12 Computer Science solutions based on the latest books for the current academic session

Can NCERT solutions for Class 12 Computer Science Constructor and Destructor be accessed on mobile devices

Yes, studiestoday provides NCERT solutions for Constructor and Destructor Class 12 Computer Science in mobile-friendly format and can be accessed on smartphones and tablets.

Are NCERT solutions for Class 12 Constructor and Destructor Computer Science available in multiple languages

Yes, NCERT solutions for Class 12 Constructor and Destructor Computer Science are available in multiple languages, including English, Hindi