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
-
To get information about event-handler
-
To get or set property values
-
Classes in other namespace can use reflection to access data and to determine which fields to persist
-
None of the above
D
Correct answer
Explanation
Reflection can inspect event handlers, get/set property values dynamically, and allow cross-namespace access to type metadata. All these are valid capabilities of the System.Reflection namespace in .NET.
-
using System.Collections.Generic; Dictionary < string , StringBuilder >() dctobj = new Dictionary < string , StringBuilder >;
-
using System.Collections.Generic; Dictionary < string , StringBuilder > dctobj = new Dictionary < string , StringBuilder >();
-
using System.Collections.Generic; Dictionary < string , StringBuilder > dctobj = new Dictionary < string , StringBuilder >;
-
using System.Text; Dictionary < string , StringBuilder > dctobj = new Dictionary < string , StringBuilder >();
B
Correct answer
Explanation
Option B shows correct Dictionary initialization syntax with proper type parameters and parentheses for instantiation. Option A is missing instantiation parentheses. Option C is missing parentheses. Option D uses wrong namespace (System.Text for StringBuilder is fine, but Dictionary needs Collections.Generic).
-
Throwable
-
Catchable
-
Runable
-
Problem
A
Correct answer
Explanation
To solve this question, the user needs to know the hierarchy of Java exceptions and errors.
Both class Error and class Exception are children of the parent class Throwable. This means that any class that extends Throwable can be used to represent an error or an exception that occurs during the execution of a Java program.
Option A is correct because Throwable is the parent class of both Error and Exception.
Option B is incorrect because there is no class called Catchable in Java.
Option C is incorrect because Runnable is an interface in Java, not a class that represents an error or an exception.
Option D is incorrect because there is no class called Problem in Java that represents an error or an exception.
Therefore, the answer is: A
-
Only local or packaged sub programs can be overloaded.
-
Overloading allows different functions with the same name that differ only in their return types.
-
Overloading allows different subprograms with the same number, type and order of the parameter.
-
Overloading allows different subprograms with the same name and same number or type of the parameters.
-
Overloading allows different subprograms with the same name but different in either number or type or order of parameter.
A,E
Correct answer
Explanation
Package overloading in PL/SQL allows multiple subprograms with the same name to coexist if they differ in their parameter signatures. Only local subprograms or packaged subprograms can be overloaded - standalone procedures cannot use overloading (A is true). Overloading requires that subprograms differ in the number, type, or order of parameters - having the same parameter signature is not allowed (E is true). Option B is false because overloading based solely on return type is not permitted. Option C is false because overloading requires differences in parameters. Option D is false because subprograms with identical parameters cannot be overloaded.
-
Packages can be nested.
-
You can pass parameters to packages.
-
A package is loaded into memory each time it is invoked.
-
The contents of packages can be shared by many applications.
-
You can achieve information hiding by making package constructs private.
D,E
Correct answer
Explanation
PL/SQL packages cannot be nested, nor can they accept parameters directly (though their subprograms can). They are loaded into memory once, not on every invocation. The true statements are that package contents can be shared and information hiding is achieved by making constructs private.
B
Correct answer
Explanation
Identifiers defined only in the package body (not in the package specification) are PRIVATE constructs, not public. They are invisible outside the package body and can only be used by other subprograms within the same package body. To make identifiers public and visible outside the package, they must be declared in the package specification. Therefore, the statement in the question is false, making option B the correct answer.
A
Correct answer
Explanation
Private constructs defined in the package body are completely hidden from external access. This enforces information hiding and encapsulation, which is a major advantage of PL/SQL packages.
A
Correct answer
Explanation
In exception handling hierarchies, a catch block can catch exceptions of its declared type or any subclass of that type due to polymorphism. If you catch Exception ( superclass), it will also catch IOException, SQLException, etc. (subclasses). This is a fundamental principle of polymorphic exception handling in object-oriented programming.
B
Correct answer
Explanation
In Java, a class cannot be both final and abstract because these modifiers have contradictory purposes. 'final' prevents inheritance (no subclasses allowed), while 'abstract' requires inheritance (must be subclassed to be instantiated). This creates an oxymoron - a class that both requires and forbids subclasses simultaneously. The compiler will reject this combination.
A
Correct answer
Explanation
Abstract classes can and often do have constructors. Although you cannot instantiate an abstract class directly, its constructor is called when a concrete subclass is instantiated using super() in the subclass constructor. This is useful for initializing fields defined in the abstract class that are common to all subclasses. The constructor executes before the subclass constructor's code.
A
Correct answer
Explanation
In Java, it is syntactically legal to access a static member or method using an instance variable of that class, although it is discouraged as a bad practice because static members belong to the class itself.
A
Correct answer
Explanation
Non-static inner classes are tied to instances of their outer class. You cannot create an inner class instance without first having an outer class instance, as the inner class implicitly holds a reference to its enclosing object.
-
No
-
Compile Error
-
Yes
-
Runtime Error
-
Not Sure
C
Correct answer
Explanation
Since Class Y is a subclass of Class X, an instance of Y can be implicitly upcast to a reference of type X. This is a fundamental polymorphic assignment in Java and compiles successfully.
A
Correct answer
Explanation
In Java, a method with no access modifier (package-private) can indeed be overridden by a method marked protected. This is valid because protected provides broader access than package-private, allowing subclasses (even in different packages) to access the method. The Liskov substitution principle permits widening access visibility in overriding methods.