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
-
A super class is a sub set of a sub class
-
class ClassTwo extends ClassOne means ClassOne is a subclass
-
class ClassTwo extends ClassOne means ClassTow is a super class
-
the class Class is the super class of all other classes in Java.
-
HashTable is a sub class of Dictionary
-
ArrayList is a sub class of Vector
-
LinkedList is a subclass of ArrayList
-
Vector is a subclass of Stack
A
Correct answer
Explanation
Hashtable extends Dictionary in Java's legacy collections framework. Note: Dictionary is deprecated and Hashtable should be Hashtable (capital T), but the inheritance relationship is correct.
-
LinkedList extends List
-
AbstractSet extends Set
-
HashSet extends AbstractSet
-
WeakHashMap extends HashMap
C
Correct answer
Explanation
HashSet extends AbstractSet in Java's collections framework. LinkedList implements List (List is an interface), AbstractSet implements Set, and WeakHashMap extends AbstractMap.
-
protected
-
extends Throwable
-
implements Throwable
-
serializable
B
Correct answer
Explanation
Exception extends Throwable in Java's exception hierarchy. Throwable is the base class, with Exception and Error as its two main subclasses. Checked and unchecked exceptions extend from Exception.
-
An instance of a class is an object
-
Objects can access both static and instance data
-
Object is the super class of all other classes
-
Objects do not permit encapsulation
D
Correct answer
Explanation
Objects are the fundamental units in OOP that DO permit encapsulation - they bundle data and methods together while restricting access to internal details. Option A is true because objects are instances of classes. Option B is true because objects can access both static (class-level) and instance data. Option C is true because in Java, Object is indeed the root class from which all other classes inherit.
-
Only Static methods of the same class
-
Only instances of the same class
-
Only methods those defined in the same class
-
Only classes available in the same package.
C
Correct answer
Explanation
In Java, private members are accessible only within the same class - whether from static methods, instance methods, or other instances of that same class. Methods defined in the same class can access private attributes regardless of whether they are static or instance methods. Other classes in the same package cannot access private members.
-
Method Invocating
-
Method Overriding
-
Method Labeling
-
Method Overloading
D
Correct answer
Explanation
Method overloading allows a class to have multiple methods with the same name but different parameter lists (different number of parameters, different types, or both). This is compile-time polymorphism. Method overriding is when a subclass provides a specific implementation of a method already defined in its superclass (same name and parameters).
-
Object
-
Class
-
Method
-
Real data types
B
Correct answer
Explanation
A class is a blueprint or template that defines the structure and behavior of objects. It specifies what attributes (variables) and methods will be common to all objects created from it. Objects are the actual instances created from this blueprint. Methods define behavior, and real data types are primitive types, not blueprints.
-
An object with only primitive attributes
-
An instance of a class which has only static methods
-
An instance which has other objects
-
None of the above
C
Correct answer
Explanation
An aggregate object is an object that contains or references other objects as part of its state. This represents a 'has-a' relationship (composition/aggregation). For example, a Car object might contain Engine, Wheel, and Seat objects. This is different from an object having only primitive attributes or only static methods.
-
Binary File
-
Image File
-
Both File and Binary Files
-
None of the above
B
Correct answer
Explanation
This is polymorphism through dynamic method dispatch. When a superclass reference points to a subclass object (File reference to ImageFile), and a method is called, Java uses the actual object's type (not the reference type) to determine which implementation to execute. Since the reference points to an ImageFile object, the ImageFile's toFile() method will be called.
-
Public class MyApplet extends java.applet.Applet {
-
public Applet MyApplet {
-
public class MyApplet extends applet implements Runnable {
-
public class MyApplet extends java.applet.Applet {
-
Object
-
Window
-
Panel
-
Component
C
Correct answer
Explanation
In Java AWT, the Applet class directly extends Panel, making Panel its immediate superclass. The class hierarchy is: Applet -> Panel -> Container -> Component -> Object.
-
Font class
-
Regular class
-
FontMetrics class
-
Graphics class
C
Correct answer
Explanation
FontMetrics is a specialized class that provides font measurement data like ascent (distance from baseline to top of characters) and descent (distance from baseline to bottom). The Font class itself represents the font but doesn't provide these detailed metrics.
D
Correct answer
Explanation
This is the correct answer. Streams can be used to input and output the data. They are of two types :-Byte Stream :- for handling binary data.Character Stream :- for handling data such as sequence of characters.Thus this is the correct answer.
B
Correct answer
Explanation
This is the correct answer. there are 7 subclasses of InputStream class.hese are :-1.) FileInputStream :- for reading bytes from a file.2.) ObjectInputStream :- for readin objects and primitive data types from input stream.3.) SequenceInputStream :- for readin from a sequence of streams.4.) ByteArrayInputStream :- for Stream that reads a byte array.5.) pipedInputStream :- for reading from a piped stream.6.) FilterInputStream :- for filtering input from Existing Stream.7.) StringBufferInputStream:- for readin from a string.These are the 7 subclasses.So this is correct answer.