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 databases
  1. Packages can be nested.

  2. You can pass parameters to packages.

  3. A package is loaded into memory each time it is invoked.

  4. The contents of packages can be shared by many applications.

  5. You can achieve information hiding by making package constructs private.

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

Package contents can be shared across multiple applications (D), and packages support information hiding by allowing private constructs not exposed to the outside (E). Packages cannot be nested (A), you pass parameters to package subprograms not the package itself (B), and packages load once per session not each time (C).

Multiple choice technology programming languages
  1. abstract double area() { }

  2. abstract double area()

  3. abstract double area();

  4. abstract double area(); { }

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

In Java, an abstract method declaration must not have a method body (no braces {}) and must end with a semicolon, preceded by the abstract keyword. Therefore, abstract double area(); is the correct syntax. Option 607836 lacks a semicolon, and the others include braces.

Multiple choice technology programming languages
  1. FileWriter

  2. CharWriter

  3. Writer

  4. OutputStream

  5. FileOutputStream

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

In Java's I/O library, java.io.Writer is the abstract superclass for all character-oriented output streams. Classes like FileWriter inherit from it. OutputStream and FileOutputStream are for byte streams, not character writing, and CharWriter is not a standard JDK class.

Multiple choice technology programming languages
  1. method

  2. garbage collection

  3. finalizer

  4. Constructor

  5. none of the above

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

Constructors are special methods invoked automatically when a new object is instantiated using the 'new' keyword. They initialize the object's state and set up required resources. Methods must be called explicitly, garbage collection runs automatically to reclaim memory, and finalizers are called before garbage collection (not during object creation).

Multiple choice technology programming languages
  1. It must have a package statement

  2. It must be named Test.java

  3. It must import java.lang

  4. It must declare a public class named Test

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

Java requires the source filename to match the public class name. If a file contains 'public class Test', it must be named Test.java for successful compilation. Package statements are optional, java.lang is auto-imported, and the class must already be declared (that's not a separate requirement).

Multiple choice technology programming languages
  1. FileWriter

  2. CharWriter

  3. Writer

  4. OutputStream

  5. FileOutputStream

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

To solve this question, the user needs to have knowledge of abstract classes and the hierarchy of classes used for writing characters.

The option A, FileWriter, is incorrect because it is a concrete class that extends CharWriter.

The option B, CharWriter, is incorrect because it is also a concrete class that extends Writer.

The option C, Writer, is the correct answer because it is the abstract class that serves as the superclass of all classes used for writing characters.

The option D, OutputStream, is incorrect because it is the superclass of all classes used for writing bytes.

The option E, FileOutputStream, is incorrect because it is a concrete class that extends OutputStream.

Therefore, the answer is: C. Writer

Multiple choice technology programming languages
  1. method

  2. garbage collection

  3. finalizer

  4. Constructor

  5. none of the above

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

Constructors are special methods invoked automatically when a new object is instantiated using the 'new' keyword. They initialize the object's state and set up required resources. Methods must be called explicitly, garbage collection runs automatically to reclaim memory, and finalizers are called before garbage collection (not during object creation).

Multiple choice technology programming languages
  1. It must have a package statement

  2. It must be named Test.java

  3. It must import java.lang

  4. It must declare a public class named Test

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

Java requires the source filename to match the public class name. If a file contains 'public class Test', it must be named Test.java for successful compilation. Package statements are optional, java.lang is auto-imported, and the class must already be declared (that's not a separate requirement).

Multiple choice technology programming languages
  1. Encapsulation

  2. Class

  3. Inheritance

  4. Polymorphism

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

Inheritance is a fundamental OOP concept where a child class acquires properties and methods from its parent class. This promotes code reuse and establishes an 'is-a' relationship between classes. Encapsulation hides implementation details, while polymorphism allows objects to take multiple forms.

Multiple choice technology programming languages
  1. namespace in C# refers to a holder where name collisions can be avoided whereas in C++ it is used to define templates

  2. namespace in C# and C++ are for same purpose

  3. namespace in C# refers to a holder where name collisions can be avoided whereas in C++ it is used to define STL classes

  4. namespace in C# refers to a holder where name collisions can be avoided whereas in C++ it is undefined.

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

Both C# and C++ use namespaces for the same purpose: to organize code and prevent naming collisions by providing a scope for identifiers. The concept is identical in both languages.

Multiple choice technology web technology
  1. True

  2. False

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

C# structs can indeed have constructors, but they cannot have parameterless constructors (a default parameterless constructor is always implicitly provided). Parameterized constructors are allowed and commonly used to initialize struct fields with specific values.

Multiple choice technology programming languages
  1. Class

  2. Constructor

  3. Interface

  4. Method

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

Externalizable is an interface in Java that extends Serializable. It provides an alternative mechanism for serialization where the class takes complete control over the serialization process, writing its own data format and protocol. This gives more control but requires more effort than implementing Serializable.