Computer Knowledge
Object-Oriented Programming
2,686 Questions
Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.
Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects
Object-Oriented Programming Questions
-
Inheritance
-
Encapsulation
-
Abstraction
-
Procedural programming
D
Correct answer
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.
-
Public member of a class
-
Private member of a class
-
Protected member of a class
-
Members of the child class
D
Correct answer
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.
-
When an object is used.
-
When a class is declared.
-
When an object is declared.
-
When a class is used.
C
Correct answer
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'
-
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.
C
Correct answer
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);
-
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.
A
Correct answer
Explanation
Declaring a destructor does not need a constructor to be declared. A destructor can be declared even when there is no constructor.
-
-
? (Question mark)
-
: (Colon)
-
:: (Scope resolution operator)
D
Correct answer
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<
-
private
-
public
-
protected
-
friend
A
Correct answer
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.
-
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.
B
Correct answer
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.
-
virtual class
-
friend class
-
abstract class
-
derived class
C
Correct answer
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.
-
overloading
-
overriding
-
ambiguity
-
destructor
C
Correct answer
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)'.
-
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.
A
Correct answer
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.
-
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.
B
Correct answer
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
-
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.
C
Correct answer
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.
-
Copy
-
Default
-
Parameterized
-
Dynamic
D
Correct answer
Explanation
There is no dynamic constructor in C++. The different types of constructors are -- Default, Parameterized and Copy.
-
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.
B
Correct answer
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.