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

Multiple choice technology programming languages
  1. Exception

  2. Compiler Error

  3. I am in Constructor

  4. Message Hello

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. A class

  2. An object

  3. A method

  4. A data field

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. A class

  2. An object

  3. A method

  4. A data field

Reveal answer Fill a bubble to check yourself
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).

Multiple choice technology programming languages
  1. program

  2. class

  3. method

  4. data

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. The program has a compilation error because TempClass does not have a default constructor.

  2. The program has a compilation error because TempClass does not have a constructor with an int argument.

  3. The program compiles fine, but it does not run because class C is not public.

  4. The program compiles and runs fine.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. public variables

  2. private variables

  3. instance variables

  4. class variables

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. in line 4

  2. in line 8

  3. in both line 4 and line 8

  4. none

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology web technology
  1. Singleton

  2. Factory

  3. Builder

  4. Prototype

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology web technology
  1. Factory

  2. Abstract Factory

  3. Singleton

  4. Builder

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Intercepting Filter

  2. Factory pattern

  3. Invoker pattern

  4. Double Dispatch pattern

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Singleton pattern

  2. One Instance pattern

  3. No such class be formed

  4. Static classes are one instance classess

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Observer pattern

  2. DataAccess pattern

  3. Publisher Pattern

  4. Subscriber Pattern

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology architecture
  1. Encapsulation

  2. Polymorphism

  3. Inheritance

  4. Data Access object

  5. None of the above

Reveal answer Fill a bubble to check yourself
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.