Alien head

OOPs Using C++

Description: Test your skill in Object Oriented Programming.
Number of Questions: 25
Created by:
Tags: C++ OOPS Programming OOPs
Attempted 0/25 Correct 0 Score 0

Which of the following is NOT a feature of OOPS?

  1. Inheritance

  2. Encapsulation

  3. Abstraction

  4. Procedural programming


Correct Option: D
Explanation:

Procedural programming is a programming methodology in which the code of a program is kept as concise as possible. Procedural programming is not suitable for coding complex problems because the code can become very complex as the program grows.

Which of the following members is NOT accessible through a 'Friend Function'?

  1. Public member of a class

  2. Private member of a class

  3. Protected member of a class

  4. Members of the child class


Correct Option: D
Explanation:

A friend function can only access the members of the class of which it is a friend. It does not have the power of accessing child class members. In inheritance, the derived class can access the members of the parent class, not the other way around.

With reference to classes and objects, when does a constructor of a class invoke by the C++ compiler ?

  1. When an object is used.

  2. When a class is declared.

  3. When an object is declared.

  4. When a class is used.


Correct Option: C
Explanation:

A constructor is a special function, which has the same name as of the class. It is called implictly when an object is declared. The user is not required to call it explicitly like a normal method. There are three types of constructors - default, parameterized and copy constructor. A class can have more than one constructor, which is called as 'constructor overloading'

Which of the following statements is TRUE with reference to 'Method Overloading'?

  1. All the methods must have different return values.

  2. All the methods must be declared in the base class and redeclared in the child class.

  3. All the methods must differ in arguments.

  4. The name of the arguments should be different.


Correct Option: C
Explanation:

To overload a method we must keep some difference in their arguments. The difference in the arguments can be either in their numbers, orders or datatypes. For example, we can have two methods with the name sum(), first with two integer arguments and second with three arguments. If the arguments are same in number, datatype or order the compiler will throw an ambiguity error which will mean that it is not able to differentitate one method with the other.
Example:
void sum(int);
void sum(int,int);
void sum(double, int);

Which of the following statements is FALSE regarding destructors?

  1. A destructor can be declared only when a constructor has also been declared.

  2. We cannot overload destructors.

  3. It has the same name as of the class.

  4. During inheritance, the child class destructor is invoked first and then the parent class destructor.


Correct Option: A
Explanation:

Declaring a destructor does not need a constructor to be declared. A destructor can be declared even when there is no constructor.

Which of the following operators is used to define the class methods, physically outside the class?

    • (Asterisk)
  1. ? (Question mark)

  2. : (Colon)

  3. :: (Scope resolution operator)


Correct Option: D
Explanation:

The (::) Scope Resolution Operator is used in C++ to define a class method, outside the class. Although the method is defined outside the class, it is still a member of the class. The scope resolution operator is used for some other purposes also. To define a method outside the class, we can use the code as given in the following example :
class sample
{
int a=10;
public:
   void show();
};
void sample :: show() // defining the method outside the class.
{
cout<

The members (variables and methods) of a class are by default specified as _________

  1. private

  2. public

  3. protected

  4. friend


Correct Option: A
Explanation:

A class has two types of members- variables and methods. To specify the accessibility of a class member, we can use access specifiers. There are three types of access specifiers available in C++ - private, public and protected. If none of the these is specified, the members have by default 'private' accessibility. A private member can be accessed inside the class but cannot be accessed using a class object. A private member cannot be inherited by a derived class also.

What will happen if the derived class and the base class contain a member with the same name?

  1. The compiler will show an error when the code is compiled.

  2. The dervied class member will hide the defintion of the base class member.

  3. The base class member will be called even with the child class object.

  4. Only child class member will be called irrespective of the object.


Correct Option: B
Explanation:

If the derived class and the base class contain a member with the same name, it is called 'Member Overriding'. For example, there is a class named 'A', which has a function named 'show()'. If we declare a child class of 'A' named 'B' and declare a function with the same name show () in B, the show() declared in B will hide the show() declared in A. If an object of class B' makes a call of show() , the one defined in the child class(B) will be processed. If an object of the base class makes a call to show(), then the one defined in the base class will be called.

Which of the following operators cannot be overloaded?

  1. ++

  2. ::

  3. >

  4. ==


Correct Option: B
Explanation:

The scope resolution operator (::) has many uses in C++. It is used to define a function outside the class and is used for removing ambiguity in class members. It cannot be overloaded.

A class that cannot be instantiated is known as

  1. virtual class

  2. friend class

  3. abstract class

  4. derived class


Correct Option: C
Explanation:

An abstract class is a class that cannot be instantiated or in other words, it cannot be used to create an object. Since object declaration of such a class is not allowed, the only way to access its members is through inheritance. We can declare a child class of the abstract class and call the members of the class. To declare a class as abstract, we must declare a 'Pure Virtual Function' in it.

When the compiler cannot differentiate between two overloaded constructors or methods, it is called

  1. overloading

  2. overriding

  3. ambiguity

  4. destructor


Correct Option: C
Explanation:

Ambiguity is a state when there are two or more methods with the same prototype, the compile cannot differentiate between them and therefore displays an error. For example, if we have two functions called 'sum(int)' , the compiler will not be able to decide which method to call when the user writes a statement like 'sum(10)'. 

Which of the following is NOT an advantage of functions?

  1. It saves disk space.

  2. Debugging (Removing the errors) is easy.

  3. Recursive calling is possible.

  4. Makes the program easier to understand.


Correct Option: A
Explanation:

Functions are programs that have names and perform a particular task. They are mostly used to perform a task again and again. They allow the users to reuse the same code without having to rewrite it. They are not considered to save disk space but they do save primary memory (RAM).

Which of the following statements is TRUE regarding 'Pure Virtual Functions'?

  1. They are compulsory to be overloaded by the child class.

  2. Are optional to be overridden by the child class.

  3. A class containing a pure virtual function is just like a normal class.

  4. Cannot have arguments.


Correct Option: A
Explanation:

 A pure virtual function is a function that is declared as virtual in the base class and redeclared in the child class. In the base class, its declaration ends with the '=0' symbol. It cannot be defined in the base class rather all the child classes must override (provide definition) the pure virtual function as per their requirements. If a derived class fails to override the pure virtual function, the compiler displays an error.

The function that calls itself is known as


  1. recursive function

  2. overloaded function

  3. inline function

  4. overridden function


Correct Option: A
Explanation:

A recursive function is a function that calls itself during execution. This allows the function to repeat itself several times displaying the result after each iteration. Recursive functions are popular among programmers since they allow them to write efficient program using minmal amount of code. 

Which of the following is NOT a use of the scope resolution operator (::) in C++?

  1. It used for defining class methods, outside the class.

  2. It is used to inherit a base class to declare a derived class.

  3. It is used to help the compiler differentiate between a local and a global variable.

  4. It used to call overridden member of the base class from the derived class.


Correct Option: B
Explanation:

The scope resolution operator is not used to inherit a class. The syntax for inheritance has a colon (:), not a scope operator. For example, to inherit a class named 'B' from a class named 'A', the syntax will be
class B : public A

Which of the following statements is TRUE about constructors and destructors?

  1. A class must have a constructor and a destructor defined to make required variable initializations and to deallocate memory at the end.

  2. Both can be declared with arguments (parameterized)

  3. Both are invoked only once per object.

  4. The names of constructor and destructor can be anything.


Correct Option: C
Explanation:

The constructor and destructor of a class can be invoked (called) only once per object. An object of a class can call a normal method as many times as required but a constructor can be called just once. The same rule applies to the destructor also.

Which of the following is not a type of constructor in C++?

  1. Copy

  2. Default

  3. Parameterized

  4. Dynamic


Correct Option: D
Explanation:

There is no dynamic constructor in C++. The different types of constructors are -- Default, Parameterized and Copy.

Which of the following statements is TRUE regarding static class members?

  1. All objects of a class can have different values of the static member variable.

  2. A static method of a class can access only the static member variables.

  3. A static member variable cannot be accessed from a non static (normal) method.

  4. A class cannot have more than one static member.


Correct Option: B
Explanation:

A function declared using the static keyword can access only the static variable of a class. If a call to a non- static member variable is made in a static function, the compiler will return an error. The syntax to call a static function is 
classname::functionname()
A static function is not called using a class object but by using the class name along with the scope resolution operator.

While overloading an operator, the name of the method must be

  1. void

  2. virtual

  3. abstract

  4. operator


Correct Option: D
Explanation:

One of the features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention.
For example: To overload the' 
+' operator for your class, you would provide a member-function named operator+ on your class, similarly to overload the operator '>', the name of the function will be operator>. While overloading an operator, the name of the method must be 'operator'.

With reference to Friend functions, which of the following statements is FALSE?

  1. A friend function can access the private members of the class, outside the class.

  2. A friend function cannot be defined inside the class.

  3. A friend function is a member of the class.

  4. A friend function can be a friend of more the one class.


Correct Option: C
Explanation:

Although declared inside the class, a friend function is not a member of the class. Therefore, while calling it, we do not need to use the object name as we do with the class members. A class member is called by using the syntax 

objectname.methodname();

whereas a friend function is called by using the syntax methodname();

Which of the following is the correct syntax to declare a Pure Virtual Function?

  1. virtual void print()=0;

  2. virtual void print=0 ()

  3. virtual void print()={0}

  4. virtual void print()=(0)


Correct Option: A
Explanation:

A pure virtual function is a function that has no body in the base class. To declare a function as virtual we just have to add 'function() = 0' in the function declaration. A class that contains at least one pure virtual function is called an 'abstract' class and it cannot be instantiated.

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 {};
  1. sub(int x,int y) : sample(x)

  2. sub(int x,int y)
    {
    sample(y);
    }

  3. sub(int x,int y) , sample(x)

  4. sub(int x,int y) : sample{x}


Correct Option: A
Explanation:

In the process of inheritance, if the base class has a parameterized constructor the derived class should also have a parameterized constructor. Therefore, if the parent class of constructor takes an integer value as input, the value must be passed from the child class constructor to the parent class constructor.

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

  2. 4

  3. 17

  4. 8


Correct Option: C
Explanation:

The size of the object is the total size of all the variables declared in all the classes, irrespective of their access specifier and the inheritance visibility mode. Therefore, if you calculate the size of all the variables in the three classes with int as 2 bytes, float 4 bytes and char 1 byte, you will see there are 4 integers (4x2 = 8), 1 char (1x1=1) and 2 floats (4x2=8).

What will be the output of the above code?

class base1 {
 public:
  base1() { cout &lt;
  1. Base 1 Constructor
    Base 1 Destructor
    Base 2 Constructor
    Base 2 Destructor
    Derived class Constructor
    Derived class Destructor

  2. Derived class ConstructorDerived class DestructorBase 2 ConstructorBase 2 DestructorBase 1 ConstructorBase 1 Destructor

  3. Derived class ConstructorBase 2 ConstructorBase 1 ConstructorBase 1 DestructorBase 2 DestructorDerived class Destructor

  4. Base 1 Constructor
    Base 2 Constructor
    Derived class Constructor
    Derived class Destructor
    Base 2 Destructor
    Base 1 Destructor


Correct Option: D
Explanation:

During inheritance, the order of constructor invocation is from parent to child and the destructor is called from child to parent. In case of mulitple inheritance, the constructors are called in order of inheritance. The base class 1 constructor will be invoked first followed by the constructor of the base 2 class, the derived class constuctor will be the last constructor to be called. The destructors will be called in the reverse order of constructors. Their order will be derived from base2->base1.

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; }
  1. The object 'objs' cannot access the show() method defined in the base class.

  2. The object 'objs' can not access the display().

  3. If we declare an object of the base class, it will be able to call the 'show()' method.

  4. The object 'objs' cannot access the print() defined in the sub class.


Correct Option: D
Explanation:

The print() is declared as public in the sub class. Therefore, an object of the sub class can easily access the print() method.

+ View questions