C++ Online Quiz


Advertisements


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 - ‘cin’ is an __

A - Class

B - Object

C - Package

D - Namespace

Answer : B

Explaination

It’s an object of istream class.

Q 3 - Pick up the valid declaration for overloading ++ in postfix form where T is the class name.

A - T operator++();

B - T operator++(int);

C - T& operator++();

D - T& operator++(int);

Answer : B

Explaination

The parameter int is just to signify that it is the postfix form overloaded. Shouldn’t return reference as per its original behavior.

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

#include<iostream>

using namespace std;
main() { 
   int const a = 5;
   
   a++; 
   cout<<a; 
}

A - 5

B - 6

C - Runtime error

D - Compile error

Answer : D

Explaination

Compile error - constant variable cannot be modified.

Answer : A

Explaination

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

Answer : B

Explaination

Compilation is the process of translating high level language statements into equivalent machine code, which is object code.

Q 9 - The default executable generation on UNIX for a C++ program is ___

A - a.exe

B - a

C - a.out

D - out.a

Answer : C

Explaination

“a.out” is the default name of the executable generated on both the UNIX and Linux operating systems.

Q 10 - i) Exceptions can be traced and controlled using conditional statements.

ii) For critical exceptions compiler provides the handler

A - Only (i) is true

B - Only (ii) is true

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

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

Answer : B

Explaination

Conditional statements are used to take alternate actions depending upon certain condition but not multi branching. C++ too provides some critical exception handlers.


cpp_questions_answers.htm

Advertisements