Class XII C++ Holiday Homework
Class XII C++ Holiday Homework
560093
HOLIDAY HOMEWORK IN COMPUTER SCIENCE FOR CLASS XII
Q 1.
i. What is the difference between Run Type error and Syntax error? Give one example of each.
ii. When will you make function inline and why?
iii. What are actual arguments and formal arguments in C++? Explain with example.
iv. Differentiate between call by reference and call by value, with example?
v. Explain the concept of inheritance in OOPs with example?
vi. Illustrate the use of local and global variables in C++, with an example.
vii. What is this pointer? Give an example to illustrate the use of it in C++.
viii. What is the significance of private, protected and public specifiers in a class? With an example explain in detail.
ix. Differentiate between a default and a parameterized constructor in context of class and object. Give suitable
example in C++.
x. What is the similarity and difference between break and continue statements?
xi. What is the difference between abstract class and concrete class?
xii. What do you mean by static variable? Explain with help of example.
xiii. How member functions and nonmember functions differ? Explain with example.
xiv. What is the difference between Actual Parameter and Formal Parameters? Also, give a suitable C++ code to
illustrate both.
xv. What do you understand by Polymorphism? Give a suitable example of the same.
xvi. Give the difference between the type casting and automatic type conversion .Also, give a suitable C++ code to
illustrate both.
Q 2.
Public members :
Functions : Enterdetails ( ) – to input data
Display ( ) – to display the details of the candidates
Winner ( ) – To display the details of the winner through the object after comparing the votes received
by three candidates.
iii. Define a class Stock in C+ + with the following description:
Private Members
ICode 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 item)
A member function FindDisc( ) to calculate discount as per the following rule:
If Qty<=50 Discount is 0
If Qty (51 and100) Discount is 5
If Qty>100 Discount is 10
Public Members:
A constructor to assign all values with 0 and null respectively
A function Buy( ) to allow user to enter values for ICode, Item, Price, Qty and call function FindDisc( ) to
calculate the discount.
A Function ShowAll( ) to allow userto view the content of all the data members.
iv. Define a class BATSMAN with the following specifications:
Private members
bcode integer type
bname (20 characters)
innings,notout, runs integer type
batavg it is calculated according to the formula : batavg= runs/(innings-notout)
calcavg function to calculate batavg.
Public members
readdata( ) function to accept values for bcode, name, innings, notout and invokem the function
calcavg( )
displaydata( ) function to display data members on the screen.
v. Define a class BALANCED_MEAL in C++ with following description:
Private Members:
Access_no Integer
Food_Name String of 25 characters
Calories Integer
Food_type String
Cost Float
AssignAccess( ) Generates random numbers between 10 and 99 and return it.
Public Members
A function INTAKE( ) to allow the user to enter the values of Food_Name,
Calories, Food_type, cost and call function AssignAccess( ) to assign Access_no.
A function OUTPUT( ) to allow user to view the content of all the data members, if the Food_type is Fruit.
Otherwise display message, “Display only for fruit”.
vi. Define a class TEST in C+ + with following description:
Private Members
TestCode of type integer
Description of type string
NoCandidate of type integer
CenterReqd (number of centers required) of type integer 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.
vii. Define a class REPORT with the following specification.
Private:
Adno 4 digit admission number
Name 20 characters
Marks an array of floating point values
Averge average marks obtained
Getavg() to compute the average obtained in five subjects
Public:
Readinfo() funtion to accept values for adno,name,marks and Invoke the function getavg()
Displayinfo() function to display all data members on the screen.
viii. Define a class named DRAMA in C++ with the following description
Private members
SHOW_NO integer
NAME_OF_THE_DRAMA Array of characters (String)
DAY integer (Total number of days the same drama is shown)
DAILY_COLLECTION float
TOTAL_COLLECTION float
Public Members
input_data : A user defined function to read an object of ENTRY type
print_data : A function to display the details of an object
update_data: A function to update the total collection and daily collection once the day changes. Total
collection will be incremented by daily collection and daily collection is made Zero
ix. Define a class DEPARTMENTAL with the following specification :
private data members
Prod_name string (45 charactes) [ Product name]
Listprice long
Dis_Price long [ Discount Price]
Net long [Net Price ]
Dis_type char(F or N) [ Discount type]
Cal_price() – The store gives a 10% discount on every product it sells. However at the time of festival
season the store gives 7% festival discount after 10% regular discount. The discount type can be checked by tracking
the discount type. Where ‘F’ means festival and ‘N’ means Non- festival .The Cal_price() will calculate the Discount
Price and Net Price on the basis of the following table.
Public members
Constructor to initialize the string elements with “NULL”, numeric elements with 0 and character elements with
‘N’
Accept() - Ask the store manager to enter Product name, list Price and discount type . The function will invoke
Cal_price() to calculate Discount Price and Net Price .
ShowBill() - To generate the bill to the customer with all the details of his/her purchase along with the bill
amount including discount price and net price.
x. Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest
rate for each of the savers. Each member of the class contains a private data member savingsBalance
indicating the amount the saver currently has on deposit. Provide member function
calculateMonthlyInterest that calculates the monthly interest by multiplying the balance by
annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static
member function modifyInterestRate that sets the static annualInterestRate to a new value.
Write a driver program to test class SavingsAccount. Instantiate two different objects of class
SavingsAccount, saver1 and saver2, with balances of 2000.00 and 3000.00, respectively. Set the
annualInterestRate to 3 percent. Then calculate the monthly interest and print the new balances for each
of the savers. Then set the annualInterestRate to 4 percent, calculate the next month's interest and print
the new balances for each of the savers.
(d) Write a user defined function Upper_half() which takes a two dimensional array A, with size N rows and N
columns as arguments and point the upper half(diagonally) of the array. eg. (2
2 3 1 5 0 2 3 1 5 0
7 1 5 3 1 1 5 3 1
If A is 2 5 7 8 1 The Output Will be 7 8 1
0 1 5 0 1 0 1
3 4 9 1 5 5
(e) Write the function Add Array() in C++ to club the values( ascending order) of two arrays.
(f) Write the definition for a function Sumseries ( ) in c++ with two Arguments/parameters double x and int n
.The function should return a value of type double and it should perform sum of the following series.
x- x2 /3!+ x3 /5!- x4/ 7!+x5/9!-……..n.
(g) Define a function SwapArray ( int [ ], int), that would accept a one dimensional integer array Numbers and its
size N. The function should rearrange the array in such a way that the values of alternate locations of the array
are exchanged
(assume the size of the array to be even)
If the array initially contains : ( 2,5,9,14,17,8,19,16)
Then after rearranged the array should contain: (5, 2, 14, 9, 8, 17, 16, 19)
(h)Write a function in C++ which accepts an integer array and its size as arguments and assigns the elements
into a two dimensional array of integers in the following format.
If the array is1,2,3 The resultant 2D array should be
1 2 3
1 2 0
1 0 0
(i) Write a function Sortscore in c ++ to sort an array of structure “Examinee” in descending order of Score
using Bubble Sort. Assume the following definition of structure.
struct Examinee
{ long rollno;
char name[20];
float score;
};
(j)Write a function TRANSFER(int A[], int B[], int Size) in C++ to copy the elements of array A into array B in such
a way that all the negative elements of A appear in the beginning of B, followed by all the positive elements,
followed by all the zeroes maintaining their respective orders in array A. For example:
(k) Write a function SWAP2BEST(int ARR[ ],int Size) in C++ to modify the content of the array in such a way that the
elements, which are multiples of 10 swap with the value present in the very next position in the array.
The function should rearrange the array as: 90, 13, 60, 16, 5, 8, 10, 1, 3, 17.
(m) Write definition for a function SUMSEQUENCE() in C++ with two arguments/parameters – double x and int n. The
function should return a value of type double and it should perform sum of the following series :
(o)Write a C++ function SUMFUN() having parameters X (of type double) and n (of type integer) with a result type as
double to find the sum of the series given below :
(p)Assume an array E containing elements of structure Student is required to be arranged in descending order of the
marks. Write a C++ function to arrange the same with the help of bubble sort the array and its size is required to be
passed as parameters to the function. Definition of structure Student is as follows:
struct Student
{ int Rollno;
char Name[25];
float Marks;
};
(q) Write a user defined function in C++ to display the multiplication of column elements of a 2 dimensional array
MATRIX [6][6] containing integers.
(r) Write a user defined function in C++ to display those elements of 2 dimensional array T[4][4] which are divisible by
100.
(s) Write a function in C+ + to find sum of rows from a two dimensional array.
(a) What is the function overloading? Explain example the term “default arguments”. ? Give an example
in C++ to illustrate both types. 2
(b) Write the names of the header files to which the following belong:
void main()
{ randomize();
int val= random(5);
char name =’F’;
for (char ch= ‘A’; ch<=name; ch=ch+2)
cout<<setw(4)<<name<<”\t”<<val;
}
(c ). Rewrite the given snippet after removing the syntactical error(s), if any. Underline each correction 2
include<iostream.h>
#define MAX 10
void main()
{ int AY[MAX]=(5,10,15,20,25);
const int loop 5;
for[int m=0; m<loop,m++]
switch(m):
{ case 0:
case 4: cout<<AY[m]*5
case 2:
case 1 cout>>AY[m]>>endl;
}
}
(d) Find the output of the following program (Assuming that all required header files are included) 2
#include <iostream.h>
struct School
{ int year;
float topper;
};
void change( School *s, int x=15)
{ s->topper=(s->topper+25)-x;
s->year++;
}
void main()
{School arr[]={{2012,170},{2012,75})
School *pointer=arr;
Change(pointer,150);
Cout<<arr[0].year<<”- ”<<arr[0].topper<<endl;
Change(++pointer);
Cout<<pointer->year<<”- ”<<pointer->topper<<endl;
}
(e) Find the output of the following program 3
#include <iostream.h>
#include <string.h>
#include <ctype.h>
void swap(char &c1,char &c2)
{char temp;
temp=c1;
c1=c2;
c2=temp;
}
void update(char *str)
{int k,j,l1,l2;
l1 = (strlen(str)+1)/2;
l2=strlen(str);
for(k=0,j=l1-1;k<j;k++,j--)
{ if(islower(str[k]))
swap(str[k],str[j]);
}
for(k=l1,j=l2-1;k<j;k++,j--)
{ if(isupper(str[k]))
swap(str[k],str[j]);
}
}
void main()
{char data[100]={"bEsTOfLUck"};
cout<<"Original Data : "<<data<<endl;
update(data);
cout<<"Updated Data "<<data;
}
f) In the following program, find the correct possible output(s) from the options and justify your answer: 3
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
struct card { char suit[10];
int digit; };
card cards[52]; // Allocate Memory
void createdeck()
{char temp[][10] = {"Clubs","Spades","Diamonds","Hearts"};
int i,m=0,cnt=1;
for(i=1;i<=52;i++)
{ strcpy(cards[i].suit,temp[m]);
cards[i].digit=cnt;
cnt++;
if(i % 13 == 0)
{ m++; cnt=1; }
}
}
card drawcard(int num)
{int rndnum;
randomize();
rndnum = random(num)+1;
return (cards[rndnum]);
}
void main()
{ createdeck();
card c;
c = drawcard(39);
if(c.digit > 10 || c.digit == 1)
{ switch(c.digit)
{ case 11: cout<<"Jack of "; break;
case 12: cout<<"Queen of "; break;
case 13: cout<<"King of "; break;
case 1: cout<<"Ace of ";
}
}
else
cout<<c.digit<<" of ";
cout<<c.suit;
}
Outputs:
i) Kind of Spades ii) Ace of Clubs
iii) Ace of Diamond iv) Queen of Hearts
AQ2 (a) What is the Data Abstraction and Data Hiding Concept ? 2
(b) Define a class Departmental with the following specification : 4
private data members
Prod_name string (45 charactes) [ Product name]
Listprice long
Dis_Price long [ Discount Price]
Net long [Net Price ]
Dis_type char(F or N) [ Discount type]
Cal_price() – The store gives a 10% discount on every product it sells. However at the time of festival
season the store gives 7% festival discount after 10% regular discount. The discount type can be
checked by tracking the discount type. Where ‘F’ means festival and ‘N’ means Non- festival .The
Cal_price() will calculate the Discount Price and Net Price on the basis of the following table.
OTG 8000
CD Player 4500
public members
Constructor to initialize the string elements with “NULL”, numeric elements with 0 and character
elements with ‘N’ Accept() - Ask the store manager to enter Product name, list Price and discount type .
The function will invoke Cal_price() to calculate Discount Price and Net Price .
ShowBill() - To generate the bill to the customer with all the details of his/her purchase along with the
bill amount including discount price and net price.
(c) Answer the questions (i) and (ii) after going through the following program: 2
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;
}
};
i) Which category of constructor - Function 4 belongs to and what is the purpose of using it?
ii) Write statements that would call the member Functions 1 and 3
BQ1.
(a) Give the difference between the type casting and automatic type conversion .Also, give a suitable C++ code
to illustrate both. 2
(b) Which C++ header file(s) will be essentially required to be included to run /execute the following C++
source code (Note : Do not include any header file, which is/are not required): 1
void main()
{char Text[ ]="SomeThing";
cout<<”Remaining SMS Chars:”<<160-strlen(Text)<<endl;
}
(c) Rewrite the following program after removing the syntactical errors (if any).
Underline each correction. 2
#include <iostream.h>
class Book
{long Bid,Qty;
public:
void Purchase(){cin>>Bid>>Qty;}
void Sale{cout<<setw(5)<<Bid<<”Old:”<<Qty<<endl; }};
void main()
{ Book B;
B.Purchase();
B.Sale()
Sale.B();
Sale();
}
(d). Find the output of the following program: 3
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
class Metro
{ int Mno,TripNo,PassengerCount;
public:
Metro(int Tmno=1)
{ Mno=Tmno;
TripNo=0;
PassengerCount=0;
}
void Trip(int PC=20)
{ TripNo++;
PassengerCount+=PC;}
void StatusShow()
{ cout<<Mno<<":"<<TripNo<<":"<<PassengerCount<<endl;}
};
void main()
{ Metro M(5),T;
M.Trip();
T.Trip(50);
M.StatusShow();
M.Trip(30);
T.StatusShow();
M.StatusShow();
}
(e). Find the output of the following program: 2
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
typedef char Str80[80];
void main()
{clrscr();
char *Notes;
Str80 Str="vR2GooD";
int L=6;
Notes=Str;
while(L>=3)
{ Str[L]=(isupper(Str[L])?tolower(Str[L]):toupper(Str[L])) ;
cout<<Notes<<endl;
L--;
cout<<L;
Notes++;
cout<<Notes;
}
}
(f). Observe the following program and find out, which output(s) out of (i) to (iv) will not be expected from the
program? What will be the minimum and the maximum value assigned to the variable Chance? 2
#include<iostream.h>
#include<stdlib.h>
void main( )
{ randomize( );
int Arr[ ]={9,6},N;
int Chance=random(2)+10;
for(int I=0; I<2;I++)
{ N=random(2);
cout<<Arr[N]+Chance<<”*”;
}
}
(i) 9*6* (iv) 20*16*
(ii) 19*17*
(iii) 19*16*
BQ2
(a) What do you understand by Data Encapsulation and Data Hiding? Also, give a suitable C++ code to illustrate both.
(b) Answer the questions (i) and (ii) after going through the following class: 2
class Travel
{ int PlaceCode ; char Place[20];float Charges;
public: Travel( ) //Function 1
{ PlaceCode=1; strcpy (Place,”DELHI”); Charges=1000;
}
void TravelPlan(float C) //Function 2
{ cout<<PlaceCode<<”:”<<Place<<”:”<<Charges<<endl;
}
~Travel( ) //Function 3
{ Cout<<”Travel Plan Cancelled”<<endl;
}
Travel (int PC,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 illustrated by Function 3? When is this function called/invoked?
2(c) Define a class SUPPLY in C++ with following description: 4
Private Members:
• Code of type integer
• FoodName of type string
• Sticker of type string
• FoodType of type string
• A member function GetType() to assign the following values for FoodType as per the given Sticker:
Sticker FoodType
GREEN Vegetarian
YELLOW Contains Egg
RED Non-Vegetarian
Public Members:
• A function FoodIn ( ) to allow user to enter values for Code,FoodName,Sticker and call function GetType ( ) to
assign respective FoodType.
• A function FoodOut ( ) to allow user to view the content of all the data members..
CQ1.
(a) Write the prototype of a function named Area, which take a float as value parameter and return a double type
value. The parameter should have a default value 5.2. (2)
(b)Write the names of the header files which are not necessary to execute the following C++ code (1)
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
void main()
{ char c, String[ ] = " System Design ";
for(int i=0; String[i]!='\0' ;i++)
if(isdigit(String[i])
cout<<endl;
else
{c=toupper(String[i]);
cout<<c;
}
}
(c) Hussain has just started working as programmer in the WORLD SOFTWARE company. In the company, he has got
his first assignment to develop a small C++ module to find the smallest number out of a given set of numbers stored
in a one dimensional array. Somehow he has committed a few logical mistakes while writing this code and so he is
not getting the desired result from the code. Find out the mistakes and correct this C++ code so that it provides the
desired result (do not add any new statement in the code). Underline each correction made: (2)
int SMALLFIND(int ARR,int Size) // Statement 1
{int SMALL=ARR[1]; //Statement 2
for (int C=2;C<Size;C++) //Statement 3
if (ARR[C]>SMALL) //Statement 4
ARR[C]=SMALL; //Statement 5
return SMALL; //Statement 6
}
(d) Find output of the following program segment: (2)
int A[][4] = {{11,21,32,43}, {20,30,40,50}};
for (int i = 1; i<2; i++)
for (int j = 0; j<4; j++)
cout<<A[i][j]<<"*\n";
(e) Find output of the following program segment: (3)
int a = 5;
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 = 3, b = 4;
demo(::a,a,b);
demo(::a,a,b);
}
(f) Write a function to accept three integers and return the smallest of three numbers(use conditional operator)
(2)
CQ2.
(a) What do you understand by Data Encapsulation and Data Hiding? Also, give a suitable C++ code to illustrate both.
(2)
(b) What is constructor overloading? Give an example to illustrate the same. (2)
(c) Define a class Train in C++ with following description: (4)
Private Members
src of type string
Tnm of type string
dest of type string
charges of float
• A member function Getdata to assign the following values for Charges
dest charages
Mumbai 1000,
Chennai 2000
Kolkatta 2500.
Public members
A parameterize constructor to initialize the data members.
A function InputData() to allow the user to enter the values
A function displaydata() to display all and call getdata function
d) Consider the following class counter: (4)
class counter
{ protected :
unsigned int count;
public :
counter()
{ count = 0; }
void inc_count()
{ count++; }
int get_count()
{ return count; }
};
Write code in C++ to publically derive another class new_counter from class counter.
Class new_counter should have the following additional function members in the public visibility mode:
(i) A parameterized constructor to initialize the value of count to the value of parameter.
(iii) Reset() to set the value of data member count to 0.
(ii) dec_count() to decrease the value of data member count by 1.
DQ1.
a. What is Inheritance? Give an example. 1
b. Write the names of the header files to which the following belong: 2
i) sqrt() ii) isalpha iii) puts() iv)strcpy()
c. Rewrite the corrected code for the following program .Underline each correction (if any) 2 #
include<iostream.h>
Structure Swimmingclub
{ int memnumber;
char memname[20];
char memtype[ ] = “LIG”;
};
void main( )
{ Swimmingclub per1,per2;
cin>>”Member number”;
cin >>memnumber.per1:”;
cout<< “ Member name:”;
cin>>per1. Member name;
per1.memtype=”HIG”;
per2=per1;
cin>> “Member number: “<<per2.memnumber:”;
cin>>”Member name:”<<”per2.memname;
cin>>” Member number :”<< per2.memtype:”;
}
d. What will be the output of the following program: 1
# include <iostream.h>
# include<ctype.h>
# include<conio.h>
# include< string.h>
void Newtext(char String[ ], int &Position)
{ char *pointer = String;
int Length =strlen(String);
for(; Position<Length-2;Position+=2;
{ *(Pointer Position)=toupper(*(Pointer Position));
}
}
e. What will be the output of the following program: 1
#include<iostream.h>
void main( )
{ clrscr( );
int v1=5, v2=10;
for(int x =1; x<=2; x++)
{ cout<<” ++ v1<<”\t”<<v2 -- <<endl;
cout<< “ - - v2<<”\t” <<v1++<<endl;
}
}
Solve all these questions in Homework note book submit on the reopening
day to get full mark.