IB Computer Science Chapter 10

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

The String class implements the Comparable interface. True False

True

You can define an interface variable that refers to an object if the object belongs to a class that implements the interface. True False

True

You can use the Arrays.sort method to sort an array of String objects. True False

True

A method that has no implementation is called a/an method.

abstract

Assume the interface Measurable has a method getMeasure(). What is wrong with this code? Measurable meas = new Measurable(); System.out.println(meas.getMeasure()); a Nothing is wrong. b The constructor for meas is missing some parameters. c You cannot construct an object of type Measurable.

You cannot construct an object of type Measurable.

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.

To use an interface, a class header should include the keyword _ followed by the name of the interface.

implements

Consider the following code snippet: Vehicle aVehicle = new Auto(); aVehicle.moveForward(200); If the Auto class inherits from the Vehicle class, and both classes have an implementation of the move Forward method, which statement is correct A) the moveForward method of the Auto class be excecuted. B) the moveForward method of the Vehicle class will be executed C) You must specify in the code which class's moveForward method is to be used D) It is not possible to determine which class's method is called.

A

If a class has an abstract method, which of the following statements is NOT true? A) You can construct an object from this class B) You can declare a variable whose type is this class C) you can inherit from this class D) All non-abstract subclasses of this class must implement this method

A

Which statement about methods in an interface is true? a All methods in an interface are automatically public. b All methods in an interface are automatically private. c All methods in an interface are automatically static. d All methods in an interface must be explicitly declared as private or public.

All methods in an interface are automatically public.

Which of the following is true regarding subclasses? A) A subclass object may be converted to a superclass object. B) a subclass object cannot be converted to a superclass object. C) a superclass object cannot be converted to a subclass object. D) Superclass and subclass objects automatically convert to each other when needed.

A

What is output by this code fragment? String s1 = "Hippo"; String s2 = "giraffe"; System.out.println(s1.compareTo(s2)); a A positive integer b A negative integer c Zero d Nothing, there's a runtime error.

A negative integer

What is output by this code fragment? String s1 = "hippo"; String s2 = "giraffe"; System.out.println(s1.compareTo(s2)); a. A negative integer. b. Zero c. Nothing, there is a compile time error. d. A positive integer.

A positive integer.

What is output by this code fragment? String s1 = "hippo"; String s2 = "giraffe"; System.out.println(s1.compareTo(s2)); a. A positive integer. b. Zero c. A negative integer. d. Nothing, there is a compile time error.

A positive integer.

Which of the following statements about an interface is true? a An interface has neither methods nor instance variables. b An interface has methods but no instance variables. c An interface has both public and private methods. d An interface has methods and instance variables.

An interface has methods but no instance variables.

In java, a class that is defined without an explicit "extends" clause ______. A) has no superclass B) is a subclass of Object C) is its own superclass D) is a syntax error

B

When designing a hierarchy of classes, features and behaviours that are common to all classes are place in____. A) ever class in the hierarchy B) the superclass C) a single subclass D) all subclasses

B

Which of the following statements about classes is NOT true? A) Every class extends the Object class either directly or indirectly B) Only classes with an explicit extends clause extend the Object class C) The Object class is the direct or indirect superclass of every class in Java D) The Object class is the direct superclass of every class that does not have an explicit extends clause

B

What must a subclass do to modify a private superclass instance variable? A) The subclass must simply use the name of the superclass instance variable B) the subclass must declare its own instance variable with the same name as the superclass instance variables C) The subclass must use a public method of the superclass to update the superclass's private instance variable D) the subclass must have its own public method to update the superclass's private instance variable

C

Which of the following is true regarding subclasses? A) A subclass inherits methods from its superclass but not instance variables B) A subclass inherits instance variables from its superclass but not methods C) A subclass inherits methods and instance variables from its superclass D) A subclass does not inherit methods or instance variables from its superclass

C

Consider the following code snippet: public void deposit(double amount) { transactionCount++; super.deposit(amount); } Which of the following statements is true? A) this method will class itself B) this method calls a public method in its superclass C) this methods calls a private method in its superclass D) This method calls a public method in its superclass

D

If a subclass contains a method with the same name as a method in its superclass, but with different parameters types, the subclass method is said to ____ the method of the superclass. A) implement B) inherit C) override D) overload

D

To create a subclass, use the _____ keyword A) inherits B) implements C) interface D) extends

D

Which of the following is true regarding subclasses? A) a subclass that inherits methods from its superclass may not override the methods B) a subclass that inherits instance variables from its superclass may not declare additional instance variables C) a subclass may inherit methods or instance variables from its superclass but not both D) a subclass may inherit methods from instance variables from its superclass, and may also implement its own methods and declare its own instance variables

D

Consider the following code snippet: public class Inventory implements Measurable { . . . public double getMeasure(); { return onHandCount; } } Question True/False: It is not necessary to declare the method as public. True False

False

IfT If two classes A and B implement interface C then a class A reference can be converted to a class B reference. True False

False

You can instantiate an object from an interface class. True False

False

You can use the Arrays.sort method to sort an array of Rectangle objects. True False

False

Suppose the class Sandwich implements the Edible interface, and you are given the variable declarations: Sandwich sub = new Sandwich(); Rectangle cerealBox = new Rectangle(5, 10, 20, 30); Edible e = null; Which of the following are legal? I. e = sub; II. sub = e; III. sub = (Sandwich) e; IV. sub = (Sandwich) cerealBox; a. I only b. I and III c. None are legal d. II and IV

I and III

Consider the following declarations: public interface Encryptable { void encrypt(String key); } public class SecretText implements Encryptable { private String text; _____________________________ //What goes here? { // code to encrypt the text using encryption key goes here } } Which of the following method headers could be used to complete the SecretText class? I. public void encrypt(String aKey) II. private void encrypt(String s) III. void encrypt(String key) IV. public void encrypt(String key) a All of them b I, III, or IV c I or IV d IV only

I or IV

Which of the following statements about interfaces is NOT true? I. Interfaces can make code more reusable. II. An interface provides no implementation. III. A class can implement only one interface type. IV. Interfaces can reduce the coupling between classes. a III & IV only b III only c I only d None, all statements are true.

III only

What does this code fragment output if Person and Quiz both implement the Measurable interface and getAverage() is a method implemented in a class called DataUtil that averages the Measurable objects in an array? Measurable[] data = {new Person("Joan", 100), new Quiz("A-",90)); System.out.println(getAverage(data)); a Nothing, the code compiles, but there's a runtime error. b It outputs 100 90. c It outputs 95. d Nothing, the code doesn't compile.

It outputs 95.

Consider the following code segment : public static Comparable max( Comparable a, Comparable b){ if (a.compareTo(b) > 0) { return a; } else { return b; } } What does the code segment do? a It returns the smaller value of two comparable objects b It returns a reference to the smaller comparable object c It returns a reference to the larger comparable object d It returns the larger value of two comparable objects

It returns a reference to the larger comparable object

If two classes A and B implement interface C, then you can create an array of type C that holds objects of both types A and B. True False

True

8. ____ can reduce the coupling between classes. a) Static methods b) Abstract methods c) Interfaces d) Objects

c) Interfaces

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.

2. A method that has no implementation is called a/an ____ method. a) interface b) implementation c) overloaded d) abstract

d) abstract

Assume the interface Measurable has a method getMeasure(). Also, the Country class implements Measurable and implements the getName() method. What is wrong with this code? Measurable meas = new Country("Japan", 1234567); System.out.println(meas.getName()); a meas is type Measurable and there is no getName() method in that class. b Nothing is wrong.

meas is type Measurable and there is no getName() method in that class.

Assume the interface Measurable has a method getMeasure(). Also, the Country class implements Measurable and implements the getName() method. What is wrong with this code? Measurable meas = new Country("Japan", 1234567); System.out.println(meas.getName()); a. meas is type Measurable and there is no getName() method in that class. b. Nothing is wrong.

meas is type Measurable and there is no getName() method in that class.

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.compareTo(value2) > 0 b value1.compareTo(value2) < 0 c value1.compareTo(value2) == 0 d value1 < value2

value1.compareTo(value2) < 0

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 Object otherPlayer = otherObject; b Player otherPlayer = otherObject; c Player otherPlayer = (Player) otherObject; d

Player otherPlayer = (Player) otherObject;

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

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.

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(&quot;resizing ...&quot;); }

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

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.

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 &lt; p.length; i++) { String s = p[i].toString(); System.out.println(&quot;p[&quot; + i + &quot;] : &quot; + s); } return; } } This code is an example of ____. a) overloading b) callback c) early binding d) polymorphism

d) polymorphism

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 display() b decreaseSize() c display(5) d increaseSize()

display()

Consider the following interface and class: public interface Student { double getGPA(); int getSemesterUnits(); } public class FullTimeStudent implements Student, Comparable { //required code goes here... } What is the minimum set of methods that must be in the FullTimeStudent class in order for it to successfully compile? a getGPA, getSemesterUnits, toString b getGPA, getSemesterUnits, getAverage c getGPA, getSemesterUnits d getGPA, getSemesterUnits, compareTo

getGPA, getSemesterUnits, compareTo


Ensembles d'études connexes

Test bank - Human development (Chap 2)

View Set

F1:Amigos de todas partes - las frases verbales para resumir el cuento

View Set

Serosa (Parietal and Visceral) (BIO 168 Ch 1)

View Set

Criminology and Justice - checkpoint 02

View Set

Financial Management of the Firm

View Set

Information Systems Security - C845

View Set

AP Chemistry Semester Test Study Guide

View Set

Chapter 35: Hypothalamic and Pituitary Agents

View Set