Unit 5 Quiz AY2021 CS1102 PDF

Title Unit 5 Quiz AY2021 CS1102
Course Programming 1
Institution University of the People
Pages 13
File Size 171.8 KB
File Type PDF
Total Downloads 7
Total Views 134

Summary

Unit 5 Quiz AY2021 CS1102...


Description

CS1102 AY2021 Unit 5 Quiz Question 1

What is the output of the following Java program?

class Food {

String flavor = "bland";

}

class Pepper extends Food {

String flavor = "spicy";

}

public class Lunch {

public static void main(String[] args) {

Pepper lunch = new Pepper();

System.out.println(lunch.flavor);

}

}

Select one:

a. bland

b. bland

spicy

c. no output

d. spicy

e. the program does not compile

Your answer is correct.

The member "flavor" of class Pepper hides the member "flavor" of class Food. A variable of type "Pepper" sees the "Pepper" member by default. See Section 5.6.2.

The correct answer is: spicy

Question 2

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

Question 3

What is the output of the following Java program?

class Food {

String flavor = "bland";

void printFlavor() { System.out.println(flavor); }

}

class Pepper extends Food {

String flavor = "spicy";

void printFlavor() { System.out.println(flavor); }

}

public class Lunch {

public static void main(String[] args) {

Food lunch = new Pepper();

lunch.printFlavor();

}

}

Select one:

a. bland

b. bland

spicy

c. no output

d. spicy

e. the program does not compile

Your answer is correct.

The "printFlavor" methods in "Food" and "Pepper" look identical, but they refer to different "flavor" member variables. Because of polymorphism, the "Pepper" override of "printFlavor" gets called, which refers to the "flavor" member declared by "Pepper". See Sections 5.5.4 and 5.6.1.

The correct answer is: spicy

Question 4

A class can _____ multiple interfaces.

Select one:

a. abstract

b. extend

c. implement

d. inherit

e. override

Your answer is incorrect.

See Section 5.7.1.

The correct answer is: implement

Question 5

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

Feedback

Your answer is correct.

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...


Similar Free PDFs