Wednesday

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

 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.
TypePKM
‘A’25
‘B’20
‘C15

А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.
CTypePerKM
A20
B18
C15

А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.
TypePKM
‘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.
TypeCharge
‘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;
}
};

0 comments:

Post a Comment

Thanks for leaving a comment. As soon as it's approved, it will appear.