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. any name

  2. x

  3. exforsys

  4. None of the Above

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

In Java and similar object-oriented languages, a constructor must have the exact same name as its class. If the class is named 'exforsys', the constructor must also be named 'exforsys'. The constructor name cannot be arbitrary (option A), cannot be the same as a field name (option B), and option D is incorrect because there is a specific answer.

Multiple choice technology programming languages
  1. Does not compile

  2. prints 0

  3. prints 10

  4. prints 7

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

The code fails to compile due to two errors: (1) 'class B extends class A' - should be 'class B extends A' without the word 'class' before A, and (2) class A has a private constructor, so it cannot be instantiated in main(). The code has multiple compilation errors preventing it from running.

Multiple choice technology programming languages
  1. public void amethod(int i) throws FileNotFoundException{ }

  2. public void amethod(int i) throws IOException, RuntimeException{ }

  3. public void amethod(int i) throws RuntimeException{ }

  4. public void amethod(int i) throws Exception{ }

  5. public void amethod(int i) { }

  6. public void amethod(int i) throws Throwable{ }

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

When overriding a method that throws IOException, you can: throw no exceptions (E), throw fewer or same checked exceptions (A - FileNotFoundException is subclass of IOException), add unchecked exceptions freely (B, C - RuntimeException is unchecked), but you cannot throw broader checked exceptions (D - Exception, F - Throwable). Options A, B, C, E all follow these rules.

Multiple choice technology programming languages
  1. private

  2. protected

  3. transient

  4. public

  5. final

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

A top-level Java class can only be declared with 'public', 'abstract', or 'final' access modifiers, or default (package-private) access. 'private', 'protected', and 'transient' cannot be applied to top-level classes.

Multiple choice technology programming languages
  1. A Bar is a Baz

  2. A Foo has a Bar

  3. A Baz is a Foo

  4. A Foo is a Baz

  5. A Baz has a Bar

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

Baz extends Foo, so every Baz object is also a Foo through inheritance (is-a relationship). Baz contains a Bar field named 'thing', so a Baz has-a Bar. Option C correctly states the inheritance relationship, and E correctly states the composition relationship.

Multiple choice technology programming languages
  1. Does not compile

  2. prints "ABC"

  3. prints "ACB"

  4. prints "CAB"

  5. run time exception

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

In Java, either this() or super() can be the first statement in a constructor, but never both. The no-arg B() constructor attempts to call this(4) followed by super(), which violates Java's constructor chaining rules. Since this(4) must be first and it will implicitly call super(), the explicit super() call causes compilation failure.

Multiple choice technology programming languages
  1. . private or nothing (default) on line 3. Nothing (default) or protected or public on line 8.

  2. public or protected on line 3. private or nothing (default) on line 8.

  3. Nothing (default) or protected or public on line 3. private or nothing (default) on line 8.

  4. public on line 3 and private on line8.

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

Line 3 has default (package-private) access, so only private or default can be used there. Line 8 is overriding in a subclass, so access can be same or wider (default, protected, public). Option A correctly states these rules.

Multiple choice technology programming languages
  1. The code will compile and run only if line 5 is removed.

  2. The code will compile and run only if line 6 is removed

  3. The code compiles and runs, printing "Test" twice

  4. The code compiles but throws an exception at runtime

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

Line 5 works because ParentTest inherits test() from Parent. Line 6 fails because pack.Parent's protected method can't be called from mypack outside the inheritance hierarchy. Protected access allows subclass access but not external package access.

Multiple choice technology programming languages
  1. Map

  2. HashMap

  3. Dictionary

  4. Vector

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

Hashtable extends the abstract Dictionary class, which was part of Java's original collection framework in version 1.0. While Dictionary is now deprecated and Hashtable implements the Map interface (since Java 2), Dictionary remains its direct superclass in the class hierarchy.

Multiple choice technology web technology
  1. Is the first instance of a function

  2. Is the first instance of a variable

  3. Converts information contained in an XML document to another data type

  4. None of the above

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

In XQuery and XSLT, constructors are functions that convert data from one type to another. For example, they can convert strings to numbers, dates, or other atomic types. The term 'constructor' in this context refers to type conversion, not object instantiation as in object-oriented programming.

Multiple choice technology programming languages
  1. String[] s =f.list();

  2. File[] files=f.list();

  3. File[] files=f.listFiles();

  4. List files=f.listFiles();

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

File.list() returns String[](array of filenames), and File.listFiles() returns File[](array of File objects). These are standard methods for listing directory contents with different return types.