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 - Choose the operator which cannot be overloaded.

A - /

B - ()

C - ::

D - %

Answer : C

Explaination

Scope resolution (::) is not permitted to be overloaded.

Q 2 - The operator used to access member function of a structure using its object.

A - .

B - ->

C - *

D - None of the above.

Answer : A

Explaination

Just the way we use dot (.) operator to access members of the class, in similar it is used to access the members of the structure too.

Answer : B

Explaination

Only members of the derived class and the same class can access a protected member.

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();

}

Answer : D

Explaination

Options (a), (b) & (c) are applicable.

Answer : B

Explaination

Defining a templated class is defining a generic class. Hence functionality of the class is generalized for several types, if applicable.

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

#include<iostream>

using namespace std;
main() { 
   const int 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.

#include<iostream>

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

Q 8 - What is the size of ‘int’?

A - 2

B - 4

C - 8

D - Compiler dependent

Answer : D

Explaination

The size of ‘int’ depends upon the complier i.e. whether it is a 16 bit or 32 bit.

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

#include<iostream>

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

A - 3 2 1 0

B - 3 2 1

C - 3 3 3

D - Compile error

Answer : B

Explaination

As the static variable retains its value from the function calls, the recursion happens thrice.

#include<iostream>

using namespace std;
void f() {
	static int i = 3;
   
	cout<<i;
	if(--i) f();
}
main() {
	f();
}

Q 10 - Does both the loops in the following programs prints the correct string length?

#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0; 
   
   while(s[i++]);
      cout<<i;
}

A - Yes, both the loops prints the correct length

B - Only for loop prints the correct length

C - Only while loop prints the correct length

D - Compile error in the program.

Answer : B

Explaination

In while loop 'i' gets incremented after checking for '\0', hence giving 1 more than the length.

#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0;
   
   while(s[i++]);
      cout<<i;
}

cpp_questions_answers.htm

Advertisements