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
B
Correct answer
Explanation
In Java, the base class must be declared before the derived class, or at least be available to the compiler. The compiler needs to know the base class's structure to process inheritance syntax and verify that methods and fields used in the derived class are valid.
A
Correct answer
Explanation
In Java, blank final variables can be initialized exactly once. A static blank final variable must be initialized in a static initializer block, while an instance blank final variable must be initialized in every constructor.
-
both are same
-
abstract class has only method signature
-
interface has only method signature
-
none of the above
-
HashMap is Interface and Map is class that implements that.
-
Both are interfaces
-
Map is Interface and Hashmap is class that implements that.
-
None of the above
C
Correct answer
Explanation
In Java, Map is an interface that defines the contract for key-value mappings, while HashMap is a specific class that implements the Map interface using a hash table.
-
Compilation error at line 5
-
Compilation error at line 9
-
Runtime exception at line 11
-
None of these
D
Correct answer
Explanation
The code compiles successfully. SubclassY extends SuperclassX (protected method, package-private field) from packageX.packageY - subclasses can access protected members. Line 5 is valid (polymorphism: SubclassY IS-A SuperclassX). Lines 9-11 access protected method and package-private field through a SubclassY reference, which is valid within the same package hierarchy. The code compiles and runs without errors, so 'None of these' (D) is correct.
-
public
-
private
-
abstract
-
protected
A,C
Correct answer
Explanation
Interface methods in Java are implicitly public and abstract. They cannot be private (no implementation to hide) or protected (interfaces define public contracts). Abstract is explicit but allowed since it's implicit anyway.
-
only pass by value
-
only pass by reference
-
supports both
-
none of the above
A
Correct answer
Explanation
Java always passes references by value - the reference itself is copied, but both copies point to the same object. This means modifications to the object are visible to the caller, but reassigning the reference is not. The 'pass by value' terminology refers to the reference copy, not the object.
-
a. FilePathXmlApplicationContext class
-
b. SystemXmlPathApplicationContext class
-
c. FileSystemXmlApplicationContext class
-
d. XmlFileApplicationContext class
C
Correct answer
Explanation
The question text is missing ('1.)'), but based on the options about ApplicationContext implementations, FileSystemXmlApplicationContext (option C) is indeed a valid Spring ApplicationContext implementation that loads configuration from the file system.
-
a. TransactionalTestExecutionListener class
-
b. AbstractTransactionalSpringContextTests class
-
c. TestExecutionListener class
-
d. AbstractTestExecutionListener class
A
Correct answer
Explanation
TransactionalTestExecutionListener is the TestContext framework class responsible for managing transactions in tests - it handles transaction creation, rollback for test isolation, and cleanup.
-
serialization support
-
platform independent
-
object diagnostic output
-
run-time class information
A,C,D
Correct answer
Explanation
CObject provides serialization support through the Serialize method, runtime class information via DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC macros, and diagnostic dumping through Dump. Platform independence is not a feature - CObject is part of Windows-specific MFC.
B
Correct answer
Explanation
The method Print() is an instance method, not a static method. Therefore, it must be called on an instance of the class (f.Print()), not on the class name itself (Foo.Print()).
-
Member Function
-
Same name as Class name
-
Default constructor initializes all non-initialized fields and variables to zero.
-
Compiler adds an empty constructor, if we do not write our own constructor
A,B,C,D
Correct answer
Explanation
Constructors are special member functions with the same name as their class. The default constructor initializes primitive fields to zero/null. If no constructor is written, the compiler provides a default constructor.
-
Java Foundation Course
-
Java Fundamental Classes
-
Java Foundation Classes
-
None of the above
C
Correct answer
Explanation
JFC stands for Java Foundation Classes, which is the GUI framework for Java including Swing.