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 c-sharp
  1. Class

  2. Constructor

  3. Object

  4. a and b

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

The statement 'String mystring;' declares a reference variable of type String. In many contexts, this is referred to as creating an object reference or an object instance (though technically the 'new' keyword is usually required for instantiation). It is not a class definition or a constructor.

Multiple choice .net c-sharp
  1. a Wahsington representative

  2. a class that encapsulates methods

  3. a means of passing arrays into methods

  4. a substitue for an inherited method

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

To answer this question, the user needs to have knowledge about programming concepts, particularly about delegates.

The correct answer is:

B. a class that encapsulates methods

Explanation:

In .NET programming, a delegate is a type that defines a method signature. It encapsulates one or more methods, allowing them to be passed around as objects. A delegate is a reference type that can be used to encapsulate a named or an anonymous method. Delegates are used extensively in event handling and call-back scenarios, where they provide a way to define a method to be called when a certain event occurs. Thus, option B is the correct answer.

Option A is incorrect because a delegate is not related to a Washington representative.

Option C is incorrect because a delegate does not provide a means of passing arrays into methods.

Option D is incorrect because a delegate is not a substitute for an inherited method.

Multiple choice .net c-sharp
  1. True

  2. False

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

To solve this question, the user needs to know the concept of passing methods as arguments in programming.

The answer to this question is:

The Answer is: A

Explanation:

In programming, it is possible to pass methods as arguments for other methods without modification. This is a common practice in functional programming, where functions are treated as first-class citizens. By passing functions as arguments, you can create more generic and reusable code.

For example, in Python, you can pass a function as an argument to another function using the following syntax:

def my_func(func):
    # Do some work
    func()
    # Do some more work

def my_other_func():
    print("Hello, world!")

my_func(my_other_func)

In this example, my_func takes a function func as an argument and calls it during its execution. The my_other_func function is defined separately and passed to my_func as an argument. When my_func is called with my_other_func as its argument, it will execute my_other_func and print "Hello, world!".

Therefore, option A is correct.

Multiple choice .net c-sharp
  1. class MyClass IFace

  2. class MyClass ; IFace

  3. class MyClass : IFace

  4. class MyCalss {IFace}

  5. class MyCalss(IFace)

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

To properly implement an interface in a class, you would use the syntax specified in option C: class MyClass : IFace.

The colon (:) is used to indicate that the class MyClass is implementing the IFace interface. This allows the class to inherit the methods and properties defined in the interface.

Options A, B, D, and E do not follow the correct syntax for implementing an interface.

Option A, class MyClass IFace, is incorrect because it does not indicate that MyClass is implementing the IFace interface.

Option B, class MyClass ; IFace, is incorrect because the semicolon (;) is not used to indicate interface implementation.

Option D, class MyCalss {IFace}, is incorrect because the interface implementation is not specified after the class declaration.

Option E, class MyCalss(IFace), is incorrect because parentheses are not used to indicate interface implementation.

Therefore, the correct answer is C. class MyClass : IFace.

Multiple choice .net c-sharp
  1. inherit the properties of the interface

  2. contain the same methods as the interface

  3. create an interface objects

  4. a and b

  5. all of the above

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

To use an interface, a class must implement it, which involves providing definitions for all methods specified in the interface. While 'inherit' is often used loosely, interfaces are implemented. The class must contain the same method signatures to satisfy the contract. Option 2763 is marked correct as it encompasses these requirements.

Multiple choice .net c-sharp
  1. System

  2. Object

  3. Drawing

  4. Console

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

In the .NET Framework and Java, 'Object' is the ultimate root of the type hierarchy. Every class, whether user-defined or built-in, implicitly inherits from the Object class if no other base class is specified. System is a namespace, not the root class.

Multiple choice .net c-sharp
  1. Override the parent class methods but maintain the implementation

  2. Maintain the same return type and arguments as the parent class, but implement it differently

  3. Have different return types and arguments than the parent class

  4. Are Virtual

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

Polymorphism (specifically subtype polymorphism) allows a child class to provide a specific implementation of a method defined in the parent class. The method signature (return type and arguments) must remain the same to satisfy the interface, while the internal logic changes.

Multiple choice .net c-sharp
  1. True

  2. False

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

An abstract class can contain both abstract methods (without implementation) and concrete methods (with implementation). The presence of at least one abstract method requires the class itself to be declared abstract, but it does not force all other methods to be abstract.

Multiple choice .net c-sharp
  1. Defines a class that inherits all the methods of A

  2. Defines a class that inherits the public and protected methods of A only

  3. Errors

  4. a and b

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

In C#, the colon syntax denotes inheritance. While a child class technically 'inherits' all members, it can only access public and protected members of the base class. Private members are inherited but remain inaccessible to the child class. Option 2784 is technically true in a structural sense, but 2785 is the standard academic answer regarding accessibility.

Multiple choice .net c-sharp
  1. One constructor takes an argument of type i

  2. There is only a default constructor

  3. One constructor takes an arguments of the type int

  4. a and b

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

The :base(i) syntax calls the parent constructor with an int parameter, so class A must have a constructor accepting int. Option A is wrong because i is the variable name, not the type. Option B is wrong because a parameterized constructor is being called. C correctly identifies an int-taking constructor exists.

Multiple choice .net c-sharp
  1. True

  2. False

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

To solve this question, the user must understand the concept of the sealed keyword in object-oriented programming languages.

Explanation:

The sealed keyword is used to restrict inheritance in object-oriented programming. When a class is declared with the sealed keyword, it means that no other class can inherit from it. In other words, a sealed class cannot be used as a base class for another class.

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

A. True: This option is correct. When a class is declared with the sealed keyword, it cannot be used as a base class. Other classes cannot inherit from a sealed class.

B. False: This option is incorrect. Classes declared with the sealed keyword cannot be used as a base class, so the statement is true.

The Answer is: A

Multiple choice .net c-sharp
  1. may contain instance variables

  2. may contain constructors

  3. may extend another class

  4. a and b

  5. all of the above

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

Abstract classes can contain instance variables, constructors (though not directly instantiated), and extend another class (single inheritance). All statements A, B, and C are true, so E is correct.

Multiple choice .net c-sharp
  1. System.Strings namespace

  2. System.Text namespace

  3. System.Chars namespace

  4. System namespace

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

In C#, the String class and Char structure are both part of the System namespace. String is an alias for System.String, and Char represents System.Char. They are fundamental types and do not require separate namespaces like System.Text or System.Strings.

Multiple choice javascript
  1. JavaScript is Object-oriented

  2. JavaScript is Object-based

  3. JavaScript is Object-driven

  4. JavaScript has no relationship with objects

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

JavaScript is described as object-based rather than object-oriented because it uses prototype-based inheritance instead of classical class-based inheritance. It supports objects and methods, but lacks traditional OOP features like classes and interfaces.