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 data type can be used to hold a wide character in C++?

A - unsigned char;

B - int

C - wchar_t

D - none of the above.

Answer : C

Explaination

wchar_t is the data type using which we can hold Unicode characters.

Answer : B

Explaination

Both the mentioned operators cannot be overloaded.

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.

Q 5 - 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() { 
   Derived obj; 
   obj.Base::f();
}

A - Base

B - Derived

C - Compile error

D - None of the above.

Answer : A

Explaination

Base object cannot refer to Derived members.

#include<iostream>

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

Q 6 - A C++ program statements can be commented using

A - Single line comment

B - Multi line comment

C - Either (a) or (b)

D - Both (a) and (b).

Answer : D

Explaination

Both styles of commenting is available in C++.

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

#include<iostream>

using namespace std;
void f() { 
   static int i; 
   
   ++i; 
   cout<<i<<" "; 
}

main() { 
   f(); 
   f(); 
   f(); 
}

A - 1 1 1

B - 0 0 0

C - 3 2 1

D - 1 2 3

Answer : D

Explaination

1 2 3, A static local variables retains its value between the function calls and the default value is 0.

#include<iostream>

using namespace std;
void f() { 
   static int i; 
   
   ++i; 
   cout<<i<<" "; 
}

main() { 
   f(); 
   f(); 
   f(); 
}

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?

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() { 
   class student { 
      int rno = 10;
   } v;
  
   cout<<v.rno;
}

A - 10

B - Garbage

C - Runtime error

D - Compile error

Answer : D

Explaination

Class member variables cannot be initialized.

#include<iostream>

using namespace std;
main() { 
   class student { 
      int rno = 10;
   } v;
  
   cout<<v.rno;
}
cpp_questions_answers.htm
Advertisements