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 of the following is not the keyword in C++?

A - volatile

B - friend

C - extends

D - this

Answer : C

Explaination

All the rest are valid keywords of C++.

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 - What is the output of the following program?

#include<iostream>

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
   }
};

class Derived:public Base {
public:
   void f() {
      cout<<"Derived\n";
   }
};
main() {
   Base *p = new Derived(); 
   
   p->f();
}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : A

Explaination

The method f() is not overridden therefore as per the pointer type respective method is called.

#include<iostream>

using namespace std;
class Base {
public:
   void f() {
      cout<<"Base\n";
   }
};
class Derived:public Base {
public:
   void f() {
      cout<<"Derived\n";
   }
};
main() {
   Base *p = new Derived(); 
   
   p->f();
}

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.

Q 6 - With respective to streams >> (operator) is called as

A - Insertion operator

B - Extraction operator

C - Right shift operator

D - Left shift operator

Answer : B

Explaination

It extracts the data from stream into variables.

Q 7 - i) Exception handling technically provides multi branching.

ii) Exception handling can be mimicked using ‘goto’ construct.

A - Only (i) is true

B - Only (ii) is true

C - Both (i) & (ii) are true

D - Both (i) && (ii) are false

Answer : A

Explaination

goto just does the unconditional branching.

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 - What is the output of the following program?

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

A - 5

B - Address of ‘x’

C - Compile error

D - 10

Answer : D

Explaination

A function can return reference, hence it can appear on the left hand side of the assignment operator.

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

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