Quantcast
Channel: Learn CBSE
Viewing all articles
Browse latest Browse all 9061

Important Questions for Class 12 Computer Science (C++) – Constructor and Destructor

$
0
0

Important Questions for Class 12 Computer Science (C++) – Constructor and Destructor

Previous years Examination Questions
2 and 3 Marks Questions

Question 1:
Observe the following C++ code and answer the questions (i) and (ii).
NOTE Assume all necessary files are included.

class TEST 
{
long TCode; 
char TTitle[20]; 
float Score; 
public:
TESTO //Member Function 1 
{
TCode = 100;
strcpy(TTitle,"FIRST Test"); 
Score=0;
}
TEST(TEST &T) //Member Function 2
{
TCode=E.TCode+l; 
strcpy(TTitle,T.TTitle); 
Score=T.Score;
}
};
void main()
{
_____________     //Statement 1
_____________     //Statement 2
}
  1. Which Object Oriented Programming feature is illustrated by the Member Function 1 and the Member Function 2 together in the class TEST?
  2. Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively.
    All Indio 2017

Аnswer:

  1. Constructor overloading feature is illustrated by the Member Function 1 and the Member Function 2 together in the class TEST.
  2. Statement 1
    TEST T1; //To execute Member Function 1
    Statement 2
    TEST T2 = T1; //To execute Member Function 2

Question 2:
Find and write the output of the following C++ program code: Delhi 2016
NOTE Assume all required header files are already being included in the program.

class Stock 
{
long int ID; 
float Rate; 
int Date; 
public:
Stock(){ID=1001 ; Rate=200; Date=l;}
void RegCode(long int I, float R)
{
ID = 1;
Rate=R;
}
void Change(int New,int DT)
{
Rate+=New;
Date=DT;
}
void Show()
{
cout<<"Date:”<<Date<<endl;
cout<<ID<<"#"<<Rate<<endl;
}
};
void main()
{
Stock A,B,C;
A.RegCode(1024,150);
B.RegCode(2015,300); 
B.Change(100,29);
C.Change(-20,20);
A.Show();
B.Show();
C.Show();
}

Аnswer:
Output of the given program would be:
Date : 1
1024#150
Date : 29
2015#400
Date : 20
1001#180

Question 3:
Observe the following C++ code and answer the questions (i) and (ii). Assume all necessary files are included:

class FICTION Delhi 2016 
{
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
{
cout<<"Fiction removed!"<<endl;
}
};
void main() //Line 1
{           //Line 2
FICTION F1, F2(101, "Dare”,75); //Line 3
for(int 1=0;I<4;I++)    //Line 4
{    //Line 5
F1. Increase(20);F2.Increase(15); //Line 6
F1. Show();F2.Show(); //Line 7
} //Line 8
} //Line 9
  1. Which specific concept of object oriented programming out of the following is illustrated by Member Functionl and Member Function 2 combined together ?
    • Data Encapsulation
    • Data Hiding
    • Polymorphism
    • Inheritance
  2. How many times the message “Fiction removed!” will be displayed after executing the above C++ code ? Out of Line 1 to Line 9, which line is responsible to display the message “Fiction removed!” ?

Аnswer:

  1. Polymorphism or constructor overloading
  2. 2 times the message “Fiction removed!” will be displayed. Line 9 is responsible to display the message “Fiction removed!”.

Question 4:
Find and write the output of the following C++ program code: All India 2016 NOTE Assume all required header files are already being included in the program.

class Share 
{
long int Code; 
float Rate; 
int DD; 
public:
Share(){Code=1000;Rate=100;DD=1;} 
void GetCode(long int C, float R)
{
Code=C;
Rate=R;
}
void Update(int Change, int D)
{
Rate+=Change;
DD=D;
}
void Status()
{
cout<<"Date: "<<DD<<endl; 
cout<<Code<<"#”<<Rate<<endl;
};
void main()
{
Share S,T,U;
S.GetCode(1324,350);
T.GetCode(1435,250);
S.Update(50,28);
U.Update(-25,26);
S.Status();
T.Status();
U.Status();
}

Аnswer:
Output of the given program would be:
Date: 28
1324#400
Date: 1
1435#250
Date: 26
1000#75

Question 5:
Observe the following C++ code and answer the questions (i) and (ii). All India 2016
NOTE Assume all necessary files are included :

class BOOK 
{
long Code;
char Title[20];
float Price; 
public:
BOOK()//Member Function 1
{
cout<<"Bought"<<endl; 
code=10;
strcpy(Title,"NoTitle"); 
Price=100;
}
BOOK(int C.char T[],float P) //Member Function 2
{
Code=C;
strcpy(Title,T);
Price=P;
}
void Update(float P) //Member Function 3
{
 Price+=P;
}
void Display() //Member Function 4
{
cout<<Code<<":"<<Title<<”:”<<Price<<endl;
}
∼BOOK() //Member Function 5 
{
cout<<"Book Discarded!"<<endl;
}
};
void main() //Line 1
{     //Line 2
BOOK B.C(101,"Truth",350); //Line 3
for(int I=0;I<4;I++) //Line 4
{  //Line 5
B.Update(50);C.Update(20); //Line 6
B.Display();C.Display(); //Line 7
} //Line 8
} //Line 9
  1. 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
    • Polymorphism
    • Inheritance
    • Data Hiding
  2. How many times the message “Book Discarded!” will be displayed after executing the above C++ code? Out of Line 1 to Line 9, which line is responsible to display the message “Book Discarded!”

Аnswer:

  1. Polymorphism or constructor overloading.
  2. 2 times the message “Book Discarded!” will be displayed. Line 9 is responsible to display the message “Book Discarded!”.

Question 6:
Differentiate between Constructor and Destructor functions giving suitable example using a class in C++. When does each of them execute? Delhi 2016
or
Write any two differences between constructor and destructor. Write the function header for constructor and destructor of a class Member. Delhi 2013
or
Differentiate between constructor and destructor functions in a class. Give a suitable example in C+ + to illustrate the difference. Delhi 2012c
or

Differentiate between constructor and destructor function with respect to object oriented programming. All India 2011
Аnswer:

Differences between constructor and destructor :

Constructor Destructor
Its name is same as the class name. Its name is same as the class name preceded by the tilde (-) sign.
It is automatically called whenever an object is created. It is automatically called whenever an object goes out of the scope.
e.g.
class Member 
{
public:
Member() //Constructor
{
cout<<"I am a constructor of Member class";
}
~Member()   //Destructor 
{
cout<<"I am destructor of Member class";
}
};

Question 7:
What is copy constructor? Give an example in C++ to illustrate copy constructor. All Indio 2016C, Delhi 2009
or
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. Delhi 2015, All Indio 2015
Аnswer:
Copy Constructor A copy constructor is that constructor which is used to initialise one object with the values from another object of same class during declaration.
e.g.

class Student 
{
int s; 
public:
Student() //Default constructor 
{
s = 10;
}
Student(Student &i)  //Copy constructor
{
s = i.s;
}
};
void main()
{
Student s1;      //Default constructor called 
Student s2(s1);  //Copy constructor called
}

Question 8:
Write the output of the following C++ program code: Delhi 2015
NOTE Assume all required header files are already being included in the program.

class Calc 
{
char Grade: 
int Bonus:
public:
Calc()
{ 
Grade='E';
Bonus=0;
}
void Down(int G) 
{
Grade-=G;
}
void Up(int G)
{
Grade+=G;
Bonus++;
}
void Show()
{
cout<<Grade<<"#"<<Bonus<<endl;
}
};
void main()
{
Calc C;
C.Down(2);
C.Show();
C.Up(7);
C.Show();
C.Down(2);
C.Show();
}

Аnswer:
Output of the given program would be:
C#0
J#1
H#1

Question 9:
Observe the following C++ code and answer the questions (i) and (ii):

class Traveller 
{
long PNR;
char TName[20];
public:
Traveller()  //Function 1
{
cout<<"Ready"<<endl;
}
void Book(long P, char N[]) //Function 2
{
PNR = P;
strcpy(TName,N);
}
void Print() //Function 3
{
cout<<PNR<<TName«endl;
}
∼Traveller() //Function 4
{
cout<<"Booking cancelled!"<<endl; 
}
};
  1. Fill in the blank statements in Line 1 and Line 2 to execute Function 2 and Function 3 respectively in the following code :
    void main()
    {
    Traveller T;
    _______   //Line 1
    _______   //Line 2
    } //Stops here
  2. Which function will be executed at }//Stops here? What is this function referred as? Delhi 2015

Аnswer:

  1. Line 1 → T.Book(20,”XYZ”);
    Line 2 → T.Print( );
  2. ∼Traveller( ) Function will be executed at }// Stops here. This function is referred as destructor.

Question 10:
Write the output of the following C++ program code: All India 2015
NOTE Assume all the required header files are already being included in the program.

class Eval 
{
char Level; 
int Point; 
public:
Eval() {Level= ’E' ; Point=0;} 
void Sink(int L)
{
Level -= L;
}
void Float(int L)
{
Level += L;
Point++;
}
void Show()
{
cout<<Level<<"#"<<Point<<endl;
}
};
void maint()
{
Eval E;
E.Sink(3);
E.Show();
E.Float(7);
E.Show();
E.Sink(2);
E.Show();
}

Аnswer:
Output of the given program would be:
B#0
I#1
G#1

Question 11:
Observe the following C++ code and answer the questions (i) and (ii) : All India 2015

class Passenger
{
long PNR; 
char Name[20]; 
public:
Passenger()    //Function 1
{
cout<<"Ready"<<endl;
}
void Book(long P,char N[]) //Function 2 I
PNR = P; strcpy(Name, N);
}
void Print() //Function 3 
{
cout<<PNR<<Name<<endl;
}
∼Passenger //Function 4
{
cout<<"Booking cancelled!"<<endl;
}
};
  1. Fill in the blank 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
  2. Which function will be executed at } //Ends here ? What is this function referred as?

Аnswer:

  1. Line 1 → P.Book(10, “ABC” ) :
    Line 2 →P.Print( ) :
  2. ~Passenger( ) function will be executed at } //Ends here. This function is referred as destructor.

Question 12:
Write the output of the following C++ program code: All India 2015c
NOTE: Assume all required header files are already being included in the program.

class Item 
{
int I no;  
char IName[20]; 
float Price;
public: 
Item()
{
Ino=l00;
strcpy(Iname,"None");
Price=100;
{ 
void Assign(int I, char Named, float P)
{
Ino+=I;
strcpyCIName, Name);
Price=P;
}
void RaiseP(float P)
{
Price+=P;
}
void ReduceP(float P) 
{ 
Price-=P;
}
void Disp()
{
cout<<Ino<<":"<<IName<<":"<<Price<<endl;
}
};
void main()
{ 
Item One, Two;
One.Disp();
One.Assignd(1, "Pen", 95); 
Two.Assign(2, "Penci1",55); 
One.RaiseP(lO);
Two.ReduceP(5);
Two.Disp();
One.Disp();
}

Аnswer:
Output of the given program would be:
100 : None : 100
102 : Penci1 : 50
101 : Pen : 105

Question 13:
Observe the following C++ code and answer the questions (i) and (ii): All India 201SC

class EandI
{
int Temperature, Humidity;
char City[30];
public:
EandI()    //Function 1
{
Temperature=0;Humidity=0; 
cout<<"Set to Zero"<<endl;
}
EandI(int T, int H, char C[])    //Function 2
{
Temperature=T;
Humidity=H; 
strcpy(City, C);
}
voidShow()    //Function 3
{
cout<<Temperature<<":"<<Humidity<<endl; 
cout<<City<<endl;
}
∼EandI()    //Function 4
{
cout<<"Data Removed!"<<endl; 
}
};
  1. Fill in the blank lines as Statement 1 and Satement 2 to execute Functions 2 and 3 respectively in the following code:
    void main( )
    {
    Eandl E;
    ___________ //Statement 1
    ___________ //Statement 2
    }//The end of main( ) function here
  2. Which function will be executed at the point where “//The end of main function here” is written in the above code? What is this function called and executed here is known as?

Аnswer:

  1. Statement 1 →
    E. EandI(25,39,”Delhi”);
    Statement 2 → E.Show( );
  2. Function 4 will be executed. It is known as Destructor and will be executed when object of class EandI goes out of scope.

Question 14:
Answer the question (i) and (ii) after going through the following class: All India 2014C

class schoolbag 
{
int pockets; 
public:
schoolbag()    //Function 1
{ 
pockets=30;  
cout<<"The bag has pockets”<<end1;
}
void company()    //Function 2
{
cout<<"The company of the Bag is ABC"<<endl;
}
school bag(int D)    //Function 3
{
pockets=D;
cout<<"Now the Bag has pockets"<<pockets<<endl;
}
∼schoolbag()    //Function 4
{
cout<<”Thanks"<<endl;
}
};
  1. In object oriented programming, what is Function 4 referred as and when does it get invoked/called?
  2. In object oriented programming, which concept is illustrated by Function 1 and Function 3 together?

Аnswer:

  1. Function 4 is referred as destructor and it is get invoked/called whenever an object goes out of scope.
  2. Function 1 and Function 3 combined together referred as constructor overloading, i.e. Polymorphism.

Question 15:
Write four characteristics of a constructor function used in a class. Delhi 2014
Аnswer:

Characteristics of the constructor function used in a class are as follows:

  1. Constructors are special member functions having same name as that of class name.
  2. Constructors do not need to be called explicitly. They are invoked automatically whenever an object of that class is created.
  3. They are used for initialisation of data members of a class.
  4. Constructors do not have a return type and that is why cannot return any value.

Question 16:
Answer the questions (i) and (ii) after going through the following class: Delhi 2014

class Health 
{
int PId,DId; 
public:
Health(int PPId);   //Function 1 
Health();           //Function 2 
Health(Health &H);  //Function 3 
void Entry();       //Function 4 
void Display();     //Function 5
}; 
void main()
{
Health H(20);    //Statement 1
}
  1. Which of the function out of Function 1, 2, 3, 4 or 5 will get executed, when the Statement 1 is executed in the above code?
  2. Write a statement to declare a new object G with reference to already existing object H using Function 3.

Аnswer:

  1. Function 1 will execute, when Statement 1 will be executed in the given code.
  2. Health G(H) ;

Question 17:
Obtain the output of the following C++ program, which will appear on the screen after its execution.
Delhi 2014
Important Note All the desired header files are already
included in the code, which are required to run the code.

class Player 
{
int Score, Level; 
char Game; 
public:
Player(char GGame='A')
{
Score=0;
Level=1;
Game=GGame; 
}
void Start(int SC); 
void Next(); 
void Disp()
{
cout<<Game<<"@”<<Level<<endl; 
cout<<Score<<endl;
}
};
void main()
{
Player P,Q('B');
P.Disp();
Q.Start(75);
Q.Next();
P.Start(120);
Q.Disp();
P.Disp();
}
void Player::Next()
{
Game=(Game=='A')?'B':'A';
}
void Player::Start(int SC)
Score+=SC;
if(Score>=100)
Level=3;
else if(Score>=50)
Level=2;
else
Level=1;
}

Аnswer:
Output of the given program would be:
A@1
0
A@2
75
A@3
120

Question 18:
Obtain the output of the following C++ after its execution. All India 2014
Important Note All the desired header files are already included in the code, which are required to run the code.

class Game 
{
int Level, Score; 
char Type; 
public:
Game(char GType='P')
{
Level=1;Score=0;
Type=GType;
}
void Play(int GS); 
void Changer(); 
void Show()
{
cout<<Type«"@"<<Level<<endl;
cout<<Score<<endl;
}
};
void main()
{
Game A('G'),B; 
B.Show(); 
A.Play(11);
A.Changer);
B.Play(25);
A.Show();
B.Show(); 
}
void Game::Change()
{
Type=(Type=='P')? 'G’:'P’;
}
void Game::Play(int GS)
{
Score+=GS; 
if(Score>=30)
Level=3;
else if(Score>=20) 
Level=2; 
else 
Level=1;
}

Аnswer:
Output of the given program would be:
P@1
0
P@1
11
P@2
25

Question 19:
Answer the questions (i) and (ii) after going through the following class:

class Hospital
{
int Pno.Dno; 
public:
Hospital(int PN):    //Function 1
Hospital();    //Function 2
Hospital(Hospital &H); //Function 3
void In();   //Function 4
void Disp();    //Function 5
};
void main()
{
Hospital H(20); //Statement 1
}
  1. Which of the function out of Functions 1,2,3, 4 or 5 will get executed, when the Statement 1 is executed in the above code?
  2. Write a statement to declare a new object G with reference to already existing object H using Function 3.

Аnswer:

  1. Function 1 will be called, when Statement 1 will be executed.
  2. Hospital G(H);

Question 20:
Write any two similarities between constructors and destructors. Write the function headers for constructor and destructor of a class Flight. All India 2013
Аnswer:

Similarities between constructor and destructor

  1. Both have same name as the class in which they are declared.
  2. If not declared by user both are available in a class by default but now they can only allocate and deallocate memory from the objects of a class, when an object is declared or deleted.

e.g.

class Flight 
{
public:
Flight()    //Constructor
{
cout<<"Constructor for class Flight";
}
∼Flight()    //Destructor
{
cout<<"Destructor for class FIight";
}
};

Question 21:
Answer the questions (i) and (ii) after going through the following class: All India 2013

class Race
{
int CarNo,Track; 
public:
Race();
Race(int CN); 
Race(Race &R); 
void Register(); 
void Driver();
};
void main()
{
Race R;
}
  1. Out of the following, which of the option is correct for calling Function 2?
    Option 1 – Race T(30) :
    Option 2-Race U(R);
  2. Name the feature of object oriented programming, which is illustrated by Function 1, Function 2 and Function 3 combined together.

Аnswer:

  1. Option 1 – Race T(30) is correct.
  2. Constructor overloading i.e. Polymorphism

Question 22:
Write the output of the following program: Delhi 2013c

#include<iostream.h> 
class Quiz 
{
int Round; 
float Score; 
public:
Quiz() {Round = l;Score = 0;}
Quiz(Quiz &Q)
{
Round = Q.Round+1;
Score = Q.Score+10;
}
void GetBonus(float B=5)
{
Score += B;
}
void ShowScore()
{ 
cout<<Round<<"#"<<Score<<endl;
}
}; 
void main()
{
Quiz A;
A.ShowScore();
A.GetBonus(lO);
A.ShowScore();
Quiz B(A);
B.GetBonus();
B.ShowScore();
}

Аnswer:
Output of the given program would be:
1#0
1#10
2#25

Question 23:
Observe the following C++ code carefully and obtain the output, which will appear on the screen after execution of it.
All India 2013

#include<iostream.h>
class Mausam
{
int City, Temp, Humidity; 
public:
Mausam(int C=l)
{
City=C;
Temp=10;
Humidity=63;
}
void Sun(int T)
{
Temp+=T;
}
void Rain(int H)
{
 Humidity+=H;
}
void CheckOut()
{
cout<<City<<":"<<Temp<<"&"<<Humidity<<"%"<<endl;
}
};
void main()
{
Mausam M, N(2);
M.Sun(5);
M.CheckOut();
N.Rain(10);
N.Sun(2);
N.CheckOut();
M.Rain(15);
M.CheckOut();
}

Аnswer:
Output of the given program would be:
1:15&63%
2 :12&73%
1:15&78%

Question 24:
Observe the following C++ code carefully and obtain the output, which will appear on the screen after execution of it.
Delhi 2013

#include<iostream.h> 
class Aroundus 
{
int Place,Humidity,Temp; 
public:
Aroundus(int P=2)
{
Place=P;
Humidity=60;
Temp= 20;
}
void Hot(int T)
{
Temp+=T; 
}
void Humid(int H)
{
Humidity+=H; 
}
void JustSee()
{
cout<<Place<<":"<<Temp<<"&"<<Humidity<<"%"<<endl;
}
};
void main()
{
Aroundus A,B(5);
A.Hot(lO);
A.JustSee();
B.Humid(15);
B.Hot(2);
B.JustSee();
A.Humid(5);
A.JustSee();
}

Аnswer:
Output of the given program would be:
2:30&60%
5; 228.75%
2:30&65%

Question 25:
Answer the questions (i) and (ii) after going through the following class: Delhi 2013 C

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
};
void main() 
{
  :
  :
{
  1. Name the feature of object oriented programming demonstrated by Function 1, Function 2 and Function 3.
  2. Write statements in C++ to execute Function 3 and Function 4 inside the main( ) function.

Аnswer:

  1. Constructor overloading i.e. Polymorphism
  2. Book B(10, “GABS”) ;
    B.Buy( );

Question 26:
Answer the questions (i) and (ii) after going through the following class: Delhi 2013

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();
};
void main() 
{
Motor M;
  :
  :
}
  1. Out of the following, which of the option is correct for calling Function 2?
    Option 1 – Motor N(M);
    Option 2 – Motor P(10) ;
  2. Name the feature of object oriented programming, which is illustrated by Function 1, Function 2 and Function 3 combined together.

Аnswer:

  1. Option 2-Motor P( 10) is correct.
  2. Constructor overloading.

Question 27:
Answer the questions (i) and (ii) after going through the following class: Delhi 2012

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<<LocationCode<<":"<< Location<<":”<<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;
}
};
  1. In object oriented programming, what are Function 1 and Function 3 combined together as?
  2. In object oriented programming, which concept is illustrated by Function 4? When is this function called/invoked?

Аnswer:

  1. Function 1 and Function 3 combined together referred as constructor overloading, i.e. polymorphism.
  2. Function 4 indicates destructor. This function is called/invoked whenever an object goes out of scope.

Question 28:
Answer the questions (i) and (ii) after going through the following class: All India 2012

class Travel 
{
int PlaceCode; 
char Place[20]; 
float Charges; 
public:
Travel()    //Function 1
{
PlaceCode = 1; 
strcpy(Place, "DELHI");
Charges = 1000;
}
void TravelPlan(float C) //Function 2 
{
cout<<PlaceCode<<":"<<Place<<":"<<Charges<<endl;
}
∼Travel()    //Function 3
{
cout<<"TravelPlan Cancelled"<<endl;
}
Travel(int PC, char P[], float C)    //Function 4 
{
PlaceCode = PC; 
strcpy(Place, P):
Charges = C;
}
};
  1. In object oriented programming, what are Function 1 and Function 4 combined together as ?
  2. In object oriented programming, which concept is illustrated by Function 3? When is this function called/invoked?

Аnswer:

  1. Function 1 and Function 4 combined together referred as constructor overloading, i.e. polymorphism.
  2. Function 3 indicates destructor/ This function is called/invoked whenever an object goes out of scope.

Question 29:
Find the output of the following program: Delhi 2012

#include<iostream.h> 
class Train 
{ 
int TNo.TripNo.PersonCount;
public:
Train(int TN = 1)
{
TNo = TN:
TripNo=0;
PersonCount=0;
}
void Trip(int TC=100)
{
TripNo++;
PersonCount+=TC;
}
void Show()
{
cout<<TNo<<":"<<TripNo<<":"<<PersonCount<<endl;
}
};
void main()
{
Train T(10),N;
N.Trip();
T.Show();
N.Trip(70);
N.Trip(40);
N.Show();
T.Show();
}

Аnswer:
Output of the given program would be:
10:0:0
1:3:210
10:0:0

Question 30:
Find the output of the following program: All India 2012

#include<iostream.h> 
class METRO 
{
int Mno,TripNo,PassengerCount; 
public:
METR0(int Tmno=1)
{
Mno=Tmno:
TripNo=0;
PassengerCount=0;
}
void Trip(int PC=20)
{
TripNo++;
PassengerCount+=PC;
}
void StatusShow()
{
cout<<Mno<<":"<<TripNo<<":"<<PassengerCount<<endl;
};
void main()
{
METRO M(5),T;
M.Trip();
T.Trip(50);
M.StatusShow();
M.Trip(30);
T.StatusShow(); 
M.StatusShow();
}

Аnswer:
Output of the given program would be:
5:1:20
1:1:50
5:2:50

Question 31:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction. Delhi 2011c

#inc1ude<iostream.h> 
#include<stdio.h> 
class AUTO 
{
char Model[20]; 
float Price;
AUTO()
{
Price = 0;
strcpy(Model,"NULL");
}
public:
void GetInfo()
{
cin>>Price; 
gets(Model);
}
void PutInfo()
{
cout<<setw(10)<<Price<<setw(10)<<Model<,<endl;
}
}
void main()
{
AUTO Car; 
Car.GetInfo(); 
Car.PutInfo();
}

Аnswer:

#include<iostream.h>
#include<stdio.h> 
#include<string.h>
#inc1ude<iomanip.h> 
class AUTO
{
char Model[20]; 
float Price; 
public:
AUTO()
{
Price = 0;
strcpy(Model, "NULL”);
}
void Getlnfo()
{
cin>>Price; 
gets(Model);
}
void Putlnfo()
{
cout<<setw(10)<<Price<<setw(10)<<Model<<endl;
}
};
void main()
{
AUTO Car;
Car.Getlnfo();
Car.Putlnfo();
}

Question 32:
Answer the questions (i) and (ii) after going through the following class; All India 2010

class Exam 
{
int Rno, MaxMarks, MinMarks, Marks; 
public:
Exam()    //Module 1
{
Rno - 101; MaxMarks = 100; 
MinMarks = 40; Marks = 75;
}
Exam(int Prno.int Pmarks) //Module 2
{
Rno = Prno; MaxMarks = 100; 
MinMarks = 40; Marks = Pmarks;
}
∼ExamO    //Module 3
{
cout<<"Exam over"<<endl;
}
void show()    //Module 4
{
cout<<Rno<<":"<<MaxMarks<<":"<<MinMarks<<endl; 
cout<<"[MarksGot]"<<Marks<<endl;
}
};
  1. As per object oriented programming, which concept is illustrated by Module 1 and Module 2 together?
  2. What is Module 3 specifically referred as, when do you think Module 3 will be invoked/called?

Аnswer:

  1. Constructor overloading or polymorphism.
  2. Function 3 is referred to as destructor. It is invoked or called, when scope of an object gets over.

Question 33:
Answer the questions (i) and (ii) after going through the following class: Delhi 2016

class TEST
{
int Regno, Max, Min, Score; 
public:
TESTO    //function 1
{
Regno = 101; Max = 100;
Min = 40; Score = 75;
}
TEST(int Pregno.int Pscore) //Function 2
{
Regno = Pregno; Max = 100; 
Min = 40; Score = Pscore;
}
~TEST()    //Function 3
{
cout<<"TEST over"<<endl;
}
void Display()   //Function 4
{
cout<<Regno<<":"<<Max<<":"<<Min<<endl; 
cout<<"[Score]"<<Score<<endl;
}
};
  1. As per object oriented programming, which concept is illustrated by Function 1 and Function 2 together?
  2. What is Function 3 specifically referred as, when do you think Function 3 will be invoked/called?

Аnswer:

  1. Constructor overloading or polymorphism.
  2. Function 3 is referred to as destructor. It is invoked or called, when scope of an object gets over.

Question 34:
Answer the questions (i) and (ii) after going through the following class: HOTS; Delhi 2009

class WORK
{ 
int Workld; char WorkType; 
public:
∼WORK()    //Function 1
{
cout<<"Un-allocated"<<endl;
}
void status()    //Function 2
{
cout<<WorkId<<":"<<WorkType<<endl;
}
WORK()    //Function    3
{
Workld = 10; WorkType = 'T';
}
WoRK(WORK &W)    //Function 4
{
WorkId = W.WorkId+12;
WorkType = W.WorkType+1;
}
};
  1. Which member function, out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class WORK is called automatically, when the scope of an object gets over? Is it known as constructor or destructor or overloaded function or copy constructor?
  2. WORK W; //Line 1
    WORK Y(W); //Line 2
    Which member function, out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class WORK will be called on execution of statement written as Line 2? What is this function specifically known as out of destructor or copy constructor or default constructor?

Аnswer:

  1. Function 1 is called, when the scope of an object gets over. It is called destructor.
  2. Function 4 will be called and this function is referred to as copy constructor.

Question 35:
Answer the questions (i) and (ii) after going through the following class: Delhi 2009C

class Factory
{
char Name[20]; 
int Workers; 
public:
Factory()    //Function 1
{
strcpy(Name, "Default”); 
Workers = 0;
}
void Details() //Function 2
{
cout<<Name<<endl<<Workers<<endl;
}
Factory(char*act_Name,int No);  //Function 3 
Factory(Factory & F);   //Function 4
};
  1. In object oriented programming, what is function 4 referred as? Also, write a statement which will invoke this function.
  2. In object oriented programming, which concept is illustrated by Function 1, Function 3 and Function 4 together?

Аnswer:

  1. Function 4 is referred to as copy constructor.
    The statement to invoke it as follows:
    Factory F2(“ABC”, 101);
    Factory F3(F2); //Invoking Function 4
  2. Function 1, Function 3 and Function 4 illustrate the concept of constructor overloading; i.e. polymorphism

Question 36:
Answer the questions (i) and (ii) after going through the following class: All India 2009

class Job 
{
int JobId; 
char JobType; 
public:
∼Job()    //Function 1
{
cout<<"Resigned"<<endl;
}
Job()    //Function 2
{
JobId = 10; JobType = 'T';
}
void TellMe()   //Function 3
{
cout<<JobId<<":"<<JobType<<endl;
}
Job(Job &J)    //Function 4
{
JobId = J.JobId+10;
JobType = J.JobType+1;
}
};
  1. Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class Job is called automatically, when the scope of an object gets over? Is it known as constructor or destructor or overloaded function or copy constructor?
  2. Job P; // Line 1
    Job Q(P1 ; // Line 2
    Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of class Job will be called on execution of statement written as Line 2? What is this function specifically known as out of destructor or copy constructor or default constructor?

Аnswer:

  1. Function 1 is called, when the scope of an object getsover. It is called destructor.
  2. Function 4 is called, when Line 2 is executed. It is called copy constructor.

Question 37:
Rewrite the following C++ program code after removing the syntax error(s) (if any). Underline each correction. Delhi 2009

#inClude<iostream.h> 
#include<stdio.h> 
class Employee 
{
int EmpId=901; 
char EName[20]; 
public
Employee(){} 
void Joining()
{
cin>>EmpId; 
gets(EName);
}
void List()
{
cout<<EmpId<<":"<<EName<<endl;
}
};
void main()
{
Employee E;
Joining.E();
E.List();
}

Аnswer:

#include<iostream.h> 
#include<stdio.h>
class Employee 
{
int Empld:   //cannot initialise data member here
char EName[20]; 
public:    //: symbol is required after public 
Employee(){EmpId = 901;} 
void Joining()
{ 
cin>>EmpId; 
gets(EName);
}
void List() 
{
cout<<EmpId<<":"<<EName<<endl;
}
};
void main()
{
Employee E;
E.Joining(); //object name is used before members 
E.List();
}

Question 38:
Rewrite the following program after removing the syntactical error(s) (if any). Underline each correction. All India 2009

#include<iostream.h> 
#include<stdio.h> 
class MyStudent 
{
int StudentId=1001; 
char Name[20]; 
public
MyStudent(){}
void Register() 
{
cin>>StudentId;
gets(Name);
}
void Display()
{
cout<<StudentId<<":"<<Name<<endl;
}
};
void Main()
{
Mystudent MS;
Register.MSC();
MS.Display();
}

Аnswer:

#include<iostream.h>
#1nclude<stdio.h> 
class MyStudent
{
int Studentld; 
char Name[20]; 
public:
MyStudent(){Studentld=1001;}
void Register()
{
cin>>StudentId; 
gets(Name);
}
void Display!)
{
cout<<StudentId<<":"<<Name<<endl;
}
};
void main()
{
MyStudent MS;
MS.Reaister();
MS.Display();
}

4 Marks Questions

Question 39:
Define a class CABS in C++ with the following specification: Delhi 2014
Data members

  • CNo – to store Cab No
  • Type – to store a character ‘A’, ‘B’ or ‘C as City Type
  • PKM – to store per Kilometre charges
  • Dist – to store Distance travelled (in KM)

Member functions

  • A constructor function to initialise Type as ‘A’ and CNo as ‘1111’
  • A function Charges( ) to assign PKM as per the following table:
  • A function Register( ) to allow administrator to enter the values for CNo and Type. Also, this function should call Charges( )to adding PKM charges.
  • A function ShowCab( ) to allow user to enter the value of Dist and display CNo, Type, PKM, PKM Dist (as Amount) on screen.
Type PKM
‘A’ 25
‘B’ 20
‘C 15

Аnswer:

class CABS 
{
int CNo; 
char Type; 
float PKM; 
float Dist; 
public;
CABS()
{
 Type = 'A';
 CNo = 1111;
}
void Charges!)
{
if(Type == 'A')
PKM = 25;
else if(Type == 'B')
PKM = 20;
else if(Type == 'C')
PKM = 15;
}
void Register()
{
cout<<"Enter value for CNo:"; 
cin>>CNo;
cout<<"Enter value for Type:"; 
cin>>Type;
Charges();
}
void ShowCab()
{
cout<<"Enter the value of Distance:”; cin>>Dist; 
cout<<"\nCab Number: "<<CNo; 
cout<<"\nType:"<<Type; 
cout<<"\nPer Kilometre Charges:"<<PKM; 
cout<<"\nAmount:"<<PKM*Dist;
}
};

Question 40:
Define a class Tourist in C++ with the following specification: All India 2014
Data members

  • CNo – to store Cab No
  • CType – to store a character A B or C as City Type
  • PerKM – to store per Kilometre charges
  • Distance – to store Distance travelled (in KM)

Member functions

  • A constructor function to initialise CType as A and CNo as ‘0000’
  • A function CityChargesO to assign PerKM as per the following table:
  • A function RegisterCab( ) to allow administrator to enter the values for CNo and CType. Also, this function should call CityChargesO to assign PerKM Charges.
  • A function Display( ) to allow user to enter the value of Distance and display CNo, CType, PerKM, PerKM*Distance (as Amount) on screen.
CType PerKM
A 20
B 18
C 15

Аnswer:

class Tourist 
{
int CNo; 
char CType; 
float PerKM; 
float Distance; 
public:
Tourist()
{
CType = 'A';
CNo = 0000; 
}
void CityCharges()
{
if(CType == 'A')
PerKM = 20;
else if(CType == 'B')
PerKM = 18;
else if(CType == 'C')
PerKM = 15;
}
void RegisterCab()
{
cout<<"Enter the Cab Number:"; 
cin>>CNo;
cout<<"Enter the Cab Type:"; 
Cin>>CType;
CityCharges();
}
void Display()
{
cout<<"Enter the Distance:";
cin>>Distance;
cout<<"Registered details are\n"; 
cout<<"Cab Number:"<<CNo<<endl; 
cout<<"Cab Type:"<<CType<<endl; 
cout<<"Charges per km :"<<PerKM<<endl;
cout<<"Amount:"<<PerKM*Distance<<endl;
}
};

Question 41:
Define a class CONTEST in C + + with the following description: All India 2014C
Private Data Members
Eventno – integer
Description – char (30)
Score – integer
qualified –  char
Public Member functions

  • A constructor to assign initial values Eventno as 11, Description as “School level”, Score as 100, qualified as ‘N’.
  • Input( )-To take the input for Eventno, description and score.
  • Award (int cutoffscore)- To assign qualified as ‘Y’, if score is more than the cutoffscore that is passed as argument to the function, else assign qualified as ‘N’.
  • Displaydata( )-to display all data members.

Аnswer:

class CONTEST
{
private:
int Eventno; 
char Description[30]; 
int Score; 
char qualified; 
public:
CONTEST()
{
Eventno=11;
Description-"School level";
Score-100;
qua1ified='N';
}
void input()
{
cout<<”Enter the event no, description and score"; 
cin>>Eventno>>Description; 
cin>>Score;
}
void Award(int cutoffscore) 
{
if(score>cutoffscore) 
qualified='Y'; 
else
qualified='N';
}
void Displaydata()
{
cout<<"Eventno:"<<Eventno; 
cout<<endl; 
cout<<"Description:";<<Description<<endl; 
cout<<"Score:"<<Score<<endl; 
cout<<"Qualified:"cout<<qualified<<endl;
}
};

Question 42:
Define a class Bus in C++ with the following specifications: HOTS; All India 2013
Data members

  • Busno – to store Bus Number
  • From – to store Place name of origin
  • To – to store Place name of destination
  • Type – to store Bus Type such as ‘O’ for ordinary
  • Distance – to store the Distance in Kilometre
  • Fare – to store the Bus Fare

Member functions

  • A constructor function to initialise Type as ‘O’ and Freight as 500.
  • A function CalcFare( ) to calculate Fare as per the following criteria:
  • A function Allocate! ) to allow user to enter values for Busno, From, To, Type and Distance. Also, this function should call CalcFare( ) to calculate Fare.
  • A function Show( ) to display the content of all the data members on screen.
Type PKM
‘O’ 15* Distance
‘E’ 20* Distance
‘L’ 24* Distance

Аnswer:

class Bus 
{
int Busno; 
char From[25]; 
char To[25]; 
char Type; 
float Distance; 
float Fare; 
public:
Bust()
{
Type = ' '; Fare = 500;
}
void CalcFare()
{
if(Type == ' ')
{
Fare = 15*Distance;
}
else if(Type == 'E')
{
Fare = 20*Distance;
} 
else if(Type == 'L')
{ 
Fare = 24*Distance;
}
}
void Allocated()
{
cout<<"Enter the values for Busno, From, To, Type and Distance";
cin>>Busno; 
cin>>From; 
cin>>To; 
cin>>Type; 
cin>>Distance;
Call C Fared();
}
void Show()
{
cout<<"\nBus No:"<<Busno; 
cout<<"\nFrom:"<<From; 
cout<<"\nTo:"<<To; 
cout<<"\nType:"<<Type; 
cout<<"\nDistance:"<<Distance;
cout<<"\nFare:"<<Fare;
}
};

Question 43:
Define a class Tourist in C++ with the following specification: Delhi 2013
Data members

  • Carno-to store Bus No
  • Origin-to store Place name
  • Destination-to store Place name
  • Type-to store Car Type such as ‘E’ for Economy
  • Distance-to store the Distance in Kilometere
  • Charge-to store the Car Fare

Member functions

  • A constructor function to initialise Type as ‘E’ and Freight as 250
  • A function CalcChargef) to calculate Fare as per the following criteria:
  • A function Enter( ) to allow user to enter values for Carno, Origin, Destination, Type and Distance. Also, this function should call CalcCharge( ) to calculate Fare.
  • A function Show( ) to display the content of all the data members on screen.
Type Charge
‘E’ 16* Distance
‘A’ 22* Distance
‘L’ 30* Distance

Аnswer:

class Tourist 
{
int Carno;
char Origin[20];
char Destination[20];
char Type;
float Distance;
float Charge;
public:
Tourist()
{
Type='E';
Charge=250;
}
void CalcCharge()
{
if(Type=='E')
Charge=16*Distance; 
else if(Type=='A') 
Charge=22*Distance; 
else if(Type='L') 
Charge=30*Distance;
}
void Enter() 
{
cout<<"Enter Carno, Origin, Type, Destination and Distance"; 
cin>>Carno; 
gets(Origin); 
gets(Destination); 
cin>>Type>>Distance; 
CalcCharge();
}
void Show()
{
cout<<"Car No:"<<Carno; 
cout<<"0rigin;"<<0rigin; 
cout<<"Destination:"<<Destination; 
cout<<"Type:"<<Type; 
cout<<" Distance:"<<Distance; 
cout<<"Charge:"<<Charge;
}
};

Computer ScienceImportant Questions for Computer ScienceNCERT Solutions

The post Important Questions for Class 12 Computer Science (C++) – Constructor and Destructor appeared first on Learn CBSE.


Viewing all articles
Browse latest Browse all 9061

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>