Class Inheritance

Practice Questions

Computer Science › Class Inheritance

Questions
10
1

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

2

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

3

Consider the following code:

public class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public class Square {

private double side;

public Square(double s) {

side = s;

}

public double getArea() {

return side * side;

}

public double getPerimeter() {

return 4 * side;

}

}

Which of the following represents a redefinition of Square that utilizes the benefits of inheritance?

4

class Z

{

public:

void Func4();

};

class Y : public Z

{

public:

virtual void Func3();

};

class B : public Y

public:

virtual void Func1();

void Func2();

};

class C : public B

public:

virtual void Func1();

};

What is the base class in the above code.

5

class Member{

public:

Member() { cout<< 1 ; }

};

class Base{

public:

Base(){ cout<< 1; }

Memeber member;

};

class Derived : public Base{

public:

Derived(){ cout << 3; }

};

int main(){

Derived der;

}

Given the code above, what is the output of the program?

6

class Pet {
public:
Pet() {}
virtual void bar() {cout << "In pet bar(); }
};

class Cat : public Pet {
public:

virtual void eat() {cout << "Cat eating"; }

virtual void bar() {cout << "In Cat bar()"; }

};

Given the above classes, what would the result of:

int main(){

Pet * petPtr = new Cat();

petPtr -> eat();

}

7

class Pet {
public:
Pet() {}
virtual void bar() {cout << "In pet bar(); }
};

class Cat : public Pet {
public:

virtual void eat() {cout << "Cat eating"; }

virtual void bar() {cout << "In Cat bar()"; }

};

Given the above classes, what would the result of:

int main(){

Cat felix;

Pet peeve;

peeve = felix;

peeve.bar();

}

8

Consider the following code:

private static class Philosopher {

private String name;

private String favoriteSubject;

public Philosopher(String n, String f) {

name = n;

favoriteSubject = f;

}

public String getName() {

return name;

}

public String getFavoriteSubject() {

return favoriteSubject;

}

public void speak() {

System.out.println("Hello, World! My name is "+name + ". My favorite subject is "+favoriteSubject);

}

}

private static class Nominalist extends Philosopher {

boolean franciscan;

public Nominalist(String n,boolean frank) {

super(n,"logic");

franciscan = frank;

}

public void speak() {

super.speak();

if(franciscan) {

System.out.println("I am a Franciscan");

} else {

System.out.println("I am not a Franciscan");

}

}

public String whoMightHaveTaughtMe() {

if(franciscan) {

return "Perhaps William of Ockham?....";

} else {

return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";

}

}

}

public static void main(String\[\] args) {

Philosopher\[\] phils = {

new Philosopher("Petrus","Ethics"),

new Nominalist("Minimus Maximus",false),

new Nominalist("Theodoric",true)};

for(int i = 0; i < phils.length; i++) {

phils\[i\].speak();

}

}

What is the output for the code above?

9

Which of the following is TRUE about the Object class?

10

For this question, consider the following code:

private static class Philosopher {

private String name;

private String favoriteSubject;

public Philosopher(String n, String f) {

name = n;

favoriteSubject = f;

}

public String getName() {

return name;

}

public String getFavoriteSubject() {

return favoriteSubject;

}

public void speak() {

System.out.println("Hello, World! My name is "+name + ". My favorite subject is "+favoriteSubject);

}

}

private static class Nominalist extends Philosopher {

boolean franciscan;

public Nominalist(String n,boolean frank) {

super(n,"logic");

franciscan = frank;

}

public void speak() {

super.speak();

if(franciscan) {

System.out.println("I am a Franciscan");

} else {

System.out.println("I am not a Franciscan");

}

}

public String whoMightHaveTaughtMe() {

if(franciscan) {

return "Perhaps William of Ockham?....";

} else {

return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";

}

}

}

public static void main(String\[\] args) {

Philosopher p = new Nominalist("Nicanor",false);

System.out.println(p.whoMightHaveTaughtMe());

}

What is the output for this main method?

Return to subject