Write the type of C++ tokens (keywords and user-defined identifiers) from the following :
(i) For
(ii) delete
(iii) default
(iv) Value
Anil typed the following C++ code and during compilation, he found four errors as follows:
(i) Function strlen should have a prototype
(ii) Undefined symbol cout
(iii) Undefined symbol endl
(iv) Function getchar should have a prototype
On asking his teacher told him to include necessary header files in the code. Write the names of the header files, which Anil needs to include, for successful
compilation and execution of the following code :
void main()
{
char S[] = 'Hello';
for(int i = 0; i<strlen(S); i++)
S[i] = S[i]+1;
cout<<S<<end1;
getchar();
}
Rewrite the following C++ code after removing any/all syntactical errors with each correction underlined.
Note: Assume all required header files are already being included in the program.
void main()
{
cout<<'Enter an integer';
cin>>N;
switch(N%2)
case 0 cout<<'Even'; Break;
case 1 cout<<'Odd'; Break;
}
Find and write the output of the following C++ program code :
#define Big(A,B) (A>B)?A+1:B+2
void main()
{
char W[] = 'Exam';
int L=strlen(W);
for(int i=0; i<L-1; i++)
W[i] = Big(W[i],W[i+1]);
cout<<W<<endl;
getch();
}
Find and write the output of the following C++ program code:
Note: Assume all required header files are already being included in the program.
void main()
{
int A[]={10,12,15,17,20,30};
for(int i = 0; i<6; i++)
{
if(A[i]%2==0)
A[i] /= 2;
else if(A[i]%3==0)
A[i] /= 3;
if(A[i]%5==0)
A[i] /=5;
}
for(i = 0; i<6; i++)
cout<<A[i]<<'#';
getch();
}
Look at the following C++ code and find the possible output(s) from the options (i) to (iv) following it. Also, write the maximum values that can be assigned to each of the variables R and C.
Note:
● Assume all the required header files are already being included in the code.
● The function random(n) generates an integer between 0 and n-1
void main()
{
randomize();
int R=random(3),C=random(4);
int MAT[3][3] = {{10,20,30},{20,30,40},{30,40,50}};
for(int I=0; I<R; I++)
{
for(int J=0; J<C; J++)
cout<<MAT[I][J]<<' ';
cout<<endl;
}
}
(i) | (ii) |
10,20,30 20,30,40 30,40,50 |
10,20,30 20,30,40 |
(iii) | (iv) |
10,20 20,30 |
10,20 20,30 30,40 |
Differentiate between private and public members of a class in the context of Object Oriented Programming. Also, give a suitable example illustrating accessibility/non-accessibility of each using a class and an object in C++.
Observe the following C++ code and answer the questions (i) and (ii).
Note: Assume all necessary files are included.
class EXAM
{
long Code;
char EName[20];
float Marks;
public:
EXAM() //Member Function 1
{
Code=100;strcpy(EName,'Noname');Marks=0;
}
EXAM(EXAM &E) //Member Function 2
{
Code=E.Code+1;
strcpy(EName,E.EName);
Marks=E.Marks;
}
};
void main()
{
___________________ //Statement 1
___________________ //Statement 2
}
(i) Which Object-Oriented Programming feature is illustrated by the Member Function 1 and Member Function 2 together in the class EXAM?
(ii) Write Statement 1 and Statement 2 to execute Member Function 1 and Member Function 2 respectively.
(i) 'Polymorphism or Constructor overloading or Function overloading' feature is illustrated by the member Function 1 and Member Function 2 together in the class Exam.
(ii) EXAM E1; //Statement 1
EXAM E2(E1); //Statement 2
Write the definition of a class RING in C++ with the following description:
Private Members
- RingNumber // data member of integer type
- Radius // data member of float type
- Area // data member of float type
- CalcArea() // Member function to calculate and assign
// Area as 3.14 * Radius*Radius
Public Members
- GetArea() // A function to allow user to enter values of
// RingNumber and Radius. Also, this
// function should call CalcArea() to calculate
// Area
- ShowArea() // A function to display RingNumber, Radius
// and Area
Answer the questions (i) to (iv) based on the following:
class One
{
int A1;
protected:
float A2;
public:
One();
void Get1(); void Show1();
};
class Two :
private One
{
int B1;
protected:
float B2;
public:
Two();
void Get2();
void Show();
};
class Three : public Two
{
int C1;
public:
Three();
void Get3();
void Show();
};
void main()
{
Three T; //Statement 1
_______; //Statement 2
}
(i) Which type of Inheritance out of the following is illustrated in the above example?
-Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance.
(ii) Write the names of all the member functions, which are directly accessible by the
object T of class Three as declared in main() function.
(iii) Write Statement 2 to call function Show() of class Two from the object T of Class Three.
(iv) What will be the order of execution of the constructors, when the object T of
class Three is declared inside main()?