You are on page 1of 31

KENDRIYA VIDYALAYA SANGATHAN, KOLKATA REGION

Computer Science Question Bank


Unit 2
Unit / Chapter
VSA
SA I
SA II
LA
(1 Mark)
(2
(3 Marks) (4 Marks)
Marks)
Object Oriented Programming in C++
a) Introduction to OOP using C+
+
b) Constructor& Destructor
c) Inheritance
d) Pointers
e) Data File Handling
Total

_
1

1
1

_
1
1

_
1
-

Total

(2)

(1) 2
(1) 4
1(3)
(3) 6

IMPORTANT QUESTIONS (1 OR 2 TIMES)


Chapter Introduction to OOP using C++
200
5

200
6

Define the term Data Hiding in the context of Object Oriented Programming. Give a
Suitable example using a C++ code to illustrate the same.
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.
Define Multilevel and Multiple inheritance in context of Object Oriented
Programming. Give suitable example to illustrate the same.

2
4

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.
200
7

What do
you understand . by Data Encapsulation
Also, give an example in C++ to illustrate both.

and Data Hiding ?

Define a class STOCK in C++ with following description :

(4
)

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 discount


per the following rule :
If Qty<=50
If 50<Qty<=l00
If Qty> lOO

Discount is 0
. Discount is 5
Discount 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.

200
8

A function ShowAll() to allow user to view the content of all the data members.

Differentiate between members, which are present within the private visibility
mode with those which are present within the public visibility modes.

Define a class Candidate in C++ with following description :


Private Members

2
4

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

less than 50

Not selected

Public Members
A function ENTER( ) to allow user to enter values for RNo, Name,
Score & call function AssignRem( ) to assign the remarks.
A function DISPLAY( ) to allow user to view the content of all the data
members.
200
9

What is copy constructor? Give an example in C++ to illustrate copy constructor.


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 Marks Grade
>=80 A
less than 80 and >=65
B
less than 65 and >=50
c
less than 50 D
:,
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.

201
0

What do
you understand . by Data
Encapsulation
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)

and Data Hiding ?

2
4

Qty of type. integer (Quantity in stock)

Discount of type float (Discount percentage on the itedJ.)

A member function FindDisc() to calculate discount


per the following rule :
If Qty<=50
If Qty> lOO

Discount is 0 If 50<Qty<=l00 . Discount is 5


Discount 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.

201
1

201
2

A function ShowAll() to allow user to view the content of all the data
members.
Differentiate between Constructor and Destructor function with respect to
Object Oriented Programming.
(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 Marks Grade
>=80 A
less than 80 and >=65
B
less than 65 and >=50
c
less than 50 D
:,
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.
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
4

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
NonRED
Vegetarian
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.
201
3

201
4

Write any two differences between Constructor and Destructor. Write the function
headers for constructor and destructor of a class Member.
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.

Write any two differences between Constructor and Destructor. Write function headers for
constructor and destructor of a class Member.
(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 :


Type Charge

2
4

2
4

' 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.
Chapter - Constructor & Destructor
200 Answer the questions (i) and (ii) after going through the following class:
5
classTest
{
charPaper[20];
intMarks;
public:
Test()//Function1
{
strcpy(Paper,Computer)
Marks=0;
}
Test(charP[])//Function2
{
strcpy(Paper,P);
Marks=0;
}
Test(intM)//Function3
{
strcpy(Paper,Computer);
Marks=M;
}
Test(charP[],intM)//Function4
{
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.
200
6

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.

200
7

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;
I /Module 2

Exam (int Prno,int Pmarks)

Rno=Prno;MaxMarks=lO O;MinMarks=40;Marks=Pmarks;
-Exam ()
I /Module 3
cout<< 11 Exam Over 11 <<endl;
void Show ()
I /Module 4
cout<<Rno<<" :"<<MaxMarks<<" :"<<MinMarks<<endl; cout<<"
[Marks Got]" <<Marks<<endl;
};

(i)

As per Object Oriented Programming, which concept is


illustrated by Module 1 and Module 2 together ?

What is Module 3 referred as ? When do you think,


Module 3 will be invoked/called ?
(ii)

200
8

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 (int K=l K<=60 K++) cout<< "-"
''
'
cout<<endl ;
,

}
void Print (int N)

11 Function [II]

for (int K=l ,K<=N , L++) cout<< "*" '


cout<<endl ;
}
void Print (char T, int N)

11 Function [IV]

{
for (int K= l ;K<=N ;K++) cout<<T ;
cout<<endl ;

}
void main ( )

{
int U=9, V=4, W=3;
char C= '("a.)' ,
Print (C, V) ;

200
9

Print (U, W) ;
}
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);
}
201
0

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 Exam Over 11 <<endl;
void Show ()
I /Module 4
cout<<Rno<<" :"<<MaxMarks<<" :"<<MinMarks<<endl; cout<<" [Marks Got]"
<<Marks<<endl;
};

i)

As per Object Oriented Programming, which concept is


illustrated by Module 1 and Module 2 together ?

ii) What is Module 3 referred as ? When do you think,


Module 3 will be invoked/called ?
201
1

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>
void Line ( )
/ / Funct ion [ I ]
{
f or
( int L= l ; L<=8 0 ; L++ )
cout<<"-"; cout<<endl ;
1',

void

Line ( int

N)

/ / Funct ion [ II ]

{
f or

( int

L= l ; L<=N ;L+ + )

void

Line ( cha r C , int N )

{
f or

( int

void

Line ( int

{
f or

( int

cout<<"*"; cout<<endl ;

/ / Funct ion [ I I I ]

L= l ;L<=N ;L++) cout<<C ; cout<<endl ;


M , int N )

/ / Funct ion [IV]

L= l ; L<=N ;L++ ) cout<<M*L ; cout<<endl ;

void
{
int
char
Line (

201
2

201
3

ma in ( )
A= 9 , B= 4 , C=3;
K='#'; Line ( K , B) ;
A,C);

}
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?
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)
(ii)
201
4

Out of the following, which of the option is correct foe calling function 2?
Option 1 Moto N(M);
Option 2 Motor P(10);
Name the feature of Object Oriented Programming, which is illustrated by
Function1,Function 2 and Function 3 combined together.

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.
Chapter - Inheritance
200 Answer the questions (i) to (iv) based on the following code: 4
5
classMedicines
{
charCategory[lO];
charDate_of_manufacture[lO];
charCompany[20];
public:
Medicines();
voidentermedicinedetails();
voidshowmedicinedetails();
};
classCapsules:publicMedicines
{
protected:
charcapsule_name[30];
charVolume_label[20];
public:
floatPrice;
Capsules();
voidentercapsuledetails();
voidshowcapsuledetails();
};
classAntibiotics:publicCapsule
{
intDosage_units;

charSide_effects[20];
intUse_within_days;
public:
Antibiotics();
voidenterdetails();
voidshowdetails();
};

200
6

(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.
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?

200
7

iii. Mention the names of functions accessible from the member function
Read_pri_details () of class printer.
Answer the questions (i) to (iv) based on the following

Class Director
{
long
DID;
char Name[20];
protected:
char Description[40] ;
void Allocate ();
public:
Director(); void Assign ();
void Show ();
};
class Factory:public
{
int FID;
char Address[20];

Directer

I /Factory

ID

protected:
int NOE;
I /No. of Employees
public:
Factory(); voidInput ();
Void Output ();
};
class ShowRoom:private Factory
{
int SID;
I /Showroo:n ID char City[20];
public:
ShowRoom ();
void Enter ();
voidDisplay ();
};
(i)Which type of inheri tance, out of the . following is illustrated in the above C+
+?
Single level Inheritance
Multi Level Inheritance
(c) Multiple Inheritance
(iii)
Write the names of members which are accessible by the object of class

showroom.
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.
Answer the questions (i) to (iv) based on the following :
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
{
(iv)

200
8

200
9

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.
Answer the questions (i) to (iv) based on the following :
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 (); void
Show ();
};
class Course: public Student , private Teacher
{
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 ?
201
0

Answer the questions (i) to (iv) based on the following


Class Director
{
long
DID;
char Name[20];
protected:
char Description[40] ;
void Allocate ();
public:
Director(); void Assign ();
void Show ();
};
class Factory:public
{
int FID;
char Address[20];
protected:
int NOE;
I /No. of Employees
public:
Factory(); voidInput ();
Void Output ();
};

Directer

I /Factory

ID

class ShowRoom:private Factory


{
int SID;
I /Showroo:n ID char City[20];
public:
ShowRoom ();
void Enter ();
voidDisplay ();
};
(i)Which type of inheri tance, out of the . following is illustrated in the above C+
+ -'
?
Single level Inheritance
Multi Level Inheritance
(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.

201
1

Answer the questions (i) to (iv) based on the following :


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)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 ?
201
2

Answer the questions (i) to (iv) based on the following :


class COMPANY
char Loca tion [20 ] ;
double
Budget , Incorne; pr otected :
void Accou n ts ( ) ; public :
COMPANY ( ) ;
void Register ( ) ; void Show ( ) ;
};
cla ss FACTORY :public
COMPANY
char
Location ( 2 0 ) ; int
Wor kers;
protected :
double
Salar y; void Computer ( ) ;
public :
FACTORY () ;
void Enter ( ) ; void Show ( ) ;
};
class SHOP : pr ivate COMPANY

cha r Loca t ion [ 2 0 ] ; f loat


Area ;
double
Sale ; public :
SHOP ( ) ;
void Inpu t ( ) ; void
Outpu t ( ) ;
};

(i)

Name the type of inheritance illustrated m the above C++ code.

(ii)

Write the name of data members, which arc accessible from m ember
functions of class SHOP.

(iii)

Write the names of all the member fonction s, which are accessible

from objects belonging to class FACTORY.


(iv)
201
3

201
4

Write the names of all the mem bers, which arc accessible from
objects of class SHOP.

Consider the following C++ code and answer the questions from (i) to (iv):
Class Student
{
Int Class,Rno;
Char Section;
Protected:
Char SName[20];
Public:
Student();
Void Stentry();
Void Stddisplay();
};
Class Score : private Student
{ Float Marks[5];
Protected:
Char Grade[5];
Public:
Score();
Void Sentry();
Void Sdisplay();
};
Class Report : public Score
{
Float Total, Avg;
Public:
Char OverallGrade,Remarks[20];
Report();
Void Revaluate();
Void RPrint();
};
(i)
Which type of Inheritance is shown in the above example ?
(ii)
Write the names of those data members, which can be directly accessed from
the objects of class Report.
(iii)
Write the names of those members functions , which can be directly accessed
from the objects of class Report.
(iv)
Write the names of those data members, which can be directly accressed
from the Sentry() function of class Score.
(d)
Consider the following C++ code and answer the questions from (i) to
(iv) :
class Student
{
int Class,Rno; char Section;
protected :
char SNarne[20]; public :
Student();
void Stentry(); void Stdisplay();

(i)

};
class Score: private Student
{
float Marks[S]; protected:
char Grade[S]; public:
Score ();
void Sentry(); void Sdisplay();
};
class Report: public Score
{
float Total, Avg; public:
char OverallGrade, Remarks [20];
Report();
void REvaluate( ); void RPrint();
}i
Which type of Inheritance is shown in the above example ?

(ii)
Write the names of those data members, which can be directly accessed from
the objects of class Report.
(iii)
Write the names of those member functions, which can be directly
accessed from the objects of class Report.
(iv)
Write the names of those data members, which can be directly accessed from
the Sentry() function of class Score.

Chapter Pointers
200
Find the output of the following program :
5

#include <iostream.h> void main ()


{
int *Queen,Moves[ ]={ll,22,33,44}; Queen=Moves;
Moves[ 2]+=22;
cout<<"Queen @"<<*Queen<<endl;
*Queen-=11; Queen+=2;
cout<<"Now @"<<*Queen<< ndl; Queen++;
cout<<"Finally @"<<*Queen<<endl;
cout<<"New Origin @"<<Moves[O]<<endl;
200
6

Find the output of the following program


# include<iostream.h>
#include<string.h> class
state
{
char *
state_name;
int size;
public;
state( ); { size=0; state_name=new char[size+1]; }

state(char *s)
{
size = strlen(s) ; state_name = new
char[size+1];} strcpy(state_name,s);
}
void display() {cout<<state
name<<endl; } void Replace (state &a,
state &b)
{
size = a.size
+ b.size;
delete
state_name;
state_name = new char[size+1] ;
strcpy(state_name, a.state_name);
strcat(state_name, b.state_name);
}
};
void main( )
{

char *temp = Delhi;


state state1(temp), state2(Mumbai), state3(Nagpur), SI,
S2; SI .Replace(state1, state2);
S2.Replace(S1, state3);
S1.display( );
S2.display( );

}
200
7

(c)

Find the output of the following program :

# inclu de <iostream.h>
void main ( )
{
int Track [ ] = {I 0, 20, 30, 40}, *Striker ;
Striker=Track ;
Track [1] + = 30 ;
cout<<"Striker>"<<* Striker<<endl ;
* Striker -= 10
; Striker++ ;
cout<< "Next @" <<* Striker<<endl ;
Striker+=2 ;
cout<< "Last @" <<*Striker<<endl;
cout << "Reset To" <<Track [O] <<endl ;

}
200 Find the output of the following program :
8

# inclu de <iostream.h> void main ( )


{

int Track [ ] = {I 0, 20, 30, 40}, *Striker ; Striker=Track


;
Track [1] + = 30 ; cout<<"Striker>"<<*
Striker<<endl ;
* Striker -= 10 ; Striker++
;
cout<< "Next @" <<* Striker<<endl ; Striker+=2 ;
cout<< "Last @" <<*Striker<<endl;
cout << "Reset To" <<Track [O] <<endl ;
}
200
9

Find the output of the following program :


#include <iostream.h> void main ()
{
int *Queen,Moves[ ]={ll,22,33,44}; Queen=Moves;
Moves[ 2]+=22;
cout<<"Queen @"<<*Queen<<endl;
*Queen-=11; Queen+=2;
cout<<"Now @"<<*Queen<< ndl; Queen++;
cout<<"Finally @"<<*Queen<<endl;
cout<<"New Origin @"<<Moves[O]<<endl;

201
0

Find the output of the following program :

#include
<iostrearn.h>
#include
<ctype.h>
void MyCode(char
Msg[],char CH)
for(int Cnt=O;Msg[Cnt] !=' \0' ;Cnt++)
if
(Msg[Cnt]>='B'
& & Msg[Cnt]<=' G' ) Msg[Cnt]=tolower(Msg[Cnt]);
else
if
(Msg[Cnt]=='A'
11
Msg[Cnt]==' a' ) Msg[Cnt]=CH;
else
if
(Cnt%2==0) Msg[Cnt]=toupper(Msg[ Cnt]);
else
Msg[Cnt]=Msg[Cnt-1];
void main ()
{
char MyText []="ApEACeDri VE" ; MyCode(MyText, '@' );
cout<< "NEW
TEXT: "<<MyText<<endl;

201
1

Find the output of the following program :


#include <iostream.h> void main ()
{
int *Queen,Moves[ ]={ll,22,33,44}; Queen=Moves;
Moves[ 2]+=22;
cout<<"Queen @"<<*Queen<<endl;
*Queen-=11; Queen+=2;
cout<<"Now @"<<*Queen<< ndl; Queen++;
cout<<"Finally @"<<*Queen<<endl;
cout<<"New Origin @"<<Moves[O]<<endl;

201
2

Find the output of the following program


#include

<iostrearn .h>

#include <ctype .h>


typedef char Str80 f 80];

void main()
{

char

*Notes;

Str80 SLr'-""vR2GooO" ;
int L=6; Notes=Str
; while

(L>=3 )

Str [L]= (isupper (Str!T.l )?to.L owcr.( SL r f r,]): toupper


(Str [L]));
cout<<Notes<<endl ;

L-- ;
Notes++;

}
201
4

Observe the following C++ code carefully and obtain the output, which will appear on
the screen after execution of it.
Important Note :
All the desired header files are already included in the code, which are required to run
the code.
void main ( )
{
char *String="SHAKTI" ;
int
*Point,Value[]={l0,15,70,19}; Point=Value ; coutcc*PointccStringccendl; String+
+;
Point++ ; coutcc*PointccStringccendl ;
}

Chapter Data File Handling


200 void main( )
6
{
char=A;

fstream fileout(data.dat,ios::out); fileout<<ch;


int p = fileout.tellg( ); cout<<p;
}
What is the output if the file content before the execution of the program is the
string ABC (Note that are not part of the file)
Write a function to count the number of words present in a text file named PARA.TXT.
Assume that each word is separated by a single blank/space character and no
blanks/spaces in the beginning and end of the file.
c.
Following is the structure of each record in a data file named COLONY.DAT.
structCOLONY

char Colony
Code[10];
char Colony
Name[10]; int
No of People;

};
Write a function in C++ to update the file with a new value of No _of_People.
The value of Colony_Code and No_of_People are read during the execution of
the program.
200 Observe the program segment given below carefully and fill the
7
blanks marked as Statement 1 and Statement 2 using tellg() and
seekp() functions for performing the required task.
#include <fstream.h>
class Customer
{
long Cno;char Name[20], Mobile[12]; public:
//Function to allow user to enter the Cno, Name, Mobile

void Enter();
//Function to allow user to enter (modify) mobile number
void Modify();
//Fnction to return value of Cno long
GetCno() {return Cno;}
};

void ChangeMobile()
{
Customer C;
fstream

F;

F.open (11 CONTACT. DAT 11 , i.os: :binary I ios: :in I ios:out)


;

long Cnoc; //Customer no. whose mobile number needs to be


c:;hanC,ed. cin>>Cnoc;
while (F.read((char*)&C,sizeof(C)))
if (Cnoc==C.GetCno())
C.Modify ();
//Statement 1
int Pos=I /To finq the curret position of file pointer \';
//Statement 2
//To move the file pointer to write the

.}

//modified record back onto the file


//for the des.ired Cnoc F.write( (char*)&C,sizeof(C));
}

'

F.close();
200
8

Observe the program segment given below carefully and fill the blanks marked as Statement
1 and Statement 2 using seekg( ), seekp( ), tellp( ) and tellg( ) functions for performing the
required task.
# include <fstream.h>
class PRODUCT
{

int Pno ;char Pname [20] ; int Qty ;


public :
void ModifyQty ( )II The function is to modify quantity of a PRODUCT
};
void PRODUCT : : ModifyQty ( )

{
fstream File ;
Fil.open ("PRODUCT.DAT", ios : : binary

I ios

: : in

I ios

: : out) ; int MPno;

cout << "Product No to modify quantity :" ;cin>> MPno ;

while (Fil.read ((char*) this, sizeof (PRODUCT) ) )


{
if (MPno= =Pno)
{
cout<< "Present Quantity:" <<Qty<<endl ; cout<< "Changed
Quantity:" ;cin>>Qty ;
int Position =
//Statement 1
//Statement 2
Fil.write ( (char*) this, sizeof (PRODUCT) ) ; I I Re-writing the record
}

Fil.cFil.close( ) ;
}
Write a function in C++ to count the no. of "Me" or "My" words present text file "DIARY.TXT".
If the file "DIARY.TXT" content is as follows :
My first book was Me and My family. It gave me chance to be known to the world.
The output of the function should be Count of Me/My in file : 4
Write a function in C++ to search for a laptop from a binary file

"LAPTOP.DAT" containing the objects of class LAPTOP (as defined below).


The user should enter the Model No and the function should search and display
the details of the laptop.
class LAPTOP
{
long ModelNo ;
float RAM, HDD ;
char Details [120] ;
public :
void StockEnter ( ) {cin>>ModelNo>>RAM>>HbD ; gets (Details) ; }
void StockDisplay ( ) {cout<<ModelNo<<RAM<<HDD<<Details<<endl
;} long ReturnModelNo ( ) {return ModelNo ;}

};
200
9

Observe the program segment given below carefully and fill the blanks marked as
Statement1 and Statement 2 using seekg() , seekp(), tellp() and tellg() functions for
performing the required task.
#include<fstream.h>
Class ITEM
{
Int Ino;
Char Iname[20]; float Price;
Public:
Void ModifyPrice(); // The function is to modify price of a particular ITEM
}:
Void ITEM :: ModifyPrice()
{
Fstream File;
File.Open(ITEM.DAT,ios::binary | ios::in | ios::out);
Int CIno;
Cout<<Item No to modify price :; cin>>Cino;
While(File.read((char *)this,sizeof(ITEM))
{
If(Cino == Ino)
{
Cout<<Present Price: <<Price<<endl;
Cout<<Changed Price:; cin>>Price;
Int FilePos = ___________; // statement 1
_____________________; Statement 2

File.write ((char *) this, sizeof(ITEM)); // Re-writing the record


}
}
File.close();

}
Write a function in C++ to count the no. of He or She words present in a text file
STORY.TXT.
If the file STORY.TXT content is as follows :
He is playing in the ground. She is
playing with her dolls.
The output of the function should be
Count of He/She in file :2
Write a function in C++ to search for a camera from a binary file
"CAMERA.DAT" containing the objects of class CAMERA (as defined below).
The user should enter the Model No and the function should search and
display the details of the camera.
Class CAMERA
{
Long ModelNo;
Float MegaPixel;
Int Zoom;
Char Details[120];
Public:
Void Enter() {cin>>ModelNo>>MegaPixel>>Zoom; gets(Details);}
Void Display() {
Cout<<ModelNo<<MegaPixel<<Zoom<<Details<<endl;}
Long GEtModelNo() { return ModelNo;}
201
0

};
Observe the program segment given below carefully and fill the
blanks marked as Statement 1 and Statement 2 using tellg() and
seekp() functions for performing the required task.
#include <fstream.h> class
Customer
long Cno;char Name[20], Mobile[12]; public:
//Function to allow user to enter the Cno, Name, Mobile
void Enter();
//Function to allow user to enter (modify) mobile number
void Modify();
//Fnction to return value of Cno long
GetCno() {return Cno;}
};

void ChangeMobile()
{

Customer C;
fstream

F;

F.open ( CONTACT. DAT


;
11

11
,

i.os: :binary I ios: :in I ios: :out)

long Cnoc; //Customer no. whose mobile number


needs to be c:;hanC,ed. cin>>Cnoc;
while

(F.read((char*)&C,sizeof(C)))

if (Cnoc==C.GetCno())
C.Modify();
Int Pos = _______________ // To find the current
position of file pointer

//To move the file pointer to write the


---

.}

//modified record back onto the file


//for the des.ired Cnoc
F.write((char*)&C,sizeof(C));
}

'

F.close();

201
1

}
Observe the program segment given below carefully and fill the blanks marked as
Statement1 and Statement 2 using seekg() , seekp(), tellp() and tellg() functions for
performing the required task.
#include<fstream.h>
Class ITEM
{
Int Ino;
Char Iname[20]; float Price;
Public:
Void ModifyPrice(); // The function is to modify price of a particular ITEM
}:
Void ITEM :: ModifyPrice()
{
Fstream File;
File.Open(ITEM.DAT,ios::binary | ios::in | ios::out);
Int CIno;
Cout<<Item No to modify price :; cin>>Cino;
While(File.read((char *)this,sizeof(ITEM))
{

If(Cino == Ino)
{
Cout<<Present Price: <<Price<<endl;
Cout<<Changed Price:; cin>>Price;
Int FilePos = ___________; // statement 1
_____________________; Statement 2
File.write ((char *) this, sizeof(ITEM)); // Re-writing the record
}
}
File.close();
}

Write a function in C++ to count the no. of "He" or "She" words present in a
text file "STORY.TXT".
If the file "STORY.TXT" content is as follows :
He is playing in the ground. She
is playing with her dolls.

The output of the f unction should be


Count of He/She in file: 2
Write a function in C++ to search for a camera from a binary file
"CAMERA.DAT" containing the objects of class CAMERA (as defined below).
The user should enter the Model No and the function should search and
display the details of the camera.
Class CAMERA
{
Long ModelNo;
Float MegaPixel;
Int Zoom;
Char Details[120];
Public:
Void Enter() {cin>>ModelNo>>MegaPixel>>Zoom; gets(Details);}
Void Display() {
Cout<<ModelNo<<MegaPixel<<Zoom<<Details<<endl;}
Long GEtModelNo() { return ModelNo;}
};
201
2

Observe the program


that follow :
class
{

segment given below carefully and the questions

Stock

int Ino , Qty ; char

Item [ 2 0 ] ;

public :
void Enter() cin>>Ino; gets(Item); cin>>Qty;}
void Issue(int Q) {Qty -= Q;}
void Purchase(int Q) {Qty + = Q; }
int Getino() { return Ino;}

};
Void PurchaeItem(int Pino, int PQty)
{
f stream Fi le ;
File .open ( " STOCK .DAT " , ios : :binary l ios : :in l ios : :'ou t ) ; Stock

S;

Int Su ccess= O ;
}
while

( Success== O & & Fi le . read ( ( cha.r *) & S, sizeof ( S) ) )

if ( Pino==S .Get Ino () )


S .Purchase ( PQty) ;
_________________ // Statement 1
_________________ / / Statement 2
Success++;

if( Su ccess== l }
cout<< "Pu rcha se Updated " < <endl ; else
cout<< "W rong
(i)

Item No" <<endl ; Fi le . close ( ) ;

Write statement 1 to position the file pointer to the appropriate


place, so that the data updation is done for the required item.

(ii)Write statement 2 to perform the write operation so that the updation is


done in the binary file.
Write a function in C++ to read the content of a text file
"DELHI.TXT" and display all those lines on screen, which are either
starting with 'D' or starting with 'M'.
Write a function in C++ to search for the details (Phoneno and calls) of those Phones,
which have more than 800 calls from a binary file Phones.dat. Assuming that this
binary file contains records objects of class Phone, which is defined below .

Class Phone
{
char Phoneno[lO];int Calls;
public :
void Get (){gets(Phoneno); cin>>Calls;}
void Billing(){cout<<Phoneno<<"#"<<Cal ls<<endl;}
int GetCalls (){return Calls;}
201
4

};
Fill in the blanks marked as Statement 1 and Statement 2, in the program segment given
below with appropriate functions for the required task.

class Customer

{
long int CNo; // Customer Number
char CNmae [20]; // customer Name
char Email[30]; // Email of customer
public:
void Allocate(); // Function to allocate a member
void show(); // Function to show customer data
void ModifyEmail() // Function to modify Email
{ cout<<Enter Modified Email :;
Gets(Email);
}
long int GetCno() {return CNo; }
};
void ChangeData()
{
fstream File;
File .open (11 CUST.DAT 11 , ios: :binary I ios::in I ios::out) ; int Change=O ,Location;
long int ChangeCno;
cout<<"Cno - whose email required to be modified:"; cin>>ChangeCno;
Customer CU;
while(!Modify && File.read((char*)&CU,sizeof (CU)))
{
if (CU.GetCno()==ChangeCno)
{
CU .ModifyEmail() ; Location=File.tellg()- sizeof (CU);
//Statement l :To place file
pointer to
the required position
//Statement 2:To write
the
object CU on to
the binary file
Change++;
}
}
if (Change)
cout< <"Email Modified ... 11 <<endl; else
cout<< "Customer not found ... 11 <<endl; File .close();
}
Write a function CountHisHer ( ) in C++ which reads the contents of a text file
diary.txt and counts the words His and Her (not case sensitive). For example, if the file contains :
Pinaky has gone to his friend's house. His friend's
name is Ravya. Her house is 12 KM from here.
The function should display the output as

Count for His:2


Count for Her:l
Assuming the class VINTAGE as declared below, write a function in C++ to read the objects of
VINTAGE from binary file VINTAGE.DAT an. d display
those vintage vehicles, which are priced between 200000 and 250000.
class VINTAGE
{
int VNO;

//Vehicle Number

char VDesc[lO] ; //Vehicle Description float Price;


.public :
void GET() {c.in>>VNO;gets {VDesc) ;cin>>Price;} void VIEW {)
{
cout<<VNO<<endl ; cout<<VDesc<<endl; cout<<Price<<endl ;
}
float ReturnPrice {) {returnPrice;}
};

You might also like