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
-
Exception
-
Compiler Error
-
I am in Constructor
-
Message Hello
A
Correct answer
Explanation
The main method is missing the 'static' keyword. Java requires the main method to be public static void to serve as an entry point - the JVM must be able to call it without instantiating an object. A non-static main method cannot be invoked by the JVM, causing an exception at runtime.
-
A class
-
An object
-
A method
-
A data field
B
Correct answer
Explanation
In object-oriented programming, an object represents a specific, distinctly identifiable real-world entity (e.g., a specific car or person) with a state and behavior. A class is a blueprint or template for creating objects, a method represents behavior, and a data field represents state or properties.
-
A class
-
An object
-
A method
-
A data field
A
Correct answer
Explanation
A class is a blueprint or template that defines the structure and behavior of objects of the same type. An object is a specific instance (option B is wrong). A method is a function defined in a class (option C is wrong). A data field is a single variable within a class (option D is wrong).
-
program
-
class
-
method
-
data
B
Correct answer
Explanation
An object is created as an instance of a class. The class defines the type, and when you instantiate it with 'new', you create an object that belongs to that class type. A program contains many objects, methods are behaviors defined in classes, and data is what objects store.
-
public
-
private
-
class
-
All of the above.
C
Correct answer
Explanation
To declare a class in Java and similar languages, the 'class' keyword is required. Public and private are access modifiers that come before 'class', not the keyword itself. 'All of the above' is incorrect because only 'class' is the required keyword for declaration.
-
The program has a compilation error because TempClass does not have a default constructor.
-
The program has a compilation error because TempClass does not have a constructor with an int argument.
-
The program compiles fine, but it does not run because class C is not public.
-
The program compiles and runs fine.
B
Correct answer
Explanation
In Java, constructors cannot have a return type. Because TempClass(int j) is declared with a void return type, it is treated as a regular method rather than a constructor. Consequently, the class has no parameterized constructor, and calling new TempClass(2) in main results in a compilation error.
-
public variables
-
private variables
-
instance variables
-
class variables
D
Correct answer
Explanation
Class variables (also called static variables) are shared across all instances of a class - they belong to the class itself, not to individual objects. Instance variables are unique to each object. Public and private are access modifiers, not variable types.
-
in line 4
-
in line 8
-
in both line 4 and line 8
-
none
A
Correct answer
Explanation
The square method in line 4 only depends on its parameter n and doesn't access any instance variables of the class, making it a perfect candidate for a static utility method. Conversely, getAge in line 8 returns the instance field age, meaning it requires an object instance and cannot be declared static.
-
Singleton
-
Factory
-
Builder
-
Prototype
B
Correct answer
Explanation
The Factory pattern creates objects without specifying their exact class. When you have a superclass and multiple subclasses, and need to instantiate the appropriate subclass based on input data, a Factory method decides which class to instantiate and returns it. Singleton ensures one instance, Builder constructs complex objects step-by-step, and Prototype clones existing objects; none solve the conditional instantiation problem.
-
Factory
-
Abstract Factory
-
Singleton
-
Builder
D
Correct answer
Explanation
The Builder pattern separates complex object construction from its representation. It constructs objects step-by-step through a fluent interface, allowing different representations from the same construction process. Factory and Abstract Factory create objects in one step, Singleton restricts instantiation; none handle stepwise construction of complex objects.
A
Correct answer
Explanation
Interface variables are implicitly public, static, and final in Java. Static means they belong to the interface, not any instance. Final means they must be initialized and cannot be changed. This makes interfaces ideal for defining constants: implementing classes share these values without creating copies. The statement is accurate.
-
Intercepting Filter
-
Factory pattern
-
Invoker pattern
-
Double Dispatch pattern
-
None of the above
D
Correct answer
Explanation
The Visitor pattern is a behavioral design pattern that allows you to add new operations to existing object structures without modifying the structures. It uses a technique called double dispatch, where the operation executed depends on both the type of visitor and the type of element being visited. The other options (Intercepting Filter, Factory, Invoker) are different design patterns altogether.
-
Singleton pattern
-
One Instance pattern
-
No such class be formed
-
Static classes are one instance classess
-
None of the above
A
Correct answer
Explanation
The Singleton pattern ensures a class has only one instance and provides a global point of access to that instance. It involves a private constructor, a static variable to hold the single instance, and a static method that returns this instance (creating it if necessary). 'One Instance' and 'Static classes' are not recognized design patterns for this purpose.
-
Observer pattern
-
DataAccess pattern
-
Publisher Pattern
-
Subscriber Pattern
-
None of the above
A
Correct answer
Explanation
The Observer pattern defines a one-to-many dependency between objects so that when one object (the subject) changes state, all its dependents (observers) are notified and updated automatically. This is commonly used in event handling systems. 'DataAccess', 'Publisher', and 'Subscriber' are not standard design pattern names for this purpose.
-
Encapsulation
-
Polymorphism
-
Inheritance
-
Data Access object
-
None of the above
D
Correct answer
Explanation
The Data Access Object (DAO) pattern abstracts and encapsulates all access to a data source, separating data persistence logic from business logic. This creates a dedicated layer for database operations, making the code more maintainable and testable. Encapsulation, Polymorphism, and Inheritance are OOP principles, not patterns for data access separation.