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

Multiple choice
  1. void

  2. virtual

  3. abstract

  4. operator

Reveal answer Fill a bubble to check yourself
D Correct answer
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'.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
C Correct answer
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();

Multiple choice
  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}

Reveal answer Fill a bubble to check yourself
A Correct answer
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.

Multiple choice
  1. 6

  2. 4

  3. 17

  4. 8

Reveal answer Fill a bubble to check yourself
C Correct answer
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).

Multiple choice
  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

Reveal answer Fill a bubble to check yourself
D Correct answer
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.

Multiple choice
  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.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

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

Multiple choice
  1. ContentProvider

  2. ContentResolver

  3. AccessProvider

  4. None of above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

ContentResolver is the client-side class that applications use to communicate with Content Providers. You call getContentResolver() to get an instance, then use its query(), insert(), update(), and delete() methods. ContentProvider is the server-side class you implement to expose data.

Multiple choice
  1. 1 2 1

  2. 2 1 1

  3. 2 1 2

  4. 2 2 2

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Multiple choice
  1. class module

  2. form module

  3. standard module

  4. code module

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

In Visual Basic 6.0, CLS files are class module files that contain object-oriented code for defining classes. Class modules are used to create objects with properties and methods. Standard modules use .BAS extension, form modules use .FRM, and code modules aren't a distinct file type.

Multiple choice
  1. It is used to declare the name and accessibility scope of the class.

  2. The declared class can implement one or more interfaces.

  3. It provides specific directives for the compiler, references to namespaces and classes, and namespace declaration.

  4. This space is above the Directive space.

  5. It declares the name and accessibility scope of the class and method.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Class declaration comes immediately after the class is named and its access and utility are provided.

Multiple choice
  1. It provides a construct for passing singleton method signatures as bound references to a consumer construct.

  2. It is not type safe and secure.

  3. These are value types and are also known as lightweight classes.

  4. These can be used for robust applications.

  5. Delegates are useful in data naming.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

This is correct about Delegates.

Multiple choice
  1. classes

  2. interfaces

  3. both (1) and (2)

  4. none of these

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In C#, structures (structs) cannot inherit from classes but can implement one or more interfaces. Structs implicitly inherit from System.ValueType and are sealed by default. Implementing interfaces allows structs to provide polymorphic behavior without inheritance.