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 - Which operator is required to be overloaded as member function only?

A - _

B - _ _

C - ++ (postfix version)

D - =

Answer : D

Explaination

Overloaded assignment operator does the job similar to copy constructor and is required to be overloaded as member function of the class.

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

#include<iostream>

using namespace std;
main() { 

   int i = 1, j = 2, k = 3, r; 

   r = (i, j, k);

   cout<<r<<endl;

}

A - 1

B - 2

C - 3

D - Compile Error

Answer : C

Explaination

Comma is called as the separator operator and the associativity is from left to right. Therefore ‘k’ is the expressions resultant.

#include<iostream>

using namespace std;
main() { 

   int i = 1, j = 2, k = 3, r; 

   r = (i, j, k);

   cout<<r<<endl;

}

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.

Answer : D

Explaination

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

Q 6 - (i) ‘ios’ is the base class of ‘istream’

(ii) All the files are classified into only 2 types. (1) Text Files (2) Binary Files.

A - Only (i) is true

B - Only (ii) is true

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

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

Answer : C

Explaination

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”; 
}

Answer : B

Explaination

Compilation is the process of translating high level language statements into equivalent machine code, which is object code.

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

#include<iostream>
#include<string.h>

using namespace std;
main() { 
   char s[] = "Hello\0Hi";
   
   cout<<strlen(s)<<" "<<sizeof(s);
}

A - 5 9

B - 7 20

C - 5 20

D - 8 20

Answer : A

Explaination

Length of the string is count of character upto ‘\0’. sizeof – reports the size of the array.

#include<iostream>
#include<string.h>

using namespace std;
main() { 
   char s[] = "Hello\0Hi";
   
   cout<<strlen(s)<<" "<<sizeof(s);
}
cpp_questions_answers.htm
Advertisements