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

#include<iostream>

using namespace std;
main() { 

   int i = 1, j = 2, k = 3, r; 

   r = (i, j, k);

   cout<<r<<endl;

}

A - 1

B - 2

C - 3

D - Compile Error

Answer : C

Explaination

Comma is called as the separator operator and the associativity is from left to right. Therefore ‘k’ is the expressions resultant.

#include<iostream>

using namespace std;
main() { 

   int i = 1, j = 2, k = 3, r; 

   r = (i, j, k);

   cout<<r<<endl;

}

Q 4 - Class function which is called automatically as soon as the object is created is called as __

A - Constructor

B - Destructor

C - Friend function

D - Inline function.

Answer : A

Explaination

If not provided the same, default constructor from the compiler is called automatically otherwise the programmer’s provided constructor gets called.

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() {
   cout<<"Hello"<<endl;
}
main() {
}

A - No output

B - Error, as the function is not called.

C - Error, as the function is defined without its declaration

D - Error, as the main() function is left empty

Answer : A

Explaination

No output, apart from the option (a) rest of the comments against the options are invalid

#include<iostream>

using namespace std;
void f() {
	cout<<"Hello"<<endl;
}
main() 
{
}

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

#include<iostream>

using namespace std;
main() { 
   int a[] = {1, 2}, *p = a;
   
   cout<<p[1]; 
}

A - 1

B - 2

C - Compile error

D - Runtime error

Answer : B

Explaination

as ‘p’ holds the base address then we can access array using ‘p’ just like with ‘a’

#include<iostream>

using namespace std;
main() { 
   int a[] = {1, 2}, *p = a;
   
   cout<<p[1]; 
}

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