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
-
They can be applied to both data & methods
-
They must precede a class's data variables or methods
-
They can follow a class's data variables or methods
-
They can appear in any order
-
They must be applied to data variables first and then to methods
A,B,D
Correct answer
Explanation
Access modifiers (public, private, protected) can be applied to both data fields and methods. They precede the member declaration and can appear in any order relative to other modifiers like static or final. The statement that they can follow data variables is incorrect.
-
They are not associated with any instance of an outer class
-
They may access final initialized variables and parameters that are in the scope of the statement block in which the class is declared
-
They may be declared public, protected or private
-
They may not implement an interface
A,B
Correct answer
Explanation
Local inner classes are declared inside method blocks and are not associated with any outer class instance. They can only access final or effectively final local variables and parameters from their enclosing scope. They cannot have access modifiers (public, protected, private) and can implement interfaces.
-
Object, Class
-
Component, Interfaces.
-
Component, Class
-
Class, Component
B
Correct answer
Explanation
A component is a self-contained software unit with well-defined functionality, and its dependencies (what it requires from other components) are specified through interfaces. Interfaces define the contract between components without exposing implementation details, enabling loose coupling and independent development.
-
Class and Operation
-
Class and Query
-
Class, Operation and Query
-
Only Operation
A
Correct answer
Explanation
In component modeling, the Interface can expose both Class objects and Operation objects, allowing clients to see available operations and the classes they act upon. Queries are typically part of a service contract, not directly placed on an Interface, so the correct pair is Class and Operation.
-
Object Model
-
Class Model
-
Component Model
-
System Model
C
Correct answer
Explanation
In Component-Based Development (CBD), the Component Model defines the standards and specifications for how component interfaces should be structured, what elements they must contain, and how components interact. Unlike object-oriented models which focus on classes, or system models which cover broader architecture, the component model specifically governs interface definition rules.
-
Inheritance represents an is-a relationship
-
. Inheritance represents a has-a relationship
-
Interfaces must be used when creating a has-a relationship
-
Instance variables can be used when creating a has-a relationship
A,D
Correct answer
Explanation
Inheritance models an is‑a relationship (a subclass is‑a type of its superclass). A has‑a relationship is expressed by including instance variables that reference other objects. Therefore the true statements are the inheritance one and the instance‑variable one; the others mischaracterize the relationships.
-
Inherits from @baseclass
-
Inherits from Work-
-
Contains most of your business logic
-
Is usually the same as a “bottom level” class
A
Correct answer
Explanation
In Pega's class hierarchy, @baseclass is the ultimate root class from which all classes inherit. Top-level classes directly inherit from @baseclass, while Work- and other pattern classes are specialized subclasses. Business logic resides in lower-level implementation classes, not top-level structural ones.
-
Classes that end in “-” must be abstract
-
Abstract classes should not have instances
-
Concrete classes must belong to a class group
-
A class group must always have at least one key
C
Correct answer
Explanation
The statement about concrete classes requiring class groups is not universally true. While Work-derived concrete classes need class groups for database mapping, Data- classes and other concrete class types do not. Classes ending in "-" are abstract by convention, and abstract classes indeed should not have instances.
-
Instances of Embed- classes can not be saved as pages inside of Work Objects
-
Instances of Data- classes can not be saved as pages inside of Work Objects
-
Instances of both Data- & Embed- classes can be saved as pages inside of Work Object
-
Embed- objects can be persisted to the database
A
Correct answer
Explanation
Embed- classes are designed specifically to be embedded within work objects and cannot exist as independent pages. Data- classes can be saved as pages. The key distinction is that Embed- objects exist only as part of their parent work object and are not separately persisted to the database.
-
Java.lang
-
Java.nio
-
Java.rmi
-
Java.basic
A
Correct answer
Explanation
The java.lang package provides classes fundamental to the Java programming language itself, including Object, String, wrapper classes, and basic math operations. This package is automatically imported into all Java programs. Java.nio handles I/O, Java.rmi handles remote method invocation, and Java.basic is not a standard package.
-
pc
-
pcc
-
pcp
-
pcpc
-
Compilation fails
-
An exception is thrown at runtime
C
Correct answer
Explanation
During serialization, the parent class constructor is called when creating c1, printing 'p', then child constructor prints 'c'. During deserialization, constructors are not called for the object itself, but the parent class constructor runs again, printing another 'p'. The output is 'pcp'. Option C is correct.
-
Protected
-
Private
-
Public
-
2&3
-
1&3
E
Correct answer
Explanation
When overriding a protected method in Java, the subclass method must have the same or wider access modifier. Protected is the baseline, so valid options are Protected (same) or Public (wider). Private is narrower and thus invalid. Option E correctly identifies Protected and Public as valid.
-
Non Static field
-
Base class fields
-
Transient Fields.
-
Static Field
C
Correct answer
Explanation
During Java serialization, fields marked with the 'transient' keyword are explicitly ignored and not written to the serialization stream. This allows developers to exclude sensitive or non-serializable data. Static fields are also not serialized, but 'transient' is the specific mechanism for ignoring instance fields.
-
Can have only one constructor
-
a.Cannot have a constructor
-
Can have more than one constructor
-
May or May not have constructors
B
Correct answer
Explanation
Anonymous classes in Java cannot have explicitly declared constructors. They can only use the implicit no-arg constructor (if extending a class) or must match a constructor signature from the parent class/interface. This is because anonymous classes have no named class definition to declare constructors for.
-
More than one Object
-
No Objects
-
Only One Objects.
-
Two Objects.
C
Correct answer
Explanation
A Singleton class ensures only ONE instance of the class can exist. It provides a global point of access to that instance through a static method. This pattern is used when exactly one object is needed to coordinate actions across the system.