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 - A trigraph character begins with

A - #

B - ##

C - ?

D - ??

Answer : C

Explaination

Few characters have alternative representation and start with ??. Eg. Fro [ equivalent is ??(

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.

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

#include<iostream>

using namespace std;
class Base {
public: 
   virtual 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 : B

Explaination

The overridden method f() of the created object for derived class gets called.

#include<iostream>

using namespace std;
class Base {
public: 
   virtual void f() { 
      cout<<"Base\n";
   }
};

class Derived:public Base {
public: 
   void f() { 
      cout<<"Derived\n";
   }
};

main() { 
   Base *p = new Derived();
   p->f();

}

Q 5 - Which feature of the OOPS gives the concept of reusability?

A - Abstraction

B - Encapsulation

C - Inheritance

D - None of the above.

Answer : C

Explaination

The process of designing a new class (derived) from the existing (base) class to acquire the attributes of the existing is called as inheritance. Inheritance gives the concept of reusability for code/software components.

Q 6 - i) single file can be opened by several streams simultaneously.

ii) several files simultaneously can be opened by a single stream

A - (i) and (ii) are true

B - (i) and (ii) are false

C - Only (i) is true

D - Only (ii) is true

Answer : C

Explaination

Q 7 - i) Exception handling technically provides multi branching.

ii) Exception handling can be mimicked using ‘goto’ construct.

A - Only (i) is true

B - Only (ii) is true

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

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

Answer : A

Explaination

goto just does the unconditional branching.

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

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

A - 5

B - Address of ‘x’

C - Compile error

D - 10

Answer : D

Explaination

A function can return reference, hence it can appear on the left hand side of the assignment operator.

#include<iostream>

using namespace std;
int x = 5;

int &f() {
	return x;
}
main() {
	f() = 10;
   cout<<x;
}

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

#include<iostream>

using namespace std;
main() {	
   int a[] = {10, 20, 30};
   
	cout<<*a+1;
}

A - 10

B - 20

C - 11

D - 21

Answer : C

Explaination

*a refers to 10 and adding a 1 to it gives 11.

#include<iostream>

using namespace std;
main() {	
   int a[] = {10, 20, 30};
   
	cout<<*a+1;
}
cpp_questions_answers.htm
Advertisements