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

Multiple choice technology programming languages
  1. True

  2. False

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

Constructors cannot be abstract (they must be implemented), static (they operate on instances), or native/synchronized (these modifiers don't apply to constructors). Constructors can be final in some languages to prevent subclassing, but the statement claims all modifiers are valid which is incorrect.

Multiple choice technology programming languages
  1. True

  2. False

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

Constructors can indeed have public, protected, or private access modifiers. Public constructors allow instantiation from anywhere, protected from subclasses and same package, and private restricts instantiation (used in singleton pattern). The default constructor's access matches the class's access modifier.

Multiple choice technology programming languages
  1. True

  2. False

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

Constructors can use this() to call another constructor in the same class (constructor chaining). This allows you to avoid code duplication by having one constructor do the common work and others call it with appropriate default parameters. The called constructor must have a different parameter list.

Multiple choice technology programming languages
  1. True

  2. False

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

When a constructor uses this() to call another constructor, it MUST be the first statement in the constructor body. This ensures initialization happens in the correct order - you can't do other work before delegating to another constructor. If it's not first, the compiler will generate an error.

Multiple choice technology programming languages
  1. True

  2. False

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

The default constructor's access modifier IS the same as the class modifier. If the class is public, the default constructor is public. If the class has package-private access, the default constructor is also package-private. This ensures the constructor is accessible wherever the class itself is accessible.