OOPs Using C++
Test your skill in Object Oriented Programming.
Questions
Which of the following is NOT a feature of OOPS?
- Inheritance
- Encapsulation
- Abstraction
- Procedural programming
Which of the following members is NOT accessible through a 'Friend Function'?
- Public member of a class
- Private member of a class
- Protected member of a class
- Members of the child class
With reference to classes and objects, when does a constructor of a class invoke by the C++ compiler ?
- When an object is used.
- When a class is declared.
- When an object is declared.
- When a class is used.
Which of the following statements is TRUE with reference to 'Method Overloading'?
- All the methods must have different return values.
- All the methods must be declared in the base class and redeclared in the child class.
- All the methods must differ in arguments.
- The name of the arguments should be different.
Which of the following statements is FALSE regarding destructors?
- A destructor can be declared only when a constructor has also been declared.
- We cannot overload destructors.
- It has the same name as of the class.
- During inheritance, the child class destructor is invoked first and then the parent class destructor.
Which of the following operators is used to define the class methods, physically outside the class?
- * (Asterisk)
- ? (Question mark)
- : (Colon)
- :: (Scope resolution operator)
The members (variables and methods) of a class are by default specified as _________
- private
- public
- protected
- friend
What will happen if the derived class and the base class contain a member with the same name?
- The compiler will show an error when the code is compiled.
- The dervied class member will hide the defintion of the base class member.
- The base class member will be called even with the child class object.
- Only child class member will be called irrespective of the object.
Which of the following operators cannot be overloaded?
- ++
- ::
- >
- ==
A class that cannot be instantiated is known as
- virtual class
- friend class
- abstract class
- derived class
When the compiler cannot differentiate between two overloaded constructors or methods, it is called
- overloading
- overriding
- ambiguity
- destructor
Which of the following is NOT an advantage of functions?
- It saves disk space.
- Debugging (Removing the errors) is easy.
- Recursive calling is possible.
- Makes the program easier to understand.
Which of the following statements is TRUE regarding 'Pure Virtual Functions'?
- They are compulsory to be overloaded by the child class.
- Are optional to be overridden by the child class.
- A class containing a pure virtual function is just like a normal class.
- Cannot have arguments.
The function that calls itself is known as
- recursive function
- overloaded function
- inline function
- overridden function
Which of the following is NOT a use of the scope resolution operator (::) in C++?
- It used for defining class methods, outside the class.
- It is used to inherit a base class to declare a derived class.
- It is used to help the compiler differentiate between a local and a global variable.
- It used to call overridden member of the base class from the derived class.
Which of the following statements is TRUE about constructors and destructors?
- A class must have a constructor and a destructor defined to make required variable initializations and to deallocate memory at the end.
- Both can be declared with arguments (parameterized)
- Both are invoked only once per object.
- The names of constructor and destructor can be anything.
Which of the following is not a type of constructor in C++?
- Copy
- Default
- Parameterized
- Dynamic
Which of the following statements is TRUE regarding static class members?
- All objects of a class can have different values of the static member variable.
- A static method of a class can access only the static member variables.
- A static member variable cannot be accessed from a non static (normal) method.
- A class cannot have more than one static member.
While overloading an operator, the name of the method must be
- void
- virtual
- abstract
- operator
With reference to Friend functions, which of the following statements is FALSE?
- A friend function can access the private members of the class, outside the class.
- A friend function cannot be defined inside the class.
- A friend function is a member of the class.
- A friend function can be a friend of more the one class.
Which of the following is the correct syntax to declare a Pure Virtual Function?
- virtual void print()=0;
- virtual void print=0 ()
- virtual void print()={0}
- virtual void print()=(0)
Which of the following is the correct syntax to call the parameterized constructor of class 'sample' from the constructor of class 'sub'?
class sample {
public:
sample(int a) {}
};
class sub {};
- sub(int x,int y) : sample(x)
- sub(int x,int y)
{
sample(y);
} - sub(int x,int y) , sample(x)
- sub(int x,int y) : sample{x}
What will be the size (in bytes)of an object of class child in the code given above?
class super {
int a;
public:
int b : protected : int c;
};
class parent : public super {
float d;
protected:
char e;
};
class child : private parent {
int f;
public:
float g;
};
Format! Style:
C++ online code formatter © 2014 by KrzaQ
Powered by vibe.d, the D language and clang-format
- 6
- 4
- 17
- 8
What will be the output of the above code?
class base1 {
public:
base1() { cout <<\nBase 1 Constructor; }
~base1() { cout <<\nBase 1 Destructor; }
};
class base2 {
public:
base2() { cout << "\nBase 2 Constructor"; }
~base2() { cout <<\nBase 2 Destructor; }
};
class derived : public base1, public base2 {
public:
derived() { cout <<\nDerived class Constructor; }
~derived() { cout <<\nDerived class Destructor; }
};
- Base 1 Constructor
Base 1 Destructor
Base 2 Constructor
Base 2 Destructor
Derived class Constructor
Derived class Destructor - Derived class ConstructorDerived class DestructorBase 2 ConstructorBase 2 DestructorBase 1 ConstructorBase 1 Destructor
- Derived class ConstructorBase 2 ConstructorBase 1 ConstructorBase 1 DestructorBase 2 DestructorDerived class Destructor
- Base 1 Constructor
Base 2 Constructor
Derived class Constructor
Derived class Destructor
Base 2 Destructor
Base 1 Destructor
Which of the following statements is FALSE?
class base {
public:
void show() {}
};
class sub : private base {
public:
void print() {}
protected:
void display() {}
};
void main() { sub objs; }
- The object 'objs' cannot access the show() method defined in the base class.
- The object 'objs' can not access the display().
- If we declare an object of the base class, it will be able to call the 'show()' method.
- The object 'objs' cannot access the print() defined in the sub class.