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

Answer : A

Explaination

As inline function gets expanded at the line of call like a macro it executes faster.

Q 2 - From the below class choose the proper definition of the member function f().

template <class T>

class abc {
   void f();
};

A - template <class T>

    void abc<T>::f() { }

B - template<class T>

    void abc::f() { }

C - template<T>

    void abc<class T>::f() { }

D - template<T>

    void abc<T>::f() { }

Answer : A

Explaination

Q 3 - In the following program f() is overloaded.

void f(int x) {

}

int f(signed x) { 
   return 1;
}

main() {

}

A - True

B - False

Answer : B

Explaination

No, as both the functions arguments is same and compiler ignores return type to consider overloading though different in return type.

Answer : D

Explaination

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

#include<iostream>

using namespace std;
main() { 
   int *p = new int; 
   delete p; 
   delete p; 
   cout<<"Done";
}

A - Done

B - Compile error

C - Runtime error

D - None of the above

Answer : C

Explaination

It is invalid to release memory more than once.

#include<iostream>

using namespace std;
main() { 
   int *p = new int; 
   delete p; 
   delete p; 
   cout<<"Done";
}

Q 6 - An array can be passed to the function with call by value mechanism.

A - True

B - False

Answer : B

Explaination

An array never is passed with call by value mechanism

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

#include <iostream>
using namespace std;
 
int main () {
   // local variable declaration:
   int x = 1;

   switch(x) {
   case 1 :
      cout << "Hi!" << endl; 
      break;
   default :
      cout << "Hello!" << endl;
   }
}

A - Hello

B - Hi

C - HelloHi

D - Compile error

Answer : B

Explaination

Hi, control reaches default-case after comparing the rest of case constants.

#include <iostream>
using namespace std;
 
int main () {
   // local variable declaration:
   int x = 1;

   switch(x) {
   case 1 :
      cout << "Hi!" << endl; 
      break;
   default :
      cout << "Hello!" << endl;
   }
}

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

#include<iostream>

using namespace std;
void swap(int m, int n) {
   int x = m;

	m = n;
	n = x;
}
main() {
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<" "<<y;
}

A - 3 5

B - 5 3

C - 5 5

D - Compile error

Answer : B

Explaination

5 3, call by value mechanism can’t alter actual arguments.

#include<iostream>

using namespace std;
void swap(int m, int n) {
   int x = m;

	m = n;
	n = x;
}
main() {
   int x = 5, y = 3;
   
	swap(x,y);
	cout<<x<<" "<<y;
}

Q 9 - Following is the invalid inclusion of a file to the current program. Identify it

A - #include <file>

B - #include "file"

C - #include < file

D - All of the above are invalid

Answer : C

Explaination

option (a) & (b) are valid. There is no such syntax or provision as in option (c).

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