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 .net
  1. A base class inherits some of the properties of a derived class.

  2. A base class inherits all of the properties of a derived class.

  3. A derived class inherits some of the properties of a base class.

  4. A derived class inherits all of the properties of a base class.

  5. None of the above.

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

In inheritance, the derived class (child) inherits ALL properties and methods from the base class (parent) - it gets everything the base class has. A base class does not inherit from derived classes - inheritance flows one way from parent to child. Option D correctly states that a derived class inherits all properties of its base class.

Multiple choice .net
  1. there is no effect on the derived class.

  2. the derived class changes when the key word Overridden is used.

  3. only the methods of the derived class change.

  4. only the properties of the derived class change.

  5. the derived class automatically changes.

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

When a base class is modified, all derived classes automatically inherit those changes because they extend from the base. This is one of the key benefits and risks of inheritance - changes ripple down automatically. The keyword 'Overridden' (typically 'override') in option B is for method overriding in derived classes, not for automatic propagation of base class changes.

Multiple choice .net
  1. math operators.

  2. method names.

  3. object names.

  4. Both a and b.

  5. All of the above.

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

Polymorphism allows the same operation to behave differently based on the objects or types involved. Operator overloading lets math operators (like +) work differently for different types, and method overloading/overriding lets methods have different behaviors based on parameters or object types. Polymorphism doesn't typically apply to object names themselves.

Multiple choice .net
  1. one method can have multiple names.

  2. one object can have multiple names.

  3. many methods can share the same name.

  4. many objects can share the same name.

  5. None of the above statements are true.

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

To understand the concept of polymorphism, it's important to have knowledge of object-oriented programming and the principles of inheritance and method overriding. Polymorphism is a concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. This concept enables different classes to have methods with the same name, but different implementations.

Now, let's go through each statement and explain whether it is true or false:

A. one method can have multiple names: This statement is false. In polymorphism, multiple methods can have the same name, but they must have different parameter lists or be defined in different classes.

B. one object can have multiple names: This statement is false. An object can have one name and can be referred to by a single reference variable.

C. many methods can share the same name: This statement is true. Polymorphism allows multiple methods with the same name but different parameter lists to be defined in different classes or even within the same class.

D. many objects can share the same name: This statement is false. Each object has a unique identity and is referred to by a specific reference variable.

E. None of the above statements are true: This statement is false. Statement C, "many methods can share the same name," is true.

So, the correct answer is C. Many methods can share the same name.

I hope this clarifies the concept of polymorphism for you. Let me know if you have any further questions.

Multiple choice .net
  1. Constructs

  2. Fields

  3. Methods

  4. Properties

  5. All of the above.

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

A class can have any combination of the following elements:

  • Constructs: These are special methods that are used to initialize an object. They are not required, but they are often used.
  • Fields: These are variables that are associated with an object. They are not required, but they are often used to store data about the object.
  • Methods: These are functions that are associated with an object. They are not required, but they are often used to perform operations on the object.
  • Properties: These are a way to access and modify the fields of an object. They are not required, but they can make the code more readable and maintainable.

Therefore, the answer to the question is E. All of the above. Any of these elements can be optional in a class.

Multiple choice .net
  1. Constructs, fields, methods, properties

  2. Properties, constructs, fields, methods

  3. Fields, properties, constructs, methods

  4. Constructs, properties, fields, methods

  5. Methods, constructs, properties, fields

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

Class elements are typically ordered by accessibility and scope: fields (internal state) first, then properties (public accessors), then constructors (initialization), then methods (behavior). This follows the principle of declaring data before the code that operates on it.

Multiple choice .net
  1. private method.

  2. public method.

  3. private variable.

  4. public variable.

  5. None of the above.

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

The standard design principle is that fields should be private variables (for encapsulation), not methods. However, option C says 'private variable' when the correct term is 'private field' - fields and variables are related but distinct concepts in OOP. Since none of the options correctly state 'private field', E is correct.

Multiple choice .net
  1. a sub procedure.

  2. a function.

  3. an event.

  4. Both a and b.

  5. All of the above.

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

Methods in classes can be either sub procedures (which perform actions without returning values) or functions (which return values). Constructors are a special type of method, and events are not methods but are raised by actions. Therefore, methods can be both subs and functions.

Multiple choice .net
  1. 1

  2. 2

  3. 3

  4. All of the above.

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

To solve this question, the user needs to have a basic understanding of object-oriented programming and the concept of constructors.

A constructor is a special method that is called when an object of a class is created. It is used to initialize the data members of the class. The number of constructors that a class can have depends on the programming language being used and the requirements of the developer.

Now, let's go through each option and explain why it is right or wrong:

A. 1: This option is incorrect because a class can have more than one constructor. In fact, many programming languages allow for the creation of multiple constructors with different parameter lists, which can be used to create objects of the class in different ways.

B. 3: This option is incorrect because the number of constructors that a class can have is not fixed. The actual number of constructors that can be defined for a class depends on the language and requirements of the developer.

C. All of the above: This option is also incorrect because option B is incorrect and option C cannot be correct if any of the options are incorrect.

D. 2: This option is incorrect because a class can have more than two constructors. As mentioned earlier, the number of constructors that a class can have depends on the programming language and the requirements of the developer.

Therefore, the correct answer is:

The Answer is: C

Multiple choice .net
  1. class.

  2. field.

  3. method.

  4. property.

  5. variable.

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

A constructor is a special method that initializes objects when they are created. It shares the same name as the class and has no return type, not even void. Constructors set initial values for fields and perform any necessary setup when an instance is created.

Multiple choice .net
  1. All constructors must have the same number of parameters.

  2. All constructors must be the same parameter data type.

  3. Some constructors can have the same list of parameters.

  4. Only two constructors in a class can have the same list of parameters.

  5. No two constructors in a class can have the same list of parameters.

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

Constructor overloading requires each constructor to have a unique parameter signature (number, order, or data types of parameters). If two constructors had identical parameter lists, the compiler couldn't determine which one to call when creating an object with those arguments.

Multiple choice .net
  1. Base.New( )

  2. BaseConstructor.New ( )

  3. CallBase.New( )

  4. Constructor.New ( )

  5. MyBase.New( )

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

In VB.NET, the 'MyBase' keyword refers to the base class in inheritance. 'MyBase.New()' explicitly calls the base class constructor from the derived class constructor, ensuring proper initialization of the inherited portion of the object.

Multiple choice java
  1. Static

  2. Private

  3. Default

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

In Java, top-level classes cannot be declared private or protected; they must be public or have default (package-private) visibility. While inner classes can be private, the question refers to standard class declarations where 'private' is invalid.

Multiple choice java
  1. True

  2. False

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

Classes themselves cannot be transient. The 'transient' keyword applies to fields in serialization (like Java), indicating which fields should not be serialized. A class as a whole is not marked transient - only individual member variables can have this modifier.

Multiple choice java
  1. Compile error

  2. Runtime Exception

  3. No error

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

This is a type mismatch error. Variable 'b' is declared as type B, but the code attempts to assign an instance of A (the parent class). A parent class instance cannot be assigned to a child class reference without explicit casting, because the child may have members not present in the parent.