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. Static Variables are Set at RunTime

  2. Sealed classes cannot be Overriden

  3. finally' block executes only when exception occurs

  4. Both A and B

  5. Both B and C

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

Static variables are initialized at runtime when the class is first loaded, and sealed classes cannot be inherited (overridden). Thus, statements A and B are true, making 'Both A and B' correct. The finally block always executes regardless of whether an exception occurs.

Multiple choice technology programming languages
  1. Adds a Reference to the parents class method

  2. it only hides the parent class method

  3. it overrides the parent class method

  4. None

  5. All the Above

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

In C#, using the new keyword as a method modifier explicitly hides the inherited method of the same signature from the parent class. It does not override it (which requires the override keyword) or add a reference to it.

Multiple choice technology programming languages
  1. extraction operator

  2. scope resolution operator

  3. insertion operator

  4. All the above

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

The scope resolution operator (::) allows defining class methods separately from the class declaration by specifying ClassName::methodName(). This operator connects the function definition to its class scope, unlike stream operators which handle I/O operations.

Multiple choice technology programming languages
  1. virtual constructor

  2. virtual destructor

  3. both A and B

  4. None of the above

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

C++ supports virtual destructors but not virtual constructors. Virtual destructors ensure proper cleanup when deleting derived objects through base pointers, while constructors cannot be virtual since they initialize the vtable itself.

Multiple choice technology programming languages
  1. Simple constructor

  2. parameterized constructor

  3. copy constructor

  4. none of the above

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

A copy constructor accepts a reference to an object of its own class, typically declared as ClassName(const ClassName& obj). This allows creating new objects by copying existing ones, while simple constructors initialize without references and parameterized constructors accept different argument types.

Multiple choice technology programming languages
  1. Direction d = NORTH;

  2. Nav.Direction d = NORTH;

  3. Direction d = Direction.NORTH;

  4. Nav.Direction d = Nav.Direction.NORTH;

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

The Direction enum is defined inside the Nav class (line 11), making it a nested enum type. To reference it from outside code (line 14 in Sprite class), you must use the full qualified name: outer class name, then inner enum name, then the enum constant. Option D shows correct syntax: 'Nav.Direction d = Nav.Direction.NORTH;' - Nav (outer class) contains Direction (enum), which contains the constant NORTH. Option A misses the Nav prefix, Option B misses Direction enum name, and Option C misses the Nav prefix entirely.

Multiple choice technology programming languages
  1. 0

  2. 1

  3. 2

  4. 3

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

Java's varargs syntax uses three dots (...) after the type. The five fragments test different placements: 'String...a' (correct), 'String.* a' (incorrect - * is not valid), 'String... a' (correct with space), 'String[]... a' (incorrect - array and varargs don't combine this way), 'String...[] a' (incorrect syntax). Therefore, 3 fragments compile correctly: 'public static void main(String...a)', 'public static void main(String... a)', and 'public static void main(String[]... a)' is also valid varargs syntax. Actually, upon closer inspection, String[]... a is actually valid - it's varargs of String arrays. So 3 fragments compile.

Multiple choice technology programming languages
  1. any class

  2. only the Target class

  3. any class in the test package

  4. any class that extends Target

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

The 'name' field has default (package-private) access: no public, private, or protected modifier. In Java, default access allows only classes within the same package to directly access the field. Option C correctly states 'any class in the test package' can access and modify name. Option A is wrong because classes outside the package cannot access it. Option B is wrong because other classes in the same package CAN access it (it's not private). Option D is wrong because extending the Target class alone doesn't grant access - you must be in the test package.

Multiple choice technology programming languages
  1. public int blipvert(int x) { return 0; }

  2. private int blipvert(int x) { return 0; }

  3. private int blipvert(long x) { return 0; }

  4. protected long blipvert(int x) { return 0; }

  5. protected int blipvert(long x) { return 0; }

  6. protected long blipvert(long x) { return 0; }

Reveal answer Fill a bubble to check yourself
A,C,E,F Correct answer
Multiple choice technology programming languages
  1. Change line 2 to:public int a;

  2. Change line 2 to:protected int a;

  3. Change line 13 to:public Sub() { this(5); }

  4. Change line 13 to:public Sub() { super(5); }

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

The problem is that Sub's no-arg constructor tries to access private field a from Super. The issue is visibility. Option C fixes it by calling this(5), which invokes the other constructor Sub(int a), which calls super(a) - the Super constructor sets a. Option D fixes it with super(5), calling Super's constructor directly. Options A and B change visibility of a to public/protected, but that alone doesn't fix the compilation error because the no-arg constructor still can't access a properly without calling a constructor chain.

Multiple choice technology programming languages
  1. Are public

  2. Are final

  3. Are Serializable

  4. All the above

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

All wrapper classes in Java (Integer, Boolean, Float, Short, Long, Double, Character) are public, final, and implement Serializable. Being final prevents inheritance, being public allows access from anywhere, and Serializable allows object serialization.

Multiple choice technology programming languages
  1. The throw keyword denotes a statement that causes an exception to be initiated

  2. A class that is declared without any access modifiers is said to have package or friendly access

  3. A class inherit the constructors of its superclass

  4. The primitive types are byte, char, short, int, long, float, double, and boolean

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

The question asks which statement is false. Constructors are NOT inherited in Java - each class defines its own constructors, and a subclass must explicitly call a superclass constructor using super(). The other statements are true: throw initiates exceptions, classes without modifiers have package-private access, and the listed types are Java's eight primitives.

Multiple choice technology programming languages
  1. Free memory

  2. Initialize a newly created object

  3. Import packages

  4. Create a JVM for applets

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

A constructor initializes a newly created object's state, sets initial values for fields, and can invoke the superclass constructor. Options A, C, and D are incorrect: constructors don't free memory (garbage collection does), they don't import packages (import statement does that), and JVM creation happens at runtime startup, not by constructors.

Multiple choice technology programming languages
  1. A static method may override another static method

  2. A static method cannot override a non-static method

  3. A non-static method cannot override a static method

  4. A non-static method may be overloaded by a static method

  5. A synchronized method cannot be overridden

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