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 trigraph character begins with

A - #

B - ##

C - ?

D - ??

Answer : C

Explaination

Few characters have alternative representation and start with ??. Eg. Fro [ equivalent is ??(

Q 2 - Choose the respective delete operator usage for the expression ptr=new int[100].

A - delete ptr;

B - delete ptr[];

C - delete[] ptr;

D - []delete ptr;

Answer : C

Explaination

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. Shouldnt return reference as per its original behavior.

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

#include<iostream>

using namespace std;
class Base {
public:
   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 : A

Explaination

The method f() is not overridden therefore as per the pointer type respective method is called.

#include<iostream>

using namespace std;
class Base {
public:
   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 : C

Explaination

A constructor cant be overridden.

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 will be the output of the following program?

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

using namespace std;
main() {
   cout<<strcmp("strcmp()","strcmp()");
}

A - 0

B - 1

C - -1

D - Invalid use of strcmp() function

Answer : A

Explaination

0, strcmp return 0 if both the strings are equal

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

using namespace std;
main() {
   cout<<strcmp("strcmp()","strcmp()");
}

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

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}

A - Fine

B - Nine

C - Compile error

D - Runtime error

Answer : B

Explaination

*s=N, changes the character at base address to N.

#include<iostream>

using namespace std;
main() {
   char s[] = "Fine";
	*s = 'N';
   
   cout<<s<<endl;
}
cpp_questions_answers.htm
Advertisements