Java II Mid-Term – Solutions can be found on Github

1. Consider the hierarchy of classes shown below.

Java II

Mid Term

1. Consider the hierarchy of classes shown below.

word image

Which represent valid class headers that would be found in this hierarchy?

a) public class ScriptedShow extends TelevisionShow {. . .
public class Comedy extends ScriptedShow {. . .

b) public class TelevisionShow extends ScriptedShow {. . .
public class ScriptedShow extends Comedy {. . .

c) public class Drama extends TelevisionShow {. . .
public class Comedy extends Drama {. . .

d) public class ScriptedShow extends RealityShow {. . .
public class RealityShow extends ScriptedShow {. . .

2. Consider the classes shown below:


public class Parent

{

public void doSomething(){/* Implementation not shown */}

}

public class Child extends Parent

{

public void doAnotherThing(){/* Implementation not shown */}

}

Which lines in the following code will compile without error?

Child kid = new Child();

kid.doSomething(); // line 1

kid.doAnotherThing(); // line 2

a) Line 1 only

b) Line 2 only

c) Lines 1 and 2

d) Neither line will compile without error

3. Consider the classes shown below:


public class Parent

{

private int value = 100;

public int getValue()

{

return value;

}

}

public class Child extends Parent

{

private int value;

public Child(int number)

{

value = number;

}

}

What is the output of the following lines of code?

Child kid = new Child(-14);

Parent adult = new Parent();

System.out.println(kid.getValue() + ” “

+ adult.getValue());

a) 100 100

b) -14 100

c) -14 -14

d) 100 -14

4. Suppose the class Value is partially defined below

public class Value

{

private int number;

public int getValue()

{

return number;

}

}

Java II Mid-Term – Consider the hierarchy of classes shown below.

A subclass of Value, LargerValue, is defined with a getValue method that returns twice the value of the parent. Which line is the body of LargerValue’s getValue method?

a) return getValue() * 2;

b) return super.getValue() * 2;

c) return number * 2;

d) return super.number * 2;

5. You are creating a Motorcycle class which is supposed to inherit from the Vehicle class. Which of the following class declaration statements will accomplish this?

a) public class Motorcycle inherits Vehicle

b) public class Motorcycle implements Vehicle

c) public class Motorcycle interfaces Vehicle

d) public class Motorcycle extends Vehicle

6. Consider the following class hierarchy:

public class Vehicle

{

private String type;

public Vehicle(String type)

{

this.type = type;

}

public String displayInfo()

{

return type;

}

}

public class LandVehicle extends Vehicle

{

public LandVehicle(String type)

{

super(type);

}

}

public class Auto extends LandVehicle

{

public Auto(String type)

{

super(type);

}

}

You have written a program to use these classes, as shown in the following code snippet:

public class VehicleTester

{

public static void main(String[] args)

{

Auto myAuto = new Auto(“sedan”);

System.out.println(“MyAuto type = ” + ______);

}

}

Complete the code in this program snippet to correctly display the auto’s type.

a) myAuto.displayInfo()

b) myAuto.super.displayInfo()

c) myAuto.super.super.displayInfo()

d) This cannot be done unless the Auto class overrides the displayInfo method.

7. Consider the following class hierarchy:

public class Vehicle

{

private String type;

public Vehicle(String type)

{

this.type = type;

}

public String displayInfo()

{

return type;

}

}

public class LandVehicle extends Vehicle

{

public LandVehicle(String type)

{

. . .

}

}

public class Auto extends LandVehicle

{

public Auto(String type)

{

. . .

}

public String displayAutoType()

{

return _____;

}

}

Complete the code in the Auto class method named displayAutoType to return the type data.

a) super(type);

b) super.type;

c) super.super.type;

d) super.displayInfo()

8. Consider the following code snippet:

Employee programmer = new Employee(10254, “exempt”);

String s = programmer.toString();

Assume that the Employee class has not implemented its own toString() method. What value will s contain when this code is executed?

a) s will contain the values of the instance variables in programmer.

b) s will contain only the class name of the programmer object.

c) s will contain the class name of the programmer object followed by a hash code.

d) This code will not compile.

9. Consider the following code snippet:

class MyListener implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

System.out.println(event);

}

}

Timer t = new Timer(interval, listener);

t.start();

What is wrong with this code?

a) The Timer object should be declared before the MyListener class.

b) The listener has not been attached to the Timer object.

c) The Timer object must be declared as final.

d) There is nothing wrong with the code.

10. Suppose you are writing an interface called Resizable, which includes one void method called resize.

public interface Resizable

{

_________________________

}

Which of the following can be used to complete the interface declaration correctly?

a) private void resize();

b) protected void resize();

c) void resize();

d) public void resize() { System.out.println(“resizing …”); }

11. Consider the following declarations:

public interface Encryptable

{

void encrypt(String key);

}

public class SecretText implements Encryptable

{

private String text;

_____________________________

{

// code to encrypt the text using encryption key goes here

}

}

Which of the following method headers should be used to complete the SecretText class?

a) public void encrypt()

b) public void encrypt(String aKey)

c) public String encrypt(String aKey)

d) void encrypt(String aKey)

12. Consider the following code snippet.

public interface Measurable

{

double getMeasure();

}

public class Coin implements Measurable

{

public double getMeasure()

{

return value;

}

}

public class DataSet

{

public void add()

{

}

}

public class BankAccount

{

public void add()

{

}

}

Which of the following statements is correct?

a)

Coin dime = new Coin(0.1, “dime”);

Measurable x = dime;

b)

Coin dime = new Coin(0.1, “dime”);

Dataset x = dime;

c)

Coin dime = new Coin(0.1, “dime”);

DataSet x == (Measureable)dime;

d)

Coin dime = new Coin(0.1, “dime”);

BankAccount x = dime;

13. Consider the following code snippet:

public class Demo

{

public static void main(String[] args)

{

Point[] p = new Point[4];

p[0] = new Colored3DPoint(4, 4, 4, Color.BLACK);

p[1] = new ThreeDimensionalPoint(2, 2, 2);

p[2] = new ColoredPoint(3, 3, Color.RED);

p[3] = new Point(4, 4);

for (int i = 0; i < p.length; i++)

{

String s = p[i].toString();

System.out.println(“p[” + i + “] : ” + s);

}

return;

}

}

This code is an example of ____.

a) overloading

b) callback

c) early binding

d) polymorphism

14. Consider the following declarations:

public interface Displayable

{

void display();

}

public class Picture implements Displayable

{

private int size;

public void increaseSize()

{

size++;

}

 

public void decreaseSize()

{

size–;

}

 

public void display()

{

System.out.println(size);

}

public void display(int value)

{

System.out.println(value * size);

}

}

What method invocation can be used to complete the code segment below?

Displayable picture = new Picture();

picture._________________;

a) increaseSize()

b) decreaseSize()

c) display()

d) display(5)

15. Consider the following class:

public class Player implements Comparable

{

private String name;

private int goalsScored;

 

// other methods go here

public int compareTo(Object otherObject)

{

__________________________________

return (goalsScored – otherPlayer.goalsScored);

}

}

What statement can be used to complete the compareTo() method?

a) Player otherPlayer = otherObject;

b) Object otherPlayer = otherObject;

c) Player otherPlayer = (Player) otherObject;

d) Object otherPlayer = (Player) otherObject;

16. The method below is designed to print the smaller of two values received as arguments. Select the correct expression to complete the method.

public void showSmaller(Comparable value1, Comparable value2)

{

if ( _________________________ )

System.out.println(value1 + ” is smaller.”);

else

System.out.println(value2 + ” is smaller.”);

}

a) value1 < value2

b) value1.compareTo(value2) > 0

c) value1.compareTo(value2) == 0

d) value1.compareTo(value2) < 0

17. Which of the following is the correct syntax for creating a File object?

a) File inFile = File(“input.txt”)

b) File inFile = new File(“input.txt”)

c) File inFile = File.open(“input.txt”)

d) File inFile = new File.open(“input.txt”)

18. Insert the missing code in the following code fragment. This code is intended to open a file and handle the situation where the file cannot be found.

try

{

String filename = . . .;

Scanner in = new Scanner(new File(filename));

. . .

}

___________________

{

exception.printStackTrace();

}

a) catch (IOException exception)

b) catch (new exception (IOException))

c) catch (IllegalArgumentException exception)

d) catch (IOException)

19. Consider the following code snippet:

try

{

File inputFile = new File(filename);

Scanner in = new Scanner(inputFile);

. . .

}

catch (Exception e)

{

}

Which of the following statements about this code is correct?

a) This code will not catch a FileNotFoundException that occurs in the try block.

b) This code will pass any exceptions back to its caller.

c) This code will catch exceptions that occur in the try block but will do nothing about the exceptions.

d) This code will not catch any exceptions that occur in the try block.

20. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt and write to an output file named dataOut.txt.

public static void main(String[] args) throws FileNotFoundException

{

String inputFileName = “dataIn.txt”;

String outputFileName = “dataOut.txt”;

File inputFile = new File(inputFileName);

Scanner in = _________;

. . .

}

a) new File(inputFileName)

b) new File(“dataIn.txt”)

c) new Scanner(inputFile)

d) new Scanner(“dataIn.txt”)

 

 

Subscribe For Latest Updates
Let us notify you each time there is a new assignment, book recommendation, assignment resource, or free essay and updates