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 - The default access specifer for the class members is

A - public

B - private

C - protected

D - None of the above.

Answer : B

Explaination

If a member/s appear in the class with following no access specifier, the default is private.

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 - In the following program f() is overloaded.

void f(int x) {

}

void f(signed x) {

}

main() {

}

A - True

B - False

Answer : B

Explaination

No, as both the functions signature is same.

Answer : D

Explaination

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();
}

Answer : C

Explaination

A constructor cant be overridden.

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

#include<iostream>

using namespace std;
main() {
   enum { 
      india, is = 7, GREAT 
   };

   cout<<india<<" "<<GREAT;
}

A - 0 1

B - 0 2

C - 0 8

D - Compile error

Answer : C

Explaination

0 8, enums gives the sequence starting with 0. If assigned with a value the sequence continues from the assigned value.

#include<iostream>

using namespace std;
main() {
   enum { 
      india, is = 7, GREAT 
   };

   cout<<india<<" "<<GREAT;
}

Q 8 - What is the size of the following union definition?

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

A - 1

B - 2

C - 4

D - 8

Answer : C

Explaination

union size is biggest element size of it. All the elements of the union share common memory.

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

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;
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