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. Composite

  2. Facade

  3. Decorator

  4. Adapter

  5. Mediator

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The Facade pattern provides a simplified interface to a complex subsystem, exactly as described in the scenario. It hides the complexities of secure programming behind a straightforward framework, allowing other programmers to use security features without understanding intricate implementation details. The Facade pattern acts as a front-facing interface that masks more complex underlying structures. Other patterns like Adapter, Decorator, and Composite serve different purposes - converting interfaces, adding responsibilities dynamically, and composing tree structures respectively.

Multiple choice technology programming languages
  1. The factory method makes objects that should be used together. This is not the case for the abstract factory.

  2. The abstract factory pattern provides an interface for creating a family of objects whereas factory method provides an interface for creating one object.

  3. In the abstract factory pattern the objects that the factory makes are to be used together. This is not necessarily true in the factory method pattern.

  4. The factory method pattern is used when the class does not know the class of the object it must create. But in the abstract factory this is known in advance.

  5. The factory method and abstract factory are essentially the same pattern but two different names are used to describe them depending on the circumstances when they are implemented.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The Abstract Factory pattern creates families of related or dependent objects through a factory interface, while Factory Method creates a single object through a method. Abstract Factory is best when products need to be compatible within a family. Factory Method defers object creation to subclasses. Option B correctly captures this distinction - Abstract Factory handles multiple related objects, Factory Method handles one object at a time. Other options either reverse the definitions or incorrectly claim the patterns are essentially the same.

Multiple choice technology programming languages
  1. Factory Method

  2. Prototype

  3. Builder

  4. Abstract Factory

  5. Singleton

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The Abstract Factory pattern is specifically designed for creating families of related or dependent objects without specifying their concrete classes. When you need multiple objects that work together as a family, Abstract Factory provides a consistent interface for creating them. Factory Method creates single objects, Prototype clones existing objects, Builder constructs complex objects step-by-step, and Singleton ensures only one instance exists - none of these handle families of dependent objects.

Multiple choice technology programming languages
  1. Exception

  2. I am in Q I am in Q with Argument

  3. I am in Q

  4. Compiler Error

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Java constructors must call either this() or super() as the first statement, but not both. The code S() { this(10); super(); } attempts to call both this(10) and super() in the same constructor, which is illegal. The compiler rejects this because super() is automatically inserted by the compiler if no this() or super() call exists, but if this() is present, the compiler won't insert super(). Additionally, the code references classes R and S but TestConstructor4 creates Q, and uses class Q without declaring it - multiple compilation errors exist.

Multiple choice technology programming languages
  1. I am in U

  2. Compiler Error

  3. I am in V

  4. I am in U I am in V

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

When V() is instantiated, the default constructor of V calls super() implicitly, which invokes U's constructor. U's constructor prints "I am in U". The method void V() in class V is NOT a constructor - it has a return type (void), so it's just a regular method that never gets called during object creation. Only U's constructor executes. This is a common trick question testing whether you recognize that constructors cannot have return types.

Multiple choice technology programming languages
  1. I am in A I am in B I am in C

  2. I am in B I am in C

  3. I am in C I am in B

  4. I am in C

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

When C c = new C() executes, Java's constructor chaining rules apply. C() implicitly calls super() which invokes B(), which implicitly calls super() to invoke A(). Each constructor prints its message: A prints "I am in A", then B prints "I am in B", then C prints "I am in C". The output is the concatenation of all three messages in order: "I am in A I am in B I am in C". This demonstrates how Java ensures parent constructors complete before child constructors execute.

Multiple choice technology programming languages
  1. I am in P null I am in Q

  2. Compiler Error

  3. I am in P I am in Q

  4. I am in Q

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Class Q extends P, and P has only a parameterized constructor P(String a). When Q() is called, it needs to call super() implicitly, but class P has no no-argument constructor. The compiler error occurs because the default constructor in Q attempts to call a nonexistent no-argument constructor in P. To fix this, Q() must explicitly call super(String) with an argument, or P needs to provide a no-argument constructor. This tests understanding that constructors aren't inherited and that subclass constructors must match available parent constructors.

Multiple choice technology programming languages
  1. I am in M, No argument Constructor I am in M, Constructor with Argument

  2. I am in M, Constructor with Argument I am in M, No argument Constructor

  3. Compiler Error

  4. Exception

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The constructor M() attempts to call this(10) after already executing System.out.println(). In Java, this() and super() calls must be the first statement in a constructor. This code violates that rule by having the print statement before this(10). The compiler error occurs because constructor chaining calls must precede any other code in the constructor body. Additionally, this(10) and this() cannot be used in the same constructor as they would create circular initialization.

Multiple choice technology programming languages
  1. protected

  2. private

  3. public

  4. friendly

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To answer this question, the user needs to know the concept of access modifiers in object-oriented programming. Access modifiers are keywords that determine the accessibility of classes, fields, methods, and other members of a class from other classes and packages. A top-level class is a class that is not a nested class or an inner class.

Now, let's go through each option and explain whether it is right or wrong:

A. protected: A protected access modifier can be applied to class members but not to top-level classes. Protected members can be accessed from within the class itself, its subclasses, and classes in the same package.

B. private: A private access modifier can be applied to class members but not to top-level classes. Private members can only be accessed from within the class itself.

C. public: A public access modifier can be applied to top-level classes, as well as class members. Public members can be accessed from anywhere in the program.

D. friendly: There is no such access modifier as "friendly". It may be a typo or a misunderstanding of the default access modifier, which is applied when no access modifier is specified. The default access modifier allows access to members from within the same package.

Therefore, the correct answer is:

The Answer is: C

Multiple choice technology programming languages
  1. EmpId is 100

  2. Compiler error

  3. EmpId is 0

  4. Exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The program fails to compile because TestInheritance1 is package-private and cannot be imported outside its package. Additionally, the non-static instance variable 'empId' is accessed directly inside the static main method without an object instance.

Multiple choice technology programming languages
  1. Compiler Error

  2. EmpId is 0

  3. EmpId is 100

  4. Exception

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The code compiles and runs successfully because empId is a public field in TestInheritance2, and public members are accessible from any package. The subclass TestInheritance_2 directly inherits empId and can access it, so the output is "EmpId is 100". Option A is wrong because public access across packages is valid. Option B is wrong because empId retains its initialized value of 100, not the default 0.

Multiple choice technology programming languages
  1. p= 0

  2. p= 100

  3. Compiler Error

  4. Exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The field 'p' is declared protected in the public class TestInheritance3. Subclasses in other packages inherit protected fields, so TestInheritance_3 can access 'p' directly. The program compiles successfully and prints the value 100.

Multiple choice technology programming languages
  1. Compiler Error

  2. name = TCS

  3. name = null

  4. Exception

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The code fails to compile because the field 'name' has default (package-private) access in TestInheritance4. Default access is restricted to the same package only. Since TestInheritance_4 is in a different package (inheritance1), it cannot access 'name'. Options B and C are wrong because the code won't reach runtime. Option D is wrong because this is a compile-time access violation, not a runtime exception.

Multiple choice technology programming languages
  1. p = 100

  2. Compiler error

  3. p = 0

  4. Exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The code fails to compile because 'p' is protected and TestInheritance_7 is in a different package. While protected members are accessible in subclasses, the access is through a parent class reference (TestInheritance7 T7 = new TestInheritance7(); T7.p). Protected members in different packages can only be accessed through inheritance or subclass references, not parent class instances. Option A is wrong because compilation fails. Option C is wrong for the same reason.

Multiple choice programming languages java
  1. name = JAVA

  2. Compiler Error

  3. Exception

  4. name = null

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Compilation fails due to multiple errors: an incomplete import statement syntax, the package-private class TestInheritance4 being inaccessible from another package, and its default-visibility field 'name' not being inherited across packages.