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 to access hardware that Java does not know about
-
to define a new data type such as an unsigned integer
-
to write optimised code for performance in a language such as C/C++
-
to overcome the limitation of the private scope of the method
A,C
Correct answer
Explanation
Native methods are defined to access hardware features that Java doesn't support directly (A), and to write performance-critical code in lower-level languages like C/C++ (C). They do not define new data types (B is wrong) and don't overcome private scope limitations (D is wrong).
-
SortedMap
-
Tree
-
Set
-
SortedSet
A
Correct answer
Explanation
TreeMap implements the SortedMap interface (now NavigableMap in newer Java versions). It maintains keys in sorted order. It does not implement Tree (not a standard interface), Set, or SortedSet.
-
Nested class is a way of logically grouping classes that are only used in one place.
-
A static nested class can refer directly to instance variables or methods defined in its enclosing class.
-
Nested classes can lead to more readable and maintainable code.
-
A static nested class interacts with the instance members of its outer class.
A
Correct answer
Explanation
The java.lang.String class is declared with the final modifier to prevent subclassing, ensuring string immutability and security.
A
Correct answer
Explanation
Type erasure is a fundamental process in Java generics where the compiler removes all generic type information at compile time. Generic types like List are converted to their raw forms (List), and type parameters are replaced with their bounds or Object. This ensures backward compatibility with pre-generic Java code while maintaining type safety during compilation.
-
implements
-
use
-
extends
-
extend
C
Correct answer
Explanation
Java uses the 'extends' keyword to declare bounded type parameters in generics, regardless of whether you're bounding to a class or interface. This creates an upper bound - the type parameter must be the specified type or a subclass. 'implements' is used for class declarations, not type parameters. 'extend' is a common misspelling - the keyword is 'extends'.
-
Error
-
Hello 1
-
Hi 3
-
Hello 3
C
Correct answer
Explanation
The line A = B reassigns the class name A to point to class B. Therefore, A() creates an instance of class B, and calling a.foo() executes B's foo method, which prints 'Hi 3'.
-
Interface Injection
-
Setter Injection
-
Constructoir Injection
-
Constructor Arguments
A,B,C
Correct answer
Explanation
Inversion of Control/Dependency Injection in Spring can be implemented through three main types: Interface Injection (where dependencies are injected through interfaces), Setter Injection (through setter methods), and Constructor Injection (through constructor parameters).
A
Correct answer
Explanation
Final variables must be initialized exactly once. Static final variables get their value from static initializers (or the declaration), while instance final variables can be initialized in constructors (or instance initializers). This is a fundamental Java rule.
-
Parent
-
Child
-
Both
-
None of these
A
Correct answer
Explanation
Static methods are bound at compile time based on reference type, not actual object type. Parent p = new Child(); p.staticMethod() calls Parent's version. This is because static methods belong to the class, not instances.
A
Correct answer
Explanation
Java allows declaring a derived class before its base class in source code. The compiler reads the entire file, so order doesn't matter. However, this is poor practice for readability. The statement is technically True.
B
Correct answer
Explanation
Static methods CANNOT be overridden - they can only be hidden. Overriding requires dynamic dispatch based on actual object type, but static methods are bound at compile time. If you define the same static method in a child class, it hides, not overrides.
-
return type should be different
-
access speciifer should be different
-
arguments should be different
-
the overloaded methods should throw a different excpetion
C
Correct answer
Explanation
Method overloading requires different parameter lists (type, count, or order). Return type, access specifier, and exception throws alone don't distinguish overloaded methods - the compiler would see them as duplicate methods.
-
Overriding method can not throw more generic exception than base method
-
Overriding method can throw new or broader checked exceptions
-
None of the above
-
Both a and b
A
Correct answer
Explanation
In Java, an overriding method in a subclass cannot throw broader or more generic checked exceptions than the overridden method in the superclass. Doing so violates the Liskov Substitution Principle and causes a compilation error.