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 - Which operator is required to be overloaded as member function only?

A - _

B - _ _

C - ++ (postfix version)

D - =

Answer : D

Explaination

Overloaded assignment operator does the job similar to copy constructor and is required to be overloaded as member function of the class.

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++.

Q 4 - Escape sequence character '\0' occupies __ amount of memory.

A - 0

B - 1

C - 2

D - 4

Answer : B

Explaination

As it is also a character is occupies 1 byte of memory.

Answer : C

Explaination

A constructor can’t be overridden.

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

#include<iostream>

using namespace std;
main() { 
   int x = 5;

   if(x==5) {	
      if(x==5) break;
      cout<<"Hello";
   } 

   cout<<”Hi”; 
}

A - Compile error

B - Hi

C - HelloHi

D - Hello

Answer : A

Explaination

compile error, keyword break can appear only within loop/switch statement.

#include<iostream>

using namespace std;
main() { 
   int x = 5;

   if(x==5) {	
      if(x==5) break;
      cout<<"Hello";
   } 

   cout<<”Hi”; 
}

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

#include<iostream>

using namespace std;
main() {	
   union abc {
		int x;
		char ch;
	} var;
	
   var.ch = 'A';
   cout<<var.x;
}

A - A

B - Garbage value

C - 65

D - 97

Answer : B

Explaination

65, as the union variables share common memory for all its elements, x gets ‘A’ whose ASCII value is 65 and is printed.

#include<iostream>

using namespace std;
main() {	
   union abc {
		int x;
		char ch;
	} var;
	
   var.ch = 'A';
   cout<<var.x;
}

Q 9 - Identify the C++ compiler of Linux

A - cpp

B - g++

C - Borland

D - vc++

Answer : B

Explaination

g++ is GNU C++ compiler for linux. Borland and vc++ (Microsoft visual c++) for windows.

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

#include<iostream>

using namespace std;
void main() {
   char *s = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}

A - C++ C++

B - C++ ++

C - ++ ++

D - Compile error

Answer : B

Explaination

After s++, s points the string “++”.

#include<iostream>

using namespace std;
void main() {
   char *s = "C++";
   
	cout<<s<<" ";
	s++;
	cout<<s<<" ";
}
cpp_questions_answers.htm
Advertisements