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

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.

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.

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

#include<iostream>

using namespace std;
main() { 
   int x = 5;

   if(x==5) {	
      if(x==5) break;
      cout<<"Hello";
   } 

   cout<<”Hi”; 
}

A - Compile error

B - Hi

C - HelloHi

D - Hello

Answer : A

Explaination

compile error, keyword break can appear only within loop/switch statement.

#include<iostream>

using namespace std;
main() { 
   int x = 5;

   if(x==5) {	
      if(x==5) break;
      cout<<"Hello";
   } 

   cout<<”Hi”; 
}

Q 8 - What is the size of the following union definition?

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

A - 1

B - 2

C - 4

D - 8

Answer : C

Explaination

union size is biggest element size of it. All the elements of the union share common memory.

#include<iostream>

using namespace std;
main() {
   union abc { 
      char a, b, c, d, e, f, g, h; 
      
      int i;
   };
   cout<<sizeof(abc);
}

Q 9 - An inline function can execute faster than a normal function.

A - True

B - False

Answer : A

Explaination

As the code of inline function gets expanded at the line of call, therefore it gets executed faster with no overhead of context switch

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