Monday

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

 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";
}
};


0 comments:

Post a Comment

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