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 programming languages
  1. Compile with error - Not allowed to override the return type of a method with a subtype of the original type.

  2. class A - return C

  3. class B - return D

  4. Runtime Exception

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

Java allows covariant return types (Java 5+): an overriding method can return a subtype of the original return type. B's getOBJ() returning D (subclass of C) is valid. The reference 'a' points to a B object, so B's getOBJ() executes via dynamic dispatch, printing 'class B - return D'.

Multiple choice technology web technology
  1. To specify the class where namespace is located

  2. Inorder to include objects into a .cs file

  3. Create an alias for a namespace

  4. None of these

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

The 'using' directive in C# creates an alias for a namespace or imports types from a namespace. It appears at the top of a .cs file to simplify code by allowing use of short type names instead of fully qualified names. The using statement for resource disposal is a different construct with similar syntax.

Multiple choice technology programming languages
  1. String

  2. Object

  3. Both

  4. None of these

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

In .NET, both System.String and System.Object are reference types. The object type is the ultimate base class of all types, while string represents a sequence of Unicode characters and is a reference type, despite possessing some value-type-like characteristics such as immutability.

Multiple choice technology architecture
  1. Extension methods

  2. Lambda Expression

  3. Anonymous methods

  4. Expression trees

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

Extension methods allow adding methods to existing types without modifying the original type or using inheritance. They are defined as static methods in static classes, with the first parameter prefixed by 'this' keyword indicating the type being extended. This provides a clean way to extend functionality of sealed classes or types you don't own.

Multiple choice technology programming languages
  1. The Object class does not provide any implementation for the hashCode method; every class must override it

  2. As far as it may be practically possible, the hashCode method defined by the Object class does return distinct integers for distinct objects

  3. For two object references referring to the same object, the hashCode method returns the same integer

  4. It returns a fixed number that internally represents the Object class for the JVM

  5. Only choice D is correct

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

According to the Java documentation, the default hashCode() method in the Object class is defined to return distinct integers for distinct objects as far as is reasonably practical (typically by converting the internal memory address of the object). Note that option C is also a true property of any valid hashCode contract, but option B describes the behavior of the default implementation specifically.

Multiple choice technology programming languages
  1. A class may inherit several interfaces, A class may inherit only one abstract class

  2. An abstract class may only contain incomplete methods (abstract methods)

  3. An interface may contain complete or incomplete methods

  4. 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
A Correct answer
Explanation

Classes support multiple interface implementation but only single abstract class inheritance due to C#'s single inheritance model for classes. Abstract classes can contain both abstract and concrete methods, unlike traditional interfaces which only had member signatures.

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

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

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

  4. Access is limited to the containing type

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

Protected internal combines both access modifiers as a union: the method is accessible to any code in the same assembly OR to derived classes anywhere, making it more permissive than either modifier alone. It is not an intersection of both conditions.

Multiple choice technology programming languages
  1. Abstraction

  2. Encapsulation

  3. Structures

  4. POP

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

Abstraction (hiding implementation details) and Encapsulation (bundling data with methods) are fundamental OOP concepts. Structures is not an OOP concept (it's a data structure feature), and POP is Procedural Oriented Programming, a different paradigm.

Multiple choice technology programming languages
  1. b=m;

  2. m=b;

  3. d =i;

  4. b1 =i;

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

In Java, a subclass reference (MyCast) can be assigned to a superclass reference (Base) without explicit casting - this is called upcasting and is always safe because a MyCast object 'is-a' Base through inheritance. The reverse (m=b) would require downcasting and could fail at runtime. Options C and D are invalid due to type incompatibility between double and boolean.

Multiple choice technology programming languages
  1. The class has no public constructors.

  2. The class is static.

  3. The class implements more than one interface.

  4. The class is generic.

  5. This scenario is impossible.

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

In C#, static classes are implicitly both abstract (cannot be instantiated) and sealed (cannot be inherited). This is the only way a class can have both characteristics.

Multiple choice technology
  1. Data layer

  2. Client logic layer

  3. Presentation layer

  4. Communication layer

Reveal answer Fill a bubble to check yourself
A,B,C,D Correct answer
Explanation

General Interface's multi-tiered approach organizes functions into four distinct layers. The Data layer manages information storage and retrieval, the Client logic layer handles business rules and processing, the Presentation layer manages user interface components, and the Communication layer handles data exchange between client and server. All four layers are essential to the architecture.