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
-
that can not be inherited
-
that can be inherited only once
-
that can have only one object
-
there is no such class
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.
-
A set of test cases for testing classes of objects
-
An input or output range of values such that only one value in the range becomes a test case
-
An input or output range of values such that each value in the range becomes a test case
-
An input or output range of values such that every tenth value in the range becomes a test case.
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.
-
Compile error
-
Runtime Exception
-
No error
-
Compile error. Type mismatch: cannot convert from A to B
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.
A
Correct answer
Explanation
The protected access modifier allows members to be accessed within the same package and by subclasses, even if those subclasses are in different packages. This statement correctly describes that protected methods can indeed be called in any subclass.
-
Events must know what object handles its event.
-
An event can only have one event handler.
-
Delegates are not type-safe.
-
A class with events can only raise a single event.
-
A delegate can only hold methods that match the delegate's method signature
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.
-
It must inherit directly or indirectly from System.Attribute
-
It must be defined in the System.Attribute namespace
-
It must have the AttributeUsageAttribute applied to its class.
-
It must only be applied to a selected type once
-
It must be applied only at the assembly level.
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.
-
cannot be instantiated
-
can be called from non static
-
can be instantiated
-
1 & 2
-
2 & 3
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.
-
Delegates
-
Structures
-
Interfaces
-
Enumerations
-
All the above
-
2 & 3
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.
-
The purpose of constructors is to initialize class members
-
Constructors can have return values
-
Constructors always have the same name as the class
-
None of the above
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.
-
A B print
-
print
-
B
-
B print
-
print B A
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".
-
Virtual keyword is used before Base class
-
Override keyword is used before Derived class
-
overload keyword is used before Derived class
-
1 & 2
-
1 & 3
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.
-
Structure is a value type and Class is a reference type
-
Class is a value type and Structure is a reference type
-
Structs can't have destructors, but classes can have destructors
-
Class can have implementation inheritance, but Structs cannot have it
-
Structs can have implementation inheritance, but Class cannot have it
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.
-
has no implementation
-
only has definitions
-
must be implemented by class or struct
-
All the above
-
1 & 2
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.
-
Either code from derived type or code in the same assembly
-
Only members within the same type
-
Only derived types or members of the same type.
-
Only code within the same assembly
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.
-
XmlSerializer
-
SoapFormatter
-
BinaryFormatter
-
Any one of the above
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.