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
-
The pattern that creates many instances of a single class in one shot
-
This pattern is to have only a single constructor to a class
-
Provides a class of which there can be no more than an instance, and provides a single global point of access to that instance
-
Creates and maintains a pool of objects, using which a single object can used and returned to the pool
C
Correct answer
Explanation
Singleton pattern ensures a class has only one instance and provides a global access point to it. This is useful for shared resources like database connections or logging services. Option A incorrectly describes creating many instances, which contradicts Singleton's purpose. Option B is insufficient because a single constructor alone doesn't prevent multiple instances. Option D describes the Object Pool pattern, which manages reusable objects.
-
Façade Pattern
-
Adapter Pattern
-
Bridge Pattern
-
Proxy Pattern
A
Correct answer
Explanation
The Facade pattern provides a simplified interface to a complex subsystem of classes, making it easier to use. It doesn't encapsulate the subsystem but provides a higher-level interface that reduces complexity for clients. Adapter Pattern converts one interface to another, Bridge Pattern separates abstraction from implementation, and Proxy Pattern controls access to an object.
B
Correct answer
Explanation
FileStream is a low-level stream class for reading and writing bytes. To write simple types (int, string, etc.) directly to a binary file, you need BinaryWriter or use serialization with FileStream. FileStream's Write methods only accept byte arrays, not typed data.
-
virtual,override
-
override,new
-
new,base
-
base,override
A
Correct answer
Explanation
In C#, method overriding requires the base class method to be marked 'virtual' (or abstract) and the derived class method to be marked 'override'. The 'virtual' keyword allows the method to be overridden, while 'override' explicitly indicates that the method overrides a base class method. 'new' hides the base method, 'base' calls the base class implementation.
-
Creates the class Test : Form
-
Creates the class Test that inherits the class Form
-
Creates the class form that inherits the class Test
-
a and b
B
Correct answer
Explanation
In C# inheritance syntax 'class derived : base', the class name before the colon (test) is the derived/child class, and the class name after the colon (Form) is the base/parent class. This means 'test' inherits from 'Form', not the reverse.
-
Delegate
-
Array
-
Enum
-
Class
C
Correct answer
Explanation
Enum is a value type in C#, while Delegate, Array, and Class are all reference types. Value types store their data directly, while reference types store a reference to the data's location in memory. Enums are derived from System.ValueType.
-
The root class(super class) of all classes in C# is Object
-
Constructors in C# can be static
-
When you override <= operator, you must override >= operator
-
If you write your own constructor in a class, C# also provides a default constructor
D
Correct answer
Explanation
If you define any custom constructor in a C# class, the compiler no longer automatically provides the parameterless default constructor. The other statements are true: Object is the root class, static constructors are supported, and comparison operators like <= and >= must be overloaded in pairs.
-
B, C
-
C, B and A
-
A, B and C
-
Object, A, B and C
D
Correct answer
Explanation
In C#, constructor execution starts from the highest base class in the inheritance hierarchy down to the derived class. Since all classes implicitly inherit from System.Object, its constructor runs first, followed by A, B, and finally C. Distractors fail to include Object or list the execution order in reverse.
-
Compilation succeeds.
-
Compilation fails due only to an error on line 3.
-
Compilation fails due only to an error on line 4.
-
Compilation fails due only to an error on line 5.
-
Compilation fails due only to an error on line 3 and 5.
-
Compilation fails due only to an error on line 3,4 and 5.
A
Correct answer
Explanation
All lines compile correctly. Line 3's array syntax 'int[] a []' is valid (array of arrays). Line 4 can assign a long array to an Object reference (upcasting). Line 5 assigns a String array to an Object array reference (covariant arrays are allowed in Java).
-
After line 8 runs.
-
After line 9 runs.
-
After line 10 runs.
-
After line 11 runs.
-
Compilation fails.
-
An exception is thrown at runtime.
F
Correct answer
Explanation
Line 10 attempts 'e2.e = el' but 'el' is undefined (typo for 'e1'). Since e2 was set to null on line 8, this would cause a NullPointerException. The GC eligibility questions become moot because the code throws an exception before completing.
-
Faster f = Faster.Higher;
-
Faster f = Better.Faster.Higher;
-
Better.Faster f = Better.Faster.Higher;
-
Bigger.Faster f = Bigger.Faster.Higher;
-
Better. Faster f2; f2 = Better.Faster.Longer;
-
Better b; b.Faster = f3; f3 = Better.Faster.Longer;
C,E
Correct answer
Explanation
Since Faster is a nested enum inside the class Better, it must be referenced using its outer class name. Thus, Better.Faster is the correct type, and Better.Faster.Higher or Better.Faster.Longer are the correct value references. Extra whitespace in Better. Faster is ignored by the compiler, making both selected options valid.
-
pre b1 b2 r3 r2 hawk
-
pre b2 b1 r2 r3 hawk
-
pre b2 b1 r2 r3 hawk r1 r4
-
r1 r4 pre b1 b2 r3 r2 hawk
-
r1 r4 pre b2 b1 r2 r3 hawk
-
Compilation fails.
D
Correct answer
Explanation
Java initialization order: static blocks execute top-to-bottom when class is loaded, then for each object: instance initializers (top-to-bottom) then constructor. The trace shows: Bird static (prints nothing), then Raptor static (r1, r4), then 'pre', then Hawk() calls Raptor() which calls Bird(): Bird instance initializer (b1), Bird constructor (b2), then back to Raptor instance initializer (r3), Raptor constructor (r2), then 'hawk'. Result: r1 r4 pre b1 b2 r3 r2 hawk.
-
They can be applied to both data & methods
-
They must precede a class's data variables or methods
-
They can follow a class's data variables or methods
-
They can appear in any order
-
They must be applied to data variables first and then to methods
B
Correct answer
Explanation
Access modifiers (public, private, protected) must appear BEFORE the variable's type or the method's return type in Java declarations. The syntax is: [modifier] type name = value. Option A is true but B is more specific about placement. Options C and E are wrong - modifiers cannot follow the declaration. Option D is wrong - order matters.
-
They are not associated with any instance of an outer class
-
They may access final initialized variables and parameters that are in the scope of the statement block in which the class is declared
-
They may be declared public, protected or private
-
They may not implement an interface
-
A collection of method declarations and constant values
-
An abstract base class
-
A way to get multiple inheritance
-
A way to inherit variables and method implementations
A
Correct answer
Explanation
An interface in Java is a collection of abstract method declarations and constant values. It defines a contract without implementation - classes implement interfaces to provide specific behavior while achieving a form of multiple inheritance of type.