C++ CBSE Class 12 Chapter 1 Part 2 Questions 16 to 30

Indian CBSE class 12 C++ Sumita Arora Chapter 1 Solved Part 2 (Question 16 to 30) .


16. Determine the data type of the expression.

If p is int, r is a float, q is a long and s is double.

The data type is double type as double is the larger data type.


17. What are the outputs of following two codes fragments? Justify your answer.

//version 1
int f = 1, i=2;

while(++i< 5)
 f * =i;

cout<<f;
:
:
//version 2
int f = 1, i= 2;

do{ 
 f * =i;
}while (++i< 5);
cout<< f;
:

The output of the first version is 12.
 
The output of the second version is 24.


18. Will the following program execute successfully? If not, state the reason(s):

(i)

#include<stdio.h>

void main() {

int s1,s2,num;
s1=s2=0;

for(x=0;x<11;x++)
{
 cin<<num;

 if(num>0)s1+=num;

 else s2=/num;
}
cout<< s1 <<s2;
}

(ii)

#include<stdio.h>
void main(){

int x,sum=0;

cin<<n;

for(x=1;x<100;x+=2)
 if x%2==0
 sum+=x;

cout<<"SUM=">>sum;

Answer

(i) The first program will encounter compile-time errors due to the following reasons:
a)The variable ‘x’ is not declared.
b)With cin statement uses the ‘<<’ symbol instead of ‘>>’.
c)Invalid semicolon at the end of ‘if’ statement.

(ii) The second program will also encounter compile time errors due to the following reasons:
a) The variable ‘n’ has not been declared.
b) There should be a parenthesis in ‘if’ statement.
c) ‘>>’ is used with cout statement instead of the ‘<<’ operator.


19. Find the syntax error(s), if any, in the following program:

(i)

#include<iostream.h>

main(){
int x[5], *y, z[5]

for(i=0;i<=5;i++)
{
 x[i]=i;
 z[i]=i+3;
 y=z;
 x=y;
}

}

(ii)

#include<iostream.h>

void main(){
int x, y;
cin>> x ;

for(x=0;x<5;++x)
cout y else cout<< x << y;
}

<b>(iii) </b>

#include<iostream.h>

void main(){

int R;W=90;

while W>60
{
 R=W-50;
 switch(W)
 { 
  20:cout<<"Lower Range"<<endl;
  30:cout<<"Middel Range"<<endl;
  20:cout<<"Higher Range"<<endl;
 }
}

}

(iv)

#include<iostream.h>

void main()
{
int X[]={60, 50, 30, 40}, Y;
Count=4;

cin>>Y;

for(I=Count-1; I>=0 , I--)
 switch(I)
 { 
 case 0:
 case1:
 case 2: cout<< Y*X[I] << endl; break;
 case 3: cout>> Y+X[I] ; break;
 }
}

(v)

#include<iostream.h>

void main()
{
int P[]={90, 10, 24, 15}, Q;

Number=4;
Q=9;

for(int I=Number-1; I>=0, I--)

switch(I)
{ 
 case 0:
 case 1:
 case 2:cout<<P[I]*Q<<endl;
 break;
 case 3:cout<<P[I]+Q;
 break;
 }
}
Answer

(i)

The first program has the syntax error(s):
a) The variable ‘i’ is not declared.
b) There should be semicolon after the declaration statement if int x[5].

(ii)

There is a syntax error in cout statement.

(iii)

The third program has the following syntax error(s):
a) There should be a ‘,’ between R and W instead of ‘;’ in declaration statement.
b) There should be a parenthesis in ‘while’ statement.
c) There is missing a use of ‘case’ keyword in switch statement.

(iv)

#include<iostream.h>

void main(){
int X[]={60, 50, 30, 40}, Y, Count=4;

cin>>Y;

for(int I=Count-1;I>=0;I--)
 switch(I)
 { 
 case 0:
 case 1:
 case 2:cout<<Y*X[I]<<endl;break;
 case 3:cout<<Y+X[I];break;
 }
}

(v)

#include<iostream.h>

void main()
{
int P[]={90, 10, 24, 15},Q,Number=4;

Q=9;

for(int I=Number-1;I>=0;I--)
 switch(I)
 { 
 case 0:
 case 1:
 case 2:cout<<P[I]*Q<<endl;
 break;
 case 3:cout<<P[I]+Q;
 break;
 }
}

20. Rewrite the following program after removing all the syntactical error(s), if any. Underline each correction.

#include<iostream.h>

void main()
{

Present=25, Past=35;

Assign(Present;Past);

Assign(Past);
}

void Assign(int Default1,Default2=30)
{
 Default1=Default1+Default2;
 
cout<<Default1>>Default2;
}
Answer
#include<iostream.h>

void Assign(int Default1,int Default2=30);

void main()
{
clrscr();

int Present=25,Past=35;

Assign(Present,Past);

Assign(Present);

getch();
}

void Assign(int Default1,int Default2)
{
Default1=Default1+Default2;

cout<<Default1<<Default2;
}

21, Given the following code fragment:

if(a==0)
 cout<<"Zero";

if(a==1)
 cout<<"One";

if(a==2)
 cout<<"Two";

if(a==3)
 cout<<"Three";

Write an alternative code (using if) that saves on number on compressions.

Answer
#include<iostream.h>

void main()
{
int a;
cout<<"enter a:";

cin>>a;

if(a==0)
 cout<<"Zero";
else if(a==1)
 cout<<"One";
else if(a==2)
 cout<<"Two";
else if(a==3)
 cout<<"Three";
else
 cout<<"other than 0 , 1 , 2 , 3";
}

22. Write the names of the header files, which is/are essentially required to run/execute the following C++ code:


#include<iostream.h>

void main()
{
char CH,Text[]="+ve Attitude" ;

for(int I=0;Text[I]!='\0';I++)
 if(Text[I]=='')
  cout<<endl;
 else
 {
 CH=toupper(Text[I]);
 cout<<CH;
 }
}
Answer

<ctype.h>


23. Find the output of the following program:

#include<iostream.h>

void main()
{
int A=5,B=10;

for(int I=1;I<=2;I++)
{
cout<<"Line1"<<A++
 <<"&"<<B-2<<endl;
cout<<"Line2"<<++B
 <<"&"<<A+3<<endl;
 }
}
Answer

Output:

Line15&8
Line211&9
Line1679
Line212&10


24. Rewrite the following program after removing all the syntactical 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=20)
{
Arg1=Arg1+Arg2
cout<<Arg1>>Arg2;
}
Answer
#include<iostream.h>

void Callme(int Arg1,int Arg2=20);

void main()
{
int One=10,Two=20; //Underline under int

Callme(One;Two);
Callme(Two);
}

void Callme(int Arg1,int Arg2=20)
{
Arg1=Arg1+Arg2

cout<<Arg1 << Arg2; //the '<<' after Arg1 is the underline part
}

25. Rewrite the following program after removing all the syntactical error(s), if any. Underline each correction.

#include<iostream.h>

typedef char[80];

void main()
{
String S="Peace";
int L=strlen(S);

cout<<S<<'has'<<L
<<'characters'<<endl;
}
Answer
#include<iostream.h>

#include<string.h> //Underline this wholeline

typedef char String[80]; //underline the String

void main()
{
String S="Peace";
int L=strlen(S);

cout<<S<<"has"<<L
<<"characters"<<endl;
}

26. Find the output of the following program:

#include<iostream.h>

void SwitchOver(int A[],int N,int split)
{
for(int K=0;K<N;K++)
 if(K<Split)
   A[K]+=K;
 else
  A[K]*=K;
}

void Display(it A[],int N)
{ 
for(int K=0; K<N; K++)
 (K%2==0)?cout<<A[K]
 <<"%":cout<<A[K]<<endl;
}

void main()
{
int H[]={30,40,50,20,10,5};

SwitchOver(H,6,3);

Display(H,6);
}
Answer

Output:

30%41
52%60
40%25


12. (a) 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 POINT=5,Number;

for(int I=1;I<=4;I--)
{
 Number=LOW+random(POINT);
 cout<<Number<<":";
 POINT--;
}
}

(i) 29:26:25:28: (ii) 24:28:25:26: (iii) 29:26:24:28: (iv) 29:26:25:26:

Answer

(iv) 29: 26: 25: 26: is correct

Justification:

The only option that satisfied the values as per the code is option (iv) because when:

I POINT Number
Minimum Maximum
1 5 25 29
2 4 25 28
3 3 25 27
4 2 25 26

(b) 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=Pints;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#

Answer

103#102#101#100# is correct answer.

28. Go through C++ code show below, and find out the possible output or outputs from the suggested Output Option (i) to (iv).

Also, write the least value and highest value, 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<<C<<"*";
}

(i) 20*21*22*23*24*25 (ii) 22*23*24*25 (iii) 23*24* (iv) 21*22*23*24*25

Answer

(ii) 22*23*24*25 is correct answer. Minimum possible value = 20, Maximum possible value = 24


29. (a) Find the output of the following program:

#include<iostream.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;
}
Answer

Output:

Oppe$0GIC


(b) Find the output of the following program:

#include<iostream.h>

void main()
{
int First=25,Sec=30;

for(int I=1;I<=2;I++0
{
 cout<<"Output1="<<First++
  <<"&"<<Sec+5<<endl;
 cout<<"Output2="<<-Sec
  <<"&"<<First-5<<endl;
}

}
Answer

Output:

Output1=25&35
Output2=-30&21
Output1=26&35
Output2=-30&22


(c) Find the output of the following program:


#include<iostream.h>
#include<ctype.h>

void Encode<char Info[],int N);

void main()
{
char Memo[]="Justnow";
Encode(Memo,2);

cout<<Memo<<endl;
}

void Encode<char Info[],int N)
{
for(int I=0;Info[I]!='\0';I++)
 if(I%2==0)
   Info[I]=Info[I]-N;
 else if(islower(Info[I]))
   Info[I]=toupper(Info[I]);
 else
   Info[I]=Info[I]+N;
}
Answer

Output:

HUqT10u


(d) 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 MoveOut(THREE_D &T,it 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;
}
Answer

Output:

11,19,6
25,15,35
35,5,45


30. Give the output of the following program:

(i)

void main(){

char *p="Difficult";

char c;

c=++*p++;

printf("%c",c);
}
Answer

Output:

E


(ii)

 
#include<iostream.h>
static int i=100;

void abc()
{
static int i=8;
cout<<"first="<<i;
}

main()
{
static int i=2;
abc();
cout<<"second="<<i<<endl;
}
Answer

Output:

first=8 second=2


(iii)

#include<iostream.h>

void Print(char *p)
{
p="Pass";
 cout<<"Value is:"<<p<<endl;
}

void main()
{
char *q="Best of Luck";
Printf(q);
cout<<"New value is:"<<q;
}
Answer

Output:

Value is: Pass
New value is: Best of Luck




Leave a Reply

Your email address will not be published. Required fields are marked *