Study Material Class-Xii Subject: Computer Science (083) 2017-2018 Basic Concepts of C++ (Review of Class Xi) Key Points: Introduction To C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

STUDY MATERIAL

CLASS-XII
SUBJECT : COMPUTER SCIENCE (083)

2017-2018

CHAPTER:01

BASIC CONCEPTS OF C++(REVIEW OF CLASS XI)

KEY POINTS:
Introduction to C++
 C++ is the successor of C language & developed by Bjarne Stroustrup at Bell Laboratories, New
Jersey in 1979.
Tokens- smallest individual unit. Following are the tokens
 Keyword-Reserve word that can‘t be used as identifier
 Identifies-Names given to any variable, function, class, union etc.
 Literals-Value of specific data type
 Variable- memory block of certain size where value can be stored and changed.
 Constant- memory block where value can be stored once but can‘t changed later on
 Operator – performs some action on data
o Arithmetic(+,-,*,/,%)
o Relational/comparison (<,>,<=,>=,==,!=).
o Logical(AND(&&),OR(||),NOT(!).
o Conditional (? :)
 Precedence of operators:
++(post increment),--(post decrement) Highest
++(pre increment),--(pre decrement),sizeof !(not),-(unary),+unary plus)
*(multiply), / (divide), %(modulus)
+(add),-(subtract)
<(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)
==(equal),!=(not equal)
&& (logical AND)
||(logical OR)
?:(conditional expression)
=(simple assignment) and other assignment operators(arithmetic assignment
operator)

, Comma operator
Low
Data type- A specifier to create memory block of some specific size and type
cout - Used to display value on monitor.
cin - To prompt value for specific variable.

6
comment-Used for better understanding of program statements and escaped by the compiler to compile .
e.g. – single line (//) and multi line(/*….*/)
Control structure :
Sequence conditional case control loop control statement
control statement statement (switch (while ,do… while, for)
statement(if) (if else) case)
Syntax Syntax Syntax Syntax
if(condition) If(condition) switch(expression) while(condition)
{ { { {
statements; statements; case (expression1): statements;
} } [statements }
else break;] do ….while loop
{ case (expression2): do
statements; [statements, {
} break;] statement;
default: } while(condition);
Statements; For loop
} for(initialization;condition;incr/decr)
{
statement;
}

Nested loop -loop within loop.


exit()-to leave from the program.
break- exit from the current loop.
continue-to transfer control to loop control statement.
goto-program to jump a different location.
Character I/O Function:
 get()-input single character from standard input device.
 put()-output single character from standard output device
 getch()-read from keyboard
 putch()-write to screen.
 getchar()-return single character from a standard input device.
 putchar()-transmit single character to standard output device.
 gets()-used to read a string of character from the standard input file(stdin).
 puts()-used to copy a null-terminated string to standard output file (stdiout).

7
Standard C++ libraries
Header Nome Purpose
stdio.h File input and output
ctype.h Character tests
string.h String operations
math.h Mathematical functions such as sin() and cos()
stdlib.h Utility functions such as malloc() and rand()
Some More Useful header file.
fstream.h graphics.h conio.h ctype.h iomanip.h iostream.h
math.h stdlib.h stdio.h stream.h string.h time.h

Header files (Some important library functions):


stdio.h : fclose fcloseall feof fflush fgetchar fgets fileno fopen fprintf fputchar fputs
fread fseek fwrite getc getchar gets

string.h : setmem stpcpy strcat strchr strcmp strcmpi strcpy


strcspn stricmp strlen strlwr strncat strncmp strncmpi strncpy
strnicmp strnset strrev strset strstr strupr

math.h : abs cos exp fabs floor fmod fmodl abs pow powl() sin sqrt tan

stdlib.h : abort abs atexit atof atoi atol calloc


div exit free rand random randomize realloc

conio.h : cgets clrscr cprintf cputs cscanf getch getche gettext putch
puttext textbackground textcolor

Some functions
 isalpha(c)-check whether the argument is alphabetic or not.
 islower(c)- check whether the argument is lowecase or not.
 isupper(c) - check whether the argument is upercase or not.
 isdigit(c)- check whether the argument is digit or not.
 isalnum(c)- check whether the argument is alphanumeric or not.
 tolower()-converts argument in lowercase if its argument is a letter.
 toupper(c)- converts argument in uppercase if its argument is a letter.
 strcat()- concatenates two string.
 strcmp-compare two string.
 pow(x,y)-return x raised to power y.
 sqrt(x)-return square root of x.
 random-return a random number between 0 and (num-1)
 randomize-intilizesthe random number generator with a random value.

8
Array- Collection of element of same type that are referred by a common name.
One Dimension array
 An array is a continuous memory location holding similar type of data in single row or single column
Two dimensional array
 A two diamensional array is a continuous memory location holding similar type of data in of both row
sand columns (like a matrix structure).
Function-Self-contained block of code that does some specific task and return a value.
Function prototypes-Function declaration that specifies the return type and data type and no of
arguments
syntax: return_type function_name(argument_type(s) argument_name(s));
Passing value to function-
 Passing by value
 Padding by address/reference
Function overloading
 Processing of using the same name for two or more function.
Function recursion
 Function that call itself either directly or indirectly.
Local variables
 Declared inside the function.
Global variables
 Declared before main function ().
Actual variables
 Variables associated with function name during function call.
Formal variables
 Variables which accept the actual variable inside the function.
Structure-Collection of logically related of different datatypes referenced under one name.
Nested structure
 A Structure definition within another
structure. typedef
 Used to define new data type name
#define Directives
 Use to define a constant number or function or to replace an instruction.

9
1 Marks questions

1) Name the
header files that
shall be needed for
the following code:
voi
d
ma
in(
)
{
c
h
a
r

S
t
r
i
n
g
[


S
t
r
i
n
g

;

c
o
u
t

<
<

s
e
t
w
(
2
)
<
<
S
t
r
i
n
g
;
}
2) Which C++
header file(s)
will be
essentially
required to be
include to
run/execute
the following
C++ code:
[CBSE-2010]
v
o
i
d
m
a
i
n
(
)
{
int
Rno=
24;
char
name[
]
=‖Alm
a
Mater
‖;
cout<
<setw
(10)<
<Rno
<<set
w(20)
<<na
me<<
endl;
}
3) Name the
header files that
shall be needed
for the following
code:
voi
d
ma
in(
)
{
c
h
a
r

w
o
r
d
[
]
=

B
o
a
r
d

E
x
a
m

;

c
o
u
t
<
<
s
e
t
w
(
2
0
)
<
<
w
o
r
d
;
}
4) Name the
header file(s)
that shall be
needed for
successful
compilation of
the following
C++ code.
voi
d
ma
in(
)
{
c
h
a
r

S
t
r
i
n
g
[
2
0
]
;

g
e
t
s
(
S
t
r
i
n
g
)
;

s
t
r
c
a
t
(
S
t
r
i
n
g
,

C
B
S
E

)
;

p
u
t
s
(
S
t
r
i
n
g
)
;
}
5) Name the
header file(s)
that shall be
needed for
successful
compilation of
the following
C++ code.
voi
d
ma
in(
)
{
c
h
a
r

T
e
x
t
[
4
0
]
;

s
t
r
c
p
y
(
T
e
x
t
,

A
I
S
S
C
E

)
;

p
u
t
s
(
T
e
x
t
)
;

}
6) Name the
header file to
which the
following
below:
(i) abs( )
(ii)
isupper(
)
7) Name the
header file to
which the
following
belong:
(i) pow ( )
(ii)rando
m( )
8) Name the
header files to
which the
following
belong:
(i) abs( )
(ii)
strcmp(
)
9) Name the
header files to
which the
following
belong: [AI
2005]
(i) puts( )
(ii)
isalnum(
)
10) Write the
names of the
header files to
which the
following
belong:
(i) gets( )
(ii)
strcmp(
) (iii)abs(
)
(iv)isaln
um( )
11) Name the
header file, to
which the
following built-
in function
belongs:
(i) strcmp(
)
(ii)getc(
)
12) Name the
header files of
C++ to which
the following
functions
belong:
(i)get( )
(ii)open( )
(iii)abs( )
(iv)strcat( )
13) Name the
header file
to be
included
for the use
of the
following
built in
functions:
(i)getc( )
(ii)strcat()
14) Name the
header file, to
which
following built
in function
belong:

10
(i) isupper( ) ( ii)setw() (iii)exp( ) (iv)strcmp( )
15) Why main( ) function is so special. Give two reasons?
16) Name the header file of C++ to which following functions belong.
(i)strcat( ) (ii) scanf( ) (iii) getchar( ) (iv)clrscr( )
17) Name the header files, to which the following built in functions belongs to:
(i)cos( )(ii)setw( )(iii)toupper( )(iv)strcpy( )
18) Name the header files, to which the following built in functions belongs to:
(i)cos( )(ii)setw( )(iii)toupper( )(iv)strcpy( )
19) Name the header file to, which following built-in functions belong:
(i) strcpy() (ii) isdigit() (iii) log() (iv) puts()
20) Name the header file to be included for the use of following built-in functions:
(i) frexp() (ii) toupper() [CBSE Sample Paper-2]
21) Name the header flies of C++ to which the following functions belong:
(i) write() (ii) arc() (iii) open() (iv) strlen()
22) Name the header files of C++ to which the following functions belong: [AI2002]
(i) get() (ii) open() (iii) abs() (iv) strcat()
23) Name the header files of C++ to which the following functions belong: [Comptt. 2002]
(i) read() (ii) open() (iii) get() (iv) strcmp()
24) Name the header fLle,to which the following built-in functions belong:
(i) strcpy 0 (ii) gets()
25) Name the header file, to which the following built-in functions belong: [AI2003]
(i) strcmp() (ii) getc()
26) Write the names ofthe header flies to which the following belong:
(i) sqrt() (ii) isalpha() (iii) puts() (iv) strcpy()
27) Write the names ofthe header flies to which the following belong: [AI 2004]
(i) gets() (ii) strcmp() (iii) abs() (iv) isalnum()
28) Write the name of header flies to which the following belong: [Comptt 2004]
(i) sqrt() (ii) strcpy() (iii) isalpha() (iv) open()

2 Marks questions:

1) Rewrite the following program after removing the syntactical error(s) if any. Underline each
correction.
#include<iostream.h>
void main( )
{ F = 10, S = 20;
test(F;S);
test(S); }
void test(int x, int y = 20)
{ x=x+y;
count<<x>>y;}
2) Find the output of the following program:
#include<iostream.h>
void main( )
{ int U=10,V=20;
for(int I=1;I<=2;I++)
{ cout<<‖[1]‖<<U++<<‖&‖<<V 5 <<endl;
cout<<‖[2]‖<<++V<<‖&‖<<U + 2 <<endl; } }
3) Rewrite the following C++ program after removing the syntax error(s) if any. Underline each
correction. [CBSE 2010]
include<iostream.h>
class FLIGHT
{
Long FlightCode;

11
Char Description[25];
public
void addInfo()
{
cin>>FlightCode; gets(Description);
}
void showInfo()
{
cout<<FlightCode<<‖:‖<<Description<<endl;
} };

void main( )
{ FLIGHT F;
addInfo.F();
showInfo.F; }
4) Rewrite the following program after removing the syntax error(s) if any. Underline each
correction.
#include<iostream.h>
void main( )
{ One=10,Two=20;
Callme(One,Two);
Callme(Two); }
void Callme(int Arg1,int Arg2)
{ Arg1=Arg1+Arg2;
Count<<Arg1>>Arg2; }
5) In the following program, find the correct possible output(s)from the options:
#include<stdlib.h>
#include<iostream.h>
void main( )
{ randomize( );
char City[ ][10]={―DEL‖, ―CHN‖, ―KOL‖, ―BOM‖, ―BNG‖};
int Fly;
for(int I=0; I<3;I++) {
Fly=random(2) + 1;
cout<<City[Fly]<< ―:‖; } }
Outputs:
(i) DEL : CHN : KOL: (ii) CHN: KOL : CHN:
(iii) KOL : BOM : BNG: (iv) KOL : CHN : KOL:
6) In the following program, find the correct possible output(s)from the options:
#include<stdlib.h>
#include<iostream.h>
void main( )
{ randomize( );
char Area[ ][10]={― NORTH‖, ―SOUTH‖, ―EAST‖, ―WEST‖};
int ToGo;
for(int I=0; I<3;I++) {
ToGo=random(2) + 1;
cout<<Area[ToGo]<<‖:‖; } }
Outputs:
(i) SOUTH : EAST : SOUTH : (ii) NORTH : SOUTH : EAST :
(iii) SOUTH : EAST : WEST : (iv) SOUTH : EAST : EAST :
7) Rewrite the following program after removing the syntactical error(s) if any. Underline each
correction.
#include<iostream.h>
const int Max 10;

12
void main()
{ int Numbers[Max]; Numbers
= {20,50,10,30,40};
for(Loc=Max-1;Loc>=10;Loc--)
cout>>Numbers[Loc]; }
8) In the following C++ program what is the expected value of Mysore from options (i) to (iv) given
below.Justify your answer.
#include<stdlib.h>
#include<iostream.h>
void main( )
{randomize( );
int Score[ ] = {25,20,34,56,72,63},Myscore;
cout<<Myscore<<endl; }
Ii) 25 (ii) 34 (iii) 20 (iv) None of the above.
9) Find the output of the following program
#include<iostream.h>
void main( )
{ long NUM=1234543; int
F=0,S=0;
do
{ int R=NUM % 10; if (R
%2 != 0)
F += R;
else
S += R;
NUM / = 10;
} while (NUM>0);
cout<<F-S; }
10) Rewrite the following program after removing the syntactical error(s), if any. Underline each
correction.
#include<iostream.h>
const int Multiple
3; void main( )
{value = 15;
for(int Counter = 1;Counter = <5;Counter ++, Value -= 2)
if(Value%Multiple = = 0)
cout<<Value * Multiple;
cout<<end1;
else
cout<<Value + Multiple <<endl; }
11) Find the output of the following program
#include<iostream.h>
#include<string.h>
#include<ctype.h>
void Convert(char Str[ ],int Len)
{ for(int Count=0;Count<Len;Count++)
{ if(isupper(Str[Count]))
Str[Count]=tolower(Str[Count]);
else if (islower(Str[Count]))
Str[Count]=toupper(Str[Count]);
else if(isdigit(Str[Count]))
Str[Count]=Str[Count]+1;
else Str[Count]=.*.;
} }
void main( )

13
{ char Text[ ]=‖CBSE Exam 2005‖; int
Size = strlen(Text);
Convert(Text,Size);
cout<<Text<<endl;
for(int C=0,R=Size . 1;C<=Size/2;C+
+,R--) { char Temp=Text[C];
Text[C]=Text[R];
Text[R]=Temp; }
cout<<Text<<endl; }
12) The following code is from a game, which generate a set of 4 random numbers. Praful is
playing this game, help him to identify the correct option(s) out of the four choices given below
as the possible set of such numbers generated from the program code so that he wins the
game. Justify your answer. [CBSE 2010]
#include<iostream.h>
#include<stdlib.h>
const int LOW=25;
void main()
{randomize();
int POINT=5, Number;
for(int I=1;I<=4;I++)
{Number=LOW+random(POINT);
cout<<Number<<":" <<endl;
POINT--; } }
(i) 29:26:25:28: (ii)24:28:25:26:
(iii) 29:26:24:28; (iv)29:26:25:26:

13) Rewrite the following program after removing the syntactical errors (if any). Underline each
correction.
#include [iostream.h]
class MEMBER
{
int Mno;float Fees;
PUBLIC:
void Register(){cin>>Mno>>Fees;}
void Display{cout<<Mno<<" :
"<<Fees<<endl;} };
void main()
{
MEMBER M;
Register();
M.Display();
}

3 Marks questions:[Hots]

1) Find the output of the following program; [Delhi 2008]


#include<iostream.h>
#include<ctype.h>
void main( )
{ char Text[ ] = ―Mind@work!‖;
for(int I=0; Text[I]!=‘\0‘;I++)
{ if(!isalpha(Text[I]))
Text[I]=‖*‖;
else if(isupper(Text[I]))
14
Text[I]=Text[I]+1;
else
Text[I] = Text[I+1]; }
cout<<Text; }
2) Find the output of the following program:
#include<iostream.h>
#include<ctype.h>
void main( )
{ char Mystring[ ] = "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];
} cout<<Mystring; }
3) Find the output of the following program.
#include<iostream.h>
void Withdef(int HisNum=30)
{for(int I=20;I<=HisNum;I+=5)
cout<<I<<‖,‖;
cout<<endl; }
void Control(int &MyNum)
{ MyNum+=10;
Withdef(MyNum); }
void main()
{ int YourNum=20;
Control(YourNum);
Withdef();
cout<<.Number=.<<YourNum<<endl; }
4) Find the output of the following program:
#include<iostream.h> void
Indirect(int Temp=20)
{ for(int I=10;I<=Temp;I+=5)
cout<<I<<‖,‖;
cout<<endl; }
void Direct(int &Num)
{Num+=10;
Indirect(Num);
} void main( )
{ int Number=20;
Direct(Number);
Indirect( );
cout<<‖Number =‖<<Number<<endl; }
5) Find the output of the following program:
#include<iostream.h>
#include<ctype.h>
void Secret(char Msg[],int
N); void main( )
{char SMS=‖ rEPorTmE‖;
Secret(SMS,2);
cout<<SMS<<endl; }
void Secret(char Msg[],int N)
{for(int c=10;Msg[]1=‘\0‘;c++)

15
if(c%2==0)
Msg[c]= Msg[c]+N;
else if (isupper(Msg[c]))
Msg[c]=tolower(Msg[c]);
else
Msg[c]= Msg[c]-N; }
6) Find the output of the following program: [CBSE 2010]
#include<iostream.h>
struct three_d
{ int x,y,z; };
void movein(three_d &t, int step=1)
{ t.x+=step;
t.y+=step;
t.z+=step; }
void moveout(three_d &t, int step=1)
{t.x-=step;
t.y+=step; t.z-
=step; }
void main()
{ three_d t1={10,20,5},t2={30,10,40};
movein(t1);
moveout(t2,5);
cout<<t1.x<<","<<t1.y<<","<<t1.z<<endl;
cout<<t2.x<<","<<t2.y<<","<<t2.z<<endl;
movein(t2,10);
cout<<t2.x<<","<<t2.y<<","<<t2.z<<endl; }
7) Write the output of the following program:
#include<iostream.h>
int func(int &x,int y=10)
{ if(x%y==0) return ++x;else return y-
-; } void main( )
{ int p=20,q=23;
q=func(p,q);
cout<<p<<q<<endl;
p=func(q);
cout<<p<<q<<endl;
q=func(p);
cout<<p<<q<<endl; }
8) Find the output of the following program. [CBSE 2010]
#include<iostream.h>
#include<ctype.h>
void Mycode(char Msg[],char ch)
{for(int cnt=0;Msg[cnt]!='\0';cnt++)
{if(Msg[cnt]>='B'&& Msg[cnt]<='G')
Msg[cnt]=tolower(Msg[cnt]);
else
if(Msg[cnt]=='A'||Msg[cnt]=='a')
Msg[cnt]=ch;
else
if(cnt%2==0)
Msg[cnt]=toupper(Msg[cnt]);
else
Msg[cnt]=Msg[cnt-1];
}}
void main()

16
{ char MyText[]="ApEACeDriVE";
Mycode(MyText,'@');
cout<<"NEW TEXT: "<<MyText<<" "<<endl; }
9) Write a function in C++ to merge the contents of two sorted arrays A & B into
third array C. Assuming array A and B are sorted in ascending order and the
resultant array C is also required to be in ascending order.

10) 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. 3 class
{
int Bno;
char Title[20];
public:
int RBno(){return Bno;}
void Enter(){cin>>Bno;gets(Title);}
void Display(){cout<<Bno<<Title<<endl;}
};

Answer to Questions

1 Marks Answer
1) Ans) iomanip.h
iostream.h 2) Ans)
iostream.h iomanip.h

3) Ans: iostream.h
iomanip.h
4) Ans) stdio.h string.h
5) Ans: string.h, stdio.h
6) Ans) (i) abs( ) - math.h, stdlib.h, complex.h
(ii)isupper( ) - ctype.h
7) Ans:
(i) abs( ) - math.h, stdlib.h, complex.h
(ii) random( ) - stdlib.h
8) Ans) (i) abs( ) - stdlib.h, math.h, complex.h
strcmp( ) - string.h
9) Ans)
(i) puts( ) - stdio.h
(ii) isalnum( ) - ctype.h
10) Ans:
(i) gets( ) - stdio.h
(ii) strcmp( ) - string.h
(iii) abs( ) - math.h, stdlib.h,complex.h
(iv) isalnum( ) - ctype.h
11) Ans:
(i) strcmp( ) - string.h
(ii)getc( ) - stdio.h
12) Ans:
(i) get( ) - iostream.h
(ii) open( ) - fstream.h
(iii) abs( ) - math.h, stdlib.h
(iv) strcat( ) - string.h
13) Ans:
17
(i) getc( ) - stdio.h
(ii) strcat( ) - string.h
14) Ans)
(i) isupper( ) - ctype.h
(ii)setw( ) - iomanip.h
(iii)exp( ) - math.h
(iv)strcmp( ) - string.h
15) Ans)Execution of the program starts and ends at main( ). The main ( ) is the driver function of the
program. If it is not present in a program, no execution can take place.
16) Ans: (i)strcat( ) - string.h
(ii)scanf( ) - stdio.h
(iii)getchar( ) - stdio.h
(iv)clrscr( ) - conio.h
17) Ans:
(i) cos( ) - math.h
(ii) setw( ) - iomanip.h
(iii) toupper( ) - ctype.h
(iv) strcpy( ) - string.h
18) Ans:
(i) cos( ) - math.h
(ii) setw( ) - iomanip.h
(iii) toupper( ) - ctype.h
(iv) strcpy( ) - string.h
19) Ans.
(i) string.h (ii) ctype.h (iii) math.h (iv) stdio.h
20) Ans. (i) math.h (ii) ctype.h
21) Ans. (i) fstream.h (ii) graphics.h (iii) fstream.h (iv) string.h
22) Ans.
(i) iostream.h (ii) fstream.h (iii) math.h (iv) string.h
23) Ans.
(i) fstream.h (ii) fstream.h (iii) iostream.h (iv) string.h
24) Ans.
(i) string.h (ii) stdio.h>
25) Ans.
(i) string.h (ii) stdio.h
26) Ans.
(i) math.h (ii) ctype.h (iii) math.h (iv) string.h
27) Ans. (i) stdio.h (ii) string.h (iii) math.h (iv) ctype.h
28) Ans. (i) math.h (ii) strcpy.h (iii) ctype.h (iv) fstream.h

2 marks Answers

1 Ans:
#include<iostream.h>
void test(int x,int y=20); //Prototype
missing void main( )
{ int F = 10, S = 20; //Data type missing
Text(F,S); //Comma to come instead of ;
Text(S);}
void Text(int x, int y)
{ x=x+y;
cout<<x<<y; //Output operator << required }
2 Ans:Output:
[1]10&15
[2]21&13
18
[1]11&16
[2]22&14
3Ans: #include<iostream.h>
class FLIGHT
{
long FlightCode;
char Description[25];
public:
void addInfo()
{
cin>>FlightCode; gets(Description);
}
void showInfo()
{
cout<<FlightCode<<‖:‖<<Description<<endl;
} };
void main( )
{ FLIGHT F;
F.addInfo();
F.showInfo;}
4Ans:
void Callme(int Arg1,int Arg2=20);
#include<iostream.h>
void main( )
{ int One=10,Two=20;
Callme(One,Two); //Given ; instead of ,
Callme(Two); }
void Callme(int Arg1,int Arg2)
{ Arg1=Arg1+Arg2;
cout<<Arg1<<Arg2;}
5 Ans)
Since random(2) gives either 0 or 1, Fly value will be either 1 or 2.
(random(n) gives you any number between 0 to n-1) City[1] is .CHN.
City[2] is .KOL.
Since I value from 0 to 2 (ie<3), 3 iterations will takes place.
So the possible output consists 3 strings separated by :, each of
them may be either .CHN. or .KOL..
So the possible output will be
(ii) CHN : KOL : CHN:
(iv) KOL :CHN : KOL:
6Ans) Since random(2) gives either 0 or 1, ToGo value will be either 1 or 2.
(random(n) gives you any number between 0 to n-1) Area[1] is .SOUTH.
Area[2] is .EAST.
Since I value from 0 to 2 (ie<3), 3 iterations will takes place. So the possible output consists 3 strings
separated by :, each of them may be either .SOUTH. or .EAST..
So the possible output will be
(i) SOUTH : EAST : SOUTH :
(iv) SOUTH : EAST : EAST :
7 Ans)#include<iostream.h>
const int Max=10;//Constant Variable .Max. must be
//initialized. Declaration Syntax Error
void main( )
{ int Numbers[Max]={20,50,10,30,40};
for(Loc=Max-1;Loc>=0;Loc--)
cout>>Numbers[Loc];}

19
8 Ans: Expected Output:
(iv) None of the
above. 9 Ans: Output: 2
10)Ans: #include<iostream.h>
const int Multiple=3;
void main( )
{ int Value = 15;
for(int Counter = 1;Counter <=5;Counter ++, Value -= 2)
if(Value%Multiple == 0)
{ cout<<Value * Multiple;
cout<<endl; }
else
cout<<Value + Multiple <<endl; }
11Ans:Output:
cbse*eXAM*3116
6113*MXAe*esbc
12. 27:
27:
27:
26:
13. #include <iostream.h>
class MEMBER
{
int Mno;
float Fees;
public:
void Register(){cin>>Mno>>Fees;}
void Display(){cout<<Mno<<" : "<<Fees<<endl;}
};
void main()
{
MEMBER M;
M.Register();
M.Display();
}
3 Marks answers

1)Ans:
Solution:
Text[ ] =

When I=0
Since Text[0] is ‗M‘, Upper Case Letter,
(isupper(Text[I]) will becomes true.
So Text[I] =Text[I]+1
So Text[0]=Text[0]+1
Text[0] =77(ASCII Value of M) + 1 = 78 =N(78 is ASCII Value of N)
Now the String Text[ ] =

20
When I=1
Since Text[1] is ‗i‘, Which is a character, but which is not
Upper case,
else part will be executed.
Ie Text[I]=Text[I+1]
Here Text[1]=Text[1+1]
=Text[2]
Ie ‗n‘ will be stored in place of ‗I‘
Now the String Text[ ] =

When I=2
Since Text[2] is ‗n‘, Which is a character, but which is not
Upper case, else part will be executed.
Ie Text[I]=Text[I+1] Here
Text[2]=Text[2+1]
=Text[3]
Ie ‗d‘ will be stored in place of ‗n‘
Now the String Text[ ] =

When I=3
Since Text[3] is ‘d‘, Which is a character, but which is not Upper
case, else part will be executed.
Ie Text[I]=Text[I+1] Here
Text[3]=Text[3+1]
=Text[4]
Ie ‗@‘, will be stored in place of ,‘d‘
Now the String Text[ ] =

When I=4
Since Text[4] is ‗@‘, Since which is not an alphabet,
(!isalpha(Text[I])) will becomes true.
Ie if(!isalpha(Text[I]))
Text[I]=‟*‟;
Ie Text[4]=‘*‘
Ie ‗*‘ will be stored in place of ‘@‘
Now the String Text[ ] =

When I=5
Since Text[5] is ‗W‘, Upper Case Letter,
21
(isupper(Text[I]) will becomes true.
So Text[I] =Text[I]+1
So Text[5]=Text[5]+1
Text[5] =87(ASCII Value of W) + 1 = 88 =X(88 is ASCII Value of X)
Now the String Text[ ] =

When I=6
Since Text[6] is ‗o‘, Which is a character, but which is not
Upper case, else part will be executed.
Ie Text[I]=Text[I+1] Here
Text[6]=Text[6+1]
=Text[7]
Ie ‗r‘ will be stored in place of ‗o‘
Now the String Text[ ] =

When I=7
Since Text[7] is ‗r‘, Which is a character, but which is not
Upper case, else part will be executed.
Ie Text[I]=Text[I+1] Here
Text[7]=Text[7+1]
=Text[8]
Ie ‗k‘ will be stored in place of ‗r‘.
Now the String Text[ ] =

When I=8
Since Text[8] is ‗k‘, Which is a character, but which is not
Upper case, else part will be executed.
Ie Text[I]=Text[I+1] Here
Text[8]=Text[8+1]
=Text[9]
Ie ‗!‘ will be stored in place of ‗k‘
Now the String Text[ ] =

When I=9
Since Text[9] is ‗!‘, Since which is not an
alphabet, (!isalpha(Text[I])) will becomes true.
Ie if(!isalpha(Text[I]))
Text[I]=‟*‟;
Ie Text[9]=‘*‘

22
Ie ‗*‘ will be stored in place of ‗!‘
Now the String Text[ ] =

Output: Nnd@*Xrk!*
2) Ans:
Output: hat@*PVUQVU*
3)Ans:
Output:
20,25,30,
20,25,30,
Number=30
4)Ans:
Output: 10,15,20,25,30,
10,15,20,
Number =30
5)Ans:
Output: teRmttoe
6) Ans: Output:
11, 21 ,6

25 , 15, 35
35, 25, 45
7)Ans: Output:
2023
1023
1111
8)Ans: Output:
New Text=@@@@ccddIIe

9) Ans: Output:

void AddNSave(int A[ ],int B[ ],int C[ ],int N,int M, int &K) 3


{
int I=0,J=0;
K=0;
while (I<N &&
J<M) if (A[I]<B[J])
C[K++]=A[I++];
else
if (A[I]>B[J])
C[K++]=B[J++];
else
{
C[K++]=A[I++];
J++;
}
for (;I<N;I++)
C[K++]=A[I];
for (;J<M;J++)

23
C[K++]=B[J];
}
10 Ans:Output:
void BookSearch()
{
fstream FIL;
FIL.open("BOOK.DAT",ios::binary|ios::in);
BOOK B;
int bn,Found=0;
cout<<"Enter Book No. to search…"; cin>>bn;
while (FIL.read((char*)&S,sizeof(S)))
if (FIL.RBno()==bn)
{
S.Display();
Found++;
}
if (Found==0) cout<<"Sorry! Book not found!!!"<<endl;
FIL.close();
}

You might also like