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 java core java
  1. 1.information hiding

  2. 2.encapsulation

  3. 3.Inheritance

  4. 4.modularity

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

To solve this question, the user needs to be familiar with the concepts of object-oriented programming and the principles of encapsulation and information hiding.

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

A. 1. information hiding: This option is correct. Information hiding is the practice of encapsulating the internal details of an object and providing access to it only through its member functions. This helps to hide the complexity and implementation details of an object, allowing for better maintainability and preventing unauthorized access to sensitive information.

B. 2. encapsulation: This option is correct. Encapsulation is the practice of bundling data and methods together within a class and providing controlled access to them. It allows for the implementation details of an object to be hidden and accessed only through the defined interface, promoting modularity and code reusability.

C. 3. Inheritance: This option is incorrect. Inheritance is a concept in object-oriented programming where a class derives properties and behaviors from another class. It is not directly related to the practice of providing access to an object only through its member functions and keeping the details private.

D. 4. modularity: This option is incorrect. Modularity refers to the practice of dividing a program into smaller, independent modules that can be developed, tested, and maintained separately. While encapsulation and information hiding contribute to modularity, modularity does not specifically refer to the practice of providing access to an object only through its member functions and keeping the details private.

The Answer is: B. 2. encapsulation

Multiple choice java core java
  1. 1.static,nonstatic class

  2. 2.Inheritance,Composition

  3. 3.Composition,Inheritance

  4. 4.Overriding,Overloading

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

A 'has a' relationship represents Composition (one object contains another), while 'is a' represents Inheritance (one object is a type of another). Option C correctly identifies this mapping. Option B reverses them, and options A/D describe unrelated concepts.

Multiple choice qn2
  1. Interfaces

  2. Abstraction

  3. Classes

  4. None

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

To answer this question, the user needs to know the concept of inheritance in object-oriented programming and how it is implemented in C#.

C# does not support multiple inheritance with classes, which means a class cannot inherit from multiple classes at the same time. However, C# does support multiple inheritance with interfaces.

Interfaces are a way to define a set of methods and properties that a class must implement. A class can implement multiple interfaces, which allows it to inherit functionality from each interface.

Abstraction is a concept in object-oriented programming that allows you to define a set of methods or properties without providing an implementation. This allows other classes to inherit from the abstract class and provide their own implementations of the methods and properties.

Therefore, the correct answer is:

The Answer is: A. Interfaces

Multiple choice qn5
  1. Converting objects to byte stream

  2. Converting objects to strings

  3. Converting objects to unicode characters

  4. None

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

To solve this question, the user needs to have knowledge about serialization. Serialization is the process of converting an object into a stream of bytes or characters, which can be stored or transmitted over a network.

Option A is correct. Serialization involves converting objects to byte streams, which can be easily transmitted over a network or stored in a file.

Option B is incorrect because serialization does not involve converting objects to strings. Although strings can be used to represent serialized data, they are not the same thing as serialization.

Option C is incorrect because serialization does not involve converting objects to Unicode characters. Unicode is a character encoding scheme that allows characters from different languages to be represented in a consistent manner. However, it is not directly related to serialization.

Option D is incorrect because serialization is a real concept that involves converting objects to byte streams or character streams.

Therefore, the correct answer is: A. Converting objects to byte stream.

Multiple choice c #
  1. Static polymorphism

  2. Dynamic polymorphism

  3. Both static and dynamic polymorphism

  4. Not a polymorphism.

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

To answer this question, the user needs to have a basic understanding of polymorphism.

Method overloading is a feature in object-oriented programming where a class can have multiple methods with the same name but with different parameters. This allows the same method name to be used for different actions, making the code more readable and easier to use.

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

A. Static polymorphism: This option is correct. Method overloading is an example of static polymorphism because the method to be called is determined at compile-time based on the number, types, and order of parameters passed to the method.

B. Dynamic polymorphism: This option is incorrect. Dynamic polymorphism is when the method to be called is determined at runtime based on the object type. This is different from method overloading, which is an example of static polymorphism.

C. Both static and dynamic polymorphism: This option is incorrect. Method overloading is an example of static polymorphism only.

D. Not a polymorphism: This option is incorrect. Method overloading is a type of polymorphism, specifically static polymorphism.

Therefore, the correct answer is: A. Static polymorphism.

Multiple choice c sharp
  1. using base keyword

  2. using super keyword

  3. creating object of parent within child

  4. using this keyword

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

In C#, the 'base' keyword is used to call the parent class constructor from a child class constructor. This is commonly done using the constructor initializer syntax (e.g., 'public Child(int x) : base(x)'). The 'super' keyword is used in Java for the same purpose, but this question is clearly C#-focused given the terminology and context. Creating a parent object inside the child class and using 'this' keyword serve different purposes.

Multiple choice c sharp
  1. Sealed

  2. Abstract

  3. Private

  4. Protected

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

A sealed class in C# cannot be inherited or derived from. This is explicitly designed using the 'sealed' keyword when you want to prevent inheritance. Abstract classes are meant to be derived from, while Private and Protected are access modifiers that apply to class members, not to class inheritance itself. You can mark a class as sealed to stop other classes from inheriting it.

Multiple choice c sharp
  1. Overriding

  2. Overloading

  3. Shadowing

  4. Method Hiding

  5. Both c and d

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

The 'new' keyword in C# is used for method hiding, which is also referred to as shadowing. When a derived class method has the same name as a base class method but isn't marked with 'override', using 'new' explicitly indicates you're intentionally hiding the base method. Shadowing and method hiding are the same concept in C# terminology. Overriding requires virtual/override keywords, while overloading refers to having multiple methods with different parameters.

Multiple choice c sharp
  1. Static polymorphism

  2. Dynamic polymorphism

  3. Both static and dynamic polymorphism

  4. Not a polymorphism

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

Method overriding is a feature that allows a subclass to provide a specific implementation of a method already defined in its superclass. The specific method to be executed is determined at runtime, making it dynamic polymorphism.

Multiple choice c sharp
  1. Serial

  2. Local

  3. Private

  4. Static

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

Variables declared inside a method are called local variables because they are only accessible within that method's scope. Local variables are created when the method is called and destroyed when the method exits. Option A (Serial) is not a standard variable classification term. Option C (Private) refers to class-level access control, not method-level scope. Option D (Static) refers to class variables shared across all instances.

Multiple choice c sharp
  1. It can be used anywhere in the program

  2. It must accept a class

  3. It must be declared within a method

  4. It represent a class object

Reveal answer Fill a bubble to check yourself
D Correct answer
Multiple choice java
  1. Methods and variables

  2. Class

  3. Variables

  4. Methods

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

Overriding (runtime polymorphism through inheritance) and overloading (compile-time polymorphism with same name, different parameters) are concepts that apply specifically to methods in object-oriented programming. They are not applicable to variables or classes.

Multiple choice accessibility
  1. A. Only Static methods of the same class

  2. B. Only instances of the same class

  3. C. Only methods those defined in the same class

  4. D. Only classes available in the same package.

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

Private attributes of a class can only be accessed by methods defined within the same class. This is encapsulation - private members are invisible outside the class boundary. Static methods are subject to the same rules as instance methods (no special access). Instances of the same class can't directly access private members of other instances. Classes in the same package have no special access to private members - they would need protected or package-private access modifiers.