MENU

Chapter 1 C plus plus Revision Tour Solutions

Question - 41 : -
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.

Answer - 41 : -

Call By Value : In call by value method, the called function creates its own copies of original values sent to it. Any changes that are made, occur on the function’s copy of values are not reflected back to the calling function.
Call By Reference : In call by reference method, the called function accesses and works with the original values using their references. Any changes that occur, takes place on the original values and are reflected back to the calling code.
Example:

//using call by reference

#include

void swap(int &, int &);

int main()

{

int a=10,b=20;

swap(a,b);

cout<

return 0;

}

void swap(int &c, int &d)

{

int t;

t=c;

c=d;

d=t;

}

output :

20 10

//using call by value

#include

void swap(int, int);

int main()

{

int a=10,b=20;

swap(a,b):

cout<

return 0;

}

void swap(int c, int d)

{

int t;

t=C;

C=d;

d=t;

}

output:

10 20

Question - 42 : -
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.

int find(int a[],int n)
{
int s=a[0] ;
for(int x=l;x
if(a[x]>s)
a[x]=s;  
return(s);
}

Answer - 42 : -

int find(int a[],int n)
{
int s=a[0];
for(int x=l;x
{
if(a[x]<=s)  
s=a[x];   
}
return(s);

Question - 43 : -
What is the benefit of using default parameter/ argument in a function ? Give a suitable example to
illustrate it using C++ code.

Answer - 43 : -

The benefit of using default arguments is that whenever there is no value supplied to the parameter
within the program, its default value can be used.
Example :

#include 
#include
int main()
int a,b=20,c; 
a=change(c,b);    
return a ;
}
int change(int a=10,b)
{
int temp; 
temp=a; 
a=b;
return temp; 
}
In the above example, change () assigns a value 10 to a because it is defined by default.

Question - 44 : -
Write a prototype of a function named percent, which takes an integer as value parameter & return a float type value. The parameter should have default value of 10.

Answer - 44 : -

float percent(int a=10) ;

Question - 45 : -
Observe the following C++ code carefully and write the same after removing syntax errors.

#define change(A,B)2*A+B;
void main()
{
float X,Y,F; 
cin>>X>>Y;
F=Change(X,Y);
cout<<"result:<
}

Answer - 45 : -

The corrected code is :

#define change(A,B)2*A B; 
void main()
{
float X,Y,F; 
cin>>X>>Y;
F=change(X,Y);  
cout<<"result:<
}

Question - 46 : -
Study the following C++ program and select the possible output(s) from it : Find the maximum and minimum
value of L.

#include
#include
#include 
void main()
{
randomize();
char P[]="C++PROGRAM";
long L;
for(int 1=0;P[I]!='R';I++)
{
L=random(sizeof(L))+5; 
cout<
}
}
(i) R-P-O-R   
(ii) P-0-R-+-
(iii) O-R-A-G
(iv) A-G-R-M-

Answer - 46 : -

c

+

+

P

R

O

G

R

A

M

0

1

2

3

4

5

6

7

8

9

Possible Values:
Possible Output is (iii) O-R-A-G
Minimum value of L = 5
Maximum Value of L = 8

Question - 47 : -
Find output of the following program segment :

#include
#include
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]=='N'?Msg[cnt]=='n'?Msg[c nt]==' ')
Msg[cnt]=CH; 
else
if(cnt%2==0)
Msg[cnt]=toupper(Msg[cnt]); 
else
Msg[cnt]=Msg[cnt-1];
}
void main()
{
char MyText[]="Input Raw";
Mycode(MyText,'@'); 
cout<<"NEW TEXT:"<
}

Answer - 47 : -

I@PPT@RRW

Question - 48 : -
Observe the following program and find out, which output(s) out of (i) to (iv) will not expected from the program ? What will be the minimum and the maximum value assigned to the variable chance ?

#include
#include 
void main()
{
randomize()
int Game[]=(10,16),P; 
int Turns=random(2)+5; 
for(int T=0;T<=2;T++)
{
P=random(2);
Cout<
 }
 }
(i) 15*22*  
(ii) 22*16* 
(iii) 16*21*
(iv) 21*22*

Answer - 48 : -

Option 1:
None of the outputs are correct Variable named chance does not exist in the program, hence no minimum and maximum values for it.
OR
Option 2 :
Error in question OR
Option 3: Assuming
Cout<
 
 
Since out of the above possible outputs only option (ii) is correct, hence The outputs not ! expected from the program are (i) (iii) and (iv)

Question - 49 : -
Find the output of the following program :

#include 
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<
in(b,c,a);
cout<
out(a,b,c);
cout<
}

Answer - 49 : -

20#300#10#
620@300@10@
620$300$3000$

Question - 50 : -
Find the output of the following program :

#include
void ChangeArray(int number, int ARR[], int Size)
{
for(int L=0;L
if(L
ARR[L]+=L;
else
ARR[L]*=L;
}
void Show (int ARR[], int Size)
{
for(int L=0;L
(L%2==0)? 
×