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. that can not be inherited

  2. that can be inherited only once

  3. that can have only one object

  4. there is no such class

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

A singleton class is a design pattern that restricts instantiation of a class to one object. This is achieved by making the constructor private, declaring a static instance variable, and providing a static public method (usually getInstance()) that returns the instance. Option C correctly states that a singleton can have only one object. Options A and B describe inheritance restrictions, which are not the definition of singleton.

Multiple choice technology testing
  1. A set of test cases for testing classes of objects

  2. An input or output range of values such that only one value in the range becomes a test case

  3. An input or output range of values such that each value in the range becomes a test case

  4. An input or output range of values such that every tenth value in the range becomes a test case.

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

Equivalence partitioning divides input or output data into ranges where the system handles all members similarly. Therefore, testing just one representative value (597078) suffices to cover the partition. Testing every value (597080) is redundant and defeats the purpose of the technique, while options 597078 and 597081 do not define equivalence classes.

Multiple choice technology programming languages
  1. Compile error

  2. Runtime Exception

  3. No error

  4. Compile error. Type mismatch: cannot convert from A to B

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

Class B extends Class A, meaning B is a subclass of A. Since a parent class instance cannot be directly assigned to a child class reference without an explicit cast (which would fail at runtime anyway), the code produces a compile-time type mismatch error.

Multiple choice technology web 2.0
  1. Events must know what object handles its event.

  2. An event can only have one event handler.

  3. Delegates are not type-safe.

  4. A class with events can only raise a single event.

  5. A delegate can only hold methods that match the delegate's method signature

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

Delegates in .NET are type-safe and can only reference methods that match their signature exactly (same return type and parameters). Option A is false because events are decoupled from handlers - they publish notifications without knowing subscribers. Option B is false because an event can have multiple subscribers through multicast delegates. Option C is false - delegates ARE type-safe by design. Option D is false because a class can define and raise multiple events.

Multiple choice technology web 2.0
  1. It must inherit directly or indirectly from System.Attribute

  2. It must be defined in the System.Attribute namespace

  3. It must have the AttributeUsageAttribute applied to its class.

  4. It must only be applied to a selected type once

  5. It must be applied only at the assembly level.

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

For an attribute class to be CLS-compliant, it must have the System.AttributeUsageAttribute applied to define its usage constraints. While inheriting from System.Attribute is required to define any custom attribute in .NET, applying AttributeUsageAttribute is the specific rule for CLS compliance.

Multiple choice technology programming languages
  1. cannot be instantiated

  2. can be called from non static

  3. can be instantiated

  4. 1 & 2

  5. 2 & 3

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

Static methods belong to the class itself, not to instances of the class. Statement 1 is correct: static methods cannot be instantiated with 'new' - you don't create objects to use them. Statement 2 is also correct: static methods can be called from non-static context (instance methods) using the class name. Statement 3 is false: you instantiate classes/objects, not methods. Therefore option D (1 & 2) is correct.

Multiple choice technology programming languages
  1. Delegates

  2. Structures

  3. Interfaces

  4. Enumerations

  5. All the above

  6. 2 & 3

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

Namespaces in C# can contain various type declarations including delegates, structures (structs), interfaces, and enumerations. All of these are valid members of a namespace and help organize code logically.

Multiple choice technology programming languages
  1. The purpose of constructors is to initialize class members

  2. Constructors can have return values

  3. Constructors always have the same name as the class

  4. None of the above

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

Constructors initialize class members and share the class name. However, constructors cannot have return values - not even 'void'. This is a fundamental difference between constructors and regular methods.

Multiple choice technology programming languages
  1. A B print

  2. print

  3. B

  4. B print

  5. print B A

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

When creating a derived class instance, the base class constructor runs first, then the derived constructor. Here, A's constructor prints "A", then B's constructor prints "B", and finally print() outputs "print".

Multiple choice technology programming languages
  1. Virtual keyword is used before Base class

  2. Override keyword is used before Derived class

  3. overload keyword is used before Derived class

  4. 1 & 2

  5. 1 & 3

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

In C# polymorphism, the 'virtual' keyword is used in the base class to allow a method to be overridden, and the 'override' keyword is used in the derived class to provide the implementation. There is no 'overload' keyword - method overloading is achieved by having multiple methods with the same name but different parameters.

Multiple choice technology programming languages
  1. Structure is a value type and Class is a reference type

  2. Class is a value type and Structure is a reference type

  3. Structs can't have destructors, but classes can have destructors

  4. Class can have implementation inheritance, but Structs cannot have it

  5. Structs can have implementation inheritance, but Class cannot have it

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

Structures are value types stored on the stack, while classes are reference types stored on the heap. Structs cannot have destructors (finalizers) and do not support implementation inheritance. Classes support both inheritance and destructors.

Multiple choice technology programming languages
  1. has no implementation

  2. only has definitions

  3. must be implemented by class or struct

  4. All the above

  5. 1 & 2

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

In object-oriented programming (particularly classic Java and C#), an interface defines a contract containing only signatures or definitions without implementation. Classes or structs implementing the interface must provide the concrete code for these members. Thus, all individual options are correct, making 'All the above' the right choice.

Multiple choice technology programming languages
  1. Either code from derived type or code in the same assembly

  2. Only members within the same type

  3. Only derived types or members of the same type.

  4. Only code within the same assembly

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

The protected internal access modifier in C# allows access from any code within the same assembly, or from derived classes in other assemblies. The other options are incorrect because they restrict access to only the same assembly, only the same type, or only derived types within the same assembly.

Multiple choice technology web technology
  1. XmlSerializer

  2. SoapFormatter

  3. BinaryFormatter

  4. Any one of the above

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

All three serializers (XmlSerializer, SoapFormatter, BinaryFormatter) can be used to serialize class instances in .NET. XmlSerializer produces XML output, SoapFormatter produces SOAP format, and BinaryFormatter produces binary serialization. The choice depends on the specific requirements like interoperability, format, and performance.