Java 2 Final Exam (ch 18, 19, 20, 31, 32)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Fill in the code to complete the following method for checking whether a string is a palindrome. public static boolean isPalindrome(String s) { if (s.length() <= 1) // Base case return true; else if _____________________________ return false; else return isPalindrome(s.substring(1, s.length() - 1)); } A. (s.charAt(0) != s.charAt(s.length() - 1)) // Base case B. (s.charAt(0) != s.charAt(s.length())) // Base case C. (s.charAt(1) != s.charAt(s.length() - 1)) // Base case D. (s.charAt(1) != s.charAt(s.length())) // Base case

A.

Is ArrayList<Integer> a subclass of ArrayList<?>? A. Yes B. No

A.

Show the output of the following code public class Test1 { public static void main(String[] args) { System.out.println(f2(2, 0)); } public static int f2(int n, int result) { if (n == 0) return 0; else return f2(n - 1, n + result); } } A. 0 B. 1 C. 2 D. 3

A.

Suppose List<String> list = new ArrayList<String>. Which of the following operations are correct? A. list.add("Red"); B. list.add(new Integer(100)); C. list.add(new java.util.Date()); D. list.add(new ArrayList());

A.

The __________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is empty. A. poll() B. remove() C. peek() D. element()

A.

To create a generic type bounded by Number, use A. <E extends Number> B. <E extends Object> C. <E> D. <E extends Integer>

A.

To create a menu bar, use ________. A. new MenuBar() B. new Menu() C. new MenuItem() D. new CheckMenuItem() E. new RadioMenuItem()

A.

To declare a class named A with a generic type, use A. public class A<E> { ... } B. public class A<E, F> { ... } C. public class A(E) { ... } D. public class A(E, F) { ... }

A.

To declare an interface named A with a generic type, use A. public interface A<E> { ... } B. public interface A<E, F> { ... } C. public interface A(E) { ... } D. public interface A(E, F) { ... }

A.

To remove all the elements in the list in the following code, replace the underlined blank space with __________. import java.util.*; public class Test { public static void main(String[] args) { ______________<Integer> list = new ArrayList<>(); list.add(0); list.add(1); list.add(2); for (int i = 0; i < 3; i++) { list.remove(i); } System.out.println(list); } } A. Collection B. List C. ArrayList D. AbstractList

A.

To create a menu, use ________. A. new MenuBar() B. new Menu() C. new MenuItem() D. new CheckMenuItem() E. new RadioMenuItem()

B.

To declare a class named A with two generic types, use A. public class A<E> { ... } B. public class A<E, F> { ... } C. public class A(E) { ... } D. public class A(E, F) { ... }

B.

To declare an interface named A with two generic types, use A. public interface A<E> { ... } B. public interface A<E, F> { ... } C. public interface A(E) { ... } D. public interface A(E, F) { ... }

B.

Which of the following methods in Thread throws InterruptedException? A. run() B. sleep(long) C. start() D. yield() E. setPriority(int)

B.

Analyze the following code. // Test.java: Define threads using the Thread class import java.util.*; public class Test { private Stack stack = new Stack(); private int i = 0; /** Main method */ public static void main(String[] args) { new Test(); } public Test() { // Start threads new Producer().start(); new Consumer().start(); } class Producer extends Thread { public void run() { while (true) { System.out.println("Producer: put " + i); stack.push(new Integer(i++)); synchronized (stack) { notifyAll(); } } } } class Consumer extends Thread { public void run() { while (true) { synchronized (stack) { try { while (stack.isEmpty()) stack.wait(); System.out.println("Consumer: get " + stack.pop()); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } } } A. The program creates two threads: one to add data to the stack and the other to get data from the stack. B. The program has a compilation error on the notifyAll() method in the Producer class because it is not invoked from the stack object. C. The program will throw an exception because the notifyAll() method in the Producer class is not invoked from the stack object. D. The program has a logic error because the lock obtained by the synchronized block for notifyAll in the Producer class is stack and it should be this (i.e., synchronized (this) { notifyAll(); }).

C.

To create a menu item, use ________. A. new MenuBar() B. new Menu() C. new MenuItem() D. new CheckMenuItem() E. new RadioMenuItem()

C.

In the following method, what is the base case? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } A. n is 1. B. n is greater than 1. C. n is less than 1. D. no base case.

A.

_______ defines a style class. A. .plaincircle {-fx-fill: white; -fx-stroke: black;} B. #plaincircle {-fx-fill: white; -fx-stroke: black;} C. plaincircle {-fx-fill: white; -fx-stroke: black;} D. +plaincircle {-fx-fill: white; -fx-stroke: black;}

A.

All the concrete classes in the Java Collections Framework implement _____________. A. the Cloneable interface B. the Serializable interfaces C. the Comparable interface D. the Comparator interface

B.

For an instance of Collection, you can obtain its iterator using ________________. A. c.getIterator() B. c.iterator() C. c.iterators() D. c.iterable()

B.

How many times is the fib method in Listing 18.2 invoked for fib(5)? A. 14 B. 15 C. 25 D. 31 E. 32

B.

How many times is the recursive moveDisks method invoked for 3 disks? A. 3 B. 7 C. 10 D. 14

B.

Is ArrayList<Integer> a subclass of ArrayList<Object>? A. Yes B. No

B.

The _________ method sets a style id named greencircle for a node or a scene. A. node.setStyleId("greencircle"); B. node.setId("greencircle"); C. node.getStylesheets.addId("greencircle"); D. node.setStyle("greencircle");

B.

The __________ method in the Queue interface retrieves and removes the head of this queue and throws an exception if this queue is empty. A. poll() B. remove() C. peek() D. element()

B.

Which of the following can be used to replace YYYYYYYY in the following code? public class WildCardDemo3 { public static void main(String[] args) { GenericStack<String> stack1 = new GenericStack<>(); GenericStack<Object> stack2 = new GenericStack<>(); stack2.push("Java"); stack2.push(2); stack1.push("Sun"); add(stack1, stack2); WildCardDemo2.print(stack2); } public static <T> void add(GenericStack<T> stack1, GenericStack<YYYYYYYY> stack2) { while (!stack1.isEmpty()) stack2.push(stack1.pop()); } } A. ? super Object B. ? super T C. ? extends T D. ? extends Object

B.

_______ defines a style id. A. .plaincircle {-fx-fill: white; -fx-stroke: black;} B. #plaincircle {-fx-fill: white; -fx-stroke: black;} C. plaincircle {-fx-fill: white; -fx-stroke: black;} D. +plaincircle {-fx-fill: white; -fx-stroke: black;}

B.

Analyze the following code: public abstract class Test implements Runnable { public void doSomething() { }; } A. The program will not compile because it does not implement the run() method. B. The program will not compile because it does not contain abstract methods. C. The program compiles fine. D. None of the above.

C.

Fill in the code to complete the following method for sorting a list. public static void sort(double[] list) { ___________________________; } public static void sort(double[] list, int high) { if (high > 1) { // Find the largest number and its index int indexOfMax = 0; double max = list[0]; for (int i = 1; i <= high; i++) { if (list[i] > max) { max = list[i]; indexOfMax = i; } } // Swap the largest with the last number in the list list[indexOfMax] = list[high]; list[high] = max; // Sort the remaining list sort(list, high - 1); } } A. sort(list) B. sort(list, list.length) C. sort(list, list.length - 1) D. sort(list, list.length - 2)

C.

Suppose a list contains {"red", "green", "red", "green"}. What is the list after the following code? list.remove("red"); A. {"red", "green", "red", "green"} B. {"green", "red", "green"} C. {"green", "green"} D. {"red", "green", "green"}

C.

Suppose there are three Runnable tasks, task1, task2, task3. How do you run them in a thread pool with 2 fixed threads? A. new Thread(task1).start(); new Thread(task2).start(); new Thread(task3).start(); B. ExecutorService executor = Executors.newFixedThreadPool(3); executor.execute(task1); executor.execute(task2); executor.execute(task3); C. ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(task1); executor.execute(task2); executor.execute(task3); D. ExecutorService executor = Executors.newFixedThreadPool(1); executor.execute(task1); executor.execute(task2); executor.execute(task3);

C.

The _________ method loads a style sheet named style.css for a node or a scene. A. node.setStyleSheet("style.css"); B. node.getStyleSheets.setStyleSheet("style.css"); C. node.getStylesheets.add("style.css"); D. node.setStylesheets("style.css");

C.

The keyword to synchronize methods in Java is __________. A. synchronize B. synchronizing C. synchronized D. Synchronized

C.

To add a menu item to a menu, use _______. A. menu.add(menuItem) B. menu.addAll(menuItem) C. menu.getItems().add(menuItem) D. menu.getMenus().add(menuItem)

C.

To add a menu to a menu bar, use _______. A. menuBar.add(menu) B. menuBar.addAll(menu) C. menuBar.getMenus().add(menu) D. menuBar.getItems().add(menu)

C.

What is the output of the following code? import java.util.*; public class Test { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<>(); list.add(new Student("Peter", 65)); list.add(new Student("Jill", 50)); list.add(new Student("Sarah", 34)); Collections.sort(list); System.out.print(list + " "); Collections.sort(list, new StudentComparator1()); System.out.println(list); } static class StudentComparator1 implements Comparator<Student> { public int compare(Student s1, Student s2) { return s1.name.compareTo(s2.name); } } static class Student implements Comparable<Student> { String name; int age; Student(String name, int age) { this.name = name; this.age = age; } public int compareTo(Student s) { return this.age - s.age; } public String toString() { return "[" + name + ", " + age + "]"; } } } A. [[Sarah, 34], [Jill, 50], [Peter, 65]] [[Sarah, 34], [Jill, 50], [Peter, 65]] B. [[Jill, 50], [Peter, 65], [Sarah, 34]] [[Jill, 50], [Peter, 65], [Sarah, 34]] C. [[Sarah, 34], [Jill, 50], [Peter, 65]] [[Jill, 50], [Peter, 65], [Sarah, 34]] D. [[Jill, 50], [Peter, 65], [Sarah, 34]] [[Sarah, 34], [Jill, 50], [Peter, 65]]

C.

Which of the following methods can be used to obtain a permit from a Semaphore s? A. get() B. ask() C. acquire() D. delete()

C.

Fill in the code to complete the following method for binary search. public static int recursiveBinarySearch(int[] list, int key) {int low = 0;int high = list.length - 1;return __________________________;}public static int recursiveBinarySearch(int[] list, int key,int low, int high) {if (low > high) // The list has been exhausted without a matchreturn -low - 1; // Return -insertion point - 1int mid = (low + high) / 2;if (key < list[mid])return recursiveBinarySearch(list, key, low, mid - 1);else if (key == list[mid])return mid;elsereturn recursiveBinarySearch(list, key, mid + 1, high);} A. recursiveBinarySearch(list, key) B. recursiveBinarySearch(list, key, low + 1, high - 1) C. recursiveBinarySearch(list, key, low - 1, high + 1) D. recursiveBinarySearch(list, key, low, high)

D.

The __________ method in the Queue interface retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty. A. poll() B. remove() C. peek() D. element()

D.

The methods for modifying element in the ___________ class are synchronized. A. ArrayList B. LinkedList C. TreeMap D. Vector E. HashSet

D.

To add a table column to a table view, use __________. A. tableView.add(tableColumn) B. tableView.getItems().add(tableColumn) C. tableView.getChildren().add(tableColumn) D. tableView.getColumns().add(tableColumn)

D.

To find a maximum object in an array of strings (e.g., String[] names = {"red", "green", "blue"}), use A. Arrays.max(names) B. Arrays.sort(names) C. Collections.max(names) D. Collections.max(Arrays.asList(names)) E. None of the above

D.

Which method on a condition should you invoke to wake all waiting threads? A. condition.wake(); B. condition.signal(); C. condition.wakeAll(); D. condition.signalAll();

D.

Which of the following declarations use raw type? A. ArrayList<Object> list = new ArrayList<>(); B. ArrayList<String> list = new ArrayList<>(); C. ArrayList<Integer> list = new ArrayList<>(); D. ArrayList list = new ArrayList();

D.

Which of the following expressions must be true if you create a thread using Thread = new Thread(object)? A. object instanceof Thread B. object instanceof Frame C. object instanceof Applet D. object instanceof Runnable

D.

Which of the following statements are true? A. The wait(), notify(), and notifyAll() methods must be invoked from a synchronized method or a synchronized block. B. When wait() is invoked, it pauses the thread and releases the lock on the object simultaneously. When the thread is restarted after being notified, the lock is automatically reacquired. C. The notify() method can wake only one waiting thread. D. An exception would occur if no thread is waiting on the object when the notify() method is invoked on the object.

D.

Analyze the following recursive method. public static long factorial(int n) { return n * factorial(n - 1); } A. Invoking factorial(0) returns 0. B. Invoking factorial(1) returns 1. C. Invoking factorial(2) returns 2. D. Invoking factorial(3) returns 6. E. The method runs infinitely and causes a StackOverflowError.

E.

Analyze the following two programs: A: public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int length) { if (length > 1) { System.out.print((length - 1) + " "); xMethod(length - 1); } } } B: public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int length) { while (length > 1) { System.out.print((length - 1) + " "); xMethod(length - 1); } } } A. The two programs produce the same output 5 4 3 2 1. B. The two programs produce the same output 1 2 3 4 5. C. The two programs produce the same output 4 3 2 1. D. The two programs produce the same output 1 2 3 4. E. Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.

E.

You can use the ________ method to force one thread to wait for another thread to finish. A. sleep(long milliseconds) B. yield() C. stop() D. suspend() E. join()

E.

The following program draws squares recursively. Fill in the missing code. import javax.swing.*; import java.awt.*; public class Test extends JApplet { public Test() { add(new SquarePanel()); } static class SquarePanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); int width = (int)(Math.min(getWidth(), getHeight()) * 0.4); int centerx = getWidth() / 2; int centery = getHeight() / 2; displaySquares(g, width, centerx, centery); } private static void displaySquares(Graphics g, int width, int centerx, int centery) { if (width >= 20) { g.drawRect(centerx - width, centery - width, 2 width, 2 width); displaySquares(_________, width - 20, centerx, centery); } } } } A. getGraphics() B. newGraphics() C. null D. g

d.


Set pelajaran terkait

Phrasal verbs, idioms and other expressions using 'get ' 1.2

View Set

Poetry Terms Quizlet- Match the term with its definition and example.

View Set

Electrical Level 2 Module 9 Grounding and Bonding

View Set

Espanol La Vocabularia de la technologia (bot567890)

View Set

Add and Subtract Fractions and Mixed Numbers with Like Denominators

View Set

Life Insurance Policy Provisions, Options, and Riders

View Set