Search Header Logo

Override Methods - Java

Authored by Ayesha Abdullah

Computers

12th Grade

Used 115+ times

Override Methods - Java
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

4 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Consider the following class definitions.

public class Apple

{

public void printColor()

{

System.out.print("Red");

}

}

public class GrannySmith extends Apple

{

public void printColor()

{

System.out.print("Green");

}

}

public class Jonagold extends Apple

{

// no methods defined

}


The following statement appears in a method in another class.

someApple.printColor();

Under which of the following conditions will the statement print "Red" ?

I. When someApple is an object of type Apple

II. When someApple is an object of type GrannySmith

III. When someApple is an object of type Jonagold

I only

II only

I and III only

II and III only

I, II, and III

2.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Consider the following class definitions.

public class Pet

{

public void speak()

{

System.out.print("pet sound");

}

}

public class Dog extends Pet

{

public void bark()

{

System.out.print("woof woof");

}

public void speak()

{

bark();

}

}

public class Cat extends Pet

{

public void speak()

{

System.out.print("meow meow");

}

}

The following statement appears in a method in another class.

myPet.speak();

Under which of the following conditions will the statement compile and run without error?

I. When myPet is an object of type Pet

II. When myPet is an object of type Dog

III. When myPet is an object of type Cat

I only

II only

I and III only

II and III only

I, II, and III

3.

MULTIPLE CHOICE QUESTION

5 mins • 1 pt

Consider the following class definitions.

public class Hero

{

private String name;

private int power;

public Hero(String n, int p)

{

name = n;

power = p;

}

public void powerUp(int p)

{

power += p;

}

public int showPower()

{ return power; }

}

public class SuperHero extends Hero

{

public SuperHero(String n, int p)

{

super(n, p);

}

public void powerUp(int p)

{

super.powerUp(p * 2);

}

}

The following code segment appears in a class other than Hero and SuperHero.

Hero j = new SuperHero("JavaHero", 50);

j.powerUp(10);

System.out.println(j.showPower());

What is printed as a result of executing the code segment?

10

20

60

70

100

4.

MULTIPLE CHOICE QUESTION

5 mins • 1 pt

Consider the following class definitions.

public class Drink

{

// implementation not shown

}

public class Coffee extends Drink

{

// There may be instance variables and constructors that are not shown.

// No methods are defined for this class.

}

The following code segment appears in a method in a class other than Drink or Coffee.

Coffee myCup = new Coffee();

myCup.setSize("large");


Which of the following must be true so that the code segment will compile without error?

The Drink class must have a public method named getSize that takes a String value as its parameter.

The Drink class must have a public method named getSize that takes no parameters.

The Drink class must have a public method named setSize that takes a String value as its parameter.

The Drink class must have a public method named setSize that takes no parameters.

The Drink class must have a String instance variable named size.

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Microsoft

Continue with Microsoft

or continue with

Facebook

Facebook

Apple

Apple

Others

Others

Already have an account?