PRACTICE SET-1 - WordPress.com

39
PRACTICE SET-1 SUB.: COMPUTER SCIENCE Q.1 Name the header file to which the following built in function belongs: (a) fabs( ) (b) setprecision( ) (c) exit( ) (d) open( ) Q.2 Write the major differences between Procedure Oriented Programming and Object Oriented Programming. Q.3 What is the difference between a Local and a Global Variable? Q.4 What is the difference between ‘a’ and “a” in C++? Q.5 How data and functions a re organized in Object Oriented Programming? Q.6 Why char is often treated as integer data type? Q.7 Define the following terms: (a) Inheritance (b) Encapsulation Q. 8 What are the different types of errors? Q.9 How a pointer variable does differ from a simple variable? Q.10 What is the difference between a call by value and call by reference in a user defined function in C++? Q.11 Reusability of code is one of the important concepts of OOP. How is it implemented in C++? Q. 12 What is Polymorphism? How is it implemented in C++? Q.13 In the following program if the value of N given by the user is 20, what maximum and minimum value the program could possibly display? #include<iostream.h> #include<stdlib.h> void main( ) { int N,Guessme; randomize( ); cin>>N; Guessme=random(N-10) + 10; cout<<Guessme<<endl; } Q.14 Will the following program execute successfully? If not state the reason. #include<stdio.h> void main( ) { int s1,s2,num; s1=s2=0; for(x=0;x<11;x++) { cin<<num; if(num>0) s1+=num; else s2=/num; } cout<<s1<<s2; } Q.15. Write the output of the following program segment: char *name=”ComPUteR”; for (int x=0;x<strlen(name);x++) if(islower(name[x])) name[x]=toupper(name[x]); else if(isupper(name[x])) if(x%2= =0) name[x]=tolower(name[x]); Else name[x]=name[x-1]; puts(name);

Transcript of PRACTICE SET-1 - WordPress.com

PRACTICE SET-1 SUB.: COMPUTER SCIENCE

Q.1 Name the header file to which the following built in function belongs:

(a) fabs( ) (b) setprecision( ) (c) exit( ) (d) open( )

Q.2 Write the major differences between Procedure Oriented Programming and Object Oriented

Programming.

Q.3 What is the difference between a Local and a Global Variable?

Q.4 What is the difference between ‘a’ and “a” in C++?

Q.5 How data and functions a re organized in Object Oriented Programming?

Q.6 Why char is often treated as integer data type?

Q.7 Define the following terms: (a) Inheritance (b) Encapsulation

Q. 8 What are the different types of errors?

Q.9 How a pointer variable does differ from a simple variable?

Q.10 What is the difference between a call by value and call by reference in a user defined function in

C++?

Q.11 Reusability of code is one of the important concepts of OOP. How is it implemented in C++?

Q. 12 What is Polymorphism? How is it implemented in C++?

Q.13 In the following program if the value of N given by the user is 20, what maximum and minimum

value the program could possibly display?

#include<iostream.h>

#include<stdlib.h>

void main( )

{ int N,Guessme;

randomize( );

cin>>N;

Guessme=random(N-10) + 10;

cout<<Guessme<<endl; }

Q.14 Will the following program execute successfully? If not state the reason.

#include<stdio.h>

void main( )

{ int s1,s2,num;

s1=s2=0;

for(x=0;x<11;x++)

{

cin<<num;

if(num>0)

s1+=num;

else

s2=/num;

}

cout<<s1<<s2;

}

Q.15. Write the output of the following program segment:

char *name=”ComPUteR”;

for (int x=0;x<strlen(name);x++)

if(islower(name[x]))

name[x]=toupper(name[x]);

else

if(isupper(name[x]))

if(x%2= =0)

name[x]=tolower(name[x]);

Else

name[x]=name[x-1];

puts(name);

ANSWERS OF PARCTICE SET-1:

Ans 1. math.h, iomanip.h, process.h, fstream.h.

Ans 2. (1) Procedure Oriented Programming emphasis on procedure (method) whereas Object Oriented

Programming emphasis on data.

(2) Procedure Oriented Programming follows top down approach whereas Object Oriented

Programming follows bottom up approach.

Ans 3. Local Variable is accessible within its block in which it is declared but a global variable is accessible

in whole program.

Ex.

#include<iostream.h>

#include<conio.h>

int a=7; // global variable

void main( )

{

int b=6; // local variable

cout<<a<<b;

}

Ans 4. (i.) ‘a’ is a character constants because it enclosed in single quotes whereas “a” is a string constants

because it enclosed in double quotes.

(ii.) The size of ‘a’ is 1 byte because it is one character whereas size of “a” is 2 bytes because in

memory “a” is represented as “a\0” where \0 (NULL) character so it have two character. (1 character

= 1 byte).

Ans 5. In Object Oriented Programming both data and functions are organized into a Single unit known as

class.

Ans 6. The memory implementation of char data type is in terms of the associated number Code i.e. ASCII

code which is integer. Therefore it is treated as int.

Ans 7. (a) Inheritance: - The capability of one class to take the property from another class is called

Inheritance.

(b) Encapsulation: - Wrapping of both data and function into single unit is known as

Encapsulation.

Ans 8. There are three different types of errors:

(a) Compile Time Error: - It occurs during compilation of a program. It can be detected by the

compiler. Two types of compiler errors - (i) Syntax Error (ii)Semantic Error

(b) Logical Error: - It occurs due to wrong use of logic. It can not be detected by the compiler.

(c) Run Time Error: - It occurs during the program run. Ex. Divide by zero.

Ans 9. A Pointer variable is used to store the address of any other variable but a simple variable is used to

store the value.

Ans 10. In call by value method, in change of dummy argument will not reflect to the actual argument but in

call by reference change in the value of dummy argument will reflect to the actual argument.

Ans 11. By the concept of Inheritance.

Ans 12. It means one name many forms, when an object has more than one behavior and it depends on its

current state it is called polymorphism. It is implemented by the concept of Overloading.

Ans 13.Maximum Value: - 19

Minimum Value: - 10

Ans 14. Error:

1. cin<<num;

2. s2=/num;

Ans 15: cOMMuTEE

PRACTICE SET-2 SUB.: COMPUTER SCIENCE

Q1.How many times is the following loop executed?

int s=0, i=0;

while(i++ < 5)

s+=i;

Q2. What will be the value of following , if j=5 initially?

(i) (5*++j)%6 (ii) (5*j++)%6

Q3. Give the output of the following program/ program segment:

(i) #include<iostream.h>

int a=3;

void demo(int x, int y, int &z)

{ a+=x+y;

z=a+y;

y+=x;

cout<<x<<y<<z<<endl;

}

void main()

{ int a=2, b=5;

demo(::a, a, b);

cout<<::a<<a<<b<<endl;

demo(::a, a, b);

cout<<::a<<a<<b<<endl;

}

(ii) #include<iostream.h>

int global=10;

void func(int &x, int y)

{ x=x-y; y=x*10;

cout<<x<<’,’<<y<<’\n’;

}

void main()

{ int global=7;

func(::global, global);

cout<<global<<’,’<<::global<<’\n’;

func(global, ::global);

cout<<global<<’,’<<::global<<’\n’;

}

(iii) char *name=”a ProFiLe”;

for(int x=0; x<strlen(name); x++)

if(islower(name[x]))

name[x]=toupper(name[x]);

else

if(isupper(name[x]))

if(x%2 !=0)

name[x]=tolower(name[x-1]);

else

name[x]--;

cout<<name<<endl;

Q4. Write the output of the following program:

#include<iostream.h>

int calc(int u)

{

if(u%2= =0)

return(u+10);

else

return(u*2);

}

void pattern(char m,int b=2)

{

for(int c=0;c<b;c++)

cout<<calc(c )<<m;

cout<<endl;

}

void main()

{

pattern(‘

*’);

pattern(‘#’,4);

pattern(‘@’,3);

}

Q5. Find the output of the following program:

#include<iostream.h>

struct PLAY

{

int Score, Bonus; };

void Calculate( PLAY &P, int N=10)

{

P.Score++;

P.Bonus+=N;

}

void main()

{

PLAY PL={ 10, 15};

Calculate(PL,5);

cout<<PL.Score<<”;”<<PL.Bonus<<endl;

Calculate(PL);

cout<<PL.Score<<”;”<<PL.Bonus<<endl;

Calculate(PL,15);

cout<<PL.Score<<”;”<<PL.Bonus<<endl;

}

Q6. Rewrite the following program after removing the syntactical errors (if any). Underline each

correction.

#include <iostream.h>

struct Pixels

{ int Color, Style; }

void ShowPoint(Pixels P)

{ cout<<P.Color,P.Style<<endl;}

void main()

{

Pixels Point1=(5,3);

ShowPoint(Point1);

Pixels Point2=Point1;

Color.Point1+=2;

ShowPoint(Point2);

}

Q7. Find the output of the following program: 2

#include <iostream.h>

struct Game

{

char Magic[20]; int Score;

};

void main()

{

Game M={“Tiger”, 500};

char *Choice;

Choice=M.Magic;

Choice[4]=’P’;

Choice[2]=’L’;

M.Score+=50;

cout<<M.Magic<<M.Score<<endl;

Game N=M;

N.Magic[0]=’A’; N.Magic[3]=’J’;

N.Score - = 120;

cout<<N.Magic<<N.Score<<endl;

}

Q8. Write a program using structure having members name and phone_no to sort the list of 10 records

alphabetically by name.

ANSWERS OF PARCTICE SET-2:

Ans 1. 5 times.

Ans. 2. (i) 0, (ii) 1.

Ans. 3. (i) 3 5 10

8 2 10

8 10 20

18 2 20

(ii) 3, 30

7, 3

4, 40

4, 3

(iii) A OROoIiE

Ans. 4. 10*2*

10#2#12#6#

10@2@12@

Ans. 5. 11:20

12:30

13:45

Ans. 6. #include <iostream.h>

struct Pixels

{ int Color,Style;};

void ShowPoint(Pixels P)

{ cout<<P.Color<<P.Style<<endl;}

void main()

{

Pixels Point1={5,3};

ShowPoint(Point1);

Pixels Point2=Point1;

Point1.Color+=2;

ShowPoint(Point2);

}

Ans. 7. TiLeP550

AiLJP430

Ans. 8 #include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

{

int i, j;

struct telephone{

char name[20];

int phone;

} p[10], t;

clrscr();

cout<<”\n Enter Name & Phone Number of 10 Persons:”;

for( i=0; i<10; i++)

{

cout<<”\n Enter Record<<i+1;

gets(p[i].name);

cin>>p[i].phone;

}

for( i=0; i<9; i++)

{

for( j=0; j<(9-i); j++)

{

if((strcmp(p[j].name, p[j+1].name)>0 )

{

t=p[j];

p[j]=p[j+1];

p[j+1]=t;

}

}

}

clrscr();

cout<<”\n Sorted Directory:”;

for( i=0; i<10; i++)

{

cout<<’\n’<<i+1<<”. Name: “<<p[i].name<<”\t Phone No. : “<<p[i].phone;

}

getch();

}

PRACTICE SET-3 SUB.: COMPUTER SCIENCE

Q1. Observe the following program SCORE.cpp carefully,if the value of num entered by the user is 5,

choose the correct possible output(s) from the following options.

#include<stdlib.h>

#include<iostream.h>

void main( )

{

randomize( );

int num,Rndnum;

cin>>num;

Rndnum=random(num) + 5;

for(int n=1;n<==Rndnum;n++)

cout<<n<<” “;

}

Output Options: i. 1 2 3 4 ii. 1 2 iii. 1 2 3 4 5 6 7 8 9 iv. 1 2 3

Q.2. Find the output of the following program:

#include<iostream.h>

struct mybox

{

int l,b,h;

};

void dimension(mybox m)

{

cout<<m.l<<” X”<<m.b<<”X”;

cout<<m.h<<endl;

}

void main( )

{

mybox b1={10,15,5},b2,b3;

++b1.h;

dimension(b1);

b3=b1;

++b3.l;

b3.b++;

dimension(b3);

b2=b3;

b2.h+=5;

b2.l--;

dimension(b2);

}

Q.3. Identify the error in the following code declaration:

struct

{

int day;

int month;

int year;

}bdate,jdate,rdate;

Q.4. Identify the error and correct the error in the following code declaration:

struct first

{

int a;

float b;

}s1;

struct second

{

int a;

float b;

}s2;

.

.

.

s1=s2;

Q.5. Give the output of the following program:

#include <iostream.h>

struct point

{

int x,y;

};

void show(point p)

{ cout<<p.x<<’:’<<p.y<<endl;

}

void main()

{

point u={20,10},v,w;

v = u;

v.x += 20;

w = v;

u.y += 10;

u.x += 5;

show(u); show(v); show(w);

}

Q. 6. What is wrong with this code?

cout<<count;

cin<<p;

if(p>0)

cout<<”That is negative. Try again.”<<endl;

cin>>p;

else

cout<<”O.K. p=”<<p<<endl;

Q. 7. Will the following code execute successfully? (Assuming that required header files have been

included).

(i) int s=10; int &t;

void main() {t=s; cout<<t<<endl;}

(ii) int x=10;

void func1(const int &a)

{ cout<<a<<endl;

a=b;

cout<<a<<endl;

}

void main()

{

int y=5;

func1(y);

}

ANSWERS OF PARCTICE SET-3:

Ans. 1. Option (iii) is correct.

Ans. 2. 10x15x6

11x16x6

10x16x11

Ans.3. We can not declare more than one variable of structure here because structure tag (name) is not used.

In such a case we can declare only a variable of any structure.

Correct code is:

struct

{

int day;

int month;

int year;

}bdate;

Ans. 4. We can not assign structure variable s2 to s1, because they are the variables of different structure.

Correct Code:

struct first

{

int a;

float b;

}s1,s2;

s1=s2;

Ans. 5. 25 : 20

40 : 10

40 : 10

Ans. 6. Since there are 2 statement between if & else, Hence these must be enclosed within { }.

Correct form is:

cout<<count;

cin<<p;

if(p>0)

{ cout<<”That is negative. Try again.”<<endl;

cin>>p;

}

else

cout<<”O.K. p=”<<p<<endl;

Ans.7 (i) Will encounter compile time error as Reference must be initialized. The corrected code will

be:

int s=10; int &t=s;

void main() {cout<<t<<endl;}

(i) Will encounter errors, because b is not defined in the given code and the code of func1() is

trying to modify the constant reference a. The corrected code will be:

int x=10;

void func1(const int &a)

{ cout<<a<<endl;

}

void main()

{

int y=5;

func1(y);

}

PRACTICE SET-4 SUB.: COMPUTER SCIENCE

Q.1. What do you understand by function overloading? Give an example illustrating its use in a C++

program?

Q.2. Define the class BOOK with the following specifications

Private members:

Bookno integer

Title 20 character

Price float

Total_cost( ) – A function to calculate total cost of N no of copies which is passed to

the function as the argument

Public Members:

input( ) – Function to read value of data member of class.

purchase( )-Function to ask the user to input the no of copies to be purchased. It

invokes total_cost() and print the total cost to be paid by the user

Q3. Declare a class to represent bank account of 10 customers with the following data members, name of

the depositor, account number, type of account (s for saving, c for current), balance amount.

The class also contains member functions to do the following: -

(i) Void getdata() – to read data members.

(ii) Void deposit(float amount) – to add amount into balance.

(iii) Void withdraw(float amount) – to reduce amount from balance.(min. balance 1000)

(iv) Void display() – to display the data members.

Q .4. Given the following incomplete code fragment: 2

int i=5; // variable1

class X { public:

static int i; // variable2

class Y { public:

int i; // variable3

void prn(int x)

{ int i; // variable4

:

:

}

};

};

int X::i=0;

Complete the function Y::prn() to assign the parameter x’s value to the four variables mentioned

in the code fragment.

Q.5. Define a class employee with the following specifications: 3

Private Members:

Empno - integer

Ename - 20 characters

Basic, da, hra - float

Netpay - float

Calc() - A function to find Basic+hra+ da with float return type.

Public Members:

Havedata() - function to accept values for Empno, Ename, Basic, hra, da and invoke

Calc() to calculate Netpay.

Displaydata() - function to display all the data members on the screen.

Q. 6. Define a class TEST in C++ with following description:

Private Members

a. TestCode of type integer

b. Description of type string

c. NoCandidate of type integer

d. CenterReqd (number of centers required) of type integer

e. A member function CALCNTR() to calculate and return the number of centers as

(NoCandidate/100+1)

Public Members

A function SCHEDULE() to allow user to enter values for TestCode, Description,

NoCandidate & call function CALCNTR() to calculate the number of Centres

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

Q. 7. Define a class in C++ with following description:

Private Members

A data member Flight number of type integer

A data member Destination of type string

A data member Distance of type float

A data member Fuel of type float

A member function CALFUEL() to calculate the value of Fuel as per the following criteria

Distance Fuel

<=1000 500

more than 1000 and <=2000 1100

more than 2000 2200

Public Members

A function FEEDINFO() to allow user to enter values for Flight Number, Destination,

Distance & call function CALFUEL() to calculate the quantity of Fuel

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

ANSWERS OF PARCTICE SET-4:

Ans 1. A function name having several definition that are differentiable by the number or types of their

arguments, is known as an overloaded function and this process is known as Function Overloading.

For Example:

float area(int a)

{

return a*a;

}

float area(float a, float b)

{

return a*b;

}

Ans. 2.

Class BOOK

{

int Bookno;

char Title[20];

float Price;

Total_cost(int N)

{

return(N*price);

}

public:

void input()

{

cout<<”Input bookno,title,price”;

cin>>Bookno>>Title>>Price;

}

void purchase()

{

int n;

float tcost;

cout<< “Input no of copies”;

cin>>n;

tcost=Total_cost(n);

cout<<tcost;

}

Ans. 3.

Class bank

{

char name[35];

Int acc_no;

Char act;

Float balance;

Public:

Void getdata()

{ cout<<”Name & A/C No.”;

Cin.getline(name,35);

Cin>>acc_no;

Cout<<”A/C Type <S of C>”;

Cin>>act;

Act=toupper(act);

}

Void deposit(float amount)

{

balance += amount; cout<<”\n Amount deposited.”;

}

Void withdraw(float amount)

{

if((balance – amount >= 1000))

{

Balance -= amount;

Cout<<”Amount Withdrawn.”;

}

else

cout<<”\nyou can withdraw only Rs.“<<balance-1000.

}

Void display()

{

cout<<”\nA/C No: “<<acc_no;

cout<<”\nA/C Holder: “<<name;

cout<<”\nA/C type: <<act;

cout<<”\nBalance(Rs.): <<balance;

}

Ans. 4. :

:

void prn(int x)

{

int i;

::i=x; //variable 1

X::i=x; //variable 2

Y::i=x; //variable 3

i=x; //variable 4

}

:

Ans. 5. < Do yourself>

Ans. 6.

class TEST

{

int TestCode;

char Description[20];

int NoCandidate,CenterReqd;

void CALCNTR();

public:

void SCHEDULE();

void DISPTEST();

};

void TEST::CALCNTR()

{

CenterReqd=NoCandidate/100 + 1;

}

void TEST::SCHEDULE()

{

cout<<”Test Code :”;cin>>TestCode;

cout<<”Description :”;gets(Description);

cout<<”Number :”;cin>>NoCandidate;

CALCNTR();

}

void TEST::DISPTEST()

{

cout<<”Test Code :”<<TestCode<<endl;

cout<<”Description :”<<Description<<endl;

cout<<”Number :”<<NoCandidate<<endl;;

cout<<”Centres :”<<CenterReqd<<endl;;

}

Ans. 7.

class FLIGHT

{

int Fno;

char Destination[20];

float Distance, Fuel;

void CALFUEL();

public:

void FEEDINFO();

void SHOWINFO();

};

void FLIGHT::CALFUEL()

{

if (Distance<1000)

Fuel=500;

else

if (Distance<2000)

Fuel=1100;

else

Fuel=2200;

}

void FLIGHT::FEEDINFO()

{

cout<<”Flight No :”;cin>>Fno;

cout<<”Destination :”;gets(Destination);

cout<<”Distance :”;cin>>Distance;

CALFUEL();

}

void FLIGHT::SHOWINFO()

{

cout<<”Flight No :”<<Fno<<endl;

cout<<”Destination :”<<Destination<<endl;

cout<<”Distance :”<<Distance<<endl;;

cout<<”Fuel :”<<Fuel<<endl;;

}

PRACTICE SET-5 SUB.: COMPUTER SCIENCE

Q. 1. What are the advantages and disadvantages of inline function?

Q. 2. What do you understand about a member function? How does a member function differ from an

ordinary function?

Q. 3. What are static class members? Explain the characteristics of a static data member.

Q. 4. Enlist the situation when inlining does not work.

Q. 5. Identify the errors in the following code fragment:

(i)

class X{ int x;

static int ctr;

public:

void init(void)

{ x=ctr=0;

}

static void prn(void)

{ cout<<ctr<<x;

}

};

:

:

(ii)

class X{

static int a;

int b;

char c;

public:

static float x;

float y;

void getval( int i, int j, char k)

{ a=i;

b=j;

c=k;

x=y=o;

}

void prn(void)

{ cout<<a<<x;

}

};

ANSWERS OF PARCTICE SET-5:

Ans. 1. See Page .No. 212 Problem 7

Ans. 2. See Page No. 212 Problem 8

Ans. 3. PageNo. 213 Problem 11

Ans. 4. See Note OR Page No. 190

Ans. 5. (i) In the given code fragment, a static member function is trying to access a non-static member &

also the definition of static members is missing. To correct it, the code should be as follows:

class X{ int x;

static int ctr;

public:

void init(void)

{ x= ctr=0;

}

static void prn(void)

{ cout<<ctr;

}

};

int X :: ctr;

(ii) In the given code fragment, the definition of static members is missing. To correct it, the code should be

as follows:

class X{

static int a;

int b;

char c;

public:

static float x;

float y;

void getval( int i, int j, char k)

{ a=i;

b=j;

c=k;

x=y=o;

}

void prn(void)

{ cout<<a<<x;

}

};

int X :: a;

float X :: x;

PRACTICE SET-6 SUB.: COMPUTER SCIENCE

Q. 1. What are the difference between a default constructor and copy constructor, giving suitable examples

of each?

Q. 2. How many times the copy constructor is called in the following code:-

sample func(sample u)

{

sample v(u);

sample w=v;

return w;

}

void main( )

{

sample x;

sample y=func(x);

sample z=func(y);

}

Q. 3. What do you mean by copy constructor? Explain with an example.

Q. 4. What will the order of the constructor invocation of the following code

class date

{

:

:

};

class time

{

:

:

};

class train

{

int rain();

date ddate;

time dtime;

};

void main()

{

date d1;

time t1;

train tr1;

}

Q. 5. Answer the questions (i) and (ii) after going through the following class:

class Seminar

{

int Time;

public:

Seminar() //Function 1

{

Time=30;cout<<”Seminar starts now”<<end1;

}

void Lecture() //Function 2

{

cout<<”Lectures in the seminar on”<<end1;

}

Seminar(int Duration) //Function 3

{

Time=Duration; cout<<”Seminar starts now”<<end1;

}

~Seminar() //Function 4

{

cout<<”Vote of thanks”<<end1;

}

};

i) In Object Oriented Programming, what is Function 4 referred as and when does it get

invoked/called?

ii) In Object Oriented Programming, which concept is illustrated by Function 1 and Function 3

together? Write an example illustrating the calls for these functions.

Q. 6. Answer the questions (i) and (ii) after going through the following program:

class Match

{

int Time;

public:

Match() //Function 1

{

Time=0;

cout<<”Match commences”<<end1;

}

void Details() //Function 2

{

cout<<”Inter Section Basketball Match”<<end1;

}

Match(int Duration) //Function 3

{

Time=Duration;

cout<<”Another Match begins now”<<end1;

}

Match(Match &M) //Function 4

{

Time=M.Duration;

cout<<”Like Previous Match ”<<end1;

}

};

iii) Which category of constructor - Function 4 belongs to and what is the purpose of using it?

iv) Write statements that would call the member Functions 1 and 3.

Q. 7. Distinguish between the following two statements:

time T1 (13, 10, 25); //statement 1

time T1=time(13, 10, 25); // statement 2.

Q. 8. What do you mean by a temporary instance of a class? What is its use? How is it created?

Q. 9. Discuss the various situations when a copy constructor is automatically invoked.

Q. 10. Why is destructor function required in classes? Illustrate with the help of an example.

Q. 11. What is a copy constructor? What do you understand by constructor overloading?

Q. 12 Write the output of the following program:

#include<iostream.h>

class A { public:

A() { cout<<”A”; }

~A() { cout<<”~A”; }

};

class B { public:

B() { cout<<”B”; }

~B() { cout<<”~B”; }

};

class C { public:

C() { cout<<”C”; }

~C() { cout<<”~C”; }

private:

B c1;

A c2;

};

class D { public:

D() { cout<<”D”; }

~D() { cout<<”~D”; }

};

class E { public:

E() { cout<<”E”; }

~E() { cout<<”~E”; }

private:

D e1;

C e2;

};

void main()

{ E e; }

Q. 13. What will be the output of following code?

#include<iostream.h>

class A { public:

A()

{ cout<<”Constructor A \t”;}

~A()

{ cout<<”Destructor A \t”;}

};

class B { public:

B()

{ cout<<”Constructor B \t”;}

~B()

{ cout<<”Destructor B \t”;}

};

class C { A ob1, ob2;

B ob3;

public:

C()

{ cout<<”Constructor C \t”;}

~C()

{ cout<<”Destructor C \t”;}

};

void main()

{

C oc;

B ob;

A oa;

}

ANSWERS OF PARCTICE SET-6:

Ans.1. Default constructor: - A constructor that accepts no parameters is called the default constructor.

Copy constructor: - A copy constructor is a constructor of the form classname(classname &). The

compiler will use the copy constructor whenever you initialize an instance using values of another

instance of same type.

e.g.: -

class x

{ int a;

Public:

x() //Default constructor

{ a=10; }

x(x &s) //Copy constructor

{ a=s.a; }

void getdata();

void putdata();

};

Ans.2. 8 times, because copy constructor is called in the following situations.

1. When the parameter is passed by value by u. 2. When v is initialized.

3. When w is initialized. 4. When w is returned.

Ans.3. A constructor that initializes an object with the data values of another object is known as copy

constructor.

Class abc

{

int i,j;

public:

abc(int a,int b)

{

i=a;

j=b;

}

abc(abc &s)

{

j=s.j;

i=s.j;

}

void print(void)

{

cout<<i<<j;

}

};

void main()

{

abc s1(2,4);

abc s2(s1);

abc s3=s1;

}

Ans. 4. The order of constructor invocation will be as follows:

date::date()

time::time()

date::date()

time::time()

train::train()

Ans. 5. (i) Destructor, it is invoked as soon as the scope of the object gets over.

(ii) Constructor Overloading (Polymorphism)

Seminar S1,S2(90);

Ans. 6. (i) Copy Constructor, it is invoked when an object is created and initialized with values of an already

existing object.

(ii) Match M1; //for Function 1

Match M2(90); //for Function 3

Ans. 7. Page. No. 255, Problem-4.

Ans. 8. Page. No. 255, Problem-5.

Ans. 9. Page. No. 255, Problem-6.

Ans. 10. Page. No. 259, Problem-12.

Ans. 11. Page. No. 259, Problem-13.

Ans. 12. Do it yourself.

Ans. 13 Do it yourself.

PRACTICE SET-7 SUB.: COMPUTER SCIENCE

Q.1. Reusability of code is one of the important concepts of OOP. How is it implemented in C++?

Q. 2. Define multilevel and multiple inheritances with an example.

Q. 3.Condider the following class definition:

class MNC

{

char cname[25];

protected:

char hoffice[25];

Public:

MNC()

{}

char country[25];

void enterdata()

{ }

void displaydata()

{}

};

class branch : public MNC

{

long noe;

char ctry[25];

protected:

void association()

{}

Public:

Branch()

{}

void add()

{}

void show()

{}

};

Class outlet : public branch

{

char state[25];

public:

outlet()

{ }

void enter()

{}

void output()

{}

};

(i) Which class constructor will be called first at the time of declaration of an object of class outlet.

(ii) How many byte does an object belonging to class outlet require?

(iii) Name the member functions which are accessed from the object of class outlet.

(iv) Name the data member which are accessed from the object of class outlet.

Q. 4. Answer the questions (i) to (iv) based on the following code:

class Trainer

{

char TNo[5], TName[20], Specialisation[10];

int Days;

protected:

float Remuneration;

void AssignRem(float);

public:

Trainer();

void TEntry();

void TDisplay();

};

class Learner

{

char Regno[10], LName[20], Program[10];

protected:

int Attendance, Grade;

public:

Learner();

void LEntry();

void LDisplay();

};

class Institute: public Learner, public Trainer

{

char ICode[10], IName[20];

public:

Institute();

void IEntry();

void IDislpay();

};

(i) Which type of Inheritance is depicted by the above example?

(ii) Identify the member function(s) that can not be called directly from the objects of the class

Institute from the following : TEntry(), LDisplay(), IEntry().

(iii) Write name of all the member(s) accessible from member functions of class Institute.

(iv) If class Institute was derived privately from class Learner and privately from class Trainer,

then, name the member function(s) that could be accessed through Objects of class Institute.

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

class PUBLISHER

{

char Pub[12];

double Turnover;

protected:

void Register();

public:

PUBLISHER();

void Enter();

void Display();

};

class BRANCH

{

char CITY[20];

protected:

float Employees;

public:

BRANCH();

void Haveit();

void Giveit();

};

class AUTHOR:private BRANCH,public PUBLISHER

{

int Acode;

char Aname[20];

float Amount;

public:

AUTHOR();

void Start();

void Show();

};

(i) Write the names of data members, which are accessible from objects belonging to class

AUTHOR.

(ii) Write the names of all the member functions which are accessible from objects belonging to class

BRANCH.

(iii)Write the names of all the members which are accessible from member functions of class

AUTHOR.

(iv) How many bytes will be required by an object belonging to class AUTHOR?

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

class CUSTOMER

{

int Cust_no;

char Cust_Name[20];

protected:

void Register();

public:

CUSTOMER();

void Status();

};

class SALESMAN

{

int Salesman_no;

char Salesman_Name[20];

protected:

float Salary;

public:

SALESMAN();

void Enter();

void Show();

};

class SHOP : private CUSTOMER , public SALESMAN

{

char Voucher_No[10];

char Sales_Date[8];

public:

SHOP();

void Sales_Entry();

void Sales_Detail();

};

(v) Write the names of data members which are accessible from objects belonging to class

CUSTOMER.

(vi) Write the names of all the member functions which are accessible from objects belonging to class

SALESMAN.

(vii) Write the names of all the members which are accessible from member functions of class

SHOP.

(viii) How many bytes will be required by an object belonging to class SHOP?

Q. 7. What is containership? How does it differ from inheritance?

Q. 8. Can a derived class get access privilege for a private member of the base class? If, yes, how?

Q. 9. Define the constructors for the following definitions;

class A { int x;

float y;

public:

A (int, float);

:

:

};

class B : public A

{

public:

:

:

};

Q. 10. Given the following set of definitions:

class X

{ :

};

class Y : public X

{ :

};

class Z : public Y

{ :

};

Z ob1;

What order will the constructors & destructors invocation be invoked in?

ANSWERS OF PARCTICE SET-7:

Ans.1. By the concept of Inheritance.

Ans. 2. Multiple Inheritances: – It is the inheritance hierarchy where in one derived class inherits from

multiple base classes for example:

Multilevel Inheritance: - It is the inheritance hierarchy where in one derived class act as a base

class fro other classes for example:

Fig.:- Multilevel Inheritance

Ans. 3. (i) MNC (ii) 129 (iv). MNC::country

(iii)MNC::enterdata(),MNC::dispalydata(),branch::add(),branch::show(),outlet::enter(), outlet::output()

Ans. 7. Page-304, Problem-7

Ans. 8. Page 307, Problem-14

A B

C

A

B

C

PRACTICE SET-8 SUB.: COMPUTER SCIENCE

Q.1. What are the types of data files? Differentiate between them?

Q.2. What is stream? Name the stream used for file I/O?

Q.3. Write functions associated with fstream class?

Q.4. What is difference between get() and getline() function?

Q.5. What happens if you open a file using stream class constructor for writing into file?

Q.6. What is the difference between the functioning of ios ::ate and ios :: app file modes?

Q.7. What is the need and usage of read() and write() functions when there are get() and put() functions for

I/O.

Q.8. Write a program to count & display the number of lines not starting with alphabet ‘A’ present in a text

file “STORY.TXT”.

Q.9. Write a program to count the number of characters in a given file ABC.TXT.

Q.10. Write a program to copy contents of one file to another.

Q.11. Write a program to invert case of each character of a file.

Q. 12 Write a user defined functions in C++ to read the content from a text file NOTES.TXT, count and

display the number of blank spaces present in it.

Q. 13. Write a C++ program which reads one line at a time from the disk file TEST.TXT, and displays it to

a monitor.

Q. 14 Write a program that displays the size of a file in bytes.

Q. 15 Write a function to count the blanks in a text file named “PARA.TXT”.

Q. 16. Assuming that a text file named TEXT1.TXT already contains some text written into it, write a

function named vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT,

which shall contain only those words from the file TEXT1.TXT which don’t start with an uppercase

vowel(i.e., with ‘A’,’E’,’I’,’O’,’U’). For example, if the file TEXT1.TXT contains:

Carry Umbrella And Overcoat When it Rains

then the file TEXT2.TXT shall contain

Carry When it Rains.

Q. 17. Write a function in C++ to search for a BookNo from a binary file “BOOK.DAT”, assuming the

binary file is containing the objects of the following class.

class BOOK

{

int Bno;

char Title[20];

public:

int RBno(){return Bno;}

void Enter(){cin>>Bno;gets(Title);}

void Display(){cout<<Bno<<Title<<endl;}

};

Q. 18. Write a function in C++ to count the number of alphabets present in a text file “NOTES.TXT”.

Q. 19 Write a function in C++ to add new objects at the bottom of a binary file “STUDENT.DAT”,

assuming the binary file is containing the objects of the following class.

class STUD

{

int Rno;

char Name[20];

public:

void Enter(){cin>>Rno;gets(Name);}

void Display(){cout<<Rno<<Name<<endl;}

};

Q.20. Write a program to append a file to another.

Q. 21. Write a program to merge the contents of two files to a third file.

ANSWERS OF PARCTICE SET-8:

Ans.1. There are two types of files in c++ namely Text file and Binary File.

In Text mode various character translations may take place such as the conversion of carriage return

and line feed sequences into new lines however, no such character translations occur in file opened

in binary mode. Any file can be opened in either text or binary mode. The only difference is the

occurrence of character translations in text mode.

Ans.2. Stream is a sequence of bytes or in other words a stream is a flow of bytes into or out of a program.

Generally three streams are used for file I/O.

ifstream: - It is derived from istream class and it is used to associate a file with an input buffer so

that the file can be read from.

ofstream: - It is derived from ostream class. It is used to associate a file with an output buffer so that

the file can be written onto.

fstream: - It is derived from iostream class and it is used to associate a file with a buffer so that the

file can be read from and written onto.

Ans.3. Get(), seekg(), seekp().

Ans.4. The difference between get() and getline() function is that getline() reads and removes the delimiter

new line(\n) from the input stream if it is encounters which is not done by the get() function.

Ans.5. If a file is opened for output using stream class constructor then new file is created if the file with the

specified name does not exist and if it exists already, the contents are lost and output starts afresh.

e.g. ofstream fout(“BILL.DAT”).

Ans. 6. Page. 367, Problem-10.

Ans. 7. Page-367, Problem-12.

Ans. 8.

# include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ifstream fin(“STORY.TXT”);

int count=0;

char line[80];

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

while(!fin.eof())

{

fin.getline(line, 80);

if(line[0]!=’A’)

count++;

}

cout<<”\n Number of lines not starting with \’A\’ are:”<<count;

fin.close();

getch();

}

Ans. 9.

# include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ifstream fin(“ABC.TXT”);

int count=0;

char ch;

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

while(!fin.eof())

{ fin.get(ch);

count++;

}

cout<<”\n Number of characters in the given file are:”<<count;

fin.close();

getch();

}

Ans. 10.

# include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ifstream fin(“FILE1”);

ofstream fout(“FILE2”);

char ch;

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

if(!fout)

{

cout<<”\n File can not be created !!”;

getch();

return;

}

while(!fin.eof())

{

fin.get(ch);

fout.put(ch);

}

fin.close();

fout.close();

cout<<”\n File Copied Successfully \n”;

getch();

}

Ans. 11.

// Program to invert case of each character of a file.

#include<fstream.h>

#include<conio.h>

#include<stdio.h>

#include<ctype.h>

void main()

{

clrscr();

char FileName[20], ch;

cout<<”\n Enter the name of the file:”;

gets(FileName);

ifstream fin(FileName);

ofstream fout(“Temp”);

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

while(!fin.eof())

{

fin.get(ch);

if(islower(ch))

fout.put(toupper(ch));

else if(isupper(ch))

fout.put(tolower(ch));

else

fout.put(ch);

}

fin.close();

fout.close();

remove(FileName); // removes the file

rename(“Temp”, FileName);

cout<<” Case Conversion Successful |n”;

getch();

}

Ans.12. Page-369, Problem-16(a).

Ans.13. Page-369, Problem-17(b).

Ans. 14

# include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ifstream fin(“ABC.TXT”);

int count=0;

char ch;

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

while(!fin.eof())

{ fin.get(ch);

count++;

}

cout<<”\n The size of the file in bytes is:”<<count;

fin.close();

getch();

}

Ans. 15. Page-370, Problem-20.

Ans. 16.

void vowelwords()

{

clrscr();

ifstream fin(“TEXT1.TXT”);

ofstream fout(“TEXT2.TXT”);

char w[20];

char ch=’ ‘;

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

if(!fout)

{

cout<<”\n File can not be created !!”;

getch();

return;

}

while(!fin.eof())

{

fin>>w;

if(w[0]==’A’||w[0]==’E’||w[0]==’I’||w[0]==’O’||w[0==’U’)

{}

else

{

fout<<w;

fout.put(ch);

}

}

fin.close();

fout.close();

cout<<”\n File Created Successfully \n”;

getch();

}

Ans. 17.

void BookSearch()

{

fstream FIL;

FIL.open(“BOOK.DAT”,ios::binary|ios::in);

BOOK B;

int bn,Found=0;

cout<<”Enter Book Num to search…”;

cin>>bn;

while (FIL.read((char*)&S,sizeof(S)))

if (B.RBno()==bn)

{

B.Display();

Found++;

}

if (Found==0) cout<<”Sorry! Book not found!!!”<<endl;

FIL.close();

}

Ans. 18.

void CountAlphabet()

{

ifstream FIL(“NOTES.TXT”);

int CALPHA=0;

char CH=FIL.get();

while (!FIL.eof())

{

if (isalpha(CH)) CALPHA++;

CH=FIL.get();

}

cout<<”No. of Alphabets:”<<CALPHA<<endl;

FIL.close();

}

Ans. 19.

void Addnew()

{

fstream FIL;

FIL.open(“STUDENT.DAT”,ios::binary|ios::app);

STUD S;

char CH;

do

{

S.Enter();

FIL.write((char*)&S,sizeof(S));

cout<<”More(Y/N)?”;cin>>CH;

}

while(CH!=’Y’);

FIL.close();

}

Ans. 20.

# include<fstream.h>

#include<conio.h>

#include<stdio.h>

void main()

{

clrscr();

char source[30], target[30];

cout<<”\n Enter the name of the file to be appended to another:”;

gets(source);

cout<<”\n Enter the name of the file to appended to:”;

gets(target);

ifstream fin(source);

ofstream fout(target, ios :: app || ios :: nocreate);

char ch;

if(!fin)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

if(!fout)

{

cout<<”\n File does not exist !!”;

getch();

return;

}

while(!fin.eof())

{

fin.get(ch);

fout.put(ch);

}

fin.close();

fout.close();

cout<<”\n File Appended Successfully \n”;

getch();

}

Ans. 21.

# include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ifstream fin1(“FILE1”);

ifstream fin2(“FILE2”);

ofstream fout(“FILE3”);

char ch;

if(!fin1)

{

cout<<”\n File 1 does not exist !!”;

getch();

return;

}

if(!fin2)

{

cout<<”\n File 2 does not exist !!”;

getch();

return;

}

if(!fout)

{

cout<<”\n File 3 can not be created !!”;

getch();

return;

}

while(!fin1.eof())

{

fin1.get(ch);

fout.put(ch);

}

fin1.close();

while(!fin2.eof())

{

fin2.get(ch);

fout.put(ch);

}

fin2.close();

fout.close();

cout<<”\n File Created Successfully \n”;

getch();

}