MENU

Chapter 1 C plus plus Revision Tour Solutions

Question - 11 : -
Define Macro with suitable example.

Answer - 11 : -

Macro are preprocessor directive created using # define that serve as symbolic constants.
They are created to simplify and reduce the amount of repetitive coding.
For instance,
#define max (a, b) a > b ? a : b
Define the macro max, taking two arguments a and b. This macro may be called like any
C function with similar syntax. Therefore, after preprocessing.
A = max (x, y);
BecomesA = x>y ?x:y;

Question - 12 : -
Write the output of the following C+ + program code :
Note : Assume all required header files are already being included in the program.

class Calc
{
char Grade;
int Bonus; 
public:
Calc (){Grade = 'E'; Bonus = 0;} 
void Down(int G)
{
Grade(-) = G;
}
void Up(int G)
Grade += G;
Bonus ++;
}
void Show()
{
cout<
 }
};
void main()
{
Calc c;
C.Down(2);
C.Show();
C.Up(7);
C.Show();
C.Down(2);
C.Show();
}

Answer - 12 : -

C#0
J#1
H#1

Question - 13 : -
Rewrite the following program after removing any syntactical errors. Underline each correction made.

#include
void main()
int A[10];
A = [3, 2, 5, 4, 7, 9, 10]; 
for(p = 0; p <= 6; p++)
if(A[p]%2=0)
int S = S + A[p]];
}
cout<
}

Answer - 13 : -

#include 
void main()
int A[10] = {3, 2, 5, 4, 7, 9, 10}; 
int S = 0, p;
for(p = 0; p <= 6; p++)
if(A[p]%2 == 0)
S = S + A[p]; 
cout<
}

Question - 14 : -
Observe 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 porgram.

#Define float Max = 70.0;
Void main)
{
int Speed 
char Stop = 'N'; 
cin >> Speed; 
if Speed > Max 
Stop = 'Y' ; 
cout <
}

Answer - 14 : -

#define float Max 70.0 //Error 1,2,3 
void main()      //Error 4
{
int Speed;          //Error 5
char Stop = 'N';
cin>>Speed; 
if(Speed>Max)      //Error 6
Stop = 'Y';
cout<
}

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 Position(int & C1, int C2 = 3)
{
C1+ = 2;
C2+ = Y; 
void main()
int P1 = 20, P2 = 4; 
position(P1); 
cout<
position(P2, P1); 
cout<
}

Answer - 15 : -

22,4
22,6

Question - 16 : -
Study the following program and select the posssible output(s) from the options (i) to (iv) following it.
Also, write the maximum and the minimum. values that can be assigned to the variable NUM.
Note :
— Assume all required header files are already being included in the prorgram.
— random(n) function generates an integer between 0 and n -1.

void main()
{
randomize(); 
int NUM;
NUM = random(3)+2;
char TEXT[] = "ABCDEFGHIJK";
for(int I=1; I <= NUM; I++)
{
for(int J = NUM; J <= 7;J++) 
cout<
cout<
}
}
(i)FGHI    
FGHI     
FGHI  
FGHI
(ii)BCDEFGH
BCDEFGH
(iii)EFGH
EFGH
EFGH
EFGH
(iv)CDEFGH

Answer - 16 : -

(iii) and (iv)
Minimum value of NUM = 2
Maximum value of NUM = 4

Question - 17 : -
Rewrite the following C++ program after removing all the syntactical errors (if any),
underlining each correction :

includeciostream.h>
#define PI=3.14
void main()
float r,a;
cout<<'enter any radius'; 
cin>>r;
a=PI*pow(r,2); 
cout<<"Area="<
}

Answer - 17 : -

#include // Error 1: Declaration Sytax error 
#include
#define PI=3.14      // Error 2: Macro declaration does not allow assignment 
void main()
{
float r,a;     // Error 3:Undefined Symbol a
cout<<"enter any radius";     //Error 4 : Character Constant
must be one or two characters long 
cin>>r;
a=PI*pow(r,2)   //Error 5 : Expression Syntax
cout<<"Area="<
}

Question - 18 : -
Rewrite the following C++ code after removing all the syntax error(s), if presents in the code. Make sure that you underline each correction done by you in the code.
Important Note:

Assume that all the required header files are already included, which are essential to run this code.
The corrections made by you do not change the logic of the program.
typedef char STR[80]; 
void main()
{
STR Txt; 
cin (Txt); 
cout<
}

Answer - 18 : -

typedef char STR[80]; 
void main()
{
STR Txt; 
gets(Txt); 
cout<
}

Question - 19 : -
Rewrite the following C+ + code after removing all the syntax error(s), if present in the code. Make sure that you underline each correction done by you in the code.

Important Note :

Assume that all the required header files are already included, which are essential to run this code.
The corrections made by you do not change the logic of the program.l
typedef char STRING[50]; 
void main()
{
City STRING;
cin (City);
cout<
}

Answer - 19 : -

typedef char STRING[50]; 
void main()
{
STRING City;
gets(City);               
×