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 - A constructor can be virtual.

A - True

B - False

Answer : B

Explaination

The purpose of the constructor cannot be overridden in the derived class hence constructor cannot be a virtual.

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 - Which is the storage specifier used to modify the member variable even though the class object is a constant object?

A - auto

B - register

C - static

D - mutable

Answer : D

Explaination

mutable is storage specifier introduced in C++ which is not available in C. A class member declared with mutable is modifiable though the object is constant.

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 - The programs machine instructions are store in __ memory segment.

A - Data

B - Stack

C - Heap

D - Code

Answer : D

Explaination

Code segments holds the program instructions and fetched by instruction pointer for execution.

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

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

#include<iostream>

using namespace std;
main() { 
   float t = 2;
	
	switch(t) {
      case 2: cout<<”Hi”;
		default: cout<<"Hello";
	}
}

A - Hi

B - HiHello

C - Hello

D - Error

Answer : D

Explaination

Error, switch expression can’t be float value

#include<iostream>

using namespace std;
main() { 
   float t = 2;
	
	switch(t) {
		case 2: cout<<”Hi”;
		default: cout<<"Hello";
	}
}

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

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : B

Explaination

*s=’N’, changes the character at base address to ‘N’.

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

cpp_questions_answers.htm

Advertisements