Log in
Sign up for FREE
arrow_back
Library

Inheritance Unit Test

star
star
star
star
star
Last updated almost 2 years ago
13 questions
5
5
5
5
5
5
5
5
5
5
5
5
40
Question 1
1.

What is the output by the code below?
public class J
{
private int x, y;
public J() {
x = y = 3;
}
public void fun() {
x = y = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y;
}
}
public class K extends J {
public void fun() {
out.println(back());
}
public String toString() {
return "class K " + super.toString();
}
}
//test code in a client program
J runner = new K();
out.println(runner);

Question 2
2.

Which of the following lines would correctly fill /* blank */ to pass x to the appropriate parent constructor?
public class P
{
private int one;
public P(){ one=0; }
public P(int num){
one=num;
}
}
public class Q extends P
{
public Q() { }
public Q(int x){
/* blank */
}
}

Question 3
3.

What is output by the code below?

Object c = new String("Hello");
String d = new String("Hello");
System.out.println(c.equals(d));

Question 4
4.

What is the output of line 2?
public class X{
public void one(){
System.out.print("Xone");
}
public void two(){
System.out.print("Xtwo");
}
}
public class Y extends X{
public void one(){
System.out.print("Yone");
}
public void two(){
System.out.print("Ytwo");
}
public void testIt(){
one();
super.one();
two();
super.two();
}
}
//code in the main of another class
Y code = new Y();
code.one(); //line 1
code.testIt(); //line 2

Question 5
5.

What is output by the code below?
public class Cat {
private int x, y;
public Cat() {
x=y=5;
}
public void fun() {
x=7;
}
public double go() {
return x;
}
public void back() {
fun();
}
public String toString() {
return x + " " + y;
}
}
public class Lion extends Cat {
private int x;
public Lion() {
x=9;
}
public void fun() {
x=3;
}
public double go() {
return x;
}
public void back() {
super.back();
}
public String toString() {
return super.toString() + " " + x;
}
}
//code in a client program
Cat fred = new Lion();
fred.fun();
System.out.println(fred.go());

Question 6
6.

What is output by the code below?
public class Whale
{
private int x, y;
public static int c;
public Whale() {
c++;
}
public void fun() {
x = 6;
}
public int back() {
return 1;
}
public String toString() {
return x + " " + y + " " + c;
}
}
public class Beluga extends Whale
{
public void fun() {
System.out.println(back());
}
public int back() {
return 2;
}
public String toString() {
return "class Beluga " + super.toString();
}
}
//code in the main of another class
Whale flipper = new Beluga();
flipper = new Whale();
flipper = new Beluga();
flipper = new Whale();
flipper.fun();
System.out.println(flipper);

Question 7
7.

Consider the following code.
public interface Box
{
int mystery(int num);
}
public abstract class Stuff
{
private int x;
public Stuff()
{
x = 5;
}
public Stuff(int newX)
{
x = newX;
}
public abstract void doStuff();
public abstract int doubleIt();
}
public class LotsOfStuff extends Stuff implements Box
{
/* implementation not shown */
}
What methods should the class LotsOfStuff implement?

Question 8
8.

Which methods must be implemented in any class that inherits from the class Stuff?
public abstract class Stuff
{
private int x;
public Stuff()
{
x = 5;
}
public Stuff(int newX)
{
x = newX;
}
public abstract void doStuff();
public abstract int doubleIt();
}

Question 9
9.

What is the number of methods a class extending abstract class Aplus should implement?
public abstract class Aplus
{
public Aplus()
{
}
public abstract String destroy();
public String toString()
{
return "--";
}
}

Question 10
10.

public class X __________________ Y _____________________ A, B, C

Question 11
11.

All variables located inside of an interface are automatically set with what access?

Question 12
12.

Which of the following is the correct way to write the interface Screen?
I.
public interface Screen
{
void updateDisplay()
{
}
int getTime();
}

II.
public interface Screen
{
public Screen()
{
}
void updateDisplay();
int getTime();
}

III.
public interface Screen
{
void updateDisplay();
int getTime();
}

Question 13
13.

Directions :: Extend class Drink to make a Tea class. Tea must have a constructor. Tea must have an instance variable for type of tea. Tea must have an instance variable for the amount left. Tea must have a Boolean method that determines if the Tea is empty. Tea must have a method that removes 1 from amount left when a drink is taken.