Programming II Final Review 5

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following is the correct syntax for starting a Java program named myProg from a command line if the program requires two arguments named arg1and arg2 to be supplied? java myProg(arg1, arg2) java myProg "arg1" "arg2" java myProg arg1 arg2 java myProg arg1, arg2

java myProg arg1 arg2

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 mouseEntered, mouseExited, and mouseClicked Only the mouseExited method Only the mouseEntered method

mouseEntered and mouseExited

Assume that you have declared a queue named myQueue to hold String elements. Which of the following statements will correctly insert an element into myQueue? myQueue.add("apple"); myQueue.push("apple"); myQueue.put("apple"); myQueue.insert("apple");

myQueue.add("apple");

Assume that you have declared a queue named myQueue to hold String elements. Which of the following statements will correctly delete an element from myQueue? myQueue.delete(); myQueue.remove(); myQueue.get(); myQueue.pop();

myQueue.remove();

Assume that you have declared a stack named myStack to hold String elements. Which of the following statements will correctly remove an element from myStack? myStack.delete(); myStack.pop(); myStack.remove(); myStack.get();

myStack.pop();

Which method of an exception object will retrieve a description of the exception that occurred? printStackTrace() printMessage() getDescription() getMessage()

getMessage()

Insert the missing code in the following segment. The code intends to get the newly selected item from the combo box. JComboBox facenameCombo = new JComboBox(); String selectedString = __________________________; (String) facenameCombo.getSelected(); (String) facenameCombo.getSelectedItem(); (String) selectedString.getSelected(); (String) selectedString.getSelectedItem();

(String) facenameCombo.getSelectedItem();

Given four JRadioButton objects in a ButtonGroup, how many radio buttons can be selected at the same time? 1 2 4 3

1

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()); 100 100 -14 100 -14 -14 100 -14

100 100

Which of the following statements about interfaces is NOT true? Interfaces can reduce the coupling between classes. A class can implement only one interface type. An interface cannot have instance variables. Interfaces can make code more reusable.

A class can implement only one interface type.

Which of the following is true about a default method in an interface declaration? A default method is a static method that does not have an implicit parameter. A default method does not provide an implementation. A class that implements the interface cannot override the default behavior. A class that implements the interface can inherit the default behavior.

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

Which of the following statements about stacks is correct? A stack implements random retrieval. A stack implements last-in, first-out retrieval. A stack stores elements in sorted order. A stack implements first-in, first-out retrieval.

A stack implements last-in, first-out retrieval.

Which of the following statements about superclasses and subclasses is true? A superclass is larger than its subclass. A superclass inherits from a subclass. A superclass extends a subclass. A subclass extends a superclass.

A subclass extends a superclass.

Consider the following code snippet: public class Employee { . . . public void setDepartment(String deptName) { . . . } } public class Programmer extends Employee { . . . public void setProjectName(String projName) { . . . } public void setDepartment(String deptName) { . . . } } Which of the following statements is correct? An object of type Programmer can call the setDepartment method of the Employeeclass on itself. An object of type Employee can call the setProjectName method on itself. The Employee class's setDepartment method overrides the Programmer class's setDepartment method. An object of type Employee can call the setDepartment method of the Programmerclass on itself.

An object of type Programmer can call the setDepartment method of the Employeeclass on itself.

You need to access values by an integer position. Which collection type should you use? Queue Map Hashtable ArrayList

ArrayList

What features do GUI builders have to speed the development of the graphical user interface of the program? I setting properties of dialog boxes II automated event-handling code generation III drag and drop of visual components

I, II, and III

Which notation, big-Oh, theta, or omega describes the growth rate of a function? I big-Oh II theta III omega

I, II, and III

The binarySearch method of the Collections class returns a value in the form of -k - 1 when the target item you are searching for was not found in the array. What does k represent? It represents the index at which the target item should be inserted. It represents the number of times the array was accessed by the binarySearchmethod, and can be used for analyzing performance. Nothing - it is an arbitrary value. It is the position of an existing item, and indicates that your target item should come after this existing item.

It represents the index at which the target item should be inserted.

Which of the following statements about the Object.clone method is NOT true? Its return type is the same as the cloned object. It returns a new object that has a state identical to the cloned object. If the object being cloned does not implement the Cloneable interface, then it throws an exception. It does not clone mutable instance variables.

Its return type is the same as the cloned object.

What type does the method getSelectedItem in the JComboBox classreturn? Object String JRadioButton JLabel

Object

Which layout manager uses a grid, but allows selected grid locations to span multiple rows or columns? I GridBagLayout II BorderLayout III GridLayout

Only I

Consider the following code snippet: Scanner in = new Scanner(. . .); in.useDelimiter("[^0-9]+"); What characters will be ignored and not read in using this code? Only numeric characters will be ignored. Only non-alphabetic characters will be ignored. Only alphabetic characters will be ignored. Only non-numeric characters will be ignored.

Only non-numeric characters will be ignored.

Which of the following correctly declares a stack that will hold String elements? Stack s = new Stack<>(); Stack<String> s = new Stack<>(); String s = new Stack(); String s = new Stack<>();

Stack<String> s = new Stack<>();

The code segment below is designed to add the elements in an array of integers. Select the expression needed to complete the code segment so that it calculates the elapsed running time. long start = System.currentTimeMillis(); int sum = 0; for (int k = 0; k < values.length; k++) { sum = sum + values[k]; } long runningTime = ____________________________; System.currentTimeMillis() - start System.currentTimeMillis() + start System.currentTimeMillis() runningTime + start - System.currentTimeMillis()

System.currentTimeMillis() - start

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 code compiles but generates an exception at run time due to the conflict. The code compiles and the implementation is chosen at run time. The class must override the method and provide its own implementation. There is no conflict because interfaces cannot provide method implementation.

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

In Java, if you forget to call a superclass constructor explicitly in the constructor of a subclass, what will happen? The code will not compile. The default constructor (the one with no arguments) of the superclass will be invoked automatically. The subclass will be constructed without invoking a constructor for the superclass. The subclass object will be given a value of null.

The default constructor (the one with no arguments) of the superclass will be invoked automatically.

Consider the following code snippet: public static void main(String[] args) throws IOException Which of the following statements about this code is correct? The main method will not terminate if any exception occurs. The main method is designed to catch and handle all types of exceptions. The main method is designed to catch and handle the IOException. The main method terminates if the IOException occurs.

The main method terminates if the IOException occurs.

Which of the following statements about checked and unchecked exceptions is NOT true? Handling of checked exceptions is enforced by the compiler. Unchecked exceptions are the programmer's fault. Unchecked exceptions belonging to subclasses of the RunTimeException class are beyond the control of the programmer. Internal errors belonging to subclasses of the Error class are beyond the control of the programmer.

Unchecked exceptions belonging to subclasses of the RunTimeException class are beyond the control of the programmer.

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? Extend the library class. Use an inner class in the interface. Create a new class that mimics the library class. Use a helper class that implements the callback method.

Use a helper class that implements the callback method.

Which of the following statements about interfaces is true? You cannot define a variable whose type is an interface. You can define an interface variable that refers to an object of any class in the same package. You can define an interface variable that refers to an object only if the object belongs to a class that implements the interface. You can instantiate an object from an interface class.

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

Consider the scope of the three objects menuLabel, mi, and the anonymous object new MyMenuListener() within the JmenuItem class. How do thier lifetimes compare? public JMenuItem makeMenuItem(final String menuLabel) { JMenuItem mi = new JMenuItem(menuLabel); class MyMenuListener implements ActionListener { public void actionPerformed(ActionEvent e) { doSomethingElse(); System.out.println(menuLabel); } } mi.addActionListener(new MyMenuListener()); return mi; } all have the same lifetimes menuLabel and new MyMenuListener() are the same and both longer than mi mi and new MyMenuListener() are the same and both longer than menuLabel from shortest to longest:menuLabel, new MyMenuListener(), mi

all have the same lifetimes

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

an anonymous object.

Assume that bands is an ArrayList of String objects, which contains a number of elements in ascending order. Select a statement to complete the code segment below, which invokes the Java library binarySearch method to search for the string "Beatles". If the list does not already contain the string, it should be inserted in an appropriate location so that the list remains sorted. int index = Collections.binarySearch(bands, "Beatles"); if (index < 0) { __________________________ } bands.add(-1 * index, "Beatles"); bands.add(-1 - index, "Beatles"); bands.add(-1 * index + 1, "Beatles"); bands.add(index + 1, "Beatles");

bands.add(-1 - index, "Beatles");

A ____ is a combination of a list and a text field. radio button combo box check box scroll bar

combo box

The Comparable interface consists of a single method called ____. comparator comparable compare compareTo

compareTo

Your program will read in an existing text file. You want the program to terminate if the file does not exist. Which of the following indicates the correct code for the main method header? public static void main(String[] args) throws FileNotFoundException public static void main(String[] args) throws UnknownFileException public static void main(String[] args) public static void main(String[] args) throws FileMissingException

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

A binary search requires ____ access. sorted random arbitrary sequential

random

Select an appropriate expression to complete the following method, which is designed to return the number of elements in the parameter array numbers. If a value appears more than once, it should be counted exactly once. public static int countElementsOnce(int[] numbers) { Set<Integer> values = new HashSet<>(); for (int num: numbers) { values.add(num); } ______________________ } return numbers.length - values.size(); return values.size(); return numbers.length; return values.length();

return values.size();

Using the merge method of the Map interface, which statement correctly updates the salesTotalByDept map of type Map<String,Integer> to update the sales total for a dept by an integer sales value? salesTotalByDept.merge(dept, sales,(d,s) -> s + 1); sales.merge(salesTotalByDept, dept, (d,s) -> s + sales); dept.merge(salesTotalByDept, sales, (d,s) -> s + sales); salesTotalByDept.merge(dept, sales, (d,s) -> s + sales);

salesTotalByDept.merge(dept, sales, (d,s) -> s + sales);

Consider the following code snippet: Map<String, Integer> scores; You expect to retrieve elements randomly by key, and want fastest retrieval times. Which of the following statements will create a structure to support this? scores = new HashMap<>; scores = new Map<>; scores = new TreeMap<>; scores = new TreeSet<>;

scores = new HashMap<>;

Consider the following code snippet: Map<String, Integer> scores; If you need to visit the keys in sorted order, which of the following statements will create a structure to support this? scores = new HashTable<>; scores = new HashMap<>; scores = new TreeMap<>; scores = new Map<>;

scores = new TreeMap<>;

A complex GUI can be created with a set of nested panels. What should determine the components that go into a single panel? the type of components the size of the components the adjacency of the components the names of the components

the adjacency of the components

Which String class method will remove spaces from the beginning and the end of a string? clean() strip() truncate() trim()

trim()

Using the merge method of the Map interface, which statement correctly updates the wordCount map of type Map<String,Integer> to count all of the occurrences of a word in a file? wordCount.merge(word, 1, (k,v) -> v + 1); word.merge(wordCount,(k,v) -> v + 1); word.merge(wordCount, 1, (k,v) -> v + 1); wordCount.merge(word,(k,v) -> v + 1);

wordCount.merge(word, 1, (k,v) -> v + 1);

Which function has a faster growth rate: θ(n^1/2) or θ(log(n))? θ(n^1/2) θ(log(n)) They can't be compared. They are the same.

θ(n^1/2)


Conjuntos de estudio relacionados

NCLEX RN-PassPoint Pracetice Exam Case Study Questions

View Set

Real Estate University | S3 | Chapter 3

View Set

hbs blood/heart, hbs 4.3-4.4 blood vessels/heart

View Set

Chapter 4 (Life Policies)- Life Insurance Policies - Provisions, Options and Riders

View Set

WORLD REGIONAL GEOGRAPHY Chapter 8 Subsaharan Africa - Study Questions

View Set