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

Multiple choice technology web technology
  1. True

  2. False

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

Java does NOT support multiple inheritance of classes to avoid the 'diamond problem' and complexity. A class can only extend one parent class, though it can implement multiple interfaces - which is different from multiple inheritance.

Multiple choice technology programming languages
  1. root

  2. object

  3. stem

  4. minor

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

In the Java programming language, the java.lang.Object class (indicated as object) sits at the top of the class hierarchy. Every class in Java implicitly or explicitly extends Object, making it the highest superclass. Other options like root, stem, and minor are not valid Java class names in this hierarchy.

Multiple choice technology programming languages
  1. Inheritance

  2. Polymorphism

  3. Generalization

  4. Abstraction

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

Overloading (compile-time polymorphism), overriding (runtime polymorphism), and dynamic method binding are all mechanisms that allow a single interface to work with different data types or objects. These are core concepts of polymorphism in object-oriented programming, enabling code flexibility and reusability.

Multiple choice technology programming languages
  1. a. An abstract class may only contain incomplete methods (abstract methods)

  2. b. An interface may contain complete or incomplete methods

  3. c. A class may inherit several interfaces, A class may inherit only one abstract class

  4. d. A class implementing an abstract class has to implement all the methods of the abstract class, but the same is not required in the case of an interface

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

A class can implement multiple interfaces but inherit from only one abstract class due to single inheritance constraints in most languages. This design allows interfaces to define multiple contracts while abstract classes provide implementation inheritance. Option D is incorrect because a concrete class must implement ALL interface members (unless the class itself is abstract), just as it must implement abstract class members.

Multiple choice technology programming languages
  1. a. Access is limited to the current assembly

  2. b. Access is limited to the containing class or types derived from the containing class.

  3. c. Access is limited to the containing type

  4. d. Access is limited to the current assembly or types derived from the containing class.

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

To understand who can access a method marked as protected internal, the user needs to know about access modifiers in C#.

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

A. Access is limited to the current assembly: This option is partially correct. When a method is marked as internal, it can only be accessed within the same assembly. However, when a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option A is incorrect.

B. Access is limited to the containing class or types derived from the containing class: This option is incorrect. When a method is marked as protected, it can be accessed by the containing class or types derived from the containing class. However, when a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option B is incorrect.

C. Access is limited to the containing type: This option is incorrect. protected internal access modifier provides more accessibility than protected access modifier. When a method is marked as protected, it can be accessed by the containing class or types derived from the containing class. However, when a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option C is incorrect.

D. Access is limited to the current assembly or types derived from the containing class: This option is correct. When a method is marked as protected internal, it can be accessed by derived types from any assembly as well as within the same assembly. Therefore, option D is the correct answer.

The Answer is: D

Multiple choice technology programming languages
  1. a. yes,all access specifiers are valid for namespace members

  2. b. No,only private access specifiers are possible

  3. c. No,implicitly all members are protected

  4. d. No,the namespace allows only public and internal elements as its members

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

Namespace members cannot have access modifiers like protected or private because namespaces organize code at the file/assembly level, not class level. Namespace members are implicitly public or internal (assembly-visible). Protected access only makes sense within class inheritance hierarchies.

Multiple choice technology programming languages
  1. a. local variable

  2. b. global variable

  3. c. static variable

  4. d. private variable

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

Variables declared inside a method have local scope - they exist only within that method's block and are not accessible outside. They're created when the method is called and destroyed when it returns. Global variables exist at class/file level, static variables persist across instances, and private is an access modifier, not a variable classification.

Multiple choice technology
  1. Methods cannot be overriden to be more private

  2. static methods cannot be overloaded

  3. private methods cannot be overloaded

  4. An overloaded method cannot throw exceptions not checked in the base class

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

Access modifiers can only be made more public in overrides, not more private (Liskov substitution principle would break). Static methods CAN be overloaded (different signatures). Private methods CAN be overloaded within the same class. Overloaded methods can declare any exceptions since they're not polymorphic - the restriction applies only to overrides.

Multiple choice technology
  1. Compile and run without error

  2. Compile time Exception

  3. Runtime Exception

  4. none

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

The code compiles because the cast from Base to Sub is syntactically allowed at compile time. However at runtime, b refers to a Base object (not a Sub), so the downcast fails with ClassCastException. You cannot cast an object to a type it isn't actually an instance of.

Multiple choice technology
  1. Set

  2. List

  3. Map

  4. Vector

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

Set interface specifically models collections that contain no duplicate elements, exactly matching the requirement for unique object storage without ordering. List allows duplicates, Map stores key-value pairs (not just unique elements), Vector is a legacy List implementation.

Multiple choice technology
  1. Static void doCalc(int x, int… args) {}

  2. Static void doCalc(int… args, int x) {}

  3. Static void doCalc(int[] args)

  4. Static void doCalc(int args…)

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

Varargs (...) must be the last parameter in a method signature. Option A places varargs last, so doCalc(1,2) maps x=1, args=[2], and doCalc(2) maps x=2, args=[]. Option B fails because varargs cannot precede a required parameter. Option C won't compile for the given calls, and option D has incorrect syntax.

Multiple choice technology
  1. Yes, but always call the default constructor.

  2. No, no need to call the default constructor.

  3. yes, always.

  4. no, always.

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

You CAN write a constructor for a Servlet, but the servlet container (not your code) calls the no-argument constructor during instantiation via reflection. If you write a parameterized constructor, it won't be used. The option 'Yes, but always call the default constructor' is misleading - the container calls it, not your code. Option A is technically correct but poorly phrased.