Exam
Which of the following are incorrect? A. An abstract class contains constructors. B. The constructors in an abstract class should be protected. C. The constructors in an abstract class are private. D. You may declare a final abstract class. E. An interface may contain constructors
C, D and E
Which of the following statements is correct? A. Generics can help detect type errors at compile time, thus make programs more robust. B. Generics can make programs easy to read. C. Generics can avoid cumbersome castings. D. Generics can make programs run faster
A, B and C
Suppose ArrayList<Double>list = new ArrayList<>(). Which of the following statements are correct? A. list.add(5.5); // 5.5 is automatically converted to new Double(5.5) B. list.add(3.0); // 3.0 is automatically converted to new Double(3.0) C. Double doubleObject = list.get(0); // No casting is needed D. double d = list.get(1); // Automatically converted to double
A, B, C and D
Suppose List list = new ArrayList(). 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, B, C and D
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
Suppose list1 is an ArrayList and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code: A: for (int i = 0; i < list1.size(); i++) sum += list1.get(i); B: for (int i = 0; i < list2.size(); i++) sum += list2.get(i); A. Code fragment A runs faster than code fragment B. B. Code fragment B runs faster than code fragment A. C. Code fragment A runs as fast as code fragment B
A
The iterator() method returns an instance of the __________ interface. A. Iterator B. Collection C. Iterable D. ArrayList
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 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 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
Assume Calendar calendar = new GregorianCalendar(). __________ returns the month of the year. A. calendar.get(Calendar.MONTH) B. calendar.get(Calendar.MONTH_OF_YEAR) C. calendar.get(Calendar.WEEK_OF_MONTH) D. calendar.get(Calendar.WEEK_OF_YEAR)
A
Analyze the following code. Which of the following statements is correct? public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } } A. The program has a compile error because an Integer instance cannot be assigned to a Number variable. B. The program has a compile error because intValue is an abstract method in Number. C. The program has a compile error because x does not have the compareTo method. D. The program compiles and runs fine
C
Fill in the code in Comparable______ c = new Date(); A. <String> B. <?> C. <Date> D. <E>
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
How many times is the recursive moveDisks method invoked for 4 disks? A. 5 B. 10 C. 15 D. 20
C
The iterator() method is defined in the __________ interface. A. Iterator B. Collection C. Iterable D. ArrayList
C
The java.lang.Number and its subclasses are introduced in Chapter 11. Analyze the following code. Number numberRef = new Integer(0); Double doubleRef = (Double)numberRef; Which of the following statements is correct? A. There is no such class named Integer. You should use the class Int. B. The compiler detects that numberRef is not an instance of Double. C. A runtime class casting exception occurs, since numberRef is not an instance of Double. D. The program runs fine, since Integer is a subclass of Double. E. You can convert an int to double, so you can cast an Integer instance to a Double instance
C
The relationship between an interface and the class that implements it is A. Composition B. Aggregation C. Inheritance D. None
C
What is the best suitable relationship between Employee and Faculty? A. Composition B. Aggregation C. Inheritance D. None
C
What is the return value for xMethod(4) after calling the following method? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } A. 12 B. 11 C. 10 D. 9
C
Which of the following class definitions defines a legal abstract class? A. class A { abstract void unfinished() { } } B. class A { abstract void unfinished(); } C. abstract class A { abstract void unfinished(); } D. public class abstract A { abstract void unfinished(); }
C
Which of the following statements is false? A. If you compile an interface without errors, a .class file is created for the interface. B. If you compile a class without errors but with warnings, a .class file is created. C. If you compile a class with errors, a .class file is created for the class. D. If you compile an interface without errors, but with warnings, a .class file is created for the interface
C
You can use a for-each loop to traverse all elements in a container object that implements _____. A. Iterator B. Collection C. Iterable D. ArrayList
C
Fill in the code to complete the following method for computing a Fibonacci number. public static long fib(long index) { if (index == 0) // Base case return 0; else if (index == 1) // Base case return 1; else // Reduction and recursive calls return __________________; } A. fib(index - 1) B. fib(index - 2) C. fib(index - 1) + fib(index - 2) D. fib(index - 2) + fib(index - 1)
C and D
Fill in the code to complete the following method for computing factorial. /** Return the factorial for a specified index */ public static long factorial(int n) { if (n == 0) // Base case return 1; else return _____________; // Recursive call } A. n * (n - 1) B. n C. n * factorial(n - 1) D. factorial(n - 1) * n
C and D
The Rational class in this chapter is defined as a subclass of java.lang.Number. Which of the following expressions is correct? A. Rational.doubleValue(); B. Rational.doubleValue("5/4"); C. new Rational(5, 4).doubleValue(); D. new Rational(5, 4).toDoubleValue(); E. new Rational(5, 4).intValue();
C and E
Does <? super Number> represent a superclass of Number? A. Yes B. No
A
Which of the following statements is incorrect? A. Integer i = 4.5; B. Double i = 4.5; C. Object i = 4.5; D. Number i = 4.5;
A
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
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
Is ArrayList<?> same as ArrayList<? extends Object>? A. Yes B. No
A
Is ArrayList<Integer> a subclass of ArrayList<? extends Number>? A. Yes B. No
A
Is ArrayList<Integer> a subclass of ArrayList<?>? A. Yes B. No
A
Is ArrayList<Number> a subclass of ArrayList<? extends Number>? 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
Which of the following statements are true? A. The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series. B. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series. C. The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series. D. The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.
A
Which of the following statements regarding abstract methods is false? A. An abstract class can have instances created using the constructor of the abstract class. B. An abstract class can be extended. C. A subclass of a non-abstract superclass can be abstract. D. A subclass can override a concrete method in a superclass to declare it abstract. E. An abstract class can be used as a data type
A
Which of the following methods are in the Collection interface? A. remove(o: Object): boolean B. removeAll(c: Collection<?>): boolean C. delete(o: E): boolean D. deleteAll(c: Collection<?>): boolean
A and B
Which of the following statements is correct? A. Comparable<String> c = new String("abc"); B. Comparable<String> c = "abc"; C. Comparable<String> c = new Date(); D. Comparable<Object> c = new Date();
A and B
Which of the following statements are true? A. An ArrayList can grow automatically. B. An ArrayList can shrink automatically. C. You can reduce the capacity of an ArrayList by invoking the trimToSize() method on the list. D. You can reduce the capacity of a LinkedList by invoking the trimToSize() method on the list
A and C
Which of the following statements are true? A. A class should describe a single entity and all the class operations should logically fit together to support a coherent purpose. B. A class should always contain a no-arg constructor. C. The constructors must always be public. D. The constructors may be protected
A and D
Which of the following methods are in the Collection interface? A. clear() B. isEmpty() C. size() D. getSize()
A, B and C
Which of the following statements are true? A. Every recursive method must have a base case or a stopping condition. B. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. C. Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case. D. Every recursive method must have a return value. E. A recursive method is invoked differently from a non-recursive method.
A, B and C
The GeometricObject and Circle classes are defined in this chapter. Analyze the following code. Which statements are correct? public class Test { public static void main(String[] args) { GeometricObject x = new Circle(3); GeometricObject y = (Circle)(x.clone()); System.out.println(x); System.out.println(y); } } A. The program has a compile error because the clone() method is protected in the Object class. B. After you override the clone() method and make it public in the Circle class, the problem can compile and run just fine, but y is null if Circle does not implement the Cloneable interface. C. To enable a Circle object to be cloned, the Circle class has to override the clone() method and implement the java.lang.Cloneable interface. D. If GeometricObject implements Cloneable and Circle overrides the clone() method, the clone() method will work fine to clone Circle objects
A, B, C and D
Which of the following are true? A. You can insert an element anywhere in an arraylist. B. You can insert an element anywhere in a linked list. C. You can use a linked list to improve efficiency for adding and removing elements at the beginning of a list. D. You should use an array list if your application does not require adding and removing elements at the beginning of a list
A, B, C and D
Which of the following is poor design? A. A data field is derived from other data fields in the same class. B. A method must be invoked after/before invoking another method in the same class. C. A method is an instance method, but it does not reference any instance data fields or invoke instance methods. D. A parameter is passed from a constructor to initialize a static data field
A, B, C and D
Which of the following methods are in the Collection interface? A. add(o: E) B. addAll(c: Collection<? extends E>) C. contains(o: Object): boolean D. containsAll(c: Collection<?>): boolean
A, B, C and D
Which of the following statements are true? A. The String class implements Comparable. B. The Date class implements Comparable. C. The Double class implements Comparable. D. The BigInteger class implements Comparable
A, B, C and D
Which of the following methods are in java.util.List? A. add(int index, E element) B. get(int index) C. set(int index, E element) D. remove(int index) E. subList(int fromIndex, int toIndex)
A, B, C, D and E
Which of the following statements are true? A. Generic type information is present at compile time. B. Generic type information is not present at runtime. C. You cannot create an instance using a generic class type parameter. D. You cannot create an array using a generic class type parameter. E. You cannot create an array using a generic class
A, B, C, D and E
Which of the following statements are true? A. Inheritance models the is-a relationship between two classes. B. A strong is-a relationship describes a direct inheritance relationship between two classes. C. A weak is-a relationship describes that a class has certain properties. D. A strong is-a relationship can be represented using class inheritance. E. A weak is-a relationship can be represented using interfaces
A, B, C, D and E
Which of the following statements are true? A. The Collection interface is the root interface for manipulating a collection of objects. B. The Collection interface provides the basic operations for adding and removing elements in a collection. C. The AbstractCollection class is a convenience class that provides partial implementation for the Collection interface. D. Some of the methods in the Collection interface cannot be implemented in the concrete subclass. In this case, the method would throw java.lang.UnsupportedOperationException, a subclass of RuntimeException. E. All interfaces and classes in the Collections framework are declared using generic type since JDK 1.5
A, B, C, D and E
Which of the following statements are true? A. java.util.List inherits all the methods from java.util.Collection. Additionally, it contains new methods for manipulating a list. B. The AbstractList class provides a partial implementation for the List interface. C. ArrayList is a concrete implementation of List using an array. D. LinkedList is a concrete implementation of List using a linked list. LinkedList contains all the methods in List and additional new methods for manipulating a linked list. E. ListIterator is a subinterface of Iterator and it provides the methods to support bi-directional traversal of a list
A, B, C, D and E
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
Analyze the following functions; public class Test1 { public static void main(String[] args) { System.out.println(f1(3)); System.out.println(f2(3, 0)); } public static int f1(int n) { if (n == 0) return 0; else { return n + f1(n - 1); } } public static int f2(int n, int result) { if (n == 0) return result; else return f2(n - 1, n + result); } } A. f1 is tail recursion, but f2 is not B. f2 is tail recursion, but f1 is not C. f1 and f2 are both tail recursive D. Neither f1 nor f2 is tail recursive
B
ArrayList<String> and ArrayList<Integer> are two types. Does the JVM load two classes ArrayList<String> and ArrayList<Integer>? A. Yes B. No
B
Fill in the most appropriate code in the blanks in the MyInt class? public class MyInt implements _______ { int id; public MyInt(int id) { this.id = id; } public String toString() { return String.valueOf(id); } public int compareTo(_______ arg0) { if (id > arg0.id) return 1; else if (id < arg0.id) return -1; else return 0; } } A. Comparable / Object B. Comparable<MyInt> / MyInt C. Comparable<MyInt> / Object D. Comparable / MyInt
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
If E is a generic type for a class, can E be referenced from a static method? A. Yes B. No
B
If you use the javac ?Xlint:unchecked command to compile a program that contains raw type, what would the compiler do? A. report compile error B. report warning and generate a class file C. report warning without generating a class file D. no error and generate a class file
B
Is ArrayList<Integer> a subclass of ArrayList<Object>? A. Yes B. No
B
Suppose list is a LinkedList that contains 1 million int values. Analyze the following code: A: for (int i = 0; i < list.size(); i++) sum += list.get(i); B: for (int i: list) sum += i; A. Code fragment A runs faster than code fragment B. B. Code fragment B runs faster than code fragment A. C. Code fragment A runs as fast as code fragment B
B
The java.util.Calendar and java.util.GregorianCalendar classes are introduced in Chapter 11. Analyze the following code. Which of the following statements is correct? 1. import java.util.*; 2. public class Test { 3. public static void main(String[] args) { 4. Calendar[] calendars = new Calendar[10]; 5. calendars[0] = new Calendar(); 6. calendars[1] = new GregorianCalendar(); 7. } 8. } A. The program has a compile error on Line 4 because java.util.Calendar is an abstract class. B. The program has a compile error on Line 5 because java.util.Calendar is an abstract class. C. The program has a compile error on Line 6 because Calendar[1] is not of a GregorianCalendar type. D. The program has no compile errors
B
To create a list to store integers, use A. ArrayList<Object> list = new ArrayList<>(); B. ArrayList<Integer> list = new ArrayList<>(); C. ArrayList<int> list = new ArrayList<int>(); D. ArrayList<Number> list = new ArrayList<>();
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
What are the base cases in the following recursive method? public static void xMethod(int n) { if (n > 0) { System.out.print(n % 10); xMethod(n / 10); } } A. n > 0 B. n <= 0 C. no base cases D. n < 0
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
Which of the following declares an abstract method in an abstract Java class? A. public abstract method(); B. public abstract void method(); C. public void abstract method(); D. public void method() {} E. public abstract void method() {}
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 YYYYYYYY { while (!stack1.isEmpty()) stack2.push(stack1.pop()); } } A. add(GenericStack<T> stack1, GenericStack<T> stack2) B. add(GenericStack<? extends T> stack1, GenericStack<T> stack2) C. add(GenericStack<T> stack1, GenericStack<? super T> stack2) D. add(GenericStack<T> stack1, GenericStack<Object> stack2)
B and C
Analyze the following code: public class Test1 { public Object max(Object o1, Object o2) { if ((Comparable)o1.compareTo(o2) >= 0) { return o1; } else { return o2; } } } A. The program has a compile error because Test1 does not have a main method. B. The program has a compile error because o1 is an Object instance and it does not have the compareTo method. C. The program has a compile error because you cannot cast an Object instance o1 into Comparable. D. The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0)
B and D
Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct? A. A a = new A(); B. A a = new B(); C. B b = new A(); D. B b = new B();
B and D
Suppose A is an interface, B is a concrete class with a no-arg constructor that implements A. Which of the following is correct? A. A a = new A(); B. A a = new B(); C. B b = new A(); D. B b = new B();
B and D
Which of the following statements are true? A. Recursive methods run faster than non-recursive methods. B. Recursive methods usually take more memory space than non-recursive methods. C. A recursive method can always be replaced by a non-recursive method. D. In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve
B, C and D
Analyze the following code. Number[] numberArray = new Integer[2]; numberArray[0] = new Double(1.5); Which of the following statements is correct? A. You cannot use Number as a data type since it is an abstract class. B. Since each element of numberArray is of the Number type, you cannot assign an Integer object to it. C. Since each element of numberArray is of the Number type, you cannot assign a Double object to it. D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it
D
Analyze the following code. 1. public class Test { 2. public static void main(String[] args) { 3. Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)}; 4. java.util.Arrays.sort(fruits); 5. } 6. } class Fruit { private double weight; public Fruit(double weight) { this.weight = weight; } } A. The program has a compile error because the Fruit class does not have a no-arg constructor. B. The program has a runtime error on Line 3 because the Fruit class does not have a no-arg constructor. C. The program has a compile error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable. D. The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable
D
Analyze the following code. public class Test { public static void main(String[] args) { java.util.Date x = new java.util.Date(); java.util.Date y = x.clone(); System.out.println(x = y); } } A. A java.util.Date object is not cloneable. B. x = y in System.out.println(x = y) causes a compile error because you cannot have an assignment statement inside a statement. C. x = y in System.out.println(x = y) causes a runtime error because you cannot have an assignment statement inside a statement. D. The program has a compile error because the return type of the clone() method is java.lang.Object
D
Analyze the following code. Which of the following statements is correct? public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println((Integer)x.compareTo(new Integer(4))); } } A. The program has a compile error because an Integer instance cannot be assigned to a Number variable. B. The program has a compile error because intValue is an abstract method in Number. C. The program has a compile error because x cannot be cast into Integer. D. The program has a compile error because the member access operator (.) is executed before the casting operator. E. The program compiles and runs fine
D
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; xMethod(x, 5); } public static void xMethod(int[] x, int length) { System.out.print(" " + x[length - 1]); xMethod(x, length - 1); } } A. The program displays 1 2 3 4 6. B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. C. The program displays 5 4 3 2 1. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException
D
Assume Calendar calendar = new GregorianCalendar(). __________ returns the week of the year. A. calendar.get(Calendar.MONTH) B. calendar.get(Calendar.MONTH_OF_YEAR) C. calendar.get(Calendar.WEEK_OF_MONTH) D. calendar.get(Calendar.WEEK_OF_YEAR)
D
Assume an employee can work for only one company. What is the best suitable relationship between Company and Employee? A. None B. Aggregation C. Inheritance D. Composition
D
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 match return -low - 1; // Return -insertion point - 1 int mid = (low + high) / 2; if (key < list[mid]) return recursiveBinarySearch(list, key, low, mid - 1); else if (key == list[mid]) return mid; else return 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
How many times is the factorial method in Listing 18.1 invoked for factorial(5)? A. 3 B. 4 C. 5 D. 6
D
Show the output of running the class Test in the following code lines: interface A { } class C { } class B extends D implements A { } public class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); if (b instanceof C) System.out.println("b is an instance of C"); } } class D extends C { } A. Nothing. B. b is an instance of A. C. b is an instance of C. D. b is an instance of A followed by b is an instance of C
D
The Rational class in this chapter extends java.lang.Number and implements java.lang.Comparable. Analyze the following code. 1. public class Test { 2. public static void main(String[] args) { 3. Number[] numbers = {new Rational(1, 2), new Integer(4), new Double(5.6)}; 4. java.util.Arrays.sort(numbers); 5. } 6. } A. The program has a compile error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it. B. The program has a runtime error because numbers is declared as Number[], so you cannot assign {new Rational(1, 2), new Integer(4), new Double(5.6)} to it. C. The program has a compile error because numbers is declared as Number[], so you cannot pass it to Arrays.sort(Object[]). D. The program has a runtime error because the compareTo methods in Rational, Integer, and Double classes do not compare the value of one type with a value of another type
D
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
The output from the following code is __________. java.util.ArrayList<String> list = new java.util.ArrayList<String>(); list.add("New York"); java.util.ArrayList<String> list1 = (java.util.ArrayList<String>)(list.clone()); list.add("Atlanta"); list1.add("Dallas"); System.out.println(list1); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]
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 is a correct interface? A. interface A { void print() { }; } B. abstract interface A { print(); } C. abstract interface A { abstract void print() { };} D. interface A { void print();}
D
______ is not a reference type. A. A class type B. An interface type C. An array type D. A primitive type
D
The method header is left blank in the following code. Fill in the header. public class GenericMethodDemo { public static void main(String[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York", "Austin"}; print(integers); print(strings); } __________________________________________ { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } } A. public static void print(Integer[] list) B. public static void print(String[] list) C. public static void print(int[] list) D. public static void print(Object[] list) E. public static <E> void print(E[] list)
D and E
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
Assume Calendar calendar = new GregorianCalendar(). __________ returns the number of days in a month. A. calendar.get(Calendar.MONTH) B. calendar.get(Calendar.MONTH_OF_YEAR) C. calendar.get(Calendar.WEEK_OF_MONTH) D. calendar.get(Calendar.WEEK_OF_YEAR) E. calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
E
Fill in the code to complete the following method for checking whether a string is a palindrome. public static boolean isPalindrome(String s) { return isPalindrome(s, 0, s.length() - 1); } public static boolean isPalindrome(String s, int low, int high) { if (high <= low) // Base case return true; else if (s.charAt(low) != s.charAt(high)) // Base case return false; else return _______________________________; } A. isPalindrome(s) B. isPalindrome(s, low, high) C. isPalindrome(s, low + 1, high) D. isPalindrome(s, low, high - 1) E. isPalindrome(s, low + 1, high - 1)
E
If you use the javac command to compile a program that contains raw type, what would the compiler do? A. report syntax error B. report warning and generate a class file C. report warning without generating a class file D. no error and generate a class file E. report warning and generate a class file if no other errors in the program.
E
What is the output of running class Test? public class Test { public static void main(String[] args) { new Circle9(); } } public abstract class GeometricObject { protected GeometricObject() { System.out.print("A"); } protected GeometricObject(String color, boolean filled) { System.out.print("B"); } } public class Circle9 extends GeometricObject { /** No-arg constructor */ public Circle9() { this(1.0); System.out.print("C"); } /** Construct circle with a specified radius */ public Circle9(double radius) { this(radius, "white", false); System.out.print("D"); } /** Construct a circle with specified radius, filled, and color */ public Circle9(double radius, String color, boolean filled) { super(color, filled); System.out.print("E"); } } A. ABCD B. BACD C. CBAE D. AEDC E. BEDC
E
Which of the following statements regarding abstract methods is false? A. Abstract classes have constructors. B. A class that contains abstract methods must be abstract. C. It is possible to declare an abstract class that contains no abstract methods. D. An abstract method cannot be contained in a nonabstract class. E. A data field can be declared abstract
E