Table of Contents
Important Questions for Class 12 Computer Science (C++) – C++ Revision Tour
Topic – 1
Fundamentals
Previous Years Examination & Important Questions
2 Marks Questions
Question 1:
Write the type of C++ tokens (keywords and user defined identifiers) from the following: All Indio 2017
- new
- While
- case
- Num_2
Аnswer:
- new → keyword
- While → user defined identifier
- case → keyword
- Num_2 → user defined identifier
Question 2:
Out of the following, find those identifiers, which cannot be used for naming Variable, Constants or Functions in a C++ program: Delhi 2016
Cost, Price*Qty, float, Switch, Address One. Delete, Number12, do
Аnswer:
Price*Qty, float, do and Address One cannot be used for naming variables, constants or functions in a C++ program.
Question 3:
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined. Delhi 2016
NOTE: Assume all required header files are already being included in the program.
#define Equation(p,q)=p+2*q void main() { float A=3.2;B=4.1; C=Equation(A,B); cout<<'Output =’<<C<<endl; }
Аnswer:
The correct code is: #define Equation(p,q)(P+2*Q) void main() { float A=3.2,B=4.1; float C=Equation(A,B); cout<<"0utput ="<<C<<endl; }
Question 4:
Out of the following, find those identifiers, which cannot be used for naming Variables, Constants or Functions in a C++ program: All India 2016
Fatal*Tax, double, Case, My Name, NeW, switch, Column31,_Amount
Аnswer:
Fatal*Tax, double, My Name, switch cannot be used for naming variables, constants or functions in a C++program.
Question 5:
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined. All India 2016
NOTE Assume all required header files are already
being included in the program.
#define Formula(a,b)=2*a+b void main() { float X=3.2;Y=4.1; Z=Formula(X,Y); cout<<'Result='<<Z<<endl; }
Аnswer:
The correct code is: #define Formula(a.b)(2*a+b) void main() { float X=3.2.Y=4.1: float Z=Forinula(X.Y): cout<<"Result="<<Z<<endl: }
Question 6:
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined. All Indio 2016C NOTE Assume all required header files are already
being included in the program.
define formula(a,b,c)a+2*b+3*c; void main() { int L=1,M=2,N=3, J=Formula(L,M,N); cout<<'output ='<<J<<endl; }
Аnswer:
The correct code is: #define Formula(a,b,c)a+2*b+3*c void main() { int L=1,M=2,N=3: int J=formula(L,M,N); cout<<"0utput="<<j<<endl; }
Question 7:
Find the correct identifiers Out of the following, which can be used for naming Variable, Constants or Functions in a C ++ program: Delhi 2015
While, for, Float, new, 2ndName, A%B, Amountl2, _Counter
Аnswer:
The correct identifiers are as follows:
While, Float, Amount 12 and_Counter
Question 8:
Find the correct identifiers out of the following, which can be used for naming Variables, Constants or Functions in a C++
For, while, INT, NeW, delete, 1stName, Add+Subtract, name1
Аnswer:
The correct identifiers are as follows:
For, INT, NeW and name1
Question 9:
Observe the following C++ code carefully and rewrite the same after removing all the syntax error(s) present in the code. Ensure that you underline each correction in the code. All India 2013
Important Note:
(i) All the desired header files are already included, which are required to run the code.
(ii) Correction should not change the logic of the program.
//define Change(A,B)2*A+B; void maint() { Float X,Y,F; cin>>X>>Y ; F=Change[X,Y]; cout<<"Result:"<<F<endline; }
Аnswer:
The correct code is: #define Change (A.B)(2*A+B) void main() { float X.Y.F: cin>>X>>Y; F=Chanae(X.Y): cout<<"Result:”<<F<<end1: }
Question 10:
Observe the following OH- code carefully and rewrite the same after removing all the syntax error(s) present in the code. Ensure that you underline each correction in the code.
Important Note:
(i) All the desired header files are already included, which are required to run the code.
(ii) Correction should not change the logic of the program. Delhi 2013
//define Convert(P,Q)P+2*Q; void main() { Float A,B,Result: cin>>A>>B; Result=Convert[A,B]; cout<<"Output:"<<Result<endl: }
Аnswer:
The correct code is: #define Convert(P,Q)(P+2*Q) void main() { float A,B,Result; cin >>A>>B; Result=Convert(A.B): cout<<"0utput"<<Result<<endl: )
Question 11:
Give the difference between the type casting and automatic type conversion. Also, give a suitable C++ code to illustrate both. All India 2012,2011 Delhi 2010
Аnswer:
Type casting It is used by the programmer to convert the value of one type to another type,
e.g. float X=(float) 15/6:
Automatic type conversion It is automatically done by the compiler itself wherever required.
e.g. float X=3;
Question 12:
What is the difference between local variable and global variable? Also, give a suitable C++ code to illustrate both. Delhi 2011
Аnswer:
A variable which is declared within the body of a function and accessible only within the function is known as local variable.
A variable which is declared outside any function and accessible to all functions in a program is known as global variable.
To illustrate this, below is given programming example:
#include<iostream.h> float area; //Global variable void cirarea() { float r; //Local variable cin>>r area=3.14*r*r; cout<<area; } void rectarea() { float l,b; //Local variables cin>>l>>b; area=l*b; cout<<area; } void main() { cirarea(); rectarea(); }
Question 13:
What is the function of typedef in C++? Also, give a suitable C++ code to illustrate it. Delhi 2011C
Аnswer:
typedef keyword is used to assign alternative names to existing types.
e.g. typedef int in;
Now, whenever we want to declare a variable of type integer, rather than using int we can use ‘in’
keyword in place of int.
e.g.
void main() { typedef int in; //in is an alternative name to int in a=10; //a is of type integer cout<<a; }
Question 14:
What is the function of #define keyword? Give an example to illustrate its use. Delhi 2009C
Аnswer:
#define keyword is a preprocessor directive that is used to define a macro or a symbolic constant.
Syntax
#define macroname expression
or
#define symbolic constant-value
e.g.
#define Area(R) 3.14*R*R
#define Max 100
Topic – 2
Control Flow and Functions Control Flow
1 Mark Questions
Question 1:
Ronica Jose has started learning C++ and has typed the following program. When she compiled the following code written by her, she discovered that she needs to include some header files to successfully compile and execute it. Write the names of those header files, which are required to be included in the code.
void main() double X,Times,Result; cin>>X>>Times; Result=pow(X,Times); cout<<Result<<endl ;
All India 2016
Аnswer:
Ciostream. h>→cout, cin
<math. h>→pow()
Question 2:
Jayapriya has started learning C++ and has typed the following program. When she compiled the following code written by her, she discovered that she needs to include some header files to successfully compile and execute it. Write the names of those header files, which are required to be included in this code.
void main() { float A,Number,Outcome; cin>>A>>Number; Outcome=pow(A,Number); cout<<Outcome<<endl; } Delhi 2016
Аnswer:
<iostream.h>→cout, cin
<math.h>→pow( )
Question 3:
Ahmad has started learning C++ and has typed the following program. When he compiled the following code written by him, he discovered that he needs to include some header files to successfully compile and execute it. Write the names of those header files, which are required to be included in the code.
void main() float Radians,Value; cin>>Radians; Value=sin(Radians); cout<<value<<endl; } All India 2016C
Аnswer:
<iostream.h>→cout, cin
<math.h>→sin( )
Question 4:
Observe the following C ++ code and write the name(s) of the header file(s), which will be essentially required to run it in a C ++ compiler.
void main() { float Area,Side; cin>>Area; Side=sqrt(Area); cout<<"0ne Side of the Square:"<<Side<<endl; } All India 2013
Аnswer:
<iostream.h>→ cout,cin
<math.h> →sqrt( )
Question 5 :
Observe the following C ++ code and write the name(s) of the header file(s), which will be essentially required to run it in a C++ compiler.
void main() { int Number; cin>>Number; if(abs(Number) == Number); cout<<"Positive"<<endl; } Delhi 2013
Аnswer:
<iostream.h>→ cout, cin
<math.h>→abs( )
Question 6:
Name the header file(s), which are essentially required to run the following program segment.
void main() } char A='K',B; if(islower(A)) B=toupper(A); else B='*'; cout<<A<<"turned to"<<B<<endl: } Delhi 2013C
Аnswer:
<iostream.h>→cout,cin
<ctype.h>→ islower( ); i supper( )
Question 7:
Write the names of the header files to which the following belong:
- puts( )
- sin( ) Delhi2009
Аnswer:
puts( )→<stdio.h>
sin( )→<math.h>
Question 8:
Write the names of the header files to which the following belong:
- setw( )
- sart( ) All India 2009
Аnswer:
setw( )→<iomanip.h>
sqrt( )→<math.h>
Question 9:
Write the names of the header files to which the following belong:
- puts( )
- randomizer( ) Delhi2009c
Аnswer:
puts( )→<stdio.h>
randomize( )→<stdlib.h>
2 Marks Questions
Question 10:
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
NOTE: Assume all required header files are already being included in the program.
void main() { cout<<"Enter an Alphabet:"; cin>>CH ; switch(CH) case 'A' cout<<"Ant";Break; case ’B' cout<<"Bear”;Break; } All India 2017
Аnswer:
The correct code is: void main() { char CH: cout<<”Enter an Alphabet:"; cin>>CH ; switch(CH) { case 'A': cout«"Ant"; break: case 'B'x cout<<"Bear"; break: } }
Question 11:
Observe the following C++ code very carefully and rewrite it after removing any/all syntactical errors with each correction underlined.
NOTE Assume all required header files are already being included in the program.
#Define float MaxSpeed = 60.5; void main() int MySpeed char A!ert =‘N’; cin>>MySpeed; if MySpeed>MaxSpeed Alert ='Y’; cout<<Alert<<endline; } All India 2015
Аnswer:
The correct code is: #define float Max Speed = 60.5; void main() { int MySpeedi; char Alert = 'N'; cin>>My Speed; if(MySpeed>MaxSpeed) Alert = 'y'; cout<<Alert<<endl; }
Question 12:
Write the output of the following C ++ program code:
NOTE Assume all required header files are already being included in the program.
void Location(int &X, int Y=4) Y += 2; X += Y; } void main() { int PX=10, PY=2; Location(PY); cout<<PX<<","<<PY<<endl; location(PX,PY); cout<<PX<<","<<PY<<endl; } All Indio 2015
Аnswer:
The output will be
10, 8
20, 8
Question 13:
Observe the following C++ code very carefully and rewrite it after removing any/all syntactical errors with each correction underlined.
NOTE Assume all required header files are already being included in the program.
//Define float Max=70.0; Void main() { int Speed char Stop='N'; cin>>Speed; if Speed>Max Stop='Y; cout<<Stop<<end; } Delhi 2015
Аnswer:
The correct code is: #define float Max = 70.0; void main() { int Speed; char stop = 'N'; cin>>Speed; if(Speed>Max) Stop = 'Y'; cout<<Stop<<endl; }
Question 14:
Write the output of the following C++ program code:
NOTE Assume all required header files are already being included in the program.
void Position(int &Cl,int C2=3) { Cl+=2; C2+=Y; } void main() { int P1=20, P2=4; Position(P1); cout<<Pl<<","<<P2<<endl; Position(P2,P1); cout<<Pl<<","<<P2<<endl; } Delhi 2015
Аnswer:
This program code will give error, i.e Y is undefined symbol.
Question 15:
Write the output of the following C ++ program code:
NOTE Assume all required header files are already being included in the program.
void Draw(int & N, CharMark='#') { for(int 1=1;I<=N;I++) cout<<Mark<<endl; N++; } void main() { int Count=3; char Sign='*'; Draw(count); Draw(Count,Sign); Draw(Count,'&'); } All India 2015C
Аnswer:
The output will be:
#
#
#
*
*
*
*
&
&
&
&
&
Question 16:
Write a user defined function DIVT( ) which takes an integer as parameter and returns whether it is divisible by 13 or not. The function should return 1 if it is divisible by 13, otherwise it should return 0. All India 2014C
Аnswer:
int DIVT(intx) { if(%u == 0) return 1; else return 0; }
Question 17:
What is the difference between actual and formal parameter? Give a suitable example to illustrate using a C++ code.
Delhi 2014
or
Difference between the actual parameters and formal parameters. Also, give a suitable C++ code to illustrate both.
Delhi 2013C; Delhi 2012; All India 2009
Аnswer:
Differences between actual and formal parameters are as follows:
Actual Parameter | Formal Parameter |
Parameters provided at the time of function calling are called actual parameters. These parameters contain actual values. |
Parameters provided at the time of function definition are called formal parameters. These parameters are simple variable declarations, i.e. they do not contain actual values. |
e.g. #inc1ude<iostream.h> #include<conio.h> void swap(int n1, int n2) //Formal Parameters { int temp = n1; n1=n2; n2=temp; cout<<"Values of numl and num2 after swapping:"; cout<<n1<<" "<n2; } void main() { int num1, num2; cout<<"Enter two numbers:"; cin>>num1>>num2; swap(num1,num2); //Actual Parameters getch(); }
Question 18:
Find the output of the following program: All India 2014C
#include<iostream.h> void in(int x, int y, int &z) } x+=y; y--; z*=(x-y); } void out(int z, int y, int &x) { x*=y; y ++; z/=(x+y); } void main() { int a=20, b=30, c=10; out(a,c,b); cout<<a<<"#"<<b<<"#"<<c<=><"#"<<endl: in(b,c,a): cout<<a<<"@"<<b<<''@"<<c=><<"#"<<endl; out(a,b,c); cout<<a<<"$"<<b<<"$"<<c<<”=>$"<<endl; }
Аnswer:
Output
20#300#10#
602 0@ 3 0 0@10@
6020$300$3000$
Question 19:
What is the benefit of using default parameter/argument in a function? Give a suitable example to illustrate it using C++ code. All Indio 2013
Аnswer:
A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user supply a value for the default parameter, the user-supplied value is used. Consider the following program:
void PrintValues(int nValue1, int nValue2=10) { cout<<"1st value:"<<nValue1<<endl; cout<<"2nd value :"<<nValue2<<endl; } void main() { PrintValues(1); //nValue2 will use default parameter of 10 PrintValues(3.4); //override default value for nValue2 } This program produce the following output : 1st value: 1 2nd value: 10 1st value: 3 2nd value: 4
Question 20:
What is the benefit of using function prototype for a function? Give a suitable example to illustrate it using a C++ code
Delhi 2013
Аnswer:
The function prototype serve to ensure that calls to the function are made with the proper number and types of arguments. In the case of function overloading, the different prototypes serve to distinguish which version of the function to call. The computer will complain with an error, if no function prototype is found for any particular call to function.
e.g. #include<iostream.h> int square(int); //Function prototype void main() for(int x=l;x<=10;x++) cout<<square(x)<<" ": cout<<endl; } int square(int y) //Function definition { return y*y; }
Question 21:
Find out the expected correct output(s) from the options (i) to (iv) for the following C++ code. Also, find out the minimum and the maximum value that can be assigned to the variable stop used in the code.
void main() { randomize(); int Begin=3,stop; for(int Run=l;Run<4;Run++) { stop=random(Begin)+6; cout<<Begin++<<stop<<”*"; } } (i) 36*46*59* (ii) 37*46*56* (iii) 37*48*57* (iv) 35*45*57* Delhi 2013C
Аnswer:
Expected output : (ii), (iii)
Minimum value is 6
Maximum value is 8
Question 22:
Go through the C++ code shown below and find out the possible output or output from the suggested output (i) to (iv). Also, write the least value and highest value, which can be assigned to the variable Guess.
#include<iostream.h> #include<stdlib.h> void main() { randomze(); int Guess, High=4; Guess=random(High)+50; for(int C=Guess;C<=55;C++) cout<<C<<"#"; (i) 50#51#52#53#54#55# (ii) 52#53#54#55# (iii) 53#54# (iv) 51#52#53#54#55 Delhi 2011
Аnswer:
Possible outputs are (i), (ii)
Least value of Guess = 50
Highest value of Guess = 53
Question 23:
Go through the C++ code shown below, and find out the possible output or output from the suggested Output Options (i) to
(iv) . Also, write the minimum and maximum values, which can be assigned to the variable MyNum.
#include<iostream.h> #include<stdlib.h> void main() { randomize(); int MyNum,Max=5; MyNum=20+random(Max); for(int N=MyNum;N<=25;N++) cout<<N<<"*"; } (i) 20*21*22*23*24*25 (ii) 22*23*24*25* (iii) 23*24* (iv) 21*22*23*24*25 All India 2011
Аnswer:
The possible outputs is (ii)
The minimum value of MyNum = 20
The maximum value of MyNum = 24
Question 24:
The following code is from game, which generates a set of 4 random numbers. Yallav 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.
#include<iostream.h> #include<stdlib.h> const int Low=15; void main() { randomize(); int P0INT=5,Number; for(int 1=1;I<=4;I++) { Number=Low+random(POINT); cout<<Number<<":"; (i) 19 : 16 : 15 : 18 : (ii) 14 : 18 : 15 : 16 : (iii) 19 : 16 : 14 : 18 : (iv) 19 : 16 : 15 : 16 : Delhi 2010
Аnswers:
(iv) 19 : 16 : 15 : 16 :
Question 25:
The following code is from a game, which generates 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.
#include<iostream.h> #include<stdlib.h> const int Low=25; void main() { randomize(); int P0INT=5,Number; for(int I=1;I<=4;I++) { Number=Low + random(POINT); cout«Number<<":”; POINT--; } }
Аnswer:
(iv) 29 : 26 : 25 : 26 :
Question 26:
Study the following program and select the possible output from it
#include<iostream.h> #include<stdlib.h> const int LIMIT=4; void main() { randomize(); int Points; Points=100+random(LIMIT); for(int P=Points';P>=100;P--) cout<<P<<"#"; cout<<endl; } (i) 103 # 102 # 101 # 100 # (ii) 100 # 101 # 102 # 103 # (iii) 100 # 101 # 102 # 103 # 104 # (iv) 104 # 103 # 102 # 101 # 100 # Delhi 2009
Аnswers:
(i) 103 # 102 # 101 # 100#
Question 27:
Study the following program and select the possible output from it
#include<iostream.h> #include<stdlib.h> const int MAX=3; void main() { randomize(); int Number; Number=50+random(MAX); for(int P=Number;P>=50;P--) cout<<P<<"#”; cout<<endl; } (i) 53 # 52 # 51 # 50 (ii) 50 # 51 # 52 # (iii) 50 # 51 # (iv) 51 # 50 # All India 2009
Аnswers:
(iv) 51 # 50 #
Question 28:
In the following program, find the correct possible output(s) from the options
#include<iostream.h> #include<stdlib.h> void main() { randomize(); int x=125,y=99; int a=random(3)+4; int b=random(2)+2; for(int i=0;i<a;i++) cout<<"&”; cout<<x<<","; for(i=0;i<b;i++) cout<<"*"; cout<<y<<endl; } (i) &&125,*99 (ii) &&125,**99 (iii) &&&&&&125,**99 (iv) &&&&125,***99
Аnswers:
(iii) &&&&&&125, **99
(iv) &&&&125, ***99
Previous Years Examination Questions (Topic 3)
(1 Mark Questions)
Question 1:
Anil typed the following C++ code and during compilation he found three errors as follows:
(i) Function strlen should have prototype
(ii) Undefined symbol cout
(iii) Undefined symbol endl All India 2017
On asking, his teacher told him to include necessary header files in the code. Write the names of the header files, which Anil needs to include, for successful compilation and execution of the following code:
void main() { char Txt [ ] = "Weicome"; for (int C=0; (Xstrlen(Txt);C++) Txt [C] = Txt[C]+1: cout<<Txt<<endl: }
Answer:
For strlen → <string.h> For cout, endl → <iostream.h>
Question 2:
Observe the following program very carefully and write the names of those header file(s), which are essentially needed to compile and execute the following program successfully. All India 2015
typedef char STRING[80] ; void main( ) ( STRING Txt [] = "We love Peace"; int Count = 0: while(Txt [Count]! = "\0") if(isalpha (Txt[Count]) ) Txt [Count++] = "@"; else Txt [Count++] = "#"; puts (Txt); }
Answer:
<ctype.h> → isalpha <stdio.h> → puts
Question 3:
Observe the following program very carefully and write the names of those
header file(s), which are essentially needed to
compile and execute the following program successfully. Delhi 2015
typedef char TEXT[80]; void main() { TEXT Str [ ]="Peace is supreme"; int Index = 0; while(Str[Index]1= "\0") if(isupper(Str [Index])) Str[Index++] = "#" ; else Str[Index++]="*" ; puts(Str); }
Answer:
<ctype.h>→isupper <stdio.h>→ puts
Question 4:
Name the header files that shall be needed for successful compilation of the following C++ code All Indio 2014 C
void main() { char str[20], str[20]; gets(str) ; strcpy(strl,str): strrev(str); puts(str) ; puts(strl); }
Answer:
<stdio.h> → puts, gets <std1ib> → strcpy(),strrev()
Question 5:
Observe the following C++ code and write the name(s) of the header file(s), which will be essentially required to run it in a C++ compiler.
void main()
{
char Text[20],C;
cin>>Text;
C=tolower(Text[0]) ;
cout<<C<<"is the first char of"
<<Text<<endl ;
} Delhi 2014
Answer:
<iostream.h>cout, cin <ctype.h>tolower( )
Question 6:
Observe the following C++ code and write the name(s) of the header file(s), which will be essentially required to run it in a C++ compiler.
void main()
{
char CH, STRC20]:
cin>>STR;
CH=toupper(STRCO]);
cout<<STR<<"starts with"<<CH<<endl ;
} All India 2014
Answer:
<iostream.h>cin, cout <ctype.h> toupper()
Question 7:
Which C++ header file(s) are 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,
void main( )
{
char STRING [ ] ="SomeThing" ;
cout<<”Bal ance Characters : "<<160-
strlen(STRING)<<endl ;
} Delhi 2012
Answer:
<iostream.h>→Cout <string.h>→ strlen()
Question 8:
Which C++ header file(s) are 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
void main()
{
char STRING[]="SomeThing" ;
cout<<"Remaining SMS Chars :"<<160-
strlen(STRING)<<endl ;
} All India 2012
Answer:
<iostream.h> → cout <string.h> → strlen()
Question 9:
Write the names of the header files, which is/are essentially required to run/execute the following C++ code.
void main()
{
char C, string[]="Excellence Overload";
forfint 1=0 ; string[I] !='\0';I++)
if(string[I]== " ”)
cout<<endl;
else
{
C=toupper(string[I]) ;
cout<<C;
} Delhi 2011
Answer:
<iostream.h>→cout <ctype.h>→toupper()
Question 10:
Write the names of the header files, which is/are essentially required to run/execute the following C++ code.
void main()
{
char CH,Text[] = "+ve Altitude";
fortint I = 0;Text [I]!=‘\0’;I++)
if(TextCI] -- 1) cout<<endl ;
else
{
CH = toupper(Text[I]);
cout<<CH;
}
} All India 2011
Answer:
<ctype.h>→toupper() <iostream.h>→cout
Question 11:
Which C++ header file(s) will be essentially required to be included to run/execute the following C++ code?
void main()
{
char Name[20];
cin>>Name;
toupper (Name[0]);
cout<<Name<<endl;
} Delhi 2011C
Answer:
<iostream.h>→C0ut,cin <ctype.h>→ toupper()
Question 12:
Which C++header file(s) will be essentially required to be included to run/execute the following C++ code?
void main()
{
int Eno=123, char EName[]
="Rehan Swarop";
cout<<setw(5)<<Eno<setw(25)
<<EName<<endl;
) Delhi 2010
Answer:
<iomanip.h> → setw() Ciostream.h> → Cout
Question 13:
Which C++header file(s) will be
essentially required to be included to run/execute the following C++ code?
void main()
{
int Rno=24;
char Name[]="Aman Singhania";
cout<<setw(10X<Rno<<setw(20)
<<Name<<endl;
} All India 2010
Answer:
<iomanip.h>→setw() <iostream.h>→cout
(2 Marks Questions)
Question 14:
Find and write the output of the following C++ program code:
NOTE Assume all required header files are already included in the program.
# define Diff(N1,N2)
((N1>N2)?N1-N2:N2-N1)
void main ()
{
int A,B,NUM[] = {10,23,14,54,321 ;
for(int CNT=4; CNT>0; CNT--)
{
A=NUM[CNT] ;
B=NUM[CNT-1] ;
cout<<Diff(A,B)<<'#' :
}
} All India 2017
Answer:
Output 22#40#9#13#
Question 15:
Look at the following C++ code and find the possible outputs(s) from the options (i) to (iv) following it. Also, .write the maximum values that can be assigned to each of the variables N and M.
NOTE
• Assume all the required header files are already being included in the code.
• The function random(n) generates an integer between 0 and n-1.
void main() { randomize() ; int N=random(3), M=random(4); int DOCK[3][3] = {{1,2,3},{2,3,4},1{3,4,5,}}: for (int R=0; R<N; R++) { for(int C=0; C<M; C++) cout<<DOCK[R][C]<<" cout<<endl ;
(i) | (ii) |
1 2 3 | 1 2 3 |
2 3 4 | 2 3 4 |
3 4 5 | |
(iii) | (iv) |
1 2 | 1 2 |
2 3 | 2 3 |
3 4 |
Answer:
Possible Outputs: (ii) and (iii)
Maximum value of N is 2.
Minimum value of N is 0.
Maximum value of M is 3.
Minimum value of M is 0.
Question 16:
Find and write the output of the following C++ program code: All India 2016
NOTE Assume all required header files are already included in the program.
typedef char TEXT[80]; void JumbleUp(TEXT T) { int L=strlen(T); for(int C=0; C<L-1; C+=2) { char CT=T[C]: T[C]=T[C+1] : T[C+1]=CT; } for(C=1: C<L; C+=2) i f(T [C]>= ’ M ' && T[C]<='U' ) T[C]='@' : } void main() { TEXT Str="HARMONIOUS"; JumbleUp(Str) ; cout<<Str<<endl : }
Answer:
The output will be AHM@N@OIS@
Question 17:
Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can he assigned to the variable PICKER. All India 2016
NOTE Assume all required header files are already being included in the code. The function random(n) generates an integer between 0 and n -1.
void main() { int PICKER: PICKER=1+random(3) ; char COLOURC][5]={"BLUE","PINK”, "GREEN”,"RED"): for(int I=0;I<=PICKER; I++) { for(int J=0;J<=I; J++) cout<<COLOUR[J] : cout<<endl: } }
(i) | (ii) | (iii) | (iv) |
PINK | BLUE | GREEN | BLUE |
PINKGREEN | BLUEPINK | GREENRED | BLUEPINK |
PINKGREEN | BLUEPINK | BLUEPINK | |
RED |
GREEN BLUEPINK GREENRED |
GREEN |
Answer:
The possible outputs are (ii) and (iv)
Minimum value of PICKER = 1
Maximum value of PICKER = 3
Question 18:
Find and write the output of the following C++ program code: Delhi 2016 NOTE Assume all required header files are already included in the program,
typedef char STRING [80];
void MIXITNOW(STRING S)
{
int Size=strlen(S) :
for(int I=0; I<Size-1; I+=2)
Char WS = S[I] ;
S[I] = S[I+1]:
S[I+1]=WS;
}
for( I=1 ; I<Size; I+=2)
if(S[I]>=’M’ && S[I]<=’U’)
S[I]=’@’;
}
void main( )
{
STRING Word = “CRACKAJACK”;
MIXITNOW(Word) ;
cout<<Word<<endl;
Answer:
Output RCCAAKAJKC
Question 19:
Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable CHANGER. Delhi 2016
NOTE Assume all the required header files are already being included in the code. The function random(n) generates an integer between 0 and n -1.
void main() { randomize(); int CHANGER; CHANGER = random(3); char CITY[ ][25] = {"DELHI", "MUMBAI","KOLKATA","CHENNAI"}; for(int 1=0; I<=CHANGER; I++) { forlint J=0; J<=I; J++) cout<<CITY[J] ; cout<< endl; Answer: The possible outputs are (i) and (ii) Minimum value of CHANGER = 0 Maximum value of CHANGER = 2 Question 20: Study the following program and select the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable NUM. Delhi 2015 NOTE Assume all required header files are already being included in the program, random(n) function generates an integer between 0 and n-1.
void main() { randomize(); int NUM; NUM=random(3)+2; char TEXT[]="ABCDEFGHIJK"; for(int 1=1; 1<=NUM; I++) for(int J=NUM; J<=7; J++) cout<<TEXT( J ) ; cout<<endl ; } }
(i) FGHI (ii) BCDEFG (iii) EFGH (iv) CDEFG
H H
FGHI BCDEFG EFGH CDEFG
H H
FGHI EFGH
FGHI EFGH
Answer:
The possible outputs are (iii) and (iv)
Minimum value of Num = 2
Maximum value of NUM = 4
Question 21:
Study the following program and select the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable VAL. All India 2015
NOTE Assume all required header files are already being included in the program. Random(n) function generates an integer between 0 and n-1.
void main()
{
randomizef);
int VAL;
VAL=random(3) + 2;
char GUESSC] =”ABCDEFGHIJK”;
for(int 1=1; K=VAL; I++)
{
for(int J=VAL; J<=7; J-F-F)
cout<<GUESS[J] ;
cout<<endl ;
(i) BCDEFGH (ii) CDEFGH (iii) EFGH (iv) FGHI
BCDEFGH CDEFGH EFGH FGHI
EFGH FGHI
EFGH FGHI
Answer:
The possible outputs are (ii) and (iii).
Minimum value of VAL = 2
Maximum value of VAL = 4
Question 22:
Observe the following C +4- code very carefully and rewrite it after removing any/all syntactical errors with each correction underlined.
NOTE Assume all required header files are already being included in the program,
const float PIE = 3.1416;
void main( )
{
float Radius =(3.5,6.2,8.6,9.11.Area;
int N;
cout<<'Enter N (Less than 5)?'; cin>>N;
for (0=0 ; C<N ; C++ )
{
Area=PIE*Radius[C] *Radius CC];
cout<<Area<<endline ;
}
} All India 2015c
Answer:
Const float PIE = 3.1416;
void main() ( float Radius[] = {3.5, 6.2, 8.6, 9.1}, Area; int N; cout <<"Enter N (Less than 5)?": cin>>N ; for (int C=O: C<N; C++) { Area = PIE* Radius[C]* Radi us[C] ; cout<<Area<<end!; } }
Question 23:
Study the following program and select the possible output(s) from the options (i) to (iv) following it. Also, write the maximum and the minimum values that can be assigned to the variable VALUE. All Indio 2015C
NOTE Assume all required header files are already being included in the program. Random(n) function generates an integer between 0 and n-1.
void main() { randomize(); int VALUE; VALUE=random(2) + 2; char Y0URTEXT[] =("ONE”,"TWO","SIX”,"TEN"} ; for(int Y=0;Y<VALUE; Y++) COUt<<Y0URTEXT[Y] ; cout<<"END"<<endl ; cout<<endl; }
(i) (ii) (iii) (iv)
ONETWOEND ONEENDTWO ONETWOSIXEND ONETWOSIXEND
Answer:
The possible outputs are (i) and (iv)
Minimum value of VALUE = 2
Maximum value of VALUE = 3
Question 24:
Deepa has just started working as a programmer in STAR SOFTWARE company. In the company, she has got her first assignment to be done using a C++ function to find the smallest number out of a given set of numbers stored in a one-dimensional array. But she has committed some logical mistakes while writing the code and is not getting the desired result. Rewrite the correct code underlining the corrections done. Do not add any additional statements in the corrected code.
All India 2014C
int find (int a[], int n) { int s=a[0]; for(int x=l; x<n; x++) if(a [x]>s) a[x]=s; return (s); }
Answer:
The correct code is
int find (int a[], int n)
{
int S=a[0];
for (int x=l; x<n; x++)
if (a [x]<s)
s=a [x] ;
return(s);
}
Question 25:
Find output of the following program segment: All India 2014C
#include<iostream.h> #include<ctyupe.h> void Mycode (char Msg [ ], Lehar CH) { {for(int cnt=0; Msg[cnt3] ! ="\0";cnt++) { if (Msg [cnt]>='B'&& Msg[cnt]<='G’) Msg [cnt]=tolower(Msg[cnt); else if(Msg[cnt]= =’N' ∪Msg[cnt]= = 'n' ∪Msg[cnt]= =' ' ) Msg[cnt]=CH; else if(cnt%2= =0) Msg[cnt]=toupper(Msg[cnt]); else Msg[cnt]=Msg[cnt-l]; } } void main() { Char MyText[]="Input Raw"; Mycode (MyText, '@' cout<<"NEW TEXT : "<<MyText<<endl
}
Answer:
Output
NEW TEXT : I@PPT@RRW
Question 26:
Rewrite the following C++ code after removing all the syntax error(s), if present in the code. Make sure that you underline each correction done by you in the code.
Important Note:
Assume that all the required header file are already included, which are essential to run this code.
The correction made by you do not change the logic of the program.
typedef char[50] STRING;
void main()
City STRING;
gets(City);
cout<<City[0]<<'\t,<<City[2];
cout<<City<<endline;
} Delhi 2014
Answer:
The correct code is
typedef char STRING[50] : //Error 1 void main() { STRING City; //Error 2 gets(City); cout<<City[o]<<"\t"<<City[2] ; //Error 3 cout<<City<<endl: //Error 4 }
Question 27:
Read the following C++ code carefully and find out, which out of the given options (i) to (iv) are the expected correct output(s) of it. Also, write the maximum and minimum value that can be assigned to the variable Start used in the code:
void main( )
{
int Guess[4]={200, 150, 20, 250};
int Start=random(2)+2;
for(int C=Start; C<4; C++)
cout<<Guess[C]<<"#" ;
}
(i) 200# 150#
(ii) 50#20#
(iii) 150#20#250#
(iv) 20#250# Delhi 2014
Answer:
Correct option is (iv) 20 # 250#
Maximum value of variable Start= 3
Minimum value of variable Start = 2
Question 28:
What is the difference between call by reference and call by value with respect to memory allocation? Give a suitable example to illustrate using C++ code. All India 2014
or
What is the difference between call by value and call by reference? Also, give a suitable C++ code to illustrate both.
All India 2010; Delhi 2009
Answer:
Differences between call by value and call by reference are as follows:
e.g. Following example illustrates the concept of call by value: #imclude<iostream.h> #include<conio.h> void swap(int x,int y) int temp = x; x = y; y = temp; cout<<"After swapping\n"; cout<<"Inside swap”<<endl ; cout<<"x : "<<x<<endl; cout<<"y : "<<y<<endl ; } void main() { int x,y; cout<<"Enter two numbers\n" cin>>x>>y; swap(x.y); cout<<”After swapping\n"; cout<<"Inside main"<<endl; cout<<"x : "<<x<<endl ; cout<<"y : "<<y; getch( ) ; }
The following example illustrates the concept of call by reference:
#include<iostream.h> #include<conio.h> void swap(int &x,int &y) ( int temp = x; x = y; y = temp; couK<<'After swapping\n"; cout<<"Inside swap"<<endl; cout<<"x : "<<x<<endl ; cout<<"y : "<<y<<endl ; } void main() { int x,y; cout<<"Enter two numbers\n"; cin>>x>>y ; swap(x,y); cout<<"After swapping\n” ; cout<<"Inside main"<<endl; cout<<"x : "<<x<<endl ; cout<<"y : "<<y; getch( ) ; }
Question 29:
Rewrite the following C++ code after removing all the syntax error(s), if present in the code. Make sure that you underline each correction done by you in the code. All India 2014
Important Note:
Assume that all the required header files are already included, which are essential to run this code.
The corrections made by you do not change the logic of the program,
typedef char[80] STR; void main() { Txt STR; gets(Txt); COUt<<Txt[0]<<'\t,«Txt[2] ; cout<<Txt<<endline;
Answer:
The corrected code is given below:
tvpedef char STR[80] ://Error 1 void main() { STR Txt; //Error 2 gets(Txt); cout<<Txt[0]<<"\t"<<Txt[2]; //Error 3 cout<<Txt<<endl: //Error 4
Question 30:
Read the following C++ code carefully and find out, which out of the given options (i) to (iv) are the expected correct output(s) of it. Also, write the maximum and minimum value that can be assigned to the variable Taker used in the code:
void main() ( int GuessMe[4]={100,50,200,20} ; int Taker=random(2)+2; for(int Chance=0;Chance <Taker;Chance++) cout<<GuessMe[Chance]<<"#" : }
(i) 100 (ii) 50#200#
(iii) 100#50#200# (iv) 100#50 All India 2014
Answer:
Correct output for the given code would be option
(iii) 100#50#200#
Maximum value of Taker = 3
Minimum value of Taker = 2
Question 31:
Find syntax error(s), if any, in the following program: (Assuming all desired header file(s) are already included)
typedef String [80] char;
void main()
{
String S;
for (L=0; L<26; C++)
S[L] = L+65;
S[L]="\0":
cout<<S<<endline :
} Delhi 2013C
Answer:
tvpedef char Strlnq[80] : void main ( ) { String S; for (int L= 0: L<26; L++) S[L]=L+65; S[L]="\0" ; cout<<S<<end l ; }
Question 32:
Based on the following C++ code, find out the expected correct output(s) from the options (i) to (iv). Also, find out the minimum and the maximum value that can be assigned to the variable Guess used in the code at the time when value of Turn is 3.
void main( )
{
char Result[][10]={"GOLD",
"SILVER”,"BRONZE"}:
int Getit=9,Guess :
for(int Turn=l;Turn<4;Turn++)
{
Guess=random(Turn); cout<<Getit-Guess<<Result [Guess]<<"*" ;
}
}
(i) 9GOLD*9GOLD*8SILVER*
(ii) 9GOLD*7BRONZE*8GOLD*
(iii) 9GOLD*8SILVER*9GOLD*
(iv) 9GOLD*8SILVER*8GOLD*
All India 2013
Answer:
Correct output is (i) 9G0LD*9G0LD*8SILVER*
Maximum value of Guess is 2
Minimum value of Guess is 0.
Question 33:
Based on the following C++ code, find out the expected correct output(s) from the options (i) to (iv). Also, find out the minimum and the maximum value that can be assigned to the variable Trick used in the code at the time when value of Count is 3
void main() { char StatusC][10]={"EXCEL”, "GOOD"."OK"i; int Turn=10,Trick; for(int Count=1;Count<4;Count++) { Trick=random(Count); cout<<Turn-Trick<<Status [T ri ck]«"#" ; } }
(i) 10EXCEL# 10EXCEL#80K#
(ii) 10EXCEL#80K#9GOOD#
(iii) 10EXCEL#9GOOD# 10EXCEL#
(iv) 10EXCEL# 10GOQD#80K# Delhi 2013
Answer:
Correct output is (iii) 10EXCEL#9GOOD#10EXCEL#
Minimum value of Trick is 0
Maximum value of Trick is 2
Question 34:
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 Turn?
#i nclude<iostream.h> #include<stdlib.h> void main() ( randomize(); int Game[]={10.16},P; int Turn=random(2)+5; for(int T=0; T<2; T++) { P=random(2) ; cout<<Game[P]+Turn<<"#" ; } }
(i) 15#22# (ii) 22#16#
(iii) 16#21# (iv) 2I#22# Delhi 2012
Answer:
(i), (iii) and (iv) can never be the output.
Minimum value of Turn =5
Maximum value of Turn =6
Question 35:
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?
#i nclude<iostream.h> #include<stdlib.h> void main() { randomze( ) ; int Arr[]={9,6},N; int Chance=random(2)+10; for(int CO;C<2;C++) { N=random(2); cout<<Arr[N]+Chance<<"#" ; } }
(i) 9#6#
(ii) 19#17#
(iii) 19#16#
(iv) 20# 16# All India 2012
Answer:
(i), (ii) and (iv) will not be expected from the program
Maximum value of Chance =11
Minimum value of Chance =10
Question 36:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include[iostream.h]
typedef char Text (80);
void main( )
{
Text T="Indian";
int Count=strlen(T) ;
cout<<T<< ' has ' <<Count<< ' characters '<<endl;
} Delhi 2011
Answer:
# include<iostream.h> #include<strinq.h> typedef char Text[80]: void main() { Text T="Indian"; int Count=strlen(T) ; cout<<T<<"has"<<Count <<"characters"<<end1 ; }
Question 37:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include<iostream.h>
typedef char[80] string;
void main()
{
string S="Peace";
int L=strlen (S) ;
cout<<3<< 'has'<<L<<'characters'<<endl;
} Delhi 2011
Answer:
#include<iostream.h> #include<string.h> tvpedef char strinq[80]: void main() { string S="Peace"; int L=strlen(S); cout<<S<< " has "<<L<<characters" <<endl; }
Question 38:
Find the output of the following program;
#include<iostream.h>
void SwitchOver(int AC], int N.intSplit)
{
for ( int K=0 ; K<N ; K++
if(K<Spl it)
A[K]+=K;
else
A[K]*=K;
}
void Display(int A[],int N)
{
for (int KO ; K<N ; K++)
(K%2=0)?cout<<A[K]<<"%" :cout <<A[K]<<endl ;
}
void main()
{
int H[]={30.40.50,20,10,5);
Switch0ver(H,6,3);
Display (H,6);
} All India 2011
Answer:
Output
30%41 52%60 40%25
Question 39:
Find the output of the following program
#includeCiostream.h>
#include<ctype.h>
void changeIt(char Text[],char C)
{
for(int K=0;Text[K]!="\0";K++)
{
if(Text[K]>="F"&&Text[K]<="L")
Text[K]=tolower(Text[K]);
else
i f (Text[K]—"E"| |Text[K]=="e" ) Text[KJO;
else
if(K%2= =0)
Text[K]=toupper(Text[K]) ;
else
Text[K]=Text[K-1];
}
}
void main()
{
char 0ldText[]="p0wERALone";
changeIt(0ldText,); cout<<"New Text: "<<01dText<<endl ;
} Delhi 2010
Answer:
Output
New Text :PPW%RRllN%
Question 40:
Find the output of the following program:
#include<iostream. h>
#include<ctype.h>
void MyCodetchar 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(Msq[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()
}
char MyText[]="ApEACeDriVE" ;
MyCodetMyText, "@");
cout<<"NEW TEXT: "<<MyText<<endl;
) All India 2010
Answer:
Output
NEW TEXT:@@e@ccddIIe
Question 41:
Find the output of the following program
#include<iostream.h>
#include<ctype.h>
void Encodei(char Info[],int N);
void main()
{
char Memo[]="Justnow";
Encode(Memo, 2);
count<<Memo<<endl;
}
void Encodeichar Info[],int N)
{
for(int I = 0;Info[I]!= "\0"; I++)
i f ( I %2= =0 )
Info[I]=Info[I]-N;
else
if(is lower (Info[I]);
Info[I]=toupper(Info[I])
else
Info[I]=Info[I]+N;
} Delhi 2009
Answer:
Output
HUqTlOu
Question 42:
Find the output of the following program
#include<iostream.h>
#include<ctype.h>
void Secretlchar Msg[],int N);
void main()
{
char SMS[]="rEPorTmE":
Secret(SMS, 2):
cout<<SMS<<endl:
}
void Secret(char Msg[],int N)
{
for(int C=O; Msg [C]!="\0”;C++)
if ( C%2 ==0)
Msg[C]=Msg[C] + N;
else if(isupper(Msg[C] ) )
Msg[C]=tolower(Msg[C]) ) :
Msg[C]=Msg[C]-N;
} All India 2009
Answer:
Output
teRmttoe
3 Marks Questions
Question 43:
Find the output of the following program
#include<iostream.h>
void ChangeArray(int Number, int ARR[],int Size)
{
for(int L= 0; L<Size; L++)
if ( L<Number)
ARR[L]+=L;
else
ARR[L]*=L;
void Show(int ARR[],int size)
{
for( int L=0; Ksize; L++)
( L%2 !=0)?cout<<ARR[L]<<"#" :
cout<<ARR[L]<<endl;
}
void main()
{
int Array[]={30,20,40,10,60,50};
ChangeArray(3, Array, 6);
Show(Array, 6);
} Delhi 2011
Answer:
Output
30 21#42 30#240 250#
Question 44:
Find the output of the following program
#include<iostream.h>
struct Dress
{
int size,cost,Tax,Total_Cost;
};
void change(Dress &S, int Flag=1)
{
if(Flag ==1) S.cost +=20;
S.Total_Cost=S.cost+
S.cost*S.Tax/100;
}
void main()
{
Dress S1=(40, 580, 8},S2 ={34, 550, 101; change(S1) ;
cout<<S1.size<<","<<S1.cost . <<","<<S1,Total_Cost<<endl;
change(S2, 2);
cout<<S2.size<<" ,''<<S2.cost <<", "<<S2 .Total_Cost<<endl ;
} Delhi 2011C
Answer:
Output
40, 600, 648 34, 550, 605
Question 45:
Find the output of the following program
#include<iostream.h>
struct POINT
{
int x, y, z;
};
void StepIn(POINT &P, int Step=1)
{
P.x+=Step;
P.y-=Step;
P.z+=Step;
}
void main()
{
POINT P1={15, 25, 5},
P2={10, 30, 201;
StepIn(P1) ;
StepIn(P2, 4);
cout<<Pl.x<<', '<<Pl.y<<', '
<<P2.z<<endl;
} Delhi 2010
Answer:
Output
16, 24, 6 14, 26, 24 26, 14, 36
Question 46:
Find the output of the following program
#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 Move0ut(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} ;
Moveln (T1) ;
Move0ut(T2, 5);
cout<<Tl.x<<', '<<Tl.y<<', ' <<Tl.z<<endl;
cout<<T2.x<<', '<<T2.y<<', '<<T2. z<<endl ;
Moveln (T2, 10);
cout<<T2.x<<' , '<<T2.y<<', ' <<T2.z<<endl ;
} All India 2010
Answer:
Output
11, 19, 4 25, 15, 35 35, 5, 25