C++ Online Quiz



Following quiz provides Multiple Choice Questions (MCQs) related to C++ Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers

Q 1 - What is the output of the following program?

#include<iostream>

using namespace std;
class abc { 
   public: 
      static int x; 
      int i; 

      abc() {
         i = ++x;
      }
   };
int abc::x;

main() { 
   abc m, n, p;
   
   cout<<m.x<<" "<<m.i<<endl;
}

A - 3 1

B - 3 3

C - 1 1

D - 1 3

Answer : A

Explaination

The static member variable x shares common memory among all the objects created for the class.

#include<iostream>

using namespace std;
class abc { 
   public: 
      static int x; 
      int i; 

      abc() {
         i = ++x;
      }
   };
int abc::x;

main() { 
   abc m, n, p;
   
   cout<<m.x<<" "<<m.i<<endl;
}

Q 2 - What is the output of the following program?

#include<iostream>

using namespace std;
class abc { 

   public: 
      int i; 

      abc(int i) { 
         i = i;
      }
};

main() { 
   abc m(5); 
   
   cout<<m.i;
}

A - 5

B - Garbage

C - Error at the statement i=i;

D - Compile error: i declared twice.

Answer : B

Explaination

i=i, is assigning member variable to itself.

#include<iostream>

using namespace std;
class abc { 

   public: 
      int i; 

      abc(int i) { 
         i = i;
      }
};

main() { 
   abc m(5); 
   
   cout<<m.i;
}

Q 3 - The following operator can be used to calculate the value of one number raised to another.

A - ^

B - **

C - ^^

D -None of the above

Answer : D

Explaination

There is no such operator in C/C++.

Answer : D

Explaination

Q 5 - Objects created using new operator are stored in __ memory.

A - Cache

B - Heap

C - Stack

D - None of the above.

Answer : B

Explaination

new operator allocates memory dynamically know as Heap/free memory.

Answer : B

Explaination

Defining a templated class is defining a generic class. Hence functionality of the class is generalized for several types, if applicable.

Q 7 - What is the output of the following program?

#include<iostream>

using namespace std;
main() {
   short unsigned int i = 0; 
   
   cout<<i--;
}

A - 0

B - Compile error

C - 65535

D - 32767

Answer : A

Explaination

0, with post decrement operator value of the variable will be considered as the expressions value and later gets decremented

#include<iostream>

using namespace std;
main() {
   short unsigned int i = 0; 
   
   cout<<i--;
}

Q 8 - What is the output of the following program?

#include<iostream>

using namespace std;
void swap(int m, int n) {
   int x = m;

	m = n;
	n = x;
}
main() {
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<" "<<y;
}

A - 3 5

B - 5 3

C - 5 5

D - Compile error

Answer : B

Explaination

5 3, call by value mechanism cant alter actual arguments.

#include<iostream>

using namespace std;
void swap(int m, int n) {
   int x = m;

	m = n;
	n = x;
}
main() {
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<" "<<y;
}

Q 9 - What is the output of the following program?

main() { }

A - No output

B - Garbage

C - Compile error

D - Runtime error

Answer : A

Explaination

It is valid to have main() function empty, therefore producing no displayable output.

Q 10 - What is the output of the following program?

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : B

Explaination

*s=N, changes the character at base address to N.

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}
cpp_questions_answers.htm
Advertisements