APCS Ch. 10 Test Review
18. 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;
a) Coin dime = new Coin(0.1, "dime"); Measurable x = dime;
11. ____ methods must be implemented when using an interface. a) Abstract. b) Private. c) Public. d) Static
a) Abstract.
9. Consider the following code snippet: public class Inventory implements Measurable { . . . public double getMeasure(); { return onHandCount; } } Why is it necessary to declare getMeasure as public ? a) All methods in a class are not public by default. b) All methods in an interface are private by default. c) It is necessary only to allow other classes to use this method. d) It is not necessary to declare this method as public.
a) All methods in a class are not public by default.
4. Which of the following statements about abstract methods is true? a) An abstract method has a name, parameters, and a return type, but no code in the body of the method. b) An abstract method has parameters, a return type, and code in its body, but has no defined name. c) An abstract method has a name, a return type, and code in its body, but has no parameters. d) An abstract method has only a name and a return type, but no parameters or code in its body.
a) An abstract method has a name, parameters, and a return type, but no code in the body of the method.
15. Consider the following code snippet: public interface Sizable { int LARGE_CHANGE = 100; int SMALL_CHANGE = 20; void changeSize(); } Which of the following statements is true? a) LARGE_CHANGE and SMALL_CHANGE are automatically public static final. b) LARGE_CHANGE and SMALL_CHANGE are instance variables c) LARGE_CHANGE and SMALL_CHANGE must be defined with the keywords private static final. d) LARGE_CHANGE and SMALL_CHANGE must be defined with the keywords public static final.
a) LARGE_CHANGE and SMALL_CHANGE are automatically public static final.
33. Consider the following declarations: public interface Measurer { int measure(Object anObject); } public class StringLengthMeasurer implements Measurer { public int measure(_________________) { String str = (String) anObject; return str.length(); } } What parameter declaration can be used to complete the callback measure method? a) Object anObject b) String anObject c) Object aString d) String aString
a) Object anObject
1. Which of the following statements about a Java interface is NOT true? a) A Java interface defines a set of methods that are required. b) A Java interface must contain more than one method. c) A Java interface specifies behavior that a class will implement. d) All methods in a Java interface must be abstract.
b) A Java interface must contain more than one method.
5. Which of the following statements about an interface is true? a) An interface has methods and instance variables. b) An interface has methods but no instance variables. c) An interface has neither methods nor instance variables. d) An interface has both public and private methods.
b) An interface has methods but no instance variables.
34. Assuming that interface Resizable is declared elsewhere, consider the following class declaration: public class InnerClassExample { public static void main(String[] args) { class SizeModifier implements Resizable { // class methods } __________________________ // missing statement } } Which of the following declarations can be used to complete the main method? a) Resizable something = new Resizable(); b) Resizable something = new SizeModifier(); c) Resizable something = new InnerClassExample(); d) SizeModifier something = new Resizable();
b) Resizable something = new SizeModifier();
17. Which of the following is true regarding a class and interface types? a) You can convert from a class type to any interface type that is in the same package as the class. b) You can convert from a class type to any interface type that the class implements. c) You can convert from a class type to any interface type that the class defines. d) You cannot convert from a class type to any interface type.
b) You can convert from a class type to any interface type that the class implements.
10. Which of the following statements about interfaces is NOT true? a) Interfaces can make code more reusable. b) An interface provides no implementation. c) A class can implement only one interface type. d) Interfaces can reduce the coupling between classes.
c) A class can implement only one interface type.
40. Which of the following statements about a mock class is true? a) A mock class does not provide an implementation of the services of the actual class. b) A mock class provides a complete implementation of the services of the actual class. c) A mock class provides a simplified implementation of the services of the actual class. d) A mock class must be an interface.
c) A mock class provides a simplified implementation of the services of the actual class.
32. Which of the following statements about helper classes is true? a) Helper classes must be inner classes. b) Helper classes must implement interfaces. c) Helper classes help reduce coupling. d) Helper classes cannot contain instance variables.
c) Helper classes help reduce coupling.
8. ____ can reduce the coupling between classes. a) Static methods b) Abstract methods c) Interfaces d) Objects
c) Interfaces
27. 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;
c) Player otherPlayer = (Player) otherObject;
25. 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)
c) display()
12. 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 ..."); }
c) void resize();
29. Which of the following statements about a callback is NOT true? a) A callback can allow you to implement a new method for a class that is not under your control. b) A callback can be implemented using an interface. c) A callback is a mechanism for specifying code to be executed later. d) A callback method declared in an interface must specify the class of objects that it will manipulate.
d) A callback method declared in an interface must specify the class of objects that it will manipulate.
3. Which statement about methods in an interface is true? a) All methods in an interface are automatically private. b) All methods in an interface are automatically public. c) All methods in an interface are automatically static. d) All methods in an interface must be explicitly declared as private or public.
d) All methods in an interface must be explicitly declared as private or public.
35. Which of the following statements about an inner class is true? a) An inner class can only be defined within a specific method of its enclosing class. b) An inner class can only be defined outside of any method within its enclosing class. c) An inner class must implement an interface. d) An inner class may be anonymous.
d) An inner class may be anonymous.
6. Which of the following statements about interfaces is NOT true? a) Interfaces can make code more reusable. b) Interface types can be used to define a new reference data type. c) Interface types can be used to express common operations required by a service. d) Interfaces have both private and public methods.
d) Interfaces have both private and public methods.
7. To use an interface, a class header should include which of the following? a) The keyword extends and the name of an abstract method in the interface b) The keyword extends and the name of the interface c) The keyword implements and the name of an abstract method in the interface d) The keyword implements and the name of the interface
d) The keyword implements and the name of the interface
19. Which of the following statements about converting between types is true? a) When you cast number types, you take a risk that an exception will occur. b) When you cast number types, you will not lose information. c) When you cast object types, you take a risk of losing information. d) When you cast object types, you take a risk that an exception will occur.
d) When you cast object types, you take a risk that an exception will occur.
20. Which of the following statements about interfaces is true? a) You can define an interface variable that refers to an object of any class in the same package. b) You cannot define a variable whose type is an interface. c) You can instantiate an object from an interface class. d) You can define an interface variable that refers to an object only if the object belongs to a class that implements the interface.
d) You can define an interface variable that refers to an object only if the object belongs to a class that implements the interface.
26. Which of the following can potentially be changed when implementing an interface? a) The parameters of a method in the interface. b) The name of a method in the interface. c) The return type of a method in the interface. d) You cannot change the name, return type, or parameters of a method in the interface.
d) You cannot change the name, return type, or parameters of a method in the interface.
2. A method that has no implementation is called a/an ____ method. a) interface b) implementation c) overloaded d) abstract
d) abstract
36. A/an ____ class defined in a method signals to the reader of your program that the class is not interesting beyond the scope of the method. a) A class cannot be defined within a method b) abstract c) interface d) inner
d) inner
23. 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
d) polymorphism