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
-
Class A 'is-a' Class B
-
Class B 'is-a' Class A
-
Class A 'has-a' Class B
-
Class B 'has-a' Class A
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'.
-
Class
-
Constructor
-
Object
-
a and b
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 inheritance
-
interface
-
abstract
-
None of the above
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.
-
Interface
-
abstract class
-
static class
-
All the above
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.
-
String doFileWork() { return "b"; }
-
String doFileWork() throws IOException ( return "b"; }
-
String doFileWork(int x) throws IOException { return "b"; }
-
String doFileWork() throws FileNotFoundException { return "b"; }
-
String doFileWork() throws NumberFormatException { return "b"; }
-
String doFileWork() throws NumberFormatException, FileNotFoundException { return "b"; }
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).
-
public class MyButton extends Button
-
public class MyButton becomes Button
-
public class MyButton inherits Button
-
public class MyButton implements Button
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.
-
private var arrC :ArrayCollection = new ArrayCollection([{label:"abc"},{label:"def"}]);
-
private var arrC :ArrayCollection =([{label:"abc"},{label:"def"}]);
-
private var arrC :ArrayCollection = new ArrayCollection([{"abc"},{"def"}]);
-
private var arrC :ArrayCollection = new ArrayCollection({label:"abc"},{label:"def"});
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.
-
Both are Interfaces
-
Both are Classes
-
OAApplicationModule is a class and OAApplicationModuleImpl is an interface
-
OAApplicationModule is an interface and OAApplicationModuleImpl is a class
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.
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.
-
Abstract
-
Virtual
-
Dynamic
-
Typeid
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.
-
Exception Handling
-
Reflection
-
Operator Overloading
-
Namespaces
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++.
-
X(X arg)
-
X(X* arg)
-
X(const X* arg)
-
X(const X& arg)
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.
-
Set
-
SortedSet
-
List
-
Tree
-
SortedMap
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.