Wednesday

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 :

ConstructorDestructor
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

0 comments:

Post a Comment

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