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 - Choose the pure virtual function definition from the following.

A - virtual void f()=0 { }

B - void virtual f()=0 { }

C - virtual void f() {} = 0;

D - None of the above.

Answer : D

Explaination

A pure virtual function cannot have a definition.

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

#include<iostream>

using namespace std;
class abc { 

   public: 
      int i; 

      abc(int i) { 
         i = i;
      }
};

main() { 
   abc m(5); 
   
   cout<<m.i;
}

A - 5

B - Garbage

C - Error at the statement i=i;

D - Compile error: ‘i’ declared twice.

Answer : B

Explaination

i=i, is assigning member variable to itself.

#include<iostream>

using namespace std;
class abc { 

   public: 
      int i; 

      abc(int i) { 
         i = i;
      }
};

main() { 
   abc m(5); 
   
   cout<<m.i;
}

Answer : C

Explaination

Virtual functions gives the ability to override the functionality of base class into the derived class. Hence achieving dynamic/runtime polymorphism.

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

#include<iostream>

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

Explaination

The overridden method f() of the created object for derived class gets called.

#include<iostream>

using namespace std;
class Base {
public: 
   virtual void f() { 
      cout<<"Base\n";
   }
};

class Derived:public Base {
public: 
   void f() { 
      cout<<"Derived\n";
   }
};

main() { 
   Base *p = new Derived();
   p->f();

}

Q 6 - Which type of data file is analogous to an audio cassette tape?

A - Random access file

B - Sequential access file

C - Binary file

D - Source code file

Answer : B

Explaination

As the access is linear.

Answer : A

Explaination

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

Q 8 - What is the size of ‘int’?

A - 2

B - 4

C - 8

D - Compiler dependent

Answer : D

Explaination

The size of ‘int’ depends upon the complier i.e. whether it is a 16 bit or 32 bit.

Q 9 - Special symbol permitted with in the identifier name.

A - $

B - @

C - _

D - .

Answer : C

Explaination

The only permitted special symbol is under score (_) in the identifier.

Q 10 - i) Exceptions can be traced and controlled using conditional statements.

ii) For critical exceptions compiler provides the handler

A - Only (i) is true

B - Only (ii) is true

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

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

Answer : B

Explaination

Conditional statements are used to take alternate actions depending upon certain condition but not multi branching. C++ too provides some critical exception handlers.


cpp_questions_answers.htm

Advertisements