MENU

Chapter 1 C plus plus Revision Tour Solutions

Question - 31 : -
Explain conditional operator with suitable example ?

Answer - 31 : -

Conditional operator is also known as ternary operator because it requires three operands and can be used to replace simple if- else code. It is used to check the condition and exucute first expression if condition is true: else execute other. .
Syntax :
Conditional expression ?
Expression 1 : Expression 2 :
Explanation : If the conditional expression is true then expression I executes otherwise expression 2 executes.
Example :
int y = 10, x;
x = y>10? 1:0;
cout<
Output : 0

Question - 32 : -
Find syntax error(s), if any, in the following program : (Assuming all desired header file(s) are already included)

typedef char String[80]; 
void main 
{
String S; int L; 
for(L=0;L<26;C++)
S [L]=L+65; 
cout<
}

Answer - 32 : -

The given code has following errors:

  1. Parenthesis () are missing after void main.
  2. In for statement, the update expression should be L+ + but it is written as C + + .

Question - 33 : -
Read the following C++ code carefully and find out, which out of the given options
(i) to (iv) are the expected correct outputs) 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<
}
(i)100#
(ii)50#200#
(iii)100#50#200#
(iv)100#50

Answer - 33 : -

(iii) 100#50#200#
Max value : 350
Min value : 170

Question - 34 : -
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<
}
(i) 200#150#
(ii) 150#20#
(iii) 150#20#250#
(iv) 20#250#

Answer - 34 : -

(iv) 20#250#
Max value : 470
Min value : 420

Question - 35 : -
Which C++ header file (s) will be include to run/execute the following C+ + code ?

void main()
int Last=26.5698742658; 
cout<
}

Answer - 35 : -

iostream.h
iomanip.h

Question - 36 : -
Find correct identifiers out of the following, which can be used for naming variable,
constants or function in a C++ program :
For, While, Float, new, 2ndName, A%B, Amount 2, Counter.

Answer - 36 : -

While, float Amount 2, _Counter.

Question - 37 : -
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() 
{
int Begin=3,Stop;
for(int Run=l;Run<4;Run++)
{
Stop=random(Begin)+6; 
cout<
}
}
(i) 36*46*57*
(ii) 37*46*56*
(iii) 37*48*57*
(iv) 35*45*57*

Answer - 37 : -

The correct output will be:
(i) 36*46*57*
Maximum Value of Stop=8
Minimum Value of Stop=6

Question - 38 : -
Find the output:
int A [2] [3] = {{1,2,3}, {5,6,7}};
for (int i = 1; i<2; i+ +)
for (int j = 0; j<3; j + +)
cout << A [i] [j] <

Answer - 38 : -

5
6
7

Question - 39 : -
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 :

typedef char STRING[80]; 
void main()
{
STRING Txt[]="We love peace"; 
int count=0; 
while(Txt[count]! = ’\0 ')
if(isalpha(Txt Count)
Txt[count++] = 1@'; 
else
Txt[count++] = '#'; 
puts(Txt);
}

Answer - 39 : -

,

Question - 40 : -
Explain in brief the purpose of function prototype with the help of a suitable example.

Answer - 40 : -

The Function prototype serves the following purposes :

It tells the return type of the data that the function will return.
It tells the number of arguments passed to the function.
It tells the data types of each of the passed arguments.
Also it tells the order in which the arguments are passed to the function.
#include
int timesTwo(int num);    // function
prototype int main()
{
int number,response; 
cout<<"Please enter a number:"; 
cin>>number;
response = timesTwo(number);    //
function call
cout<<"The answer is"<
return 0;
}
//timesTwo function 
int timesTwo (int num)
{
int answer;    //local variable 
answer = 2*num; 
return(answer);
}

×