Self-Quiz Unit 5 - Attempt Review

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

10/14/21, 12:39 AM Self-Quiz Unit 5: Attempt review

Dashboard / My courses /
CS 1102 - AY2022-T1 / 30 September - 6 October /
Self-Quiz Unit 5

Started on Monday, 4 October 2021, 2:26 PM


State Finished
Completed on Monday, 4 October 2021, 2:31 PM
Time taken 5 mins 7 secs
Marks 2.00/5.00
Grade 40.00 out of 100.00

https://my.uopeople.edu/mod/quiz/review.php?attempt=5697673&cmid=265365 1/6
10/14/21, 12:39 AM Self-Quiz Unit 5: Attempt review

Question 1

Correct

Mark 1.00 out of 1.00

What is the output of the following Java program?

class Food {

    Food() { System.out.println("bland"); }

class Pepper extends Food {

    Pepper() { System.out.println("spicy"); }

public class Lunch {

    public static void main(String[] args) {

        Food lunch = new Pepper();

    }
}

Select one:
a. bland

b. bland 

spicy

c. no output

d. spicy

e. the program does not compile

Your answer is correct.

To construct an object, the compiler automatically calls the constructors for any superclasses of an object (unless there is an explicit
"super" call). Thus, "Food()" prints "bland", and then "Pepper()" prints "spicy". See Section 5.6.3.
The correct answer is: bland

spicy

https://my.uopeople.edu/mod/quiz/review.php?attempt=5697673&cmid=265365 2/6
10/14/21, 12:39 AM Self-Quiz Unit 5: Attempt review

Question 2

Incorrect

Mark 0.00 out of 1.00

A subclass will _____ one superclass.

Select one:
a. abstract

b. extend

c. implement

d. inherit 

e. override

Your answer is incorrect.


See Section 5.5.1.
The correct answer is: extend

https://my.uopeople.edu/mod/quiz/review.php?attempt=5697673&cmid=265365 3/6
10/14/21, 12:39 AM Self-Quiz Unit 5: Attempt review

Question 3

Correct

Mark 1.00 out of 1.00

What is the output of the following Java program?

abstract class Food {

    abstract void printFlavor();

class Pepper extends Food {

    void printFlavor() { System.out.println("spicy"); }

public class Lunch {

    public static void main(String[] args) {

        Food pepper = new Food();

        pepper.printFlavor();

    }
}

Select one:
a. bland

b. bland
spicy

c. no output

d. spicy

e. the program does not compile 

Your answer is correct.


The program tries to initialize "pepper" to an object of type "Food", but "Food" is an abstract class. Variables can have abstract classes
as their type, but objects cannot. See Section 5.5.5.
The correct answer is: the program does not compile

https://my.uopeople.edu/mod/quiz/review.php?attempt=5697673&cmid=265365 4/6
10/14/21, 12:39 AM Self-Quiz Unit 5: Attempt review

Question 4

Incorrect

Mark 0.00 out of 1.00

What is the output of the following Java program?

class Food {

     void flavor() { System.out.println("bland"); }

 }

 class Pepper extends Food {

     void flavor() { System.out.println("spicy"); }

 }

 public class Lunch {

     public static void main(String[] args) {

         Food lunch = new Pepper();

         lunch.flavor();

     }

 }

Select one:
a. bland

b. bland 

spicy

c. no output

d. spicy

e. the program does not compile

Your answer is incorrect.


The method "flavor" in class "Pepper" overrides the one in class "Food". Because of polymorphism, the variable "lunch" uses the
"Pepper" method even though it is type "Food". This is because it refers to an object of type "Pepper". See Section 5.5.4.
The correct answer is: spicy

https://my.uopeople.edu/mod/quiz/review.php?attempt=5697673&cmid=265365 5/6
10/14/21, 12:39 AM Self-Quiz Unit 5: Attempt review

Question 5

Incorrect

Mark 0.00 out of 1.00

What is the output of the following Java program?

class Food {

    Food() { System.out.println("bland"); }

class Pepper extends Food {

    Pepper() { this("spicy"); }

    Pepper(String flavor) { System.out.println(flavor); }

public class Lunch {

    public static void main(String[] args) {

        Food lunch = new Pepper();

    }
}

Select one:
a. bland

b. bland
spicy

c. no output

d. spicy 

e. the program does not compile

Your answer is incorrect.

First the "Food" constructor prints "bland". Then the "Pepper" constructor that takes no parameters calls 'this("spicy")', which represents
the "Pepper" constructor that takes a String parameter. It prints its parameter, "spicy". See Section 5.6.3.

The correct answer is: bland

spicy

◄ Learning Journal Unit 5

Jump to...

Unit 4 Programming Assignment Solution ►


https://my.uopeople.edu/mod/quiz/review.php?attempt=5697673&cmid=265365 6/6

You might also like