Inheritance

A visually appealing illustration depicting the concept of inheritance in object-oriented programming, featuring elements like superclass, subclass, and various programming symbols.

Understanding Inheritance in Object-Oriented Programming

This quiz is designed to test your knowledge of inheritance, one of the fundamental concepts of object-oriented programming. Whether you're a student or a professional brushing up on your skills, this quiz will challenge you.

You'll encounter questions covering:

  • Superclasses and subclasses
  • Accessors and mutators
  • Encapsulation concepts
  • Real-world application scenarios
  • Code behavior and expected outputs
10 Questions2 MinutesCreated by CodingTree502
What is a superclass?
(a) An instance method that modifies the object's internal state.
(b) An instance method that provides information about the state of an object, without modifying it.
(c) Code that interacts with another class or objects of that class.
(d) The parent class in an inheritance relationship.
(e) Hiding the implementation details of an object from the clients of the object, usually using the private keyword.

What is a mutator?

(a) An instance method that modifies the object's internal state.
(b) An instance method that provides information about the state of an object, without modifying it.
(c) Code that interacts with another class or objects of that class.
(d) The parent class in an inheritance relationship.
(e) Hiding the implementation details of an object from the clients of the object, usually using the private keyword.
What is encapsulation?
(a) An instance method that modifies the object's internal state.
(b) An instance method that provides information about the state of an object, without modifying it
(c) Code that interacts with another class or objects of that class.
(d) The parent class in an inheritance relationship.
(e) Hiding the implementation details of an object from the clients of the object, usually using the private keyword.
What is an accessor?
(a) An instance method that modifies the object's internal state.
(b) An instance method that provides information about the state of an object, without modifying it.
(c) Code that interacts with another class or objects of that class.
(d) The parent class in an inheritance relationship.
(e) Hiding the implementation details of an object from the clients of the object, usually using the private keyword.
The instruction super( ); does which of the following?
(a) calls the method super as defined in the current class
(b) calls the method super as defined in the current class'parent class
(c) calls the method super as defined in java.lang
(d) calls the constructor as defined in the current class
(e) calls the constructor as defined in the current class'parent class
Which of the followings statements is NOT true about subclasses?
(a) A subclass can override inherited methods
(b) A subclass can directly access private fields of its superclass
(c) A subclass can add new public or private fields
(d) A subclass inherits the non-private fields and methods of its parent class
(e) A subclass is defined using the extends keyword
Consider the following class definitions.
 

public class A {

  public int al;

  public void methodA() {

    bl = 0;                             // Statement I

  }

  public void methodC() {

    al = 0;

  }

}

public class B extends A {

  public int bl;

  public void methodB() {

    methodC();                       // Statement II

    al = 0;                              // Statement III

  }

}

Which of the labeled statements in the methods shown above will cause a compile-time error?

(a) I only
(b) III only
(c) I and II
(d) I and III
(e) II and III
A soda is a drink and a refrigerator contains many drinks, including sodas. Three classes "Drink", "Soda", and "Refrigerator" are declared to represent drink, soda, and refrigerator objects. Which of the following is the most appropriate set of class declarations?
 

A:   public class Drink extends Refrigerator {

  private Soda mySoda;

 

}

B:   public class Soda extends Refrigerator {

 

}

C:   public class Drink extends Soda {

  public class Drink extends Soda

}

public class Refrigerator {

  private Drink[] myDrinks;

 

}

D:   public class Soda extends Drink {

 

}

public class Refrigerator {

  private Drink[] myDrinks;

 

}

E:   public class Drink extends Drink {

  public Drink drink;

}

(a)
(b)
(c)
(d)
(e)
Consider the following classes:
 

public class Super {

  public String toString() {

    return "super";

  }

}

 

public class Duper extends Super {

  public String toString() {

    return “duper";

  }

}

 

public class SuperDuper extends Duper {

  public String toString() {

    return super.toString() + "duper";

  }

}

What is output to the console when the following code is executed?

SuperDuper sd = new SuperDuper();

System.out.println(sd.toString());

(a) super
(b) duper
(c) duperduper
(d) superduper
(e) Nothing in printed, because an error occurs
Given the following class declarations, what is the output from Student s1 = new GradStudent(); followed by s1.getInfo();?
 

public class Student {

   public String getFood() {

      return "Pizza";

   }

   public String getInfo()  {

      return this.getFood();

   }

}

 

public class GradStudent extends Student {

   public String getFood() {

      return "Taco";

   }

}

(a) Won't compile since GradStudent doesn't have a getInfo method
(b) Taco
(c) Pizza
(d) Won't compile since you are creating a GradStudent, not a Student
(e) Won't compile since you use this.getFood()
{"name":"Inheritance", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"This quiz is designed to test your knowledge of inheritance, one of the fundamental concepts of object-oriented programming. Whether you're a student or a professional brushing up on your skills, this quiz will challenge you. You'll encounter questions covering: Superclasses and subclasses Accessors and mutators Encapsulation concepts Real-world application scenarios Code behavior and expected outputs","img":"https:/images/course8.png"}
Powered by: Quiz Maker