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 web technology
  1. private

  2. b.final

  3. c.protected

  4. d.don't use any keyword at all (make it default)

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

In Java, a top-level class with no access modifier (default/package-private) is accessible only within its own package. Private cannot be used for top-level classes, final prevents inheritance but doesn't control access, and protected allows broader access.

Multiple choice technology web technology
  1. Line 1, line 2 and line 3 only

  2. b.Line 1 only

  3. c.Line 1 and line 2 only

  4. d.Line 2 only

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

Abstract classes cannot be instantiated directly (true). Constructors cannot be abstract (true). Subclasses must implement abstract methods only if they are concrete - abstract subclasses can leave them undefined (false). Static methods cannot be abstract (false).

Multiple choice technology web technology
  1. Visible only inside the package.

  2. b.Visible only in the class and its subclass in the same package.

  3. c.Visible in all classes in the same package and subclasses in other packages.

  4. d.Visible only in the class where it is declared.

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

Protected members are visible within the same package (like default) AND to subclasses in other packages. This combines package-level access with inheritance access. Private is class-only, default is package-only.

Multiple choice technology programming languages
  1. No default constructor is provided by the compiler

  2. The compiler provides the default constructor

  3. The compiler generates error

  4. None of the Above

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

In the given code, a class named "exforsys" is defined. It does not have any member variables or functions.

Now, let's go through each option and determine which one is true:

A. No default constructor is provided by the compiler: This option is incorrect. By default, if no constructor is defined in a class, the compiler provides a default constructor. So, this option is false.

B. The compiler provides the default constructor: This option is correct. When no constructor is defined in a class, the compiler automatically generates a default constructor. So, this option is true.

C. The compiler generates an error: This option is incorrect. Since the class does not have any member variables or functions, there is no reason for the compiler to generate an error. So, this option is false.

D. None of the above: This option is incorrect. As explained earlier, the correct answer is option B. So, this option is false.

Therefore, the correct answer is:

B. The compiler provides the default constructor.

Multiple choice technology programming languages
  1. Private members of the class

  2. protected members of the class

  3. Both A and B

  4. None of the Above

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

Friend functions in C++ are granted special access to the private and protected members of the class they befriend. This breaks normal encapsulation rules when needed. Friend functions are commonly used for operator overloading and when external functions need deep class access. They do not access public members differently than any other function.

Multiple choice technology programming languages
  1. Create

  2. Initialize

  3. Load

  4. New

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

The New procedure (constructor) is called when an object is instantiated, making it the ideal place to initialize class-level variables. In VB.NET, Sub New is the constructor that runs automatically when you create an instance using 'New'. Create is not a standard procedure name. Initialize is not a built-in event. Load is a form event that fires after the constructor, when the form is loaded, but variables need initialization earlier. The constructor is the first code that runs when an object is created.

Multiple choice technology programming languages
  1. The object itself

  2. a copy of reference

  3. the original reference

  4. a reference to a copy

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

In Java, object references are passed by value - meaning a copy of the reference is passed to the method, not the original reference itself or the object. The called method receives its own copy of the reference, which points to the same object. If the method reassigns the reference parameter, it doesn't affect the caller's reference - but modifications to the object's state are visible since both references point to the same object. Option B correctly captures this 'copy of reference' behavior.

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 because they're meant to be called when creating an instance of a class. An abstract class cannot be instantiated directly, so having an abstract constructor would be contradictory. When you inherit from an abstract class, you must call a concrete constructor from the base class. If constructors could be abstract, there would be no guaranteed way to initialize the base class. This is a fundamental rule in object-oriented programming.

Multiple choice technology programming languages
  1. True

  2. False

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

In Java, if a class contains one or more abstract methods, the class itself must be declared abstract. A non-abstract (concrete) class cannot have abstract methods because it cannot be instantiated - abstract methods have no implementation and must be implemented by concrete subclasses. This is a fundamental rule of Java's object-oriented design.

Multiple choice technology
  1. True

  2. False

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

According to the Java Language Specification, a source file is officially termed a 'compilation unit'. This is the formal terminology used throughout Java's documentation and specifications. Each compilation unit typically contains at most one public class or interface declaration and can include package and import statements.

Multiple choice technology
  1. Classes

  2. Function

  3. Method

  4. Object

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

In Java, functions are called 'methods' because they must belong to a class. C/C++ has standalone functions that exist independently of classes, while Java requires all code to be within classes. This terminology reflects Java's object-oriented design where everything exists within a class context.