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 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 - Which of the following is not the keyword in C++?

A - volatile

B - friend

C - extends

D - this

Answer : C

Explaination

All the rest are valid keywords of C++.

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 - Escape sequence character '\0' occupies __ amount of memory.

A - 0

B - 1

C - 2

D - 4

Answer : B

Explaination

As it is also a character is occupies 1 byte of memory.

Q 6 - With respective to streams >> (operator) is called as

A - Insertion operator

B - Extraction operator

C - Right shift operator

D - Left shift operator

Answer : B

Explaination

It extracts the data from stream into variables.

Answer : A

Explaination

When the program is in execution phase the possible unavoidable error is called as an exception.

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 - A single line comment in C++ language source code can begin with _____

A - ;

B - :

C - /*

D - //

Answer : D

Explaination

Two immediate forward slashes are used to comment a single line. A single can be commented by beginning with /* and should be terminated with */ , in general used for multi-line comments.

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