Prog 2 Final

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The code segment below displays a pattern of asterisks. Select an expression to complete the code segment so that the resulting algorithm has O(n) running time. for (int k = 0; k < n; k++) { for _______________________ { System.out.print("*"); } System.out.println(); } A.) (int j = n; j > 0; j = j / 2) B.) (int j = 1; j <= 10; j++) C.) (int j = 1; j < k; j++) D.) (int j = n; j > 0; j--)

B.) (int j = 1; j <= 10; j++)

Which of the following statements about the TreeSet class is NOT correct? A.) Elements are stored in nodes. B.) Elements are arranged in linear fashion. C.) To use a TreeSet, it must be possible to compare the elements. D.) Elements are stored in sorted order.

B.) Elements are arranged in linear fashion.

Which of the following classes implement the Comparable interface? I Date II Collections III String A.) I only B.) I and III only C.) I and II only D.) II and III only

B.) I and III only

A ____ has an addActionListener() method for specifying which object is responsible for implementing the action associated with the object. A.) JSlider B.) JButton C.) JFrame D.) JLabel

B.) JButton

Which of the following statements about an inner class is true? A.) An inner class may not be declared within a method of the enclosing scope. B.) The methods of an inner class can access variables declared in the enclosing scope. C.) An inner class may only be declared within a method of the enclosing scope. D.) An inner class can access variables from the enclosing scope only if they are passed as constructor or method parameters.

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

Consider the sort method for selection sort shown below: 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 control to read int i = 1; i < a.length - 1; i++. What would be the result? A.) The sort would still work correctly. B.) The sort would not consider the first array element. C.) An exception would occur D.) The sort would not consider the last array element.

B.) The sort would not consider the first array element.

Which of the following statements about manipulating objects in a set is correct? A.) A set iterator visits elements in the order in which they were added to the set. B.) You can remove an element at the position indicated by an iterator. C.) If you try to add an element that already exists, an exception will occur. D.) You can add an element at the position indicated by an

B.) You can remove an element at the position indicated by an iterator.

You intend to use a hash set with your own object class. Which of the following statements is correct? A.) You cannot override the hashCode method in the Object class. B.) You can use the hash method of the Objects class to create your own hashCode method by combining the hash codes for the instance variables. C.) You can override the hash method for your class provided that it is compatible with its equals method. D.) Your class's hashCode method does not need to be compatible with its equals method.

B.) You can use the hash method of the Objects class to create your own hashCode method by combining the hash codes for the instance variables.

Use the ____ method to add a mouse listener to a component. A.) addActionListener B.) addMouseListener C.) addMouseActionListener D.) addListener

B.) addMouseListener

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

Which method is NOT part of the ListIterator interface? A.) next B.) delete C.) add D.) previous

B.) delete

Which method is NOT part of the ListIterator interface? A.) hasPrevious B.) hasMore C.) add D.) hasNext

B.) hasMore

Insert the missing code in the following code fragment. This fragment is intended to read floating-point numbers from a text file. Scanner in = new Scanner(. . .); while (____________) { double hoursWorked = in.nextDouble(); System.out.println(hoursWorked); } A.) in.getNextDouble() B.) in.hasNextDouble() C.) in.hasNextInt() D.) in.peek()

B.) in.hasNextDouble()

Assume you have created a linked list named myList that currently holds some number of String objects. Which of the following statements correctly adds a new element to the beginning of myList? A.) myList.insert("Harry"); B.) myList.addFirst("Harry"); C.) myList.add("Harry"); D.) myList.put("Harry");

B.) myList.addFirst("Harry");

A stack is a collection that ____. A.) does not remember the order of elements but allows elements to be added in any position. B.) remembers the order of elements, and allows elements to be added and removed only at one end. C.) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end. 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 added and removed only at one end.

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? A.) BowlingGame otherGame = (Object) other; return otherGame.score - score; B.) return score - other.score; C.) BowlingGame otherGame = (Object) other; return score - otherGame.score; D.) return other.score - score;

B.) return score -other.score;

Consider the following code snippet: public class Vehicle { protected int numberAxles; . . . } Which statement is true about the accessibility of data in the numberAxles variable? A.) It is only accessible by the Vehicle class's methods and by all of its subclasses B.)It is only accessible by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package. C.) It is only accessible by the Vehicle class's methods. D.) It is accessible by any class.

B.)It is only accessible by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.

Consider the following code snippet: File inputFile = new File("input.txt"); You wish to read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this? A.) Scanner in = new Scanner("input.txt"); B.)Scanner in = new Scanner(inputFile); C.) Scanner in = Scanner.open(inputFile); D.) Scanner in = Scanner("input.txt");

B.)Scanner in = new Scanner(inputFile);

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? A.) You must call the component object's paintComponent() method. B.)You must call the component object's repaint() method. C.) You do not have to do anything - the component object will be automatically repainted when its data changes. D.) You must use a Timer object to cause the component object to be updated.

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

Consider the following code snippet: Scanner in = new Scanner(. . .); String result = ""; double number = 0; if (in.hasNextDouble()) { number = in.nextDouble(); } result = in.next(); If the input file contains the characters 626.14 average, what values will number and result have after this code is executed? A.) number will contain the value 0 and result will contain the value 626.14. B.)number will contain the value 626.14 and result will contain the value average. C.) number will contain the value 626 and result will contain the value 14. D.) number will contain the value 0 and result will contain the value average.

B.)number will contain the value 626.14 and result will contain the value average.

Which of the following statements about sets is correct? A.) Attempting to remove an element that is not in the set generates an exception. B.) You can add an element to a specific position within a set. C.) A set is a collection of unique elements organized for efficiency. D.) A set allows duplicate values.

C.) A set is a collection of unique elements organized for efficiency.

Which of the following statements about interfaces is NOT true? A.) An interface can supply a default implementation. B.) Interfaces can make code more reusable. C.) An interface can describe the state that should be maintained. D.) An interface describes the behavior that an implementation should supply.

C.) An interface can describe the state that should be maintained.

Which of the following statements about white space in Java is correct? A.) In Java, white space includes spaces, tab characters, newline characters, and punctuation. B.) In Java, white space includes spaces and tab characters only. C.) In Java, white space includes spaces, tab characters, and newline characters. D.) In Java, white space includes spaces only.

C.) In Java, white space includes spaces, tab characters, and newline characters.

You need to access values in objects by a key that is not part of the object. Which collection type should you use? A.) Hashtable B.) ArrayList C.) Map D.) Queue

C.) Map

Assume inputFile is a Scanner object used to read data from a text file that contains a series of double values. Select an expression to complete the following code segment, which reads the values and prints them in standard output, one per line, in a field 15 characters wide, with two digits after the decimal point. while (inputFile.hasNextDouble()) { double value = inputFile.nextDouble(); ___________________________________ // statement to display double value } A.) System.out.printf("%15f",value); B.) System.out.printf("%.2f",value); C.)System.out.printf("%15.2f\n",value); D.) System.out.printf("%15.2f",value);

C.) System.out.printf("%15.2f\n",value);

Which of the following statements about the try/finally statement is NOT true? A.) The finally clause is executed if the body of the try completes without exception. B.) The finally clause is executed if the body of the try throws exception. C.) The finally clause is executed after the exception is propagated to its handler. D.) The finally clause typically represents cleanup code that is to be executed whether or not an exception occurs.

C.) The finally clause is executed after the exception is propagated to its handler.

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? A.)The Timer object must be declared as final. B.)There is nothing wrong with the code. C.) The listener has not been attached to the Timer object. D.) The Timer object should be declared before the MyListener class.

C.) The listener has not been attached to the Timer object.

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 variable. C.) The subclass must use a public method of the superclass (if it exists) 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.) The subclass must use a public method of the superclass (if it exists) to update the superclass's private instance variable..

When you start a Java program from a command line and supply argument values, the values ____. A.) are stored as float values B.) are stored as int values C.) are stored as String values D.) are stored as the type of value indicated by the argument

C.) are stored as String values

Suppose you wish to sort an array list of objects, but the object class does not implement the Comparable interface. Because you are not allowed to modify this class, you decide to provide a comparator object that implements the Comparator interface. Which method must you implement from this interface to achieve your objective? A.) sort B.) compareObject C.) compare D.) compareTo

C.) compare

Which sort algorithm starts by cutting the array in half and then recursively sorts each half? A.) insertion sort B.) quicksort C.) merge sort D.) selection sort

C.) merge sort

Assume that you have declared a map named myMap to hold String values with Integer keys. Which of the following statements will correctly add an association into myMap? A.) myMap.insert(3, "apple"); B.) myMap.add(3, "apple"); C.) myMap.put(3, "apple"); D.) myMap.push(3, "apple");

C.) myMap.put(3, "apple");

Consider the following class hierarchy: public final class Shape { private String mycolor; public Shape(String mycolor) { this.type = mycolor; } public String getColor() { return mycolor; } } public class Triangle extends Shape { public Triangle(String mycolor) { super(mycolor); } } } What is wrong with this class hierarchy definition? A.) Nothing is wrong with the code. B.) There should be more subclasses of the Shape class than just Triangle. C.)There cannot be any subclasses of the Shape class. D.) It is not possible to use super in the Triangle constructor.

C.)There cannot be any subclasses of the Shape class.

Select an appropriate expression to complete the following method, which is designed to return the sum of the two smallest values in the parameter array numbers. public static int sumTwoLowestElements(int[] numbers) { PriorityQueue<Integer> values = new PriorityQueue<>(); for (int num: numbers) { values.add(num); } ______________________ } A.) return values.remove() * 2; B.) return values.peek() + values.remove(); C.) return values.peek() + values.peek(); D.) return values.remove() + values.remove();

D.) return values.remove() + values.remove();

How many times can an array with 4,096 elements be cut into two equal pieces? A.) 10 B.) 8 C.) 16 D.) 12

D.) 12

If the method makeMenuItem is called four times, how many MyMenuListener objects will be created? public JMenuItem makeMenuItem(String menuLabel) { JMenuItem mi = new JMenuItem(menuLabel); class MyMenuListener implements ActionListener { public void actionPerformed(ActionEvent e) { doSomething(); } } mi.addActionListener(new MyMenuListener()); return mi; } A.) 1 B.) 2 C.) 3 D.) 4

D.) 4

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? A.) It is necessary only to allow other classes to use this method. B.) It is not necessary to declare this method as public. C.) All methods in an interface are private by default. D.) All methods in a class are not public by default.

D.) All methods in a class are not public by default.

The sort method of the Arrays class sorts arrays containing objects of classes that implement the ____ interface. A.) Ordered B.) Sortable C.) Array D.) Comparable

D.) Comparable

Which of the sorts in the textbook can be characterized by the fact that even in the worst case the running time will be O(n log(n)))? I quicksort II selection sort III merge sort A.) I and III only B.) I only C.) II only D.) III only

D.) III only

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

D.) The main method terminates if the FileNotFoundException occurs.

Assume we are using quicksort to sort an array in ascending order. What can we conclude about the indexes of two pivot elements placed in consecutive recursive calls? A.) They are both in the same half of the array. B.) They are in different halves of the array. C.) They are adjacent. D.) They are randomly located.

D.) They are randomly located.

Consider the code snippet shown below. Assume that employeeNames is an instance of type LinkedList<String>. for (String name : employeeNames) { // Do something with name here } Which element(s) of employeeNames does this loop process? A.) elements meeting a condition B.) the most recently added elements C.) no elements D.) all elements

D.) all elements

In general, the expression ____ means that f grows no faster than g. A.) f(n) = log g B.) f(n) = log g^2 C.) g(n) = O(f(n)) D.) f(n) = O(g(n))

D.) f(n) = O(g(n))

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

D.) getMessage()

The ____ method of the Character class will indicate if a character contains white space. A.) isValid() B.) getChar() C.) hasNext() D.) isWhiteSpace()

D.) isWhiteSpace()

Select an appropriate expression to complete the following method, which is designed to visit the elements in theList and replace each occurrence of the string "hello" with the string "goodbye". public static void helloGoodbye(LinkedList<String> theList) { ListIterator<String> iterator = theList.listIterator(); while (iterator.hasNext()) { if (iterator.next().equals("hello")) { _____________________________ } } } A.) iterator.next() = "goodbye"; B.)iterator.previous("goodbye"); C.)iterator.replace("hello", "goodbye"); D.)iterator.set("goodbye");

D.) iterator.set("goodbye");

A ____ is a data structure used for collecting a sequence of objects that allows efficient addition and removal of already-located elements in the middle of the sequence. A.) queue B.) priority queue C.) stack D.) linked list

D.) linked list

Assume you have created a linked list named myList that currently holds some number of String objects. Which of the following statements correctly removes an element from the end of myList? A.) myList.remove(); B.) myList.getLast(); C.) myList.pop(); D.) myList.removeLast();

D.) myList.removeLast();

Assuming that names is a Queue of String objects, select a statement to complete the code segment below. The code is designed to remove the last element from the queue, which is guaranteed to have at least one element. Queue<String> aQueue = new LinkedList<>(); while (names.size() > 1) { aQueue.add(names.remove()); } names.remove(); while (aQueue.size() > 0) { ____________________________ } A.) aQueue.add(names.peek()); B.) aQueue.add(names.remove()); C.) names.add(aQueue.peek()); D.) names.add(aQueue.remove());

D.) names.add(aQueue.remove());

Which container is used to group multiple user-interface components together? A.) text area B.) rectangle C.) table D.) panel

D.) panel

Select an appropriate expression to complete the following method, which is designed to return the sum of the two smallest values in the parameter array numbers. public static int sumTwoLowestElements(int[] numbers) { PriorityQueue<Integer> values = new PriorityQueue<>(); for (int num: numbers) { values.add(num); } ______________________ } A.) return values.remove() * 2; B.) return values.peek() + values.remove(); C.) return values.peek() + values.peek(); D.) return values.remove() + values.remove();

D.) return values.remove() + values.remove();

Which reserved word must be used to call a method of a superclass? A.) this B.) my C.) parent D.) super

D.) super

Consider the classes shown below: public class Parent { public int getValue() { return 24; } public void display() { System.out.print(getValue() + " "); } } public class Child extends Parent { public int getValue() { return -7; } } Using the classes above, what is the output of the following lines of code? Parent kid = new Child(); Parent adult = new Parent(); kid.display(); adult.display(); A.) -7 24 B.) 24 24 C.) -7 -7 D.) 24 -7

A.) -7 24

Which of the following statements about a Java interface is NOT true? A.) A Java interface must contain more than one method. B.) A Java interface defines a set of methods that are required. C.) A Java interface specifies behavior that a class will implement. D.) All methods in a Java interface must be public.

A.) A Java interface must contain more than one method.

In a linked list data structure, when does the reference to the first node need to be updated? I inserting into an empty list II deleting from a list with one node III deleting an inner node A.) I and II only B.) I only C.) II only

A.) I and II only

Which of the following algorithms would be efficiently executed using a LinkedList? A.) Remove first n/ 2 elements from a list of n elements. B.) Read n / 2 elements in random order from a list of n elements. C.) Tracking paths in a maze. D.) Binary search.

A.) Remove first n/ 2 elements from a list of n elements.

What can be determined about obj from the code below? JMenuItem menuItem = new JMenuItem("Exit"); menuItem.addActionListener(obj); A.) The class of obj implements ActionListener B.) menuItem implements ActionListener C.) obj is of type MenuListener D.) obj is an object of an inner class

A.) The class of obj implements ActionListener

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 have an object reference 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.) You can construct an object from this class.

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; } A.) all have the same lifetimes B.) menuLabel and new MyMenuListener() are the same and both longer than mi C.) mi and new MyMenuListener() are the same and both longer than menuLabel D.) from shortest to longest:menuLabel, new MyMenuListener(), mi

A.) all have the same lifetimes

When an event occurs, the event source notifies all ____. A.) event listeners B.) components C.) panels D.) interfaces

A.) event listeners

Assume that you have declared a map named myMap to hold String values with Integer keys. Which of the following statements will correctly delete an association from myMap? A.) myMap.remove(3); B.) myMap.remove("apple"); C.) myMap.delete(3); D.) myMap.pop(3);

A.) myMap.remove(3);

If an element is present in an array of length n, how many element visits, on average, are necessary to find it using a linear search? A.) n / 2 B.) 2^n C.) n D.) n^2

A.) n / 2

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? A.) public static void main(String[] args) throws FileNotFoundException B.) public static void main(String[] args) throws UnknownFileException C.) public static void main(String[] args) D.) public static void main(String[] args) throws FileMissingException

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

The JFrame has a content pane with a default BorderLayout manager. Which method allows changing it to a FlowLayout manager? A.) setLayout B.) setFlowLayout C.) assignLayout D.) changeLayout

A.) setLayout

Consider the following code snippet, which is meant to override the equals() method of the Object class: public class Coin { . . . public boolean equals(Coin otherCoin) { . . . } . . . } What is wrong with this code? a.) A class cannot override the equals() method of the Object class. b.) The equals() method must be declared as private. c.) A class cannot change the parameters of a superclass method when overriding it. d.)There is nothing wrong with this code.

c.) A class cannot change the parameters of a superclass method when overriding it..

Why is the following code, which assumes ChoiceQuestion is a subclass of Question, considered poor strategy? if (q instanceof ChoiceQuestion) { // Do the task the ChoiceQuestion way } else if (q instanceof Question) { // Do the task the Question way } a.) Question should be the subclass, not the other way around. b.) instanceof is not an operator but rather a method. c.)instanceof should not be used for type tests that can be solved by using polymorphism. d.) the instanceof reserved word should only be used in a class constructor.

c.) instanceof should not be used for type tests that can be solved by using polymorphism.

Consider the following code snippet: public class Vessel { . . . public void setVesselAttributes() { . . . } } public class Speedboat extends Vessel { . . . public void setvesselattributes() { . . . } } Which of the following statements is correct? a.) The subclass is shadowing a superclass method. b.) The subclass is overloading a superclass method. c.) The subclass is overriding a superclass method. d.) The subclass is defining its own distinct method.

d.) The subclass is defining its own distinct method.

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 have an object reference 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.) You can construct an object from this class.


संबंधित स्टडी सेट्स

Principles of Biology- Macromolecules: carbohydrates and lipids

View Set

Accounting Exam I review Finished

View Set

College Personal Finance Chapter 6 Introduction to Consumer Credit

View Set

Chapter 6 Biology (Endomembrane System

View Set

Workbook Chapter 39: Caring for People with Developmental Disabilities

View Set

Critical Thinking Midterm Study Guide

View Set