Assignment 1: They Speak Java
BackgroundPage 1 of 4

1. Interview Questions

Interviewer: “I have 3 warm up Java questions for you. Get a pencil and a paper, and you have about 15 minutes to solve all 3 questions.”

Question 1

What is the output of the following code?

The possible choices are:

Question 2

Assume that there's a Duck class that looks like this. Duck is an animal that lives in the earth.

Now, what is the output of the following code?

The possible choices are:

Question 3

Assume that a Jel programmer made one class for each animal known to be in the earth, like so:

All of them have a toString() method, which returns its name.

Each class has an empty constructor, and can possibly have several of the following methods:

An example:


Let's say we need to implement a method, called printDescription(), which takes an instance of one of above classes and prints out its description.


The output of the above code is going to look like this:

can be our friend
can fly
loves meat
can eat us!

To implement this, you can use instanceof expression and casting.

Question for You

What is the problem with the code in the screenshot above? Note - the above code will still print a correct output.

An obvious one is that printDescription() method will be very long, because you need a if statement for every animal. But what's the deeper problem? How would you rewrite the above code to avoid that problem?

Before Moving on to Next Page

Please write down your answer briefly on a piece of paper or type it on your computer before proceeding to the next page.

Next Page

We will go over the answers. Next Page

Assignment 1:
They Speak Java