CSE 205 - Final Multiple Choice Questions

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

Exam 2 - Complete the code for the myFactorial recursive method shown below, which is intended to compute the factorial of the value passed to the method: return (anInteger * (myFactorial(anInteger - 1) ));

a) return (anInteger * (myFactorial(anInteger - 1) ));

Exam 3 - Assume you are using a doubly-linked list data structure with many nodes. What is the minimum number of node references that are required to be modified to remove a node from the middle of the list? Consider the neighboring nodes. a) 3 b) 2 c) 4 d) 1

b) 2

Exam 1 - 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 instance variables but no methods.

b) An interface has methods but no instance variables.

Exam 2 - Which component can be added to a menu? I MenuBar II Menu III MenuItem a) I b) I and II c) II and III d) I, II and III

b) I and II c) II and III

Exam 1 - To override a superclass method in a subclass, the subclass method ____. a) Must use a different method name. b) Must use the same method name and the same parameter types. c) Must use a different method name and the same parameter types. d) Must use a different method name and different parameter types.

b) Must use the same method name and the same parameter types.

Exam 1 - Consider the following code snippet of a function object public interface Measurer { double measure(______ anObject); } Complete this code to allow the interface to handle all classes? a) Class b) Object c) Any d) Void

b) Object

Exam 1 - When declared as protected, data in an object can be accessed by ____. a) Only by that class's methods and by all of its subclasses b) Only by that class's methods, by all of its subclasses, and by methods in classes within the same package. c) Only by that class's methods. d) By any class.

b) Only by that class's methods, by all of its subclasses, and by methods in classes within the same package.

Exam 1 - Consider the following code snippet: public interface Sizable { int LARGE_CHANGE = 100; void changeSize(); } Which of the following indicates how to use the constant LARGE_CHANGE in your program? a) LARGE_CHANGE b) Sizable.LARGE_CHANGE c) Sizable(LARGE_CHANGE) d) You cannot directly use the LARGE_CHANGE constant in your program.

b) Sizable.LARGE_CHANGE

Exam 1 - Which of the following statements about comparing objects is correct? a) The equals method is used to compare whether two references are to the same object. b) The equals method is used to compare whether two objects have the same contents. c) The == operator is used to compare whether two objects have the same contents. d) The equals method and the == operator are perform the same actions.

b) The equals method is used to compare whether two objects have the same contents.

Exam 2 - Consider the following code snippet: Pane pane = new Pane(); Label label = new Label("Show the answer"); TextField field = new TextField(); pane.add(label); pane.add(field); Which of the following statements is true? a) This code will correctly show all nodes. b) The label and field should be added to the pane using pane.getChildren().add() method. c) The pane should be created after making the label and field. d) The pane must be initialized with nodes without adding later.

b) The label and field should be added to the pane using pane.getChildren().add() method.

Exam 2 - Consider the following cide snippet: class MouseClickHandler implements EventHandler<MouseEvent> { public void handle (MouseEvent event) { if(event.getEventType() == MouseEvent.MOUSE_PRESSED) ( ) if(event.getEventType() == MouseEvent.MOUSE_ENTERED) ( ) if(event.getEventType() == MouseEvent.MOUSE_EXITED) ( ) int x = event.getX(); int y = event.getY(); shape.setCenterX(x); shape.setCenterY(y); } } What is wrong with this code? a) The class cannot have empty blocks for the mouse pressed, mouse entered, and mouse exited cases. b) The shape is moved always during mouse moved. c) The class has implemented the wrong interface. d) The handle method cannot access the x and y coordinates of the mouse.

b) The shape is moved always during mouse moved.

Exam 3 - Consider the sort method shown below for selection sort: public static void sort(int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } Suppose we modify the loop condition to read i < a.length. What would be the result? a) The sort would work exactly the same as before the code modification. b) The sort would work, but run one more iteration. c) The sort would work but with one less iteration. d) An exception would occur.

b) The sort would work, but run one more iteration.

Exam 3 - Consider the minimumPosition method from the SelectionSorter class. Complete the code to write a maximumPosition method that returns the index of the largest element in the range from index from to the end of the array. private static int minimumPosition(int[] a, int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) { if (a[i] < a[minPos]) { minPos = i; } } return minPos; } private static int maximumPosition(int[] a, int from) { int maxPos = from; for (int i = from + 1; i < a.length; i++) { ________________ } return maxPos; } a) if(a[i] == a[maxPos]) { maxPos = i; } b) if(a[i] <= a[maxPos]) { maxPos = i; } c) if(a[i] > a[maxPos]) { maxPos = i; } d) if(a[i] < a[maxPos]) { maxPos = i; }

c) if(a[i] > a[maxPos]) { maxPos = i; }

Exam 1 - Consider the following inheritance hierarchy diagram: Vehicle Air Vehicle LandVehicle WaterVehic Auto Which of the following statements is correct? a) Auto class inherits from LandVehicle class, and LandVehicle class inherits from Vehicle class. b) Auto class inherits from LandVehicle class, and Vehicle class inherits from LandVehicle class. c) LandVehicle class inherits from Auto class, and LandVehicle class inherits from Vehicle class. d) LandVehicle class inherits from Auto class, and Vehicle class inherits from LandVehicle class.

a) Auto class inherits from LandVehicle class, and LandVehicle class inherits from Vehicle class.

Exam 1 - Consider the following code snippet: public class Motorcycle extends Vehicle { . . . public Motorcycle(int numberAxles) { super(numberAxles); } } What does this code do? a) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class. b) It invokes the constructor of the Motorcycle class from within the constructor of the Vehicle class. c) It invokes a private method of the Vehicle class from within a method of the Motorcycle class. d) This code will not compile.

a) It invokes the constructor of the Vehicle class from within the constructor of the Motorcycle class.

Exam 1 - 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 moveForward method with the same set of parameters and the same return type, which statement is correct? a) The moveForward method of the Auto class will be executed. 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) The moveForward method of the Auto class will be executed.

Exam 3 - Assume we are using quicksort to sort an array in ascending order. What can we conclude about the elements to the left of the currently placed pivot element? a) They are all less than or equal to the pivot element. b) They are all greater than or equal to the pivot element. c) They are all sorted. d) None can equal the pivot element.

a) They are all less than or equal to the pivot element.

Exam 1 - Which of the following statements about classes is true? a) You can create an object from a concrete class, but not from an abstract class. b) You can create an object from an abstract class, but not from a concrete class. c) You cannot have an object reference whose type is an abstract class. d) You cannot create subclasses from abstract classes.

a) You can create an object from a concrete class, but not from an abstract class.

Exam 3 - Consider the following code snippet: Queue<String> stringQueue = new LinkedList<>(); stringQueue.add("ab"); stringQueue.add("abc"); stringQueue.add("a"); while (stringQueue.size() > 0) { System.out.print(stringQueue.remove() + ","); } What output will be produced when this code is executed? a) ab,abc,a, b) abc,ab,a, c) a,abc,ab, d) a,ab,abc,

a) ab,abc,a,

Exam 2 - Which of the following is an event source? a) A Button object. b) An event handler. c) An inner class. d) An event adapter.

a) A Button object.

Exam 1 - A class from which you cannot create objects is called a/an ____. a) Abstract class. b) Concrete class. c) Non-inheritable class. d) Superclass.

a) Abstract class.

Exam 2 - Consider the helper method reversePrint, which uses recursion to display in reverse the elements in a section of an array limited by the firstIndex and lastIndex arguments. What statement should be used to complete the recursive method?

a) array[firstIndex]

Exam 3 - The merge sort algorithm presented in section 14.4, which sorts an array of integers in ascending order, uses the merge method which is partially shown below. Select the condition that would be needed to complete the method so that the elements are sorted in descending order. private static void merge(int[] first, int[] second, int[] a) { int iFirst = 0; int iSecond = 0; int j = 0; while (iFirst < first.length && iSecond < second.length) { if (_____________________________) { a[j] = first[iFirst]; iFirst++; } else { a[j] = second[iSecond]; iSecond++; } j++; } // rest of the method follows here } a) first[iFirst] > second[iSecond] b) first[iFirst] < second[iSecond] c) iFirst > iSecond d) iFirst < iSecond

a) first[iFirst] > second[iSecond]

Exam 1 - Which of the following statements about classes is NOT true? a) You cannot create an object from a class declared with the keyword final. b) You can override methods in a class declared with the keyword final. c) You cannot extend a class declared with the keyword final. d) You cannot create subclasses from a class declared with the keyword final.

b) You can override methods in a class declared with the keyword final.

Exam 1 - You are creating a class inheritance hierarchy about motor vehicles that will contain classes named Vehicle, Auto, and Motorcycle. Which of the following statements is correct? a) Vehicle should be the default class, while Auto and Motorcycle should be the subclasses. b) Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses. c) Vehicle should be the subclass, while Auto and Motorcycle should be the superclasses. d) Vehicle should be the subclass, while Auto and Motorcycle should be the default classes.

b) Vehicle should be the superclass, while Auto and Motorcycle should be the subclasses.

Exam 2 - Consider the following code snippet: public class MyApp extends Application { public void start (Stage stage){ Button button = new Button("Calculate"); Label label = new Label("Total amount due"); . . . } public class MyHandler implements EventHandler<ActionEvent> { public void handle (ActionEvent event) { label.setText("Hell"); } } } What is wrong with this code? a) label must be declared as an instance variable. b) button must be declared as an instance variable. c) Both label and button must be declared as instance variables. d) No error. It does work correctly.

a) label must be declared as an instance variable.

Exam 2 - Suppose you are developing a payroll application that computes and displays weekly payheck amounts for various employees. As a result of the design phase, the partial Employee class below is developed. Select the method header that best completes the class, according to the method comments. public class Employee { private double hourlyRate; private int hoursWorked; /** Modifies the hourly rate for this employee. @param newHourlyRate the rate per hour earned by the employee **/ __________________________ { / / method body } } a) public void changeRate(double newHourlyRate) b) public void changeRate(double newHourlyRate, int hoursWorked) c) public double changeRate (double newHourlyRate) d) public double changeRate (double newHourlyRate, int hoursWorked)

a) public void changeRate(double newHourlyRate)

Exam 3 - Choose the order of the following growth rates, from slowest to fastest: θ(n^3), θ(nlog(n)), θ(n^3/2), θ(2^n). a) θ(n log(n)), θ(n^3/2), θ(n^3), θ(2^n) b) θ(n^3), θ(n log(n)), θ(n^3/2), θ(2^n) c) θ(n^3/2), θ(n log(n)), θ(n^3), θ(2^n) d) θ(2^n), θ(n^3), θ(n^3/2), θ(n log(n))

a) θ(n log(n)), θ(n^3/2), θ(n^3), θ(2^n)

Exam 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.

Exam 3 - Consider the following code snippet: LinkedList<String> words = new LinkedList<>(); words.addFirst("abc"); words.addLast("def"); words.addFirst("ghi"); System.out.print(words.removeLast()); System.out.print(words.removeFirst()); System.out.print(words.removeLast()); What will this code print when it is executed? a) ghiabcdef b) defghiabc c) abcdefghi d) abcghidef

b) defghiabc

Exam 3 - Consider the following code snippet: public static void sort(int[] a) { for (int i = 1; i < a.length; i++) { int next = a[i]; int j = i; while (j > 0 && a[j - 1] > next) { a[j] = a[j - 1]; j--; } a[j] = next; } } What sort algorithm is used in this code? a) quicksort b) insertion sort c) merge sort d) selection sort

b) insertion sort

Exam 3 - Consider the following code snippet: LinkedList<String> myLList = new LinkedList<>(); myLList.add("Mary"); myLList.add("John"); myLList.add("Sue"); ListIterator<String> iterator = myLList.listIterator(); iterator.next(); iterator.next(); iterator.add("Robert"); iterator.previous(); iterator.previous(); iterator.remove(); System.out.println(myLList); What will be printed when this code is executed? a) [Mary, John, Robert, Sue] b) [John, Robert, Sue] c) [Mary, Robert, Sue] d) [Mary, John, Sue]

c) [Mary, Robert, Sue]

Exam 3 - The partial binary search method below is designed to search an array of String objects sorted in ascending order. Select the expression that would be needed to complete the method. public static int search(String[] a, int low, int high, String item) { if(low <= high) { int mid = (low + high) / 2; int result = ____________________________; if (result == 0) { return mid; } else if (result < 0) { return search(a, mid + 1, high, item); } else { return search(a, low, mid - 1, item); } } return -1; } a) a[low].compareTo(item) b) item.compareTo(a[mid]) c) item.equals(a[mid]) d) a[mid].compareTo(item)

d) a[mid].compareTo(item)

Exam 3 - Select an appropriate expression to complete the method below. The method should return the number of times that the string stored in name appears in theList. public static int count(LinkedList<String> theList, String name) { int number = 0; Iterator<String> iter = theList.iterator(); while (______________________) { if (iter.next().equals(name)) { number++; } } return number; } a) theList.next() != null b) iter.hasNext() c) theList.hasNext() d) iter.next() != null

b) iter.hasNext()

Exam 3 - A queue is a collection that ____. a) remembers the order of elements, and allows elements to be added and removed only at one end. b) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end. c) does not remember the order of elements but allows elements to be added in any position. d) remembers the order of elements and allows elements to be inserted in any position.

b) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end.

Exam 2 - The statement that would add smallButton to the button group in the following code is _________. RadioButton smallButton = new RadioButton("Small"); ToggleGroup group = new ToggleGroup(); a) choicesgroup.add(smallButton); b) smallButton.setToggleGroup(group); c) smallButton.add(group); d) You cannot add smallButton to the button group.

b) smallButton.setToggleGroup(group);

Exam 3 - The code segment below prints some of the elements in an array with size n. Select an expression to complete the code segment so that the resulting algorithm has O(log n) running time. for __________________________ { System.out.println(array[j]); } a) (int j = 0; j < array.length; j = j + 2) b) (int j = 0; j < array.length / 2; j++) c) (int j = 1; j < array.length; j = j * 2) d) (int j = 0; j < array.length; j++)

c) (int j = 1; j < array.length; j = j * 2)

Exam 3 - Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search? a) 2 b) 8 c) 4 d) 3

c) 4

Exam 2 - When you make a timeline, you need to define ______. a) Timer b) EventHandler c) Keyframes d) Duration

c) Keyframes

Exam 2 - Which statement correctly describes the class relationship shown in this diagram? Quiz < ---- Question a) Quiz class depends on Question class b) Question class aggregates Quiz class c) Quiz class aggregates Question class

c) Quiz class aggregates Question class

Exam 1 - A class that represents a more specifc entity in an inheritance heirarchy is called a/an _______. a) Default Class b) Superclass c) Subclass d) Inheritance class

c) Subclass

Exam 1 - Consider the following code snippet: public class BankAccount implements Comparable<BankAccount> { . . . public int compareTo(T other) { What is wrong with this code? a) The type parameter for the Comparable interface in the implements clause must be the same as the implementing class. b) The type parameter for the Comparable interface in the implements clause must be the generic <T>. c) The type parameter for the compareTo method must be the same as the implementing class. d) The type parameter for the compareTo interface must be the generic <T>.

c) The type parameter for the compareTo method must be the same as the implementing class.

Exam 1 - Consider the following code snippet: Auto consumerAuto = new Auto(4, "gasoline"); String s = consumerAuto.toString(); Assume that the Auto 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 consumerAuto. b) s will contain only the name of the consumerAuto object. c) s will contain the name of the consumerAuto object followed by a hash code. d) This code will not compile.

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

Exam 1 - To test whether an object belongs to a particular type, use ___. a) the this reserved word. b) the subclassOf reserved word. c) the instanceof operator. d) the equals method.

c) the instanceof operator.

Exam 2 - Given the following diagram: Customer <----- BankAccount What does this diagram indicate about the relationship between the customers and bank accounts? a) A bank account may be owned by only one customer. b) A bank account may be owned by one or more customers. c) A customer may have only one bank account. d) A customer may have one or more bank accounts.

d) A customer may have one or more bank accounts.

Exam 1 - Which of the following is true regarding subclasses? a) A subclass has access to private instance variables of its superclass. b) A subclass does not have access to public instance variables of its superclass. c) A subclass must specify the implicit parameter to use methods inherited from its superclass. d) A subclass has no access to private instance variables of its superclass.

d) A subclass has NO access to private instance variables of its superclass.

Exam 2 - Which statement correctly describes the class relationship shown in this diagram? Vehicle /\ | Car a) Vehicle class depends on Car class b) Car class depends on Vehicle class c) Vehicle class inherts from Car class d) Car class inherits from Vehicle class

d) Car class inherits from Vehicle class

Exam 2 - Which statement correctly describes the class relationship shown in this diagram? Cash Registered | \/ Coin a) Coin class inherits from CashRegister class. b) CashRegister class aggregates Coin class. c) Coin class depends on CashRegister class d) CashRegister class depends on Coin class

d) CashRegister class depends on Coin class

Exam 2 - What is the nickname for the latest graphical user interface library in Java? a) Applet b) GUI c) Swing d) JavaFX

d) JavaFX

Exam 3 - You need to access values in the opposite order in which they were added (last in, first out), and not randomly. Which collection type should you use? a) Hashtable b) Map c) Queue d) Stack

d) Stack

Exam 3 - In each iteration, selection sort places which element in the correct location? a) The smallest in the array. b) The largest element in the array. c) A random element. d) The smallest element not yet placed in prior iterations.

d) The smallest element not yet placed in prior iterations.

Exam 1 - 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 call itself. b) This method calls a public method in its subclass. c) This method calls a private method in its superclass d) This method calls a public method in its superclass.

d) This method calls a public method in its superclass.

Exam 3 - Consider the following code snippet: Stack<String> stringStack = new Stack<>(); stringStack.push("ab"); stringStack.push("abc"); stringStack.push("a"); while (!stringStack.empty()) { System.out.print(stringStack.pop() + ","); } What output will be produced when this code is executed? a) abc,ab,a, b) a,ab,abc, c) ab,abc,a, d) a,abc,ab,

d) a,abc,ab,

Exam 3 - Complete the code shown to define the Comparator interface for comparing Auto objects. public interface Comparator<Auto> { _____________________ } a) boolean compare(Auto a, Auto b); b) single compare(Auto a, Auto b); c) single compareTo(Auto a, Auto b); d) int compare(Auto a, Auto b);

d) int compare(Auto a, Auto b);

Exam 3 - Suppose you wish to implement the Comparable interface to allow your Vehicle class to compare Auto objects only. Which of the following is the correct way to do this? a) public class Auto implements Comparable<Vehicle> b) public class Vehicle implements Comparable(Auto) c) public class Vehicle implements Comparable d) public class Vehicle implements Comparable<Auto>

d) public class Vehicle implements Comparable<Auto>

Exam 2 - Consider the square method shown below that takes a non-negatice int argument. Complete the code for the square method so that it correctly calls the squareHelper method to produce the square of n.

d) return squareHelper(n, n)


Conjuntos de estudio relacionados

A&P Ch 20 Vessels and Circulation

View Set

Risk Management for Small Unit Leaders Post-test

View Set

The Call of the Wild: Quiz Study Notes

View Set

Chapter 7 Alternating current, Power Distribution, and Voltage Terms

View Set

Finishing, Polishing, and Cleaning Materials

View Set

LARE 4 Site Engineering/Construction Comprehensive Set 2016

View Set