KENDRIYA VIDYALAYA SANGATHAN, KOLKATA REGION

Computer Science Question Bank

Unit – 2

Unit / Chapter / VSA
(1 Mark) / SA I
(2 Marks) / SA II
(3 Marks) / LA
(4 Marks) / Total
Object Oriented Programming in C++
a)Introduction to OOP using C++ / _ / 1 / _ / 1 / (2) 6
b)Constructor& Destructor / _ / 1 / _ / _ / (1)2
c)Inheritance / - / - / - / 1 / (1)4
d)Pointers / - / - / 1 / - / 1(3)
e)Data File Handling / 1 / 1 / 1 / - / (3) 6
Total
IMPORTANT QUESTIONS (1 OR 2 TIMES)
Chapter – Introduction to OOP using C++
2005 / Define the term Data Hiding in the context of Object Oriented Programming. Give a
Suitable example using a C++ code to illustrate the same. / 2
Define a class TravelPlan in C++ with the following descriptions :
Private Members:
PlanCode of type long
Place of type character array (string)
Number_of_travellers of type integer
Number_of_buses of type integer
Public Members:
A constructor to assign initial values of Plan Code as 1001, Place as “Agra”,
Number_of_travellers as 5, Number_of_buses as 1
A function NewPlan( ) which allows user to enter PlanCode, Place and
Number_of_travellers. Also, assign the value of Number_of_buses as per the
following conditions :
Number_of_travellersNumber_of_buses
Less than 20
Equal to or more than 20 and less than 40
Equal to 40 or more than 40 3
A function ShowPlan( ) to display the content of all the data members on screen. / 4
2006 / Define Multilevel and Multiple inheritance in context of Object Oriented Programming. Give suitable example to illustrate the same. / 2
Define a class named ADMISSION in C++ with the following descriptions:
Private members:
AD_NO integer (Ranges 10 - 2000)
NAME Array of characters (String)
CLASS Character
FEES Float
Public Members:
Function Read_Data ( ) to read an object of ADMISSION type
Function Display() to display the details of an object
Function Draw-Nos ( ) to choose 2 students randomly.
And display the details. Use random function to generate admission nos. to match with
AD_NO. / (4)
2007 / Whatdoyouunderstand.by DataEncapsulationand Data·Hiding?Also,giveanexampleinC++toillustrateboth. / 2
Define a class STOCK in C++ with following description :
Private Members
•!Code of type integer (Item Code)
•Item of type string (Item Name)
•. Price of type float (Price of each item)
•Qty of type. integer (Quantity in stock)
•Discount of type float (Discount percentage on the itedJ.)
•A member function FindDisc() to calculate discounts
per the following rule :
If Qty<=50Discount is 0
If 50<Qty<=l00. Discount is 5
If Qty> lOODiscount is 10
Public Members
• A function Buy() to allow user to enter values for Code, Item, Price,. Qty and call function FindDisc() to calcula& the Discount.
•A • function ShowAll() to allow user to view the content of all the data members. / 4
2008 / Differentiatebetweenmembers,whicharepresentwithintheprivatevisibility
modewiththosewhicharepresentwithinthepublicvisibilitymodes. / 2
Define a class Candidate in C++ with following description :
Private Members
•A data member RNo (Registration Number) of type long
•A data member Name of type string
•A data member Score of type float
•A data member Remarks of type string
•A member function AssignRem( ) to assign Remarks as per the Score obtained by a candidate. Score range and the respective Remarks are shown as follows :
Score Remarks
>=50 Selected
lessthan50 Notselected
PublicMembers
  • AfunctionENTER()toallowusertoentervaluesforRNo,Name,ScorecallfunctionAssignRem()toassigntheremarks.
  • AfunctionDISPLAY()toallowusertoviewthecontentofallthedatamembers.
/ 4
2009 / What is copy constructor? Give an example in C++ to illustrate copy constructor. / 2
Define a class Applicant in C++ with following description :
Private Members
•A data member ANo (Admission Number) of type long
•A data member Name of type string
•A data member Agg (Aggregate Marks) of type 'float
•A data member Grade of type char
•A member function GradeMe() to find the Grade as per the Aggregate Marks obtained by a student. Equivalent Aggregate Marks range and the respective Grades are shown
as follows :
Aggregate MarksGrade
>=80A
less than 80 and >=65B
less than 65 and >=50c
less than 50D:,
Public Members
•A function ENTER() to allow user to enter values for ANo, Name, Agg & call function GradeMe() to find the Grade.
•A function RESULT() to allow user to view the content of all the data members. / 4
2010 / Whatdoyouunderstand . by DataEncapsulationand Data • Hiding ? Also, give an example in C++ to illustrate both.
Define a class STOCK in C++ with following description :
Private Members
•!Code of type integer (Item Code)
•Item of type string (Item Name)
•. Price of type float (Price of each item)
•Qty of type. integer (Quantity in stock)
•Discount of type float (Discount percentage on the itedJ.)
•A member function FindDisc() to calculate discounts
per the following rule :
If Qty<=50Discount is 0 If 50<Qty<=l00. Discount is 5
If Qty> lOODiscount is 10
Public Members
• A function Buy() to allow user to enter values for Code, Item, Price,. Qty and call function FindDisc() to calcula& the Discount.
•A • function ShowAll() to allow user to view the contentof all the data members.
2011 / Differentiate between Constructor and Destructor function with respecttoObjectOrientedProgramming. / 2
(c)Define a class Applicant in C++ with following description :
Private Members
•A data member ANo (Admission Number) of type long
•A data member Name of type string
•A data member Agg (Aggregate Marks) of type 'float
•A data member Grade of type char
•A member function GradeMe() to find the Grade as per the Aggregate Marks obtained by a student. Equivalent Aggregate Marks range and the respective Grades are shown
as follows :
Aggregate MarksGrade
>=80A
less than 80 and >=65B
less than 65 and >=50c
less than 50D:,
Public Members
•A function ENTER() to allow user to enter values for ANo, Name, Agg & call function GradeMe() to find the Grade.
•A function RESULT() to allow user to view the content of all the data members. / 4
2012 / What is the difference between the members in private visibility mode and the members in protected visibility mode inside a class? Also, give a suitable C++ code to illustrate both. / 2
Define a class RESTRA in C++ with following description:
Private Members
  • FoodCode of type int
  • Food of type String
  • FType of type string
  • Sticker of type string
  • A member function GetSticker() to assign the following values for Sticker as per the given FType:
FType / Sticker
Vegetarian / GREEN
Contains Egg / YELLOW
Non-Vegetarian / RED
Public Members
  • A function GetFood() to allow user to enter values for FoodCode,Food,FType and call function GetSticker() to assign Sticker.
  • A function ShowFood() to allow user to view the content of all data members.
/ 4
2013 / Write any two differences between Constructor and Destructor. Write the function headers for constructor and destructor of a class Member. / 2
Define a class Tourist in C++ with the following specification:
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 Kilometers
  • Charge – to store the car fare
Member Functions
  • A contructor function to initialize Type as ‘E’ and Freight as 250
  • A function CalcCharge() to calculate Fare as per the following citeria:
Type Charge
‘E’ 16 * Distance
‘A’ 22 * Distance
‘L’ 30 * Distance
  • A function Enter() to alow 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.
/ 4
2014 / Write anytwodifferences between Constructor andDestructor.Write functionheadersforconstructoranddestructorofaclassMember. / 2
(c)Define a class Tourist in C++ with the following specification :
Data Members
•Camo - 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 Kilometers
•Charge - to store the Car Fare Member Functions
•A constructor function to initialize Type as 'E' and Freight as 250
•A function CalcCharge() to calculate Fare as per the following criteria :
TypeCharge
' E '16*Distance
'A '22*Distance
' L '30*Distance
•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. / 4
Chapter - Constructor & Destructor
2005 / Answer the questions (i) and (ii) after going through the following class:
class Test
{
char Paper[20];
int Marks;
public:
Test () // Function 1
{
strcpy (Paper, “Computer”)
Marks = 0;
}
Test (char P [] ) // Function 2
{
strcpy(Paper,P);
Marks = 0;
}
Test (int M) // Function 3
{
strcpy(Paper,”Computer”);
Marks = M;
}
Test (char P[], int M) // Function 4
{
strcpy (Paper, P);
Marks = M;
}
};
(i) Which feature of Object Oriented Programming is demonstrated using
Function 1, Function 2, Function 3 and Function 4 in the above class Test?
(ii) Write statements in C++ that would execute Function 2 and Function 4 of
class Test. / 2
2006 / Answer the questions (i) and (ii) after going through the following class:
class Interview
{ int month;
public:
Interview (int y) {month=y ;} //Constructor 1
Interview (Interview&t); //Constructor 2
};
i. Create an object, such that it invokes Constructor 1.
ii. Write complete definition for Constructor 2. / 2
2007 / Answerthequestions(i)and(ii)aftergoingthroughthefollowing
class:
classExam
intRno,MaxMarks,MinMarks,Marks;public:
Exam()I/Module1
Rno=l01;MaxMarks=lOO;MinMarks=40;Marks=75;Exam(intPrno,intPmarks) I/Module2
Rno=Prno;MaxMarks=lOO;MinMarks=40;Marks=Pmarks;
-Exam()I/Module3
cout<11ExamOver11<endl;
voidShow()I/Module4
cout<Rno<":"<MaxMarks<":"<MinMarks<endl;cout<"[MarksGot]"<Marks<endl;
};
(i)As per Object Oriented Programming,which concept isillustrated byModule 1andModule 2together ?
(ii)What is Module 3 referred as ? Whendo you think,
Module3willbeinvoked/called ? / 2
2008 / Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used in the following program jointly illustrated
by the functions [I] to [IV].
# include <iostream.h>
void Print ( ) // Function [I]
{
for(intK=l·K<=60 ·K++)cout<"-"·
',
cout<endl;
}
voidPrint(intN)11Function[II]
{
for(intK=l·,K<=N·,L++)cout<"*"'·cout<endl;
}
voidPrint(charT,intN)11Function[IV]
{
for(intK=l;K<=N;K++)cout<T;cout<endl;
}
voidmain ()
{
intU=9,V=4,W=3;
charC='("a.)',·
Print(C,V);
Print(U,W);
} / 2
2009 / Write the output of the following C++ code. Also, write the name of feature of Object Oriented programming used in the following program jointly illustrated by the functions (i) to (iv) :
#include<iostream.h>
void Line() // Function 1
{
for(int L=1; L<= 80; L++) cout<”-“;
cout<endl;
}
void Line(int N) // Function 2
{
for(int L=1; L<=N; L++) cout<”*”;
cout<endl;
}
void Line(char C,A,int N) // Function 3
{
for(int L=1; L<=N; L++) cout<C;
cout<endl;
}
void Line(intM,int N) // Function 4
{
for(int L=1; L<=N; L++) cout<M*L;
cout<endl;
}
void main()
{
int A=9,B=4,C=3;
char K=’#’;
Line(K,B);
Line(A,C);
} / 2
2010 / Answer the questions (i) and (ii) after going through the following
class :
class Exam
int Rno,MaxMarks,MinMarks,Marks; public:
Exam ()I /Module 1
Rno=l01;MaxMarks=l O O;MinMarks=40;Marks=75; Exam (int Prno,int Pmarks)I /Module 2
Rno=Prno;MaxMarks=lO O;MinMarks=40;Marks=Pmarks;
-Exam ()I /Module 3
cout< 11 ExamOver 11 <endl;
voidShow ()I /Module 4
cout<Rno<" :"<MaxMarks<" :"<MinMarks<endl; cout<" [Marks Got]" <Marks<endl;
} ;
i)As per Object Oriented Programming,which concept isillustrated byModule 1andModule 2together ?
ii)What is Module 3 referred as ? Whendo you think,
Module3willbeinvoked/called ? / 2
2011 / Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used in the following ' program jointly illustrated by the functions [I] to [IV] :
*include< iost ream .h>
voidLine ( )/ / Funct ion[ I ]
{
f or( intL= l ; L<=8 0 ; L++ )cout<"-"; cout<endl ;
1',voidLine ( intN )/ / Funct ion[ II ]
{
f or( intL= l ; L<=N ;L+ + )cout<"*"; cout<endl ;
voidLine ( cha rC , int N )/ / Funct ion[ I I I ]
{
f or( intL= l ;L<=N ;L++)cout<C ; cout<endl ;
voidLine ( intM , intN )/ / Funct ion[IV]
{
f or( intL= l ; L<=N ;L++ ) cout<M*L ; cout<endl ;
voidma in ( )
{
intA= 9 , B= 4 , C=3;
charK='#'; Line ( K , B) ;
Line ( A , C ) ;
}
2012 / Answer the questions(i) and (ii) after going through the following class :
class Travel
{
intPlaceCode; char Place[20]; float Charges;
public:
Travel() // Function 1
{
PlaceCode =1; strcpy(Place,”DELHI”); Charges=1000;
}
Travel(float C) // Function 2
{
Cout<PlaceCode<”:”<Place<”:”<Charges<endl;
}
~Travel() // Function 3
{
Cout<”Travel Plan Cancelled”<endl;
}
Travel(intPC,char P[],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 referred as ?
ii)In Object Oriented Programming, which concept is illustrate by Function 3? When is this function called/invoked? / 2
2013 / Answer the question (i) and (ii) after going through the following class :
Class Motor
{
Int MotorNo,Track;
Public:
Motor(); // Function 1
Motor (int MN); // Function 2
Moto (Motor & M); // Function 3
Void Allocate(); // Function 4
Void Move(); // Function 5
};
Void main()
{
Motor M;
:
:
}
(i)Out of the following, which of the option is correct foe calling function 2?
Option 1 – Moto N(M);
Option 2 – Motor P(10);
(ii)Name the feature of Object Oriented Programming, which is illustrated by Function1,Function 2 and Function 3 combined together.
2014 / 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 ()
{
Motor M;
}
(i)Out of the following, which of the option is correct for calling Function 2 ?
Option 1 - Motor N(M ); Option 2 - Motor P(lO );
(ii)Name the feature of Object Oriented Programming, which is illustrated by Function 1, Function 2 and Function 3 combined together. / 2
Chapter - Inheritance
2005 / Answer the questions (i) to (iv) based on the following code: 4
class Medicines
{
char Category[lO];
char Date_of_manufacture[lO];
char Company[20];
public:
Medicines();
void entermedicinedetails();
void showmedicinedetails();
} ;
class Capsules: public Medicines
{
protected:
char capsule_name[30];
char Volume_label[20];
public:
float Price;
Capsules();
void entercapsuledetails();
void showcapsuledetails();
};
class Antibiotics: public Capsule
{
intDosage_units;
char Side_effects[20];
intUse_within_days;
public:
Antibiotics() ;
void enterdetails();
void showdetails();
};
(i) How many bytes will be required by an object of class Medicines and an object
of class Antibiotics respectively?
(ii) Write names of all the member functions accessible from the object of class
Antibiotics.
(iii) Write names of all the members accessible from member functions of class
Capsules.
(iv) Write names of all the data members, which are accessible from objects of class
Antibiotics. / 4
2006 / Answer the questions (i) to (iii) based on the following code
class stationary
{
char Type;
char Manufacturer [10];
public:
stationary();
void Read_sta_details( );
void Disp_sta_details( );
};
class office: public stationary
{
intno_of_types;
float cost_of_sta;
public:
void Read_off_details( );
void Disp_off_details( );
};
class printer: private office
{
intno_of_users;
char delivery_date[10];
public:
void Read_pri_details( );
void Disp_pri_details( );
};
void main ( )
{ printer MyPrinter; }
i. Mention the member names which are accessible by MyPrinter declared in main() function
ii. What is the size of MyPrinter in bytes?
iii. Mention the names of functions accessible from the member function
Read_pri_details () of class printer. / 4
2007 / Answerthequestions(i)to(iv)basedonthefollowing
Class·Director
{
long DID;
char Name[20];
protected:
charDescription[40];
voidAllocate();
public:
Director();voidAssign();void Show();
};
classFactory:publicDirecter
{
intFID;I/FactoryIDI
charAddress[20];
protected:
intNOE;I/No.ofEmployees
public:
Factory();voidInput();
Void Output();
};
classShowRoom:private Factory
{
intSID;I/Showroo:nIDcharCity[20];
public:
ShowRoom();
voidEnter();
voidDisplay();
};

(i)Whichtypeof·inheri tance·,outofthe.followingisillustratedinthe aboveC++?

(a)Single level Inheritance

(b)MultiLevelInheritance

(c)Multiple Inheritance
(iii)Write the names of members which are accessible by the object of class showroom.
(iv)Write the names of member function which are accessible by the object of class showroom.
(v)Write the names of members which are accessible by the object of class Factory. / 4
2008 / Answerthequestions(i)to(iv)based·onthefollowing:
class Student
{
int Rno ;
char Name[20] ; float Marks ;
protected :
void Result ( ) ; public :
Student ( ) ;
void Register ( ) ; void Display ( ) ;
};
class Faculty
{
long FCode ;
char FName [20] ;
protected :
float Pay ; public :
Faculty ( ) ; void Enter ( ) ; void Show ( ) ;
};
class Course : public Student, private Faculty
{
long CCode [IO]; char CourseName [50]; char StartDate [8J, EndDate [8] ;
public :
Course ( ) ;
void Commence ( ) ; void CDetail ( ) ;
};
(i)Which type of inheritance is illustrated in the above C++ code ?
(ii)Write the names of all the data members, which is/are accessible from member function Commence of class Course.
(iii)Write the names of member functions, which are accessible from objects of class Course.
(iv)Write the names of all the members, which are accessible from objects of class Faculty. / 4
2009 / Answerthequestions(i)to(iv)based·onthefollowing:
Class student
{
int Rollno;
char SName [20]; float Marksl
protected:
void Result (); public:
Student();
void Enroll( );void Display( );
};
Class teacher
{
long TCode;
char TName [20]; protected:
float Salary; public:
Teacher(); void Enter (); voidShow ();
};
class Course: public Student , privateTeacher
{
long CCode[lO];char CourseName[ SO]; char StartDate[8],EndDate[8];
public:
Course( );
void Commence (); void CDetail ();
} ;
(i)Write the names of member functions, which are accessible from objects of class Course.
(ii)Write the names of all the data members, which is/are accessible from member function Commence of class Course.
(iii)Write the names of all .the members, which are accessible from objects of class Teacher.
(iv)Which type of Inheritance 1s illustrated in the above C++ code ?
2010 / Answerthequestions(i)to(iv)basedonthefollowing
Class·Director
{
long DID;
char Name[20];
protected:
charDescription[40];
voidAllocate();
public:
Director();voidAssign();void Show();
};
classFactory:publicDirecter
{
intFID;I/FactoryIDI
charAddress[20];
protected:
intNOE;I/No.ofEmployees
public:
Factory();voidInput();
Void Output();
};
classShowRoom:private Factory
{
intSID;I/Showroo:nIDcharCity[20];
public:
ShowRoom();
voidEnter();
voidDisplay();
};

(i)Whichtypeof·inheri tance·,outofthe.followingisillustratedinthe aboveC++-'?·

(d)Single level Inheritance

(e)MultiLevelInheritance

(f)Multiple Inheritance
(vi)Write the names of members which are accessible by the object of class showroom.
(vii)Write the names of member function which are accessible by the object of class showroom.
Write the names of members which are accessible by the object of class Factory.
2011 / Answerthequestions(i)to(iv)based·onthefollowing:
Class Student
{
Int Rollno;
Char SName[20];
Float Marks1;
Protected :
Void Result();
Public:
Student();
Void Enroll(); void display();
};
Class Teacher
{
Long TCode;
Char TName[20];
Protected:
Float salary;
Public:
Teacher();
Void Enter();
Void show();
};
Class Course : public Student , private Teacher
{
Long CCode[10]; char CourseName[50];
Char StartDate[8],EndDate[8];
Public:
Course();
Void Commerce();
Void CDetails();
};
(i)Write the names of member functions, which are accessible from objects of class Course.
(ii)Writethenamesofallthedatamembers,whichis/areaccessiblefrommemberfunctionCommenceof classCourse.
(iii)Writethenamesofall.themembers,which areaccessiblefromobjectsofclassTeacher.
(iv)Which type of Inheritance 1s illustrated in the above C++ code ?
2012 / Answerthequestions(i)to(iv)basedonthefollowing:
class COMPANY
charLoca tion [20 ] ;
doubleBudget , Incorne; pr otected :
voidAccou n ts ( ) ; public :
COMPANY ( ) ;
voidRegister ( ) ; voidShow ( ) ;
} ;
cla ss FACTORY :publicCOMPANY
charLocation ( 2 0 ) ; intWor kers;
protected :
doubleSalar y; voidComputer ( ) ;
public :
FACTORY () ;
void Enter ( ) ; voidShow ( ) ;
} ;
classSHOP : pr ivate COMPANY
cha rLoca t ion [ 2 0 ] ; f loatArea ;
doubleSale ; public :
SHOP ( ) ;
voidInpu t ( ) ; voidOutpu t ( ) ;
} ;
(i)Name the type ofinheritance illustrated m the above C++code.
(ii)Writethenameofdatamembers,which arcaccessiblefrommemberfunctionsofclassSHOP.