Ch. 10 Quiz

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the following code snippet: public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } } public class ButtonTester { public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); ... } } Which of the following statements is correct?

A ClickListener object has been added as a listener for the action events that buttons generate.

Which of the following is an event source?

A JButton object.

Which of the following statements about a callback is NOT true?

A callback method declared in an interface must specify the class of objects that it will manipulate.

Which of the following statements is correct about inheritance and interfaces?

A class can extend at most one class and can implement multiple interfaces.

Which of the following statements about interfaces is NOT true?

A class can implement only one interface type.

Which of the following is true about a default method in an interface declaration?

A class that implements the interface can inherit the default behavior.

The ____ class in the javax.swing package generates a sequence of events, spaced apart at even time intervals.

Timer

You wish to implement a callback method for an object created from a library class that you cannot change. What is the preferred way to accomplish this?

Use a helper class that implements the callback method.

What is the preferred way to implement event listeners?

Use inner classes as event listeners.

Which of the following can potentially be changed when implementing an interface?

You cannot change the name, return type, or types of parameters of a method in the interface.

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.

inner

Event listeners are often installed as ____ classes so that they can have access to the surrounding fields, methods, and final variables.

inner

Which of the following are not allowed in a Java 8 interface declaration?

instance variables

Consider the following code snippet: public interface Measurable { double getMeasure(); ____________ boolean largerThan(Measurable other) { return getMeasure() > other.getMeasure(); } } Which of the following completes the interface declaration correctly?

default

The method below is designed to return the smaller of two Comparable objects received as arguments. Assume that the objects are instances of the same class. Select the correct expression to complete the method. public static Comparable smaller(Comparable value1, Comparable value2) { if (_________________________ ) return value1; else return value2); }

value1.compareTo(value2) < 0

Suppose you are writing an interface called Resizable, which includes one void method called resize that accepts no parameters. public interface Resizable { _________________________ } Which of the following can be used to complete the interface declaration correctly?

void resize();

Consider the following code snippet: public class MyMouseListener { public void mousePressed(MouseEvent event) { double x; double y; _______ System.out.println("x: " + x + ", y: " + y); } } Which of the following statements should be in the indicated position to print out where the mouse was pressed?

x = event.getX(); y = event.getY();

Using the given definition of the Measurable interface: public interface Measurable { double getMeasure(); } Consider the following code snippet, assuming that BankAccount has a getBalance method and implements the Measurable interface by providing an implementation for the getMeasure method: Measurable m = new BankAccount(); System.out.println(_________________________); Select the correct expression to display the balance of the bank account.

((BankAccount) m).getBalance()

Which of the following correctly completes the Java statement shown to add a listener to a button using a lambda expression? button.addActionListener(_________________________________);

(ActionEvent event) -> System.out.println("Clicked!")

Which of the following correctly defines a functional interface that, given an object, returns the value of a Coin?

(Object obj) -> ((Coin) obj).getValue()

What type of edge does UML use to denote interface implementation?

A dotted arrow from the class to the interface terminated with a triangular tip.

Which of the following statements about a mock class is true?

A mock class provides a simplified implementation of the services of the actual class.

When you use a timer, you need to define a class that implements the ____ interface.

ActionListener

Which of the following is an example of a functional interface, having a single abstract method?

ActionListener

Consider the definition of the Measurable interface and the code snippet defining the Inventory class: public interface Measurable { double getMeasure(); } public class Inventory implements Measurable { . . . public double getMeasure() { return onHandCount; } } Why is it necessary to declare getMeasure as public in the Inventory class?

All methods in a class are not public by default.

Which statement about methods in an interface is true?

All methods in an interface are automatically public.

Which of the following statements about abstract methods is true?

An abstract method has a name, parameters, and a return type, but no code in the body of the method.

Which of the following statements about an inner class is true?

An inner class that is defined inside a method is not publicly accessible.

Which of the following statements about interfaces is NOT true?

An interface can describe the state that should be maintained.

Which of the following statements about an interface is true?

An interface has methods but no instance variables.

What role does an interface play when using a mock class?

An interface should be implemented by both the real class and the mock class to guarantee that the mock class accurately simulates the real class when used in a program.

Which of the following is true about interface types and abstract classes?

An interface type cannot have instance variables whereas an abstract class can.

Consider the following code snippet: BankAccount account = new BankAccount(500); Which of the following statements correctly clones the account?

BankAccount clonedAccount = (BankAccount) account.clone();

Consider the following code snippet. public interface Measurable { double getMeasure(); } public class Coin implements Measurable { public Coin(double aValue, String aName) { ... } public double getMeasure() { return value; } ... } public class BankAccount implements Measurable { public BankAccount(double initBalance) { ... } public void getMeasure() { return balance; } ... } Which of the following statements is correct?

Coin dime = new Coin(0.1, "dime"); Measurable m = dime;

____ are generated when the user presses a key, clicks a button, or selects a menu item.

Events

Which of the following statements about the Object.clone method is NOT true?

Its return type is the same as the cloned object.

Which of the following code statements creates a graphical button that has "Calculate" as its label ?

JButton button = new JButton("Calculate");

To process keyboard events, you need to define a class that implements the ____ interface.

KeyListener

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?

LARGE_CHANGE and SMALL_CHANGE are automatically public static final.

Which of the following statements about lambda expressions is NOT true?

Lambda expressions cannot contain a method body enclosed in braces with a return statement.

How do you specify what the program should do when the user clicks a button?

Specify the actions to take in a class that implements the ActionListener interface.

Assume that the Measurable interface is defined with a static sum method that computes the sum of the Measurable objects passed in as an array, and that BankAccount implements the Measurable interface. Also assume that there is a variable branchAccounts that is an object reference to a populated array of BankAccount objects for a bank branch. Which of the following represents a correct invocation of the sum method to find the total balance of all accounts at the branch?

Measurable.sum(branchAccounts)

Listeners are typically implemented as inner classes. Which of the following statements is NOT true about inner class access to variables from the surrounding class?

Methods of an inner class cannot access variables from the surrounding class.

To process mouse events, you need to define a class that implements the ____ interface.

MouseListener

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?

Object anObject

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?

Player otherPlayer = (Player) otherObject;

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?

Resizable something = new SizeModifier();

If you have multiple classes in your program that have implemented the same interface in different ways, how is the correct method executed?

The Java virtual machine must locate the correct method by looking at the class of the actual object.

What does the MouseAdapter class provide?

The MouseAdapter class implements all of the methods of the MouseListener interface as do-nothing methods, eliminating the need to provide an implementation for all 5 methods of the MouseListener interface.

Consider the following code snippet: class MouseClickedListener implements ActionListener { public void mouseClicked(MouseEvent event) { int x = event.getX(); int y = event.getY(); component.moveTo(x,y); } } What is wrong with this code?

The class has implemented the wrong interface.

Assume a class implements two interfaces, both of which define a default method with the same signature. Which statement is true about this conflict of inherited methods?

The class must override the method and provide its own implementation.

Using the given definition of the Measurable interface: public interface Measurable { double getMeasure(); } Consider the following code snippet, assuming that BankAccount has a getBalance method and implements the Measurable interface by providing an implementation for the getMeasure method: Measurable m = new BankAccount(); System.out.println(m.getBalance()); Which of the following statements is true?

The code does not compile because a variable of type Measurable does not have a getBalance method.

Using the given definition of the Measurable interface: public interface Measurable { double getMeasure(); } Consider the following code snippet, assuming that BankAccount has a getBalance method and implements the Measurable interface by providing an implementation for the getMeasure method: Measurable m = new Measurable(); System.out.println(m.getMeasure()); Which of the following statements is true?

The code does not compile because interface types cannot be instantiated.

Consider the definition of the Measurable interface and the code snippet defining the Inventory class: public interface Measurable { double getMeasure(); } public class Inventory implements Measurable { . . . double getMeasure() { return onHandCount; } } What is wrong with this code?

The getMeasure() method must be declared as public.

To use an interface, a class header should include which of the following?

The keyword implements and the name of the interface

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?

The listener has not been attached to the Timer object.

Which of the following statements about an inner class is true?

The methods of an inner class can access variables declared in the enclosing scope.

Assume a class extends another class and implements an interface, both of which define the same method. Which statement is true about this conflict of inherited methods?

The subclass inherits the superclass method and ignores the default method from the interface.

Assume that the TimerListener class implements the ActionListener interface. If the actionPerformed method in TimerListener needs to be executed once per second, what statement should be used to complete the following code segment? ActionListener listener = new TimerListener(); _________________________________ // missing statement timer.start();

Timer timer = new Timer(1000, listener);

Which of the following is true regarding a class and interface types?

You can convert from a class type to any interface type that the class implements.

Which of the following statements about interfaces is true?

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

You have a class that extends the JComponent class. In this class you have created a painted graphical object. Your code will change the data in the graphical object. What additional code is needed to ensure that the graphical object will be updated with the changed data?

You must call the component object's repaint() method.

Which of the following statements about events and graphical user interface programs is true?

Your program must instruct the Java window manager to send it notifications about specific types of events to which the program wishes to respond.

A class that implements an interface must provide an implementation for all ____ methods.

abstract

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

abstract

To respond to a button event, a listener must supply instructions for the ____ method of the ActionListener interface.

actionPerformed

To associate an event listener with a JButton component, you must use the ___ method of the JButton class.

addActionListener

Use the ____ method to add a mouse listener to a component.

addMouseListener

Consider the following code snippet: myImage.add(new Rectangle(10,10,10,10)); This code is an example of using ____.

an anonymous object.

Suppose listener is an instance of a class that implements the MouseListener interface. How many methods does listener have?

at least 5

Which of the following correctly defines a ButtonListener as an inner class and creates an instance of ButtonListener as a listener for the button?

class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("Button clicked"); } } button.addActionListener(new ButtonListener());

Which of the following is the correct class header for a MouseClickListener class that wants to take advantage of the do-nothing methods provided by the MouseAdapter class?

class MouseClickListener extends MouseAdapter

Which container is used to group multiple user-interface components together?

panel

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._________________;

display()

An ____ belongs to a class whose methods describe the actions to be taken when a user clicks a user-interface graphical object.

event listener

The methods of an ____ describe the actions to be taken when an event occurs.

event listener

When an event occurs, the event source notifies all ____.

event listeners

In UML, a dotted arrow with a triangular tip denotes ____________________.

interface implementation

Consider the following code snippet: import ____________________ import java.awt.event.ActionListener; /** An action listener that prints. */ public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } } Which of the following statements will complete this code?

java.awt.event.ActionEvent;

If the user wants to process keystrokes, which methods of the KeyListener interface must be implemented?

keyPressed, keyReleased, and keyTyped

You wish to detect when the mouse is moved into a graphical component. Which methods of the MouseListener interface will provide this information?

mouseEntered

If the user wants to paint a user-interface component in a special way whenever the mouse is pointing inside it, which methods of the MouseListener interface are useful?

mouseEntered and mouseExited

If the user presses and releases a mouse button in quick succession without moving the mouse, which methods of the MouseListener interface are called?

mousePressed, mouseReleased, and mouseClicked.

To build a user interface that contains graphical components, the components ____.

must be added to a panel that is contained within a frame.

Assuming that the ClickListener class implements the ActionListener interface, what statement should be used to complete the following code segment? ClickListener listener = new ClickListener(); JButton myButton = new JButton("Submit"); JPanel myPanel = new JPanel(); myPanel.add(myButton); ______________________ //missing statement

myButton.addActionListener(listener);

Using the following definitions of the Measurable and Named interfaces. public interface Measurable { double getMeasure(); } public interface Named { double getName(); } Assume BankAccount provides the code for the getMeasure() and getName() methods. Which of the following could correctly represent the class header for BankAccount?

public class BankAccount implements Measurable, Named

Consider the following class: public class ClickListener implements ActionListener { __________________________________________ { System.out.println("button event ..."); } } Which of the following method headers should be used to complete the ClickListener class?

public void actionPerformed(ActionEvent event)

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?

public void encrypt(String aKey)

The ____ method should be called whenever you modify the shapes that the paintComponent method draws.

repaint

Consider the code snippet below: public class RectangleComponent extends JComponent { private Rectangle box; private static final int BOX_X = 100; private static final int BOX_Y = 100; private static final int BOX_WIDTH = 20; private static final int BOX_HEIGHT = 30; public RectangleComponent() { // The rectangle that the paint method draws box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.draw(box); } public void moveTo(int x, int y) { box.setLocation(x, y); repaint(); } } Which statement causes the rectangle to appear at an updated location?

repaint();

Consider the following class: public class BowlingGame implements Comparable { private int score; // other methods go here public int compareTo(Object otherObject) { BowlingGame otherGame = (BowlingGame) otherObject; __________________________________; } } What statement can be used to complete the compareTo() method?

return (score - otherGame.score)

Consider the following class: public class Stock implements Comparable { private String name; private double price; // other methods go here public int compareTo(Object otherObject) { Stock otherStock = (Stock) otherObject; __________________________________; } } Which is the best statement to use to complete the compareTo() method?

return Double.compare(price, otherStock.price)

Consider the following class: public class Temperature implements Comparable { private int value; // other methods go here public int compareTo(Object otherObject) { Temperature otherTemp = (Temperature) otherObject; __________________________________; } } Which is the best statement to use to complete the compareTo() method?

return Integer.compare(value, otherTemp.value)

Consider the following code snippet that uses the parameterized Comparable interface. public class BowlingGame implements Comparable<BowlingGame> { private int score; ... public int compareTo(BowlingGame other) { _______________________________ } } Which of the following completes the compareTo implementation?

return score - other.score;

Consider the following code snippet: public interface Measurable { double getMeasure(); ____________ double sum(Measurable[] objects) { // implementation to compute the sum of the Measurable objects } } Which of the following completes the interface declaration correctly?

static


Kaugnay na mga set ng pag-aaral

Fundamentals HESI Prep-Ch 25-Patient Education and the Nursing Process

View Set

Clinical Anatomy FINAL: UE focus

View Set

Unidad 6: El porvenir (The Future)

View Set

MED SURG 1: EXAM ONE EAQ QUESTIONS

View Set

PrepU- Pharm- Ch 2: Basic Concepts and Processes

View Set