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 technology programming languages
  1. Class A 'is-a' Class B

  2. Class B 'is-a' Class A

  3. Class A 'has-a' Class B

  4. Class B 'has-a' Class A

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

Inheritance represents an 'is-a' relationship. When class B extends class A, B is a subclass of A, meaning 'Class B is-a Class A'. The reverse ('Class A is-a Class B') is incorrect because subclasses have more specific behavior. Composition represents 'has-a'.

Multiple choice technology web technology
  1. True

  2. False

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

Interfaces define abstract contracts with method signatures only, without implementation details like fields or method bodies. This is the fundamental purpose of interfaces - to specify what a type can do without defining how. Concrete types that implement interfaces provide the actual implementation. Therefore, the statement is false.

Multiple choice technology web technology
  1. multiple inheritance

  2. interface

  3. abstract

  4. None of the above

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

C# does not support multiple inheritance of classes to avoid the diamond problem and complexity. A class can only inherit from one base class. However, C# supports interfaces (option B) and abstract classes (option C), which are alternatives to achieve similar functionality. The correct answer is A - multiple inheritance (of classes) is not supported.

Multiple choice technology web technology
  1. Interface

  2. abstract class

  3. static class

  4. All the above

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

C# uses interfaces as the primary alternative to multiple inheritance. A class can implement multiple interfaces, gaining polymorphic behavior from each, while still inheriting implementation from only one base class. Abstract classes (option B) are single-inherited like regular classes. Static classes (option C) cannot be inherited at all. Therefore, interfaces are the correct alternative.

Multiple choice technology programming languages
  1. String doFileWork() { return "b"; }

  2. String doFileWork() throws IOException ( return "b"; }

  3. String doFileWork(int x) throws IOException { return "b"; }

  4. String doFileWork() throws FileNotFoundException { return "b"; }

  5. String doFileWork() throws NumberFormatException { return "b"; }

  6. String doFileWork() throws NumberFormatException, FileNotFoundException { return "b"; }

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

When overriding a method, the overriding method cannot throw broader or new checked exceptions. It can only throw the same exceptions, subclasses of those exceptions, or unchecked exceptions (RuntimeException). Options A (no exception), D (same FileNotFoundException), E and F (NumberFormatException is unchecked/RuntimeException) are valid. Option B (IOException is broader than FileNotFoundException) violates this rule. Option C is not an override (different parameter list - overloaded method).

Multiple choice technology web technology
  1. public class MyButton extends Button

  2. public class MyButton becomes Button

  3. public class MyButton inherits Button

  4. public class MyButton implements Button

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

ActionScript uses the 'extends' keyword for class inheritance, following Java's syntax. The correct declaration is 'public class MyButton extends Button'. Option B uses 'becomes' which is not valid. Option C uses 'inherits' which is not ActionScript syntax. Option D uses 'implements' which is for interfaces, not class inheritance.

Multiple choice technology web technology
  1. private var arrC :ArrayCollection = new ArrayCollection([{label:"abc"},{label:"def"}]);

  2. private var arrC :ArrayCollection =([{label:"abc"},{label:"def"}]);

  3. private var arrC :ArrayCollection = new ArrayCollection([{"abc"},{"def"}]);

  4. private var arrC :ArrayCollection = new ArrayCollection({label:"abc"},{label:"def"});

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

ArrayCollection must be instantiated with the 'new' keyword and constructor. Option A shows correct syntax: 'new ArrayCollection([{label:"abc"},{label:"def"}])'. Option B is missing 'new' and constructor call. Option C has malformed objects without proper key-value structure. Option D passes two separate objects instead of an array.

Multiple choice technology web technology
  1. Both are Interfaces

  2. Both are Classes

  3. OAApplicationModule is a class and OAApplicationModuleImpl is an interface

  4. OAApplicationModule is an interface and OAApplicationModuleImpl is a class

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

In Oracle's naming convention, classes ending with 'Impl' are implementation classes, while names without 'Impl' are typically interfaces. OAApplicationModule is the interface, and OAApplicationModuleImpl is its concrete implementation class. Option D correctly identifies this relationship.

Multiple choice technology web technology
  1. True

  2. False

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

C# does not support multiple inheritance of classes. While it supports multiple inheritance through interfaces, the statement about multiple inheritance of classes is false.

Multiple choice technology programming languages
  1. Abstract

  2. Virtual

  3. Dynamic

  4. Typeid

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

Virtual keyword in C++ enables dynamic method resolution through virtual functions and polymorphism. Virtual functions allow methods to be overridden in derived classes and the correct method to be called at runtime based on the actual object type. Abstract, Dynamic, and Typeid serve different purposes - abstract for pure virtual functions, typeid for type information.

Multiple choice technology programming languages
  1. Exception Handling

  2. Reflection

  3. Operator Overloading

  4. Namespaces

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

C++ does not support reflection natively. Exception handling is supported via try-catch blocks, operator overloading is a core feature allowing operators to be redefined for user-defined types, and namespaces are supported to organize code. Reflection would require runtime type information that isn't part of standard C++.

Multiple choice technology programming languages
  1. X(X arg)

  2. X(X* arg)

  3. X(const X* arg)

  4. X(const X& arg)

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

A copy constructor in C++ must take a constant reference to the same class type. It uses X(const X& arg) to avoid unnecessary copying and allow passing both lvalues and rvalues. The other options use pointer syntax which would create different constructor signatures and not serve as copy constructors.

Multiple choice technology programming languages
  1. Set

  2. SortedSet

  3. List

  4. Tree

  5. SortedMap

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

TreeMap implements the SortedMap interface (and AbstractMap). It provides a red-black tree implementation of a Map that maintains its keys in sorted order. SortedMap is the interface that defines the contract for maps with sorted keys. Options A (Set), B (SortedSet), C (List), and D (Tree) are incorrect interfaces for TreeMap.