Computer Knowledge

Object-Oriented Programming

2,239 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 technology programming languages
  1. A class that is abstract may not be instantiated.

  2. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.

  3. A static variable indicates there is only one copy of that variable.

  4. A method defined as private indicates that it is accessible to all other classes in the same package.

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

An abstract class cannot be instantiated directly, and a static variable has only one copy shared across all instances of a class. The final keyword does not mean a method is implemented elsewhere (that is native), and private restricts access to the defining class only.

Multiple choice technology programming languages
  1. public

  2. private

  3. friendly

  4. transient

Reveal answer Fill a bubble to check yourself
A,B,D Correct answer
Explanation

public and private are access modifiers controlling class member visibility. transient is a field modifier indicating a variable should not be serialized. 'friendly' is not a Java modifier - the default (package-private) access has no keyword.

Multiple choice technology programming languages
  1. to get to access hardware that Java does not know about

  2. to define a new data type such as an unsigned integer

  3. to write optimised code for performance in a language such as C/C++

  4. to overcome the limitation of the private scope of the method

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

Native methods are declared in Java but implemented in platform-specific languages like C or C++. This is done to access low-level hardware features or to optimize performance-critical code.

Multiple choice technology programming languages
  1. SortedMap

  2. Tree

  3. Set

  4. SortedSet

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

To solve this question, the user needs to know what the TreeMap class is used for and which collection interface it implements.

The TreeMap class in Java is used to implement a map interface that sorts the keys in natural order. It provides an efficient means of storing key/value pairs in sorted order, making it useful for scenarios where you need to perform operations such as lookups, insertions, and deletions in a sorted collection.

Now, let's go through each option and explain why it is right or wrong:

A. SortedMap: This option is correct. TreeMap implements the SortedMap interface, which extends the Map interface and provides additional methods for retrieving and manipulating elements in a sorted order.

B. Tree: This option is incorrect. While TreeMap is implemented using a tree structure, it does not directly implement the Tree interface.

C. Set: This option is incorrect. TreeMap does not implement the Set interface, which is used for collections of unique elements.

D. SortedSet: This option is incorrect. TreeMap does not implement the SortedSet interface, which is used for sorted collections of unique elements.

The Answer is: A

Multiple choice technology programming languages
  1. A final method in class X can be abstract if and only if X is abstract.

  2. A private static method can be called only within other static methods in class X.

  3. A protected method in class X can be overridden by any subclass of X.

  4. A non-static public final method in class X can be overridden in any subclass of X.

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

Protected methods in Java are accessible within the same package and to subclasses (even in different packages), and can be overridden by any subclass unless marked final. Option C is correct. Option A is wrong because final methods cannot be abstract. Option B is wrong because private static methods can be called from non-static methods too. Option D is wrong because final methods cannot be overridden.

Multiple choice technology programming languages
  1. True

  2. False

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

Static member functions belong to the class rather than individual objects, so they don't have a 'this' pointer. They can only access static members of the class. The 'this' pointer only exists for non-static member functions that operate on specific object instances.

Multiple choice technology programming languages
  1. Operator overloading

  2. Virtual functions

  3. Virtual base class

  4. Function overloading

Reveal answer Fill a bubble to check yourself
A,B,D Correct answer
Explanation

Polymorphism in C++ is achieved through compile-time methods (function overloading, operator overloading) and runtime methods (virtual functions). Virtual base class is used to solve the diamond problem in multiple inheritance but is not itself a type of polymorphism - it's an inheritance-related concept.

Multiple choice technology programming languages
  1. a only

  2. a and b

  3. b only

  4. none of the above

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

Statement (a) is false - struct members are public by default in C++ (class members are private by default). Statement (b) is true - new and delete operators can be overloaded. Statement (c) is technically incorrect because the subscript operator [] and assignment operator = must be non-static member functions, not friend functions.

Multiple choice technology programming languages
  1. Value types are stored in heap and reference types are stored in stack area.

  2. Boxing allows users to convert value types to reference types

  3. Unboxing allows users to convert value type to reference type.

  4. During runtime boxing creates a temporary reference type box for the object on the heap.

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

Boxing converts value types to reference types by creating an object on the heap and copying the value type value there (Option B and D are correct). Option A is wrong because value types are stored on stack, not heap. Option C is incorrect because unboxing converts reference types back to value types, not value to reference.

Multiple choice technology programming languages
  1. True

  2. False

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

Non-static inner classes in Java cannot have static members because they are associated with an instance of the enclosing class. Static members belong to the class itself, not an instance, creating a fundamental conflict. However, static nested classes (declared with 'static' keyword) can have static members. The question is technically correct but should specify 'non-static inner class'.

Multiple choice technology programming languages
  1. Parent

  2. Child

  3. Both

  4. None of these

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

Static methods are bound at compile-time based on the reference type, not the actual object type. When you have a parent class reference and assign a child object, calling a static method invokes the parent class version, not the child's.