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
-
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.
-
Its non-static members
-
Static members of the enclosing class
-
Its Static members
-
Non-Static members of the enclosing class
D
Correct answer
Explanation
Static nested classes cannot access instance (non-static) members of the enclosing class because they are not associated with any instance of the enclosing class. They can only access static members of the enclosing class and their own members (both static and non-static).
-
Reading data through key board
-
Using system properties and environment variables
-
Running non-java programs from within a java application.
-
All of the Above
-
None of the Above
-
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 may be applied to both data members and methods, must precede the member they modify, and can appear in any order relative to other modifiers. They cannot follow a member declaration, and there is no requirement to place them on data before methods, so those statements are false.
-
Public
-
Private
-
Abstract
-
Final
-
Protected
A,C,D
Correct answer
Explanation
Top-level classes (not inner classes) can only have public or package-private (no modifier) access. They can additionally be abstract or final. Private and protected are NOT allowed for top-level classes - these modifiers only apply to class members (fields, methods, inner classes).
-
Prints true,false
-
Print false,true
-
Prints true,true
-
compile time error
C
Correct answer
Explanation
ob2 references the same object as ob1, which is an instance of c1. Therefore ob2 instanceof Object is true and ob2 instanceof c1 is also true, giving the output true true. The stored answer matches this result.
-
Casting/Uncasting
-
Boxing/Unboxing
-
Packing/Unpacking
-
Stack & Heap Conversion
B
Correct answer
Explanation
Boxing converts a value type to a reference type by storing it on the heap and creating a reference to it. Unboxing reverses this by extracting the value type from the reference. The other terms like packing/uncasting are not standard terminology in .NET or Java.
-
are public
-
are serializable
-
are immutatable
-
extend java.lang.Number
C
Correct answer
Explanation
All wrapper classes in Java are immutable, meaning their values cannot be changed after creation. While all are public and serializable, and most extend Number, the statement about immutability is the correct answer to what IS true (despite the question asking what 'aren't' true).
-
SessionContext
-
ApplicationContext
-
FormContext
-
None of the above
C
Correct answer
Explanation
In ClarifyCRM's object model, BOContext (Business Object Context) inherits from FormContext, which provides the base functionality for managing form-level data and operations within the application framework. Options A and B are incorrect context types.
-
the new method should return int
-
the new method can return any type of values
-
the argument list of new method should exactly match that of overriden method
-
the return type of new method should nottch that of overriden method
C
Correct answer
Explanation
When overriding a method in Java, the argument list (method signature) must exactly match the original method. The return type can be the same or a covariant type (subtype), but the parameter types, order, and number must remain identical for proper override behavior.
-
I am in Static. I am in Static1. Before calling. I am in Init. Constructor. I am in Init. Constructor.
-
Before calling. I am in Static. I am in Static1. I am in Init. Constructor. I am in Init.Constructor.
-
Before calling. I am in Static. I am in Static1. Constructor. I am in Init. Constructor. I am in Init.
-
Before calling. Constructor. I am in Static. I am in Static1. I am in Init. Constructor. I am in Init.
A
Correct answer
Explanation
Static initialization blocks execute first when the class loads, in the order they appear. Then the main method executes. When objects are created with 'new', instance initialization blocks execute before the constructor. Static blocks run only once per class load, not per object creation.
B
Correct answer
Explanation
Methods in a class can share the same name through method overloading, as long as they have different parameter lists (signatures). The compiler distinguishes them by the number, types, and order of parameters. The statement is false because method overloading is a fundamental feature of object-oriented programming.
B
Correct answer
Explanation
Constructors are special methods that initialize objects and never have a return type, not even void. They must have the same name as their class and are automatically called when an object is created using the new keyword. The presence of any return type (including void) would make it a regular method, not a constructor.
-
public Thread(Object obj)
-
public Thread(String name)
-
public Thread(Runnable trgt)
-
public Thread(ThreadGroup grp, Runnable trgt, String name)
-
public Thread(ThreadGroup grp, Object ob)
B,C,D
Correct answer
Explanation
Thread constructors accept either a Runnable object (for the task to execute), a ThreadGroup, a name string, or combinations thereof. Option A is invalid because Thread doesn't accept a plain Object parameter. Option E is invalid because Object doesn't implement Runnable. Options B (String name), C (Runnable target), and D (ThreadGroup + Runnable + name) match valid Thread constructor signatures in Java.