Friday

Important Question Answers - Computer Science with C++ - Question no. 3

 

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

Wednesday

Important Questions for Board Examination - Flow of control

 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 ParameterFormal 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

Friday

Function Overloading Question Answers - Computer Science with C++ - Class 12

 1. How does the compiler interpret more than one definitions having same name? What steps does it follow to distinguish these? 

Ans. 

The compiler will follow the following steps to interpret more than one definitions having same name: (i) if the signatures of subsequent functions match the previous function’s, then the second is treated as a redeclaration of the first. (ii) if the signature of the two functions match exactly but the return type differ, the second declaration is treated as an erroneous re-declaration of the first and is flagged at compile time as an error. (iii) if the signature of the two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded.

2. Discuss the benefits of constructor overloading. Can other member function of a class be also overloaded? Can a destructor be overloaded? What is your opinion? 

Ans. 

Constructor overloading are used to increase the flexibility of a class by having more number of constructors for a single class. By this we can initialize objects more than one way. Yes, Other member function of a class can also be overloaded. A destructor cannot be overloaded.

3. Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used

in the following program jointly illustrated by the functions [I] to [IV]:

#include<iostream.h>

void Line() //Fuction [I]

{ for(int L=1;L<=80;L++) cout<<"-";

cout<<endl;

}

void Line(int N) //Fuction [II]

{ for(int L=1;L<=N;L++) cout<<"*";

cout<<endl;

}

void Line(char C,A,int N) //Fuction [III]

{ for(int L=1;L<=N;L++) cout<<C;

cout<<endl;

}

void Line(int M,int N) //Fuction [IV]

{ for(int L=1;L<=N;L++) cout<<M*L;

cout<<endl;

}

void main()

int A=9,B=4,C=3; 

char K='#'; 

Line(K,B); 

Line(A,C); 

Ans. 

Output: # # # # 9 18 27 

The name of feature of Object Oriented Programming used in the above program jointly illustrated by the functions [I] to [IV] is known as ‘function overloading’.


4. Write definitions for two versions of an overloaded function. This function’s 1st version sum() takes an argument, int array, and returns the sum of all the elements of the passed array. The 2nd version of sum() takes two arguments, an int array and a character (‘E’ or ‘O’). If the passed character is ‘E’, it returns the sum of even elements of the passed array and is the passed character is ‘O’, it returns the sum of odd elements. In case of any other character, it returns 0 (zero).

Ans:

int sum(int a[])

{

int n,sum=0;

cout<<"Enter n:";

cin>>n;

for(int i=0;i<n;i++)

{

sum=sum+a[i];

}

return sum;

}

//

int sum(int a[],char c)

{

int even=0,odd=0;

switch(c)

{

case 'E':

for(int i=0;i<5;i++)

{

if(a[i]%2==0)

{

even=even+a[i];

}

return even; 

case 'O': 

for(int j=0;j<5;j++) 

if(a[j]%2!=0) 

odd=odd+a[j]; 

}

 return odd; 

}