Computer Knowledge

Object-Oriented Programming

2,239 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. Poor Unicode support.

  2. java.lang.Cloneable does not contain clone() method.

  3. The byte type is signed.

  4. The creators of the Java programming language modeled the syntax after C and C++.

  5. java.io.InputStream is an abstract class and not an interface.

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

Joshua Bloch (author of Effective Java) has famously noted that the byte type being signed (range -128 to 127) is the strangest aspect of Java. This is because most hardware and languages use unsigned bytes (0-255), forcing awkward conversions when working with binary data. Other options are not quotes he's identified as 'strangest'.

Multiple choice technology programming languages
  1. process(bytes);

  2. BitUtils.process(bytes);

  3. app.BitUtils.process(bytes);

  4. util.BitUtils.process(bytes);

  5. import util.BitUtils. *; process(bytes);

  6. SomeApp cannot use the process method in BitUtils.

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

The process() method in BitUtils is declared as private static. Private methods are not accessible outside their own class, even with fully qualified names or imports. SomeApp cannot access this method regardless of how it's called.

Multiple choice technology programming languages
  1. Error: Non static method intialize cannot accessed from static context main

  2. Error: Method initialize is already defined

  3. i=0 after i=369 j=318

  4. Error: incompatible types

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

Line int i=initialize(369) tries to call initialize(int) which has void return type, but int i expects an int value. This is a type mismatch - void methods cannot return values to initialize variables.

Multiple choice technology programming languages
  1. process(bytes);

  2. BitUtils.process(bytes);

  3. util.BitUtils.process(bytes);

  4. SomeApp cannot use methods in BitUtils.

  5. import util.BitUtils.*; process(bytes);

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

Since SomeApp does not import the util package or the BitUtils class, it must use the fully qualified class name util.BitUtils.process(bytes); to call the static method.

Multiple choice technology programming languages
  1. public class MinMax<?> {

  2. public class MinMax<? extends Number> {

  3. public class MinMax<N extends Object> {

  4. public class MinMax<N extends Number> {

  5. public class MinMax<? extends Object> {

  6. public class MinMax<N extends Integer> {

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

The type parameter N must extend Number to have the doubleValue() method. Option D (N extends Number) is correct. Option F (N extends Integer) also works since Integer extends Number and has doubleValue(). Options with ? are invalid because ? is a wildcard, not a type parameter name.

Multiple choice technology programming languages
  1. Starting of Main block Static block In the Main block Display block

  2. Static block Display block Starting of Main block In the Main block Display block

  3. Error: Non static method cannot accessed from static context

  4. Starting of Main block In the Main block Display block

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

Static blocks execute when the class is loaded, before any object is created. Inside the static block, the code attempts to call the non-static method display(), which results in a compilation error because non-static methods cannot be referenced from a static context. Therefore, option C correctly identifies this compilation failure.

Multiple choice technology programming languages
  1. The type List is assignable to List<A>.

  2. The type List<Object> is assignable to List<?>.

  3. The type List<D> is assignable to List<? extends B>.

  4. The type List<? extends A> is assignable to List<A>.

  5. The type List<Object> is assignable to any List reference.

  6. The type List<? extends B> is assignable to List<? extends A>.

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

In Java generics, wildcard types are used for covariance. List is assignable to List&gt;, List is assignable to List extends B&gt; because D extends B, and List extends B&gt; is assignable to List extends A&gt; because B extends A.

Multiple choice technology programming languages
  1. Error: Super(java.lang.String) in Super cannot be applied to ()

  2. First Second

  3. Second First

  4. Runtime Exception

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

The program compiles successfully because the superclass constructor Super(String s) is explicitly called. During execution, sub.name is "Second" and sup.name is "First", printing "Second First".

Multiple choice technology programming languages
  1. public void foo() { }

  2. public int foo() { return 3; }

  3. public Two foo() { return this; }

  4. public One foo() { return this; }

  5. public Object foo() { return this; }

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

When overriding a method, you can keep the same return type (One) or use a covariant return type (a subclass of One). Two is a subclass of One, so option C is valid covariant return. Option D keeps the original return type One. Options A and B change return type to void/int which aren't covariant with One. Option E returns Object which is a superclass, not allowed.

Multiple choice technology programming languages
  1. Color skyColor = BLUE;

  2. Color treeColor = Color.GREEN;

  3. Color purple = new Color( 0xff00ff);

  4. if( RED.getRGB() < BLUE.getRGB() ) {}

  5. Color purple = Color.BLUE + Color.RED;

  6. if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}

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

In enum code within the class, enum constants must be qualified with the enum name. Option B correctly references Color.GREEN. Option F uses Color.RED.ordinal() and Color.BLUE.ordinal() which are valid enum methods. Option A fails because BLUE isn't qualified. Option C tries to instantiate an enum with new which is illegal. Option D has the same qualification issue. Option E attempts to add enum values which isn't defined.

Multiple choice technology databases
  1. Single Project

  2. Multiple project

  3. Multiple projects of same type

  4. None of the above

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

A VOB (Versioned Object Base) can contain and support multiple projects simultaneously. This allows different projects to share common versioned elements while maintaining their own development streams. Option A is too restrictive, and option C unnecessarily limits projects to the same type.

Multiple choice technology web technology
  1. Implements java.io.Serializable

  2. Override equals() method

  3. Override hashCode() method

  4. Both a, b and c

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

Hibernate component classes must implement java.io.Serializable to support caching and detach operations. They should override equals() and hashCode() to work correctly in collections and as map keys. All three requirements ensure proper behavior in Hibernate's persistence framework.

Multiple choice technology platforms and products
  1. A public class which contains a main method with proper signature (Arguments, return types etc).

  2. We can run any classes

  3. Only those classes which have public no argument constructors.

  4. None of these

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

In Eclipse Package Explorer, you can directly run any class that has a properly defined main method. The main method must be public, static, void, and accept a String array as its parameter. Simply having a public class or constructor is not sufficient for direct execution.

Multiple choice technology programming languages
  1. public static int main(char args[])

  2. public static void main(String args[])

  3. public static void MAIN(String args[])

  4. public static void main(String args)

  5. public static void main(char args[])

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

The Java runtime looks for a method with the exact signature public static void main(String[] args) to start program execution. The array notation can be written as String[] args or String args[], both are equivalent. The method must be public, static, return void, and accept a String array as its parameter.