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
Correct answer
Explanation
Yes, hiding private constructs in the package body is a key advantage of packages. This implements information hiding and encapsulation - the package specification exposes only what users need, while implementation details remain private and inaccessible. This prevents users from depending on or misusing internal implementation details, allowing the package developer to change the internal logic without affecting users.
-
a) Only local or packaged sub programs can be overloaded.
-
b) Overloading allows different functions with the same name that differ only in their return types.
-
c) Overloading allows different subprograms with the same number, type and order of the parameter.
-
d) Overloading allows different subprograms with the same name and same number or type of the parameters.
-
e) 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 Oracle allows multiple subprograms with the same name if they differ in their parameter signatures. Only local or packaged subprograms can be overloaded (standalone subprograms cannot). The key rule is: same name but different in number, type, or order of parameters. Return types alone are insufficient for overloading - two subprograms differing only in return type would cause ambiguity.
-
a) Packages can be nested.
-
b) You can pass parameters to packages.
-
c) A package is loaded into memory each time it is invoked.
-
d) The contents of packages can be shared by many applications.
-
e) You can achieve information hiding by making package constructs private.
D,E
Correct answer
Explanation
Oracle packages support information hiding through private constructs (package body vs specification) and enable code sharing across applications. Packages cannot be nested, you cannot pass parameters to packages themselves (only to their subprograms), and packages are loaded once per session then cached - not reloaded on each invocation. These design features make packages efficient and modular.
-
return super.hashCode();
-
return name.hashCode() + age * 7;
-
return name.hashCode() + comment.hashCode() / 2;
-
return name.hashCode() + comment.hashCode() / 2 - age * 3;
B
Correct answer
Explanation
The hashCode method must use the same fields as equals() to maintain the hashCode contract. The equals method compares age and name, so option B correctly uses only those fields. Options C and D incorrectly include 'comment' which is not part of equals comparison, violating the contract.
B
Correct answer
Explanation
Abstract methods cannot be static because abstract methods are designed to be overridden in subclasses, while static methods belong to the class itself and are not inherited for overriding. Making a method both abstract and static would be a contradiction in terms.
A
Correct answer
Explanation
In Java, the Hashtable class extends the abstract class Dictionary. Although Dictionary is obsolete, it remains the direct superclass of Hashtable.
A
Correct answer
Explanation
The java.lang package is automatically and implicitly imported by the Java compiler into every compilation unit, meaning classes like String or System do not require explicit import statements.
A
Correct answer
Explanation
Marker interfaces are interfaces with no methods that serve to mark a class for a specific purpose. Serializable, Cloneable, and Remote are classic examples - they tell the JVM or framework to treat the implementing class specially (e.g., make it serializable).
B
Correct answer
Explanation
Only objects of classes that implement the Cloneable interface can be cloned using the clone() method. Attempting to clone an object that does not implement this interface throws a CloneNotSupportedException.
A
Correct answer
Explanation
Static variables belong to the class itself and are shared among all instances, whereas non-static (instance) variables have unique values for each individual object instance.
A
Correct answer
Explanation
The java.util.Locale class represents a specific geographical, political, or cultural region, allowing developers to localize numbers, dates, and strings.
-
Regular Expressions
-
Parameterization
-
Descriptive Programming
-
All the Above
C
Correct answer
Explanation
Descriptive Programming allows you to define objects dynamically in your code using property-value pairs, making it ideal for objects whose properties change at runtime. Regular Expressions have limited flexibility, while Parameterization is for test data, not object identification.
-
Object Spy
-
Object Repository Manager
-
Object Identification
-
All the above
C
Correct answer
Explanation
In UFT (Unified Functional Testing), Object Identification is the feature where you define and configure the mandatory and assistive properties that the tool uses to identify and recognize test objects during runtime. The Object Spy is used to view the properties and methods of an object at runtime, while Object Repository Manager is used to manage shared object repositories. Only Object Identification lets you configure how the tool identifies objects.
-
The equals method does NOT properly override the Object.equals method.
-
Compilation fails because the private attribute p.name cannot be accessed in line 5.
-
To work correctly with hash-based data structures, this class must also implement the hashCode method.
-
When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.
A
Correct answer
Explanation
The equals method shown does NOT properly override Object.equals(Object obj) because it takes a Person parameter instead of Object. This violates the overriding contract - the signature must match. For proper overriding, the parameter should be Object with an instanceof check inside. This equals method will not be called polymorphically when a Person is compared to a non-Person Object.