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
  1. Compiler error

  2. Runtime Exception

  3. No errors

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

This is a valid upcast. Variable 'a' is declared as type A (the parent class), and it's assigned an instance of B (the child class). This is always valid because a child class 'is-a' parent class through inheritance - the child has all the members the parent has.

Multiple choice java
  1. True

  2. False

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

The answer is: A

Explanation:

An abstract class can contain both abstract and non-abstract methods. Abstract methods are those that do not have an implementation, while non-abstract methods have a complete implementation. However, any class that contains at least one abstract method must be declared as abstract, which means that it cannot be instantiated directly.

Multiple choice java
  1. A methods in object

  2. An operator and keyword

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

To solve this question, the user needs to have knowledge about programming concepts and specifically about the instanceof operator.

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

A. A methods in object: This option is incorrect. instanceof is not a method in an object. It is an operator that is used to check if an object belongs to a particular class or its subclasses.

B. An operator and keyword: This option is correct. instanceof is both an operator and a keyword in programming languages like Java. It is used to determine if an object is an instance of a particular class or its subclasses. The instanceof operator returns true if the object is an instance of the specified class or a subclass, and false otherwise.

Therefore, the correct answer is:

B. An operator and keyword

Multiple choice java
  1. True

  2. False

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

Java supports local inner classes defined within method bodies. These classes are scoped to the method block and can access the method's local variables (if final or effectively final).

Multiple choice java
  1. By marking it private

  2. By marking it volatile

  3. By marking it transient

  4. You can not.

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

The transient keyword in Java explicitly prevents a field from being serialized. When an object is serialized, transient fields are skipped and get default values upon deserialization.

Multiple choice .net c-sharp
  1. Can be used anywhere in the program

  2. Is declared within a method

  3. Must accept a class

  4. Represent a class object

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

A local variable is declared within a method, constructor, or block and is only accessible within that scope. It cannot be used anywhere in the program (that would be a global variable), it doesn't 'accept a class,' and it doesn't necessarily represent a class object (it could be a primitive type like int or an object reference).

Multiple choice .net c-sharp
  1. is an object of a class

  2. represents an attribute of an object

  3. is a method of a class

  4. a and c

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

To answer this question, we need to understand the concept of instance variables in object-oriented programming.

An instance variable represents an attribute of an object. It is a variable that is unique to each instance (or object) of a class. It holds data that is specific to that particular object.

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

A. represents an attribute of an object: This option is correct. As explained above, an instance variable represents an attribute of an object. It holds data that is specific to that particular object.

B. is an object of a class: This option is incorrect. An instance variable is not an object itself, but rather a variable that is associated with an object.

C. is a method of a class: This option is incorrect. An instance variable is not a method. Methods are actions or behaviors that objects can perform, while instance variables hold data or state of an object.

D. a and c: This option is incorrect. As explained above, an instance variable represents an attribute of an object, but it is not a method of a class. Therefore, the correct answer is not D.

The correct answer is: A

Multiple choice .net c-sharp
  1. creates a button control

  2. initializes a button control

  3. instantiates button control

  4. a and b

  5. a and c

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

The 'new' keyword instantiates the Button class, creating a specific instance in memory. This process also creates the control for use in the UI. 'Initializes' usually refers to setting initial values, while 'instantiates' and 'creates' are the most accurate terms for this code line.

Multiple choice .net c-sharp
  1. Represents the behavior of an object

  2. Represents the attribute of an object

  3. Represents another class

  4. a and b

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

To solve this question, the user needs to know the difference between instance methods and instance variables in object-oriented programming.

An instance method represents the behavior of an object. It describes what actions an object can perform or what operations it can carry out. On the other hand, an instance variable represents the attribute of an object. It stores the state of an object and describes its characteristics.

Therefore, option A is the correct answer as it accurately describes what an instance method is. Option B is incorrect as it describes an instance variable. Option C is also incorrect as it does not accurately describe an instance method. Option D is incorrect as it is a combination of both correct and incorrect answers.

The Answer is: A

Multiple choice .net c-sharp
  1. is used to create objects

  2. must have the same name as the class it is declared within

  3. maybe overloaded

  4. b and c

  5. all of the above

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

To solve this question, it is important to understand the concept of constructors in object-oriented programming.

A constructor is a special method within a class that is used to create and initialize objects of that class. It is called automatically when an object of the class is created.

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

A. is used to create objects: This option is correct. Constructors are used to create objects of a class. They initialize the object's state and allocate memory for it.

B. must have the same name as the class it is declared within: This option is correct. Constructors must have the same name as the class they are declared within. This is a naming convention that helps identify and associate the constructor with its corresponding class.

C. maybe overloaded: This option is correct. Constructors can be overloaded, which means that a class can have multiple constructors with different parameter lists. Overloading allows for flexibility in creating objects with different initializations.

D. b and c: This option is correct. Both option B and C are correct, as explained above.

E. all of the above: This option is correct. Since options A, B, and C are all correct, option E, which states "all of the above," is the correct answer.

The Answer is: E

Multiple choice .net c-sharp
  1. Creates the class Test : Form

  2. Creates the class Test that inherits the class Form

  3. Creates the class form that inherits the class Test

  4. a and b

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

To understand the code snippet and determine the correct option, it helps to have knowledge of object-oriented programming and the concept of inheritance.

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

A. Creates the class Test : Form: This option is incorrect because the colon (:) in this context does not denote inheritance. Instead, it is likely a typo or syntax error. The correct syntax for inheritance in many programming languages, including C#, is to use the keyword "inherits" or a similar keyword.

B. Creates the class Test that inherits the class Form: This option is correct. The code snippet is likely written in a language that uses the "inherits" keyword to denote inheritance. In this case, the class Test is being created, and it is inheriting the properties and methods of the class Form.

C. Creates the class Form that inherits the class Test: This option is incorrect because the class Form is not being created. Instead, the code snippet is defining the class Test.

D. a and b: This option is incorrect because option A is incorrect. The correct answer would be b, as explained above.

The correct answer is: B. Creates the class Test that inherits the class Form