C++ CBSE Class 12 Chapter 1 Part 3 Questions 31 to 45

Indian CBSE C++ Sumita Arora Chapter 1 sloved Part 3. Here questions number 31 to 45 are solved.


31. Give the output of the following:

(i)

#include<iostream.h>

void Execute(int &X,int Y=200)
{
int TEMP=X+Y;
X+=TEMP;

if(Y!=200)
 cout<<TEMP<<X<<Y<<endl;
}

void main()
{
int A=50,B=20;
Exwcute(B);

cout<<a<<B<<endl;

Exwcute(A,B);

cout<<A<<B<<endl;
}

(ii)

#include<iostream.h>
void Execute(int &B,int C=200)
{
int TEMP=B+C;
B+=TEMP;

if(C==100)
 cout<<TEMP<<B<<C<<endl;
}

void main(){
int M=90,N=10;
Exwcute(M);

cout<<M<<N<<endl;
Exwcute(M,N);

cout<<m<<N<<endl;
}
Answer

(i)Output:

50240
290340240
340240

(ii)Output:

38010
77010


32. Find the output of the following program:

#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()
{
char MyText[]="ApEACeDriVE";

MyCode(MyText,'@');

cout<<"NEW TEXT:"<<MyText<<endl;
}
Answer

(i) Output:

NEW TEXT:@@e@ccddI@e


33. Find the output of the following program:

#include<iostream.h>

struct Package
{
int Length,Breadth,Height;
};

void Occupies(Package M)
{
cout<<M.Length<<"x"
 <<M.Breadth<<"x";

cout<<M.Height<endl;
}

void main()
{
Package P1={100,150,50},P2,P3;

++P1.Length;

Occupies(P1);
P3=P1;

++P3.Breadth;

P3.Breadth++;
Occupies(P3);

P2=P3;
P2.Breadth+=50;

P2.Height--;

Occupies(P2);
}
Answer

Output:

101x150x50
101x152x50
101x202x49


34. What values will be assigned to the variables ua, ub, uc and fail after the execution of the following program segment:

void main()
{
int i=0,ua=0,ub=0,uc=0,fail=0;

while(i<=5)
{
 switch(i++)
 { 
 case 1:
 case 2: ++ua;
 case 3:
 case 4: ++ub;
 case 5: ++uc;
 default: ++fai;
 } //switch
} //while

cout<<ua<<endl;
cout<<ub<<endl;
cout<<uc<<endl;
cout<<fail<<endl;
}
Answer

The values assigned to the variable ua, ub, uc and ‘fail’ are:

ua = 2
ub = 4
uc = 5
fail = 6


35. What is wrong with this code:

cout<<" " Enter n:";
cin>>n;

if(n<0)
 cout<<"That is negative."
   <<"Please enter again"<<endl;
cin>>n;
else
 cout<<"O.K. n="<<n<<endl;
Answer

There are following errors in the above code:
 
(i) There should be only one double quotation before the ‘Enter’ in the first statement.
 
(ii) There are more than one statement under ‘if’ statement without the braces which is not valid.


36. Identify the possible error(s) in the following code fragment. Discuss the reason(s) of error(s) and correct the code:

cin>>i >> j;

while(i<j)
 cout<,i*j;
 i++;
Answer

The correct code is:

cin>> i >>j ;
 
while(i<j)
{
 cout<<i*j;
 i++;
}

37. Given the following code fragment:

i=2;
start:

cout<< i ;
i+=2;

if(i<51) goto start;
 cout<<"\nThank You";

Rewrite the above code using a while loop.

Answer
int i=2;

while(i<51)
{
 cout<<i;
 i+=2;
}

cout<<"\nThank You";

38. Is it necessary to include a header file in a program? If it is not done, what happens?

Answer

No, it is not necessary to include a header file in a C++ program at all. The header file is included only when the program requires functions, variables or const from the header file. For instance, to use the function ‘sin’ or any mathematical functions we must include the header file <cmath> .


39. Name the header files that shall be needed for the following code:

void main()
{
char Text[]="Computer";
 cout<<setw(15)<<Text;
}
Answer

(i) iomanip.h for setw()
 
(ii) iostream.h for cout


40. Briefly describe the importance of iostream.h file.

Answer

iostream.h is an important header file. This header file includes functions and objects like cin and cout to perform basic input an output operations. Without cin and cout you cannot input or output any values from the console screen. So whenever we write C++ program we always include the header first.


41. How do the following two statements differ in operation?

cin >>ch ;

cin.get(ch);
Answer

The difference between cin>>ch and cin.get(ch) is that when >> operator is used, the white spaces (e.g., tabs), spaces etc. and new-line characters (‘\n’ ,’\r’) are ignored whereas it is not so with cin.get(ch).


42. What is an array? What is the need for array?

Answer

An array is a collection of variables of the same type that are stroe in sequentail manner i.e. the values lie in consecutive to one another.

Array is a collection of data of same type. This means arrays are useful for storing many values of the same type. Suppose your program requires 10 variables of the same type, now you can declare 10 variables of different names or if you use an array you can declare just one array. Which do you think is more efficient and easier to
maintain 10 variables or one variable? of course, one variable is easier to maintain. Hence, array makes our program more maintainable.


43. What do you understand by two-dimensional arrays? State some situation that can be easily represented by two- dimensional arrays.

Answer

Two-dimensional array: A two-dimensional array is an array in which each element is an array. For instance, an array A[m][n] is an M by N table with M rows and N columns containing M x N elements. Two-dimensional arrays are used to represent tables, matrices, etc.

For example, below marks table can be represented easily by 2D array :

1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

Supose the above matrix is represented by an array ‘arr’ then it can be represented by arr[i][++sum] ,substitue ‘i’ with the value 1 to 4 for the rows and ‘sum=i+1’ at first and iterate the column three times.


44. Can you think of a difference between an array of strings and other two-dimensional arrays? What is it? Support your answer with examples.

Answer

Array of strings:

Array of a string is used to store a string. The first index determines the number of strings and the second index determines the maximum length of each string.


int a[2][3];
int i,j;

for(i=0;i<2;i++)
{
 for(j=0;j<3;++j)
 {
 cout<<"Enter element:";
 cin>>a[i][j];
 }
}

Two-dimensional array:

Two-dimensional array is used to store numeric value or matrices. The first index determines the number of rows and the second index determines maximum columns.

char string[3][31];
int i;

cout<<"Enter 3 strings:";

for(i=0; i<3; i++)
  cin.getline(string[i],31);

45. If an array is initialized at the time of declaration, what thing one must bear in mind?

Answer

The general form of array initialization is as shown below:
 
data_type array_name[size] = { values };
or
data_type array_name[]={ values};

If an array is initialized at the time of declaration,we must bear in mind the following things:
 
a) The initializers must be the same type as the data_type of the array.
 
b) The number of initializers must be must one lessert than the ‘size’ of the array.




Leave a Reply

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