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
B
Correct answer
Explanation
Generics were introduced in Java 5 (J2SE 5.0, released in 2004), not in Java 1.4. Java 1.4 was released in 2002 and did not include generic type support. The introduction of generics allowed developers to write type-safe collections and eliminate the need for explicit casting when retrieving objects from collections.
-
Public
-
Protected
-
Private
-
Default
B
Correct answer
Explanation
In Objective-C, instance variables default to @protected access, meaning they are accessible within the class and its subclasses. This differs from C++ and Java where the default is typically private. @public would allow external access, @private restricts to the defining class only.
C
Correct answer
Explanation
In Objective-C, 'self' refers to the current object instance receiving the message (like 'this' in C++ or Java). 'super' is for calling superclass methods, 'delegate' is a design pattern role, and 'this' is not used in Objective-C.
-
Encaplulation
-
Polymorphism
-
Multiple Inheritence
-
Method overriding
C
Correct answer
Explanation
Objective-C supports encapsulation (through @private/@public), polymorphism (through method overriding and dynamic typing), and method overriding. However, Objective-C does NOT support multiple inheritance - classes can only inherit from one superclass. Protocols provide a similar capability without true multiple inheritance.
-
NSString
-
NSObject
-
NSControl
-
NSButton
B
Correct answer
Explanation
NSObject is the root class in the Objective-C runtime and Cocoa framework. All other classes like NSString, NSControl, and NSButton inherit from NSObject, making it the base of the class hierarchy.
-
@interface and @implement
-
@property and @synthesis
-
@category and @protocol
-
none
B
Correct answer
Explanation
The @property directive declares accessor methods in the interface, and @synthesize implements them in the implementation. This combination automatically generates getter and setter methods for instance variables.
-
@property <type> <name>;
-
@property (<parameters>) <type> <name>;
-
@property (<parameters>) <name>;
-
none
B
Correct answer
Explanation
Property declarations require the @property directive followed by optional attributes in parentheses, then the type and name. The full syntax is @property (attributes) type name; where attributes like nonatomic, strong, etc. are optional but commonly used.
-
Compile error
-
Runtime Exception
-
No error
-
Rintime Error
A
Correct answer
Explanation
The code attempts to assign a superclass object (A instance) to a subclass reference (B b). Even though B extends A, an A object is not a B. Java forbids this unsafe assignment at compile-time. You cannot assign a parent class instance to a child class reference without explicit casting.
-
Compiler error
-
Runtime Exception
-
No errors
-
Runtime Error
C
Correct answer
Explanation
Class B extends Class A, making B a subclass of A. In Java, assigning an instance of B to a reference variable of type A (A a = new B()) is a valid polymorphic upcast. This code compiles successfully and executes without generating any runtime exceptions or errors.
-
Yes, always it should implement
-
No, Never
-
No, Not when A is abstract
-
None of the above
C
Correct answer
Explanation
Concrete classes implementing an interface must implement all interface methods. However, abstract classes are exempt - they can declare abstract methods instead of implementing them. This allows subclasses to provide the actual implementations. The 'always' and 'never' options are too absolute.
-
volatile
-
private
-
default
-
static
B
Correct answer
Explanation
In Java, a top-level class cannot be declared private - it can only be public or package-private (no modifier). Private only applies to nested classes. Volatile, static, and default (package access) modifiers are valid for classes. The claimed answer B is CORRECT.
-
double is primitive datatypes. Double is a class
-
Both are primitive dataType
-
Both are class type
-
double is class and Double is primitive Type
A
Correct answer
Explanation
In Java, 'double' is a primitive data type that stores 64-bit floating-point numbers, while 'Double' is a wrapper class that provides object representation of double values. This is part of Java's type system where primitives have direct memory representation and wrapper classes enable object-oriented operations. Options B, C, and D incorrectly classify these types.
A
Correct answer
Explanation
In Java, an interface can extend another interface using the extends keyword. This is the correct syntax for building interface hierarchies and creating inheritance relationships between interfaces.
B
Correct answer
Explanation
Java does not support multiple inheritance of classes to avoid complexity and the diamond problem. A class can only extend one parent class, though it can implement multiple interfaces.
B
Correct answer
Explanation
Typecasting between objects of different classes is only allowed if they are in the same inheritance hierarchy (parent-child relationship). You cannot cast between completely unrelated classes.