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

#include<iostream>

using namespace std;
class abc { 
   void f();
   void g();
   int x;
};

main() {
   cout<<sizeof(abc)<<endl;
}

A - 12

B - 4

C - 8

D - Compile error

Answer : B

Explaination

Only the class member variables constitutes as the size of the class or its object.

#include<iostream>

using namespace std;
class abc { 
   void f();
   void g();
   int x;
};
main() {
   cout<<sizeof(abc)<<endl;
}

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++.

Answer : B

Explaination

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

Q 5 - Which operator is used to resolve the scope of the global variable?

A - −>

B - .

C - *

D - ::

Answer : D

Explaination

Scope resolution operator is used to resolve for the global scope of a variable if the local and global variables conflict by name.

Q 6 - HAS-A relationship between the classes is shown through.

A - Inheritance

B - Container classes

C - Polymorphism

D - None of the above.

Answer : B

Explaination

A class containing anther class object as its member is called as container class and exhibits HAS A relationship.

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;
main() {	
   union abc {
		int x;
		char ch;
	} var;
	
   var.ch = 'A';
   cout<<var.x;
}

A - A

B - Garbage value

C - 65

D - 97

Answer : B

Explaination

65, as the union variables share common memory for all its elements, x gets ‘A’ whose ASCII value is 65 and is printed.

#include<iostream>

using namespace std;
main() {	
   union abc {
		int x;
		char ch;
	} var;
	
   var.ch = 'A';
   cout<<var.x;
}

Q 9 - Identify the C++ compiler of Linux

A - cpp

B - g++

C - Borland

D - vc++

Answer : B

Explaination

g++ is GNU C++ compiler for linux. Borland and vc++ (Microsoft visual c++) for windows.

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