Documente Academic
Documente Profesional
Documente Cultură
2.
67/1
68/2
#include <iosteam.h>
Void main( )
{ First = 10, second = 20 ;
Jumpto (First ; second) ;
Jumpto (second) ;
}
Void jumpto (int N1, int n2 = 20)
{
N1 = N1 + N2
cout << N1 >> N2 ;
3.
(i)
#include <ioseam.h>
Void main( )
{
int first = 10, second = 20 ;
Jumpto(first, sfcond) ;
Jumpto(second) ;
}
Void jumpto(int N1 int N2 = 20)
{
N1=N1 + N2 ;
Cout << N1 << N2 ;
}
What is wrong with the following while loops (and how does the correct
ones look like) :
(i) int counter = 1 ;
(ii) int counter = 1 ;
While (counter < 100)
while (counter < 100)
{
cout << counter << \n ;
counter++ ;
Counter - - ;
}
In this loop the variable counter initialized to 1 . Inside the loop, counter
thedecremented so it will have value 1, 0, 1, 2, 3,.In particular, It will
69/3
(ii)
4.
always be less than 100, so this is infinite loop.To fix, we need to use
counter ++instead of counter - - to fix, or change test from (counter <
100) to (counter >= 0 i.s., as :
.
:
While ( counter < 100 )
{ :
Counter ++ ;
}
In this loop there are not brackets surrounding the code block of the
while loop. Therefore, onlythe line immdiately the while statement
repeats. Since that line does not modify counter, Its vaiues will always be
1. Hence, its another infinite loop. The fact that the last line is indented
and seems to be part of the loop code block is purely cosmetic and of no
consequence to the actual loop. To fix, we need to add grouping bracket
around the indented lines after the while statement i.e., as :
.
:
While (counter < 100 )
{
:
Counter ++ ;
}
In the following program, find the correct possible output(s) from the
options :
70/4
#include <stdlib.h>
#include <iosteam.h>
Void main( )
{
Randomize ( ) ;
Char area [ ] [10] = {NORTH , SOUTH , EAST ,
WEST } ;
{
5.
Outputs :
(i) SOUTH :EAST :SOUTH :
(ii) NOUTH : SOUTH : EAST :
(iii) SOUTH : EAST : WEST :
(iv) SOUTH : EAST : EAST :
(i), (ii), (iv).
Find the output of the following program.
#include<iosteam.h>
Void withdef (int HisNum = 30)
{
for (int I = 20 ; I <= HisNum ; I += 5)
cont << I<< , ;
cout << endl ;
}
void control (int &myNum)
{
MyNum += 10 ;
Withdef (MyNum) ;
71/5
}
Void main( )
{
Int YourNum = 20 ;
Control (YourNum) ;
Withdef( )
Cout << Number = << YourNum << endl ;
}
20, 25, 30
6.
20, 25, 30
Number = 30
What is the defference between call by value and call by reference in a
user defind funtion in C++ ? give an example to illustarate the same.
In call by value mathod, the call function creates its own copies of the
originalvalue sent to it. Any changes, that are made, occur on the called
Functions copy of values and are not reflected back to the calling
function.
In call by references mathod, the called function accesses and works
with the original values using their references. Any changes, that occur,
take placeon the original values and are reflected back to the calling
code.
Example of call by value
Void main( )
{
int a = 5 ;
Cout << a = <<a ;
Change (a) ;
Cout << \n a = << a ;
}
void change (int b)
{
b = 10 ;
}
7.
73/6
Output will be :
output will be :
a=5
a=5
a=5
a = 10
Find the output of the following program :
#include <iosteam.h>
#include <ctype.h>
Void main( )
{
char Mysting[ ] =What@OUTPUT! ;
for (int I = 0 ; Mystring[I]! =\0 ;I++)
{
if (!isalpha(Mystring[I]))
Mystring[I] = *
else if (isupper(Mystring[I]))
Mystring[I] = Mystring[I] + 1 ;
Else
Mystring (I) = Mystring[I + 1] ;
}
75/7
8.
77/8
};
Void Display (pixel P)
{ cout << Col << Row << p . R << endl ;
}
Void main( )
{
pixel X = {40, 50}, Y, Z ;
9.
X=Z;
X . C += 10 ;
Y=Z;
Y . R += 20 ;
Z . C -= 15 ;
Display ( X ) ;
Display ( Y ) ;
Display ( Z ) ;
Col 50 Row 50
Col 50 Row 70
Col 25 Row 50
Rewrite the following after removing the syntax error(s) if any . underline
each correction.
88/9
#include <iosteam.h>
Void main( )
{ one = 10, two = 20 ;
Callme (one ; two) ;
Callme (two) ;
}
Void callme (int Arg1, int Arg2 = 20)
{ Arg1 = Arg1 + Arg2
Cout << Arg1 >> Arg2 ;
}
10.
89/10
(i)
(iii)
11.
}
}
Output :
DEL :CHN : KOL :
(ii) CHN : KOL : CHN :
KOM : BOM : BNG :
(iv) KON : CHN : KOL :
Find the output of the following program :
#include <iosream.h>
#include <ctype.h>
Void main( )
{
Char line[ ] = Good@LOGIC! ;
For (int I = 0 ; Line (I) != \0 ; I++)
{
If (!isalpha (Line[I]))
Line[I] = $
else if (islower (Line[I]))
Line[I] = Line[I] + 1 ;
else
Line (I) = Line [I + 1] ;
}
Cout << Line ;
}
(b)
(c)
89/11
(iii)
12.
(ii)
(iii)
(iv)
#include <iosrteam.h>
Void Execute (int & B, int C = 100)
{
int TEPM = B + C ;
B + = TEPM ;
If (C = = 100)
Cout << TEPM << B << C << endl ;
}
void main( )
{ int M = 90, N = n 10 ;
Execute (M) ;
cout << M << N << endl ;
Execute (M, N) ;
Cout << M << N <, endl ;
}
91/12
13.
92/13
};
Void occupies)package M)
{
cout << M.Length << << M. Breadth <, ;
Cout << M. Heigth << endl ;
14.
}
Void main( )
{
package P1 = {100, 150, 50}, P2, P3 ;
++P1.Length ;
Occupies(P1) ;
P3 = P1 ;
++P3.Breadth++ ;
P3.Breadth++ ;
Occupies(P3) ;
P2 = P3 ;
P2. Breadth += 50 ;
P2.Heigth -- ;
Occupies(P2) ;
}
Find the output of the following program :
#include <iosrteam.h>
#include <string.h>
#include <ctype.h>
Void Convert(char Str[ ], int Len)
{
for (int Cout = 0 ; Cout < Len ; Count ++)
{
If (isupper(Str[Cout] = tolower(Str[cout]) ;
else if (islower(sStr[cout]))
Str[Cout] = toupper(Str[Cout]) ;
else if (isdigit(Str[Cout]))
Str[Cout] = Str[Cout] + 1 ;
else Str[Cout] =* ;
}
}
Void main( )
{ char Text[ ] = ``CBSE Exam 2005 ;
int Size = strlen(Text) ;
Convert (Text, Size) ;
for (int C = 0, R = Size 1 ; C <= Size/2 ; C++, R --)
{
Char Tepm = Text[c] ;
Text[c] = Text[R] ;
Text[R] = Tepm ;
92/14
}
15.
16
Fill in the blank spaces with code to provide required functionality for the
following incomplete program code that reads the name of the person in
the following format :
Lastname
/ Firstname
#include
#include
void Displayname(-----name1, ------name2);
int Find( char ch, name) ;
char * substr(char * name, int spos, int epos);
int main( )
{
------- name [---];
------- firstname[--];
------- lastname[--];
int slashposn;
int lastnameLength;
cout << Enter name (first and last separatted by a /): ;
-------------------------------- ;
slashposn = Find(/, name);
-------------- = substr(name, 0, slashposn);
lastNameLength =____________- slashposn 1;
lastName = substr(name,slashposn+1, lastNameLength);
cout << Reversed, the name is ;
DisplayName(first, lastName);
Return 0;
}
Void DisplayName(-----name1, -----name2) // display name using
format name2,name1,
{
// write code for this function.
}
int Find(char ch, name) ;
{
// write code for this function.
}
Char * substr(char * name, int spos, int epos);
{
// write code for this function.
}
A function pritchar is defined as
void printchar(char ch = *, int len = 40)
{ for(int = 0 ; < len ; + +) cout << ch ;
cout << endl ;
}
How will you invoke the function princhar for following output :
(i) to print* 40 times
(ii) to print* 20 times
(iii) to print=40 times
(iv) to print =20 times
Printchar( ) ;
Printchar(*, 20) ;
94/15
131
printchar(=) ;
printchar(=, 30) ;
17.
Which of the following overloaded functions are called in each of the calls
in main() ? If any of the calls are the ambiguous (that is, they cant be
resolved), list call the candidate functions
void func (int a) { }
void func (int a, float b) { }
void func (int a, double y) { }
int main( )
{
func (1) ;
func (1) ;
func (2, 3) ;
return 0 ;
// # 1
// # 2
// # 3
// a
// b
// c
}
Call a is exact match for func (int), # 1
Call a match though promotion for func (int)
(char can be promoted to int)
Call c is ambiguous between # 2 and # 3
An int can be converted to either of the flot and double
18.
Write a C++ program that use an area( ) function for the caculation of
area of a triangle or a rectangle or a requar. Number of sides (3 for
triangle,2 for retengle and 1 for square) suggest about the shape for
which area is to be calculated.
#include.iostream.h>
#include<conio.h>
#include<math.h>
float area(float a, float b, float c)
{
float s, ar;
s = (a + b + c) / 2;
ar = sqrt(s * (s a) * (s b) * (s c));
return ar;
}
float area(float a, float b)
{
return a*b;
}
float area(float a)
{
return a*a;
}
int main ( )
{
Clrscr ( );
int choice, s1, s2, s3, ar;
do
132
19
(i)
(ii)
(iii)
(iv)
Average (4,7) return an int aberage of the two int arguments when
called is one fine, and it returns a double average of the two int
arguments when called in a second file in the same program.
(v)
20.
21.
1.
2.
not.
Write difinitions for two version of an overloded function. This funsions
1st version sum( ) takes an arguments,int array, and return the sum of all
the element of the assed array. The 2 nd version of sum( )take to
arguments, an int array and a character
(E or O). if the
passedcharacter is E, it return the sum of even elements of the passed
array and if the passed charater is O, it return the sum of odd elements.
In case of any other character, it retutn 0 (zero).
What do you understand about a member function. ? how does a
member function differ from an ordinary function ?
Member functions have full access privilege two woth the publicand
private member of the class while, in genral, nonmember functions have
access only to the publik member of the class.
Member functions are defined within the class scope that is they are not
visible outside the scope of class. Nonmember function are also visible
outside the scope of class.
137
22.
191
23.
193
Class X
24.
:
:
//same as above
};
int x :: a;
float x :: x;
Write the output of the following program :
#include <iostream.h>
class Counter
{
private :
unsigned int count ;
public :
counter( ) {count = 0 ; }
void inc_count( ) {count ++ ;}
int get_count( ) {return count ;}
};
Void main( )
{
198
Counter C1, C2 ;
Cout << \t C1 = << C1.get_count( ) ;
Cout << \t C2 = << C2.get_Count( ) ;
C1.inc_count( ) ;
C2.inc_Count( ) ;
C2.inc_count( ) ;
Cout << \tC1= << C1.get_Count( ) ;
Cout << \tC2= << C2.get_Count( ) ;
C1 = 0
25.
C2 = 0
C1 = 1
C2 = 2
PURCHASE( )
193
float TOTAL_COST(int N)
float TOTAL ;
TOTAL = N * PRICE ;
return TOTAL ;
}
public :
void INPUT( )
{
cout << \nEnter BOOK NO ;
Cin >> BOOK_NO ;
cout << \nEnter BOOK Titel : ;
gets(BOOK_TITLE) ;
cout << \nEnter price : ;
cin >> PRICE ;
}
Void PURCHAS( )
{ int n ;
float TOT ;
cout << \nEnter the number of copets to be
purchased : ;
cin >> n ;
TOT = TOTAL_COST(n) ;
Cout<< \nTOtal Amount is : << TOT ;
}
26
(i)
};
Difine a class to represent bowlers in a cricket team> Incude the
following member :
Data Members
First name, Last name,overs bowled, Number of Maiden overs, Runs
given, Wickets taken.
Member Functions
To assign the initial values
(ii) To update the information.
(iii)
27.
203
206
28.
201
int I, j ;
void abc (void) ;
static int z ;
public :
float k ;
void get(void) ;
void prin(void) ;
};
Y 0b2 ;
X 0b3 ;
:
}
//end fun1( )
void func1( ) ;
{
X 0b4;
:
}
//endfun1( )
Void fun2( )
{
}:
//end func2( )
29.
Is there any defference between List x; and List x().,, supposing that
there exists a class namely List ?
There is a big defference !
List x ; statement declares an object of class List type whereas the
statement List x ( ) ;declares a function by the name x and having ruturn
233
type as List.
For excample, following function f() declareas a local List object called x :
void f( )
{ List x ;
// Local object named x (of class List)
30.
31.
1.
2.
3.
4.
5.
32.
}
But following function g() declaers a function called x() that return an
object of List type:
void g( )
{
List x( ) ;
// function named x (that return a List)
}
Which constructor gets call for the statements #1 and #2 in the code
given below ?
class Test { int a, b ;
public :
Test( ) ;
Test(int i, int j = 5) ;
};
int main( )
//statement #1
{ Test a[10] ;
// statement #2
Test p (10, 15) ;
}
Default constructor Test::Test() for statement #1
And parameterised constructor Test::Test(int i, int j) for statement #2.
What will be the order of constructor invocation for the following code
snippet ?
class Date { :
};
Class Time { :
};
Class Train {
int train( );
Date dep_date;
Time dep_time;
:
};
int main( )
{ Date D1;
Time T1;
Train TR1;
:
}
The order of constructor invocation will be as following :
Date :: Date( ) contructor for D1 object.
Time :: Time( ) contructor for T1 object.
Date :: Date ( ) for dep_date (of TR1) object.
Time :: Time( ) for dep_time (of TR1) object.
Train :: Train( ) for TR1 object.
What will be the output of following code
#include <iosream.h>
234
235
238.
class A
Class B
{ public :
A( )
{cout << constructor A \n ; }
~A( )
{cout << Destrutor A \n ; }
};
{ public :
B( )
{ cout << constructor B \t ; }
~B( )
A ob1, ob2 ;
B ob3 ;
Public :
C( )
{ cout <<\n Constructor C\t ; }
~C( )
{ cout << Destructor C\n ; }
};
Void main( )
{ C oc ;
B ob ;
A ob ;
}
33.
Constructor A
Constructor A
Constructor B
Constructor C
Constructor B
Constructor A
Destructor A
Destructor B
Destructor C
Destructor B
Destructor A
Destructor A
Fine the syntax error(s), if any (given reason for error) :
class ABC
{ int x = 10 ;
float y ;
ABC( ) { y = 5 ; }
~( ) { }
}
Void main( )
{
ABC a1, a2 ;
}
240.
1.
34.
35.
36.
Erroneous statement
int x = 10 ;
2.
~( ) { }
3.
245
Identify the error(s) in the following and correct it, explaining every
chang being introduced in the program :
#include <iostream.h>
Class sample { int i;
float j;
void sample (void)
{
i = 0;
j = 0.0;
}
init( )
{
cin >> i >> j ;
247
245
}
Display( )
{
cout << i = << i << \n ;
Cout << j = << j << \ n ;
}
Void sample (void) { }
37.
};
Sample s1, s2;
What will be the output of the following ?
#include <iostream.h>
class MAIN {
public :
249
MAIN( )
{
calculate( ) ;
Cout << \t << constructor. \n ; }
void calculate( )
{
show( ) ;
cout << \t << calculating\n ;
}
Void show( )
{
cout << I am displaying
}
};
38.
void main( )
{
MAIN one ;
}
Consider the following and answer the question given below :
class MNC
{
char cname[25] ;
protected :
char Hoffice[25] ;
public :
MNC( ) ;
char Country[25] ;
void EnterDate( ) ;
voidDisplayDate( ) ;
};
class Branch : public MNC
{
long NOE ;
char Ctry[25] ;
protected :
void Assoiciation( ) ;
public :
Branch( ) ;
void Add( ) ;
void show( ) ;
};
Class Outlet : public Branch
Char state[25] ;
Public :
Outlet( ) ;
void Enter( ) ;
void Outlet( ) ;
};
(i) which class constructor will be called first at the time of declaration of
an object of class Outlet ?
(ii) how many bytes does an object belonging to class Outlet require ?
(iii) name the member function(s), which are accessed from the
object(s) of class Outlet.
(iv) name the data member(s), which are accessible from the object(s)
of class Branch.
(i) first of all constructor of class MNC will be called, then of Branch and
then at last of Outlet.
(ii) 129
(iii) MNC :: EnterData( ), MNC:: DisplayData( ), Branch :: Add( ), Branch
:: show( ), Outlet :: Enter( ), Outlet ::Output( ).
(iv) MNC :: Country.
39.
Given the following set of definition. What data member and function are
directly accessible by the function mentioned here ?
void inform (void) ;
class x
{
int a ;
float b ;
void inti (void) ;
public :
char ch; void gett(void) ;
protected :
double amt ;
void getamt (void) ;
friend void A (void) ;
};
class Y : public X
{
int X ;
public :
int j ;
void readit (void) ;
protected :
void inform (void) ;
friend void B (void) ;
};
Void display (void) ;
The function :: inform() can access public data member of class X and Y
and the function display() provided, the defintion of :: inform() ocurs
after the declaration of X, Y and display().
X :: init(), X :: gett(),X :: getamt() and A() can access all the member of
X and :: inform().
Y :: redit(), Y :: inform () and B() can access all the member of Y, public
and protected member of X (the inherted member) and :: inform () (only
with the scope resolution operator :.
:: display() can access public member of X and Y, :: inform(); and A()
and () if only their declartions (A()s and B()s occur before display().
40.
41.
public:
B( ) { cout << B; }
};
Class C
{
public:
C( ) { cout << C; }
~C( ) { cout << ~C; }
private:
Ba;
A b;
};
class D
{
public:
D( ) { cout << D ;
~D( ) { cout << ~D; }
};
class E : public C
{
public:
E( ) { cout << E; }
~E( ) { cout << ~E; }
Private:
D d; B b;
};
void main( )
{
Ee;
Cout << endl ;
}
Assuming that the program compiles and runs correctly, what does it
print out ?
BACDBE
~E~B~D~C~A~B
Answer the question (i) to (iv) based on the following code :
class Teacher
{
char Tno[5], Tname[20], Dept[10] ;
int Workload ;
protected :
float Salary ;
void Assignsal (float) ;
public :
Teacher( ) ;
void Tentry( ) ;
void Tdisplay( ) ;
};
class Student
{
char Admno[20], Steam[10] ;
protected :
Student( ) ;
void Sentry( ) ;
void Sdisplay( ) ;
};
class School : public Student, public Teacher
{
Char Scode [10], SchName[20] ;
(i)
42.
Public :
School( ) ;
void SchEntry( ) ;
void SchDisplay( ) ; } ;
which type of Inheritance is depicted by the above example ?
(ii) Identify the member function(s) that cannot be called directly from
the object of class School from the following :
TEntry( )
Sdisplay( )
SchDisplay( )
(iii) write name of all the member(s) accessible from member function of
class School.
(iv) if class was derived from privately from class Teacher and privately
from class Student,then, name the member function(s) that could be
accessed though object of class school.
(i) Multiple Inheritance
(ii) AssignSal( )
(iii) Data ?Members :
Salary, Attendance, TotMarks, Scode, Schname.
Member functions: AssignSal( ), Tentry( ), Tdisplay( ),Sentry( ),
Sdisplay(), SchEntry( ), SchDisplay( )
Define multilevel and Multiple inheritance in context of object orianted
programming. Give suitable example to illustrate the same.
(b) Answer the question (i) to (iii) based on the following code :
class stationery
{
char Type ;
char Manufacturer[10] ;
public :
stationery( ) ;
void Read_sta_details( ) ;
void Disp_Sta_details( ) ;
};
class office : public statinery
{
int no_of_types ;
float cost_of_sta ;
public :
void Read_off_details( ) ;
void Disp_off_details( ) ;
};
class printer : private office
{
int no_of_user ;
char delivery_date[10] ;
public :
void Read_pri_details( ) :
void Disp_pri_details( ) ;
};
void main( )
{ printer Myprinter ;
}
(i)
(ii)
(iii)
(a)
(b)
43.
44.
None
Read_pri_details ( ) , Disp_pri_detais ( )
Read_sta_details ( ), Disp_pri_detais ( )
Read_off_details ( ) , Disp_off_details ( ),
Read_pri_details ( ), Disp_pri_details ( )
How does the public derivation of a class differ from private and
protected derivation ?
In what order are class constructors and class destructors called when a
derived class object is created and destroyed ?