Java Exam III
Which of the following statements are true?
The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
Analyze the following recursive method.public static long factorial(int n) {return n * factorial(n - 1);}
The method runs infinitely and causes a StackOverflowError.
Analyze the following code.import java.util.*;public class Test {public static void main(String[] args) throws Exception {Set<String> set = new TreeSet<String>();set.add("Red");set.add("Green");set.add("Blue");System.out.println(set.first());}}
The program cannot compile, because the first() method is not defined in Set.
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);}}
The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
Analyze the following code.import java.util.*;public class Test {public static void main(String[] args) throws Exception {TreeSet<String> set = new TreeSet<String>();set.add("Red");set.add("Yellow");set.add("Green");set.add("Blue");SortedSet temp = set.headSet("Purple");System.out.println(temp.first());}}
The program displays Blue
Analyze the following code.import java.util.*;public class Test {public static void main(String[] args) throws Exception {TreeSet<String> set = new TreeSet<String>();set.add("Red");set.add("Green");set.add("Blue");System.out.println(set.last());}}
The program displays Red
Analyze the following code.import java.util.*;public class Test {public static void main(String[] args) throws Exception {TreeSet<String> set = new TreeSet<String>();set.add("Red");set.add("Yellow");set.add("Green");set.add("Blue");SortedSet temp = set.tailSet("Purple");System.out.println(temp.first());}}
The program displays Red
Analyze the following code:import java.util.*;public class Test {public static void main(String[] args) {HashSet<String> set1 = new HashSet<String>();set1.add("red");Set set2 = set1.clone();}}
The program will be fine if set1.clone() is replaced by (HashSet)(set1.clone()) Line 5 has a compile error because set1.clone() returns an Object. You have to cast it to Set in order to compile it. The program will be fine if set1.clone() is replaced by (Set)(set1.clone())
Analyze the following code:import java.util.*;public class Test {public static void main(String[] args) {Set<String> set1 = new HashSet<String>();set1.add("red");Set set2 = set1.clone();}}
The program will be fine if set1.clone() is replaced by (Set)((HashSet)set1).clone() The program will be fine if set1.clone() is replaced by (HashSet)((HashSet)set1).clone() Line 5 is wrong because the declared type for set1 is Set and the clone method is not defined Set.
Which of the data types below could be used to store elements in their natural order based on the compareTo method?
TreeSet
The elements in ________ are sorted.
TreeSet TreeMap
A heap is represented using an array. Is the array {64 42 59 32 39 44} a heap?
Yes
What is the output for the following code?import java.util.*;public class Test {public static void main(String[] args) {Set<A> set = new HashSet<A>();set.add(new A());set.add(new A());set.add(new A());set.add(new A());System.out.println(set);}}class A {int r = 1;public String toString() {return r + "";}public boolean equals(Object o) {return this.r == ((A)o).r;}}
[1, 1, 1, 1]
What is the output for the following code?import java.util.*;public class Test {public static void main(String[] args) {Set<A> set = new HashSet<A>();set.add(new A());set.add(new A());set.add(new A());set.add(new A());System.out.println(set);}}class A {int r = 1;public String toString() {return r + "";}public int hashCode() {return r;}}
[1, 1, 1, 1]
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s1 is ________.
[1, 2, 3, 5, 6]
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.removeAll(s2), s1 is ________.
[1, 5]
What is the output for the following code?import java.util.*;public class Test {public static void main(String[] args) {Set<A> set = new HashSet<A>();set.add(new A());set.add(new A());set.add(new A());set.add(new A());System.out.println(set);}}class A {int r = 1;public String toString() {return r + "";}public boolean equals(Object o) {return this.r == ((A)o).r;}public int hashCode() {return r;}}
[1]
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s2 is ________.
[2, 3, 6]
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.retainAll(s2), s1 is ________.
[2]
The printout of the following code is ________.LinkedHashSet<String> set1 = new LinkedHashSet<String>();set1.add("New York");LinkedHashSet<String> set2 = set1;set1.add("Atlanta");set2.add("Dallas");System.out.println(set2);
[New York, Atlanta, Dallas]
The printout of the following code is ________.LinkedHashSet<String> set1 = new LinkedHashSet<String>();set1.add("New York");LinkedHashSet<String> set2 = (LinkedHashSet<String>)(set1.clone());set1.add("Atlanta");set2.add("Dallas");System.out.println(set2);
[New York, Dallas]
To empty a Collection or a Map, you use the ________ method.
clear
Method A invokes method A itself. This is called ________.
direct recurion
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 casereturn 1;elsereturn ________; // Recursive call}
factorial(n - 1) * n n * factorial(n - 1)
All the update methods in the Java Collection framework are synchronized.
false
The elements in HashSet are ordered.
false
Two objects are equal if their hashCodes are the same.
false
You can use index to traverse elements in a set.
false
Fill in the code to complete the following method for computing a Fibonacci number.public static long fib(long index) {if (index == 0) // Base casereturn 0;else if (index == 1) // Base casereturn 1;else // Reduction and recursive callsreturn ________;}
fib(index - 1) + fib(index - 2) fib(index - 2) + fib(index - 1)
Which of the following are correct methods in Map?
get(Object key) put(Object key, Object value)
Method A invokes method B, which in turn invokes function C. This is called ________.
indirect recursion
What is the return type value for the hashCode() method?
int
Which of the following are correct methods in Map?
isEmpty() containsKey(Object key) containsValue(Object value) remove(Object key)
To get an iterator from a set, you may use the ________ method.
iterator
When a collision occurs during the insertion of an entry to a hash table, ________ finds the next available location sequentially.
linear probing
A hashing function ________.
maps a key to an index in the hash table
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);}}
n <= 0
In the following method, what is the base case? static int xMethod(int n) { if (n == 1)return 1;elsereturn n + xMethod(n - 1);}
n is 1
The ________ algorithm does not compare keys.
radix sort
The most efficient algorithm for sorting integer keys is ________.
radix sort
Fill in the code to complete the following function for computing factorial.public static long factorial(int n) {if (n == 0) // Base case________elsereturn n * factorial(n - 1); // Recursive call}
return 1;
Fill in the code to complete the following function for computing a Fibonacci number.public static int fib(int index) {if (index == 0 || index == 1) // Base case________else // Reduction and recursive callsreturn fib(index - 1) + fib(index - 2);}
return index
Which of the following is correct to perform the set union of two sets s1 and s2?
s1.addAll(s2)
Which of the following is correct to perform the set difference of two sets s1 and s2?
s1.removeAll(s2)
Which of the following is correct to perform the set intersection of two sets s1 and s2?
s1.retainAll(s2)
A list is sorted by selecting the largest element in the list and swapping it with the last one. This technique is called ________.
selection sort
The ________ places all entries with the same hash index into the same location, rather than finding new locations.
separate chaining scheme
Suppose set1 is ["Atlanta", "Macon"] and set2 is ["Atlanta", "Macon", "Savannah"], which of the following returns true?
set2.contains("Savannah")
The base case ________ the recursion.
stops
To add a new node, you need to start a process by first placing it as ________ and move it up to maintain the heap property.
the last node in the heap
To remove the root, you need to start a process by first placing ________ to the place of the root and move it down to maintain the heap property.
the last node in the heap
A recursive method can always be converted into a nonrecursive method using iterations.
true
If two strings are equal, the two strings have the same hashCodes.
true
The classes HastSet, LinkedHashSet, TreeSet, ArrayList, Vector, Stack, LinkedList, HashMap, LinkedHashMap, and TreeMap are cloneable and serializable.
true
The keys must be unique for Map.
true
The union, difference, and intersection apply to set as well as list, because the methods addAll, removeAll, and retainAll are defined in the Collection interface.
true
Two objects have the same hashCodes if they are equal.
true
If two objects o1 and o2 are equal, what are the values for o1.equals(o2) and o1.hashCode() == o2.hashCode()?
true true
What is the output of the following code?import java.util.*;import java.util.*;public class Test {public static void main(String[] args) {Set<String> set1 = new HashSet<>();set1.add("Atlanta");set1.add("Macon");set1.add("Savanna");Set<String> set2 = new HashSet<>();set2.add("Atlanta");set2.add("Macon");set2.add("Savanna");Set<String> set3 = new HashSet<>();set3.add("Macon");set3.add("Savanna");set3.add("Atlanta");System.out.println(set1.equals(set2) + " " + set1.equals(set3));}}
true trues1
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 casereturn true;else if ________return false;elsereturn isPalindrome(s.substring(1, s.length() - 1));}
(s.charAt(0) != s.charAt(s.length() - 1)) // Base case
The Collection interface is the base interface for ________.
ArrayList Set List LinkedList
Which of the following statements are true?
Each node is greater than or equal to any of its children. A heap is a complete binary tree. A binary tree is complete if every level of the tree is full except that the last level may not be full and all the leaves on the last level are placed left-most.
Which of the following statements are true?
Every recursive method must have a base case or a stopping condition. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case.
Suppose your program frequently tests whether a student is in a soccer team and also need to know the student's information such as phone number, address, and age, what is the best data structure to store the students in a soccer team?
HashMap
Suppose you write a program that reads numbers and displays the distinct numbers. The order of the number does not matter. The best data structure for storing the numbers in the program is ________.
HashSet
Suppose your program frequently tests whether a student is in a soccer team, what is the best data structure to store the students in a soccer team?
HashSet
Which of the following statements are true?
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. A recursive method can always be replaced by a non-recursive method. Recursive methods usually take more memory space than non-recursive methods.
The Map is the base interface for ________.
LinkedHashMap HashMap TreeMap
If you want to store non-duplicated objects in the order in which they are inserted, you should use ________.
LinkedHashSet
Suppose you write a program that reads numbers and displays the distinct numbers in their input order. The best data structure for storing the numbers in the program is ________.
LinkedHashSet
Which of the following data types have iterators?
LinkedHashSet ArrayList HashSet TreeSet LinkedList
________ measures how full the hash table is.
Load factor
Which of the following data types does not implement the Collection interface?
Map
A heap is represented using an array. Is the array {1 2 4 5 9 3} a heap?
No
What is the printout of invoking xfunction(1234)?public static void xfunction(n) {if (n <= 0) {System.out.print(n % 10 + " ");xfunction(n / 10);}}
Nothing
The best-time complexity for bubble sort is ________.
O(n)
The time to merge two sorted lists of size n is ________.
O(n)
The worst-time complexity for bubble sort is ________.
O(n*n)
The worst-time complexity for quick sort is ________.
O(n*n)
The average-time complexity for heap sort is ________.
O(nlogn)
The average-time complexity for merge sort is ________.
O(nlogn)
The average-time complexity for quick sort is ________.
O(nlogn)
The worst-time complexity for heap sort is ________.
O(nlogn)
The worst-time complexity for merge sort is ________.
O(nlogn)
What is correct about a pivot?
A pivot can be chosen arbitrarily. A pivot divides a list into two sublists, the elements in the first list are no larger than the pivot and the elements in the second list are larger than the pivot.
Analyze the following code:public class Test {public static void main(String[] args) {Map<String, String> map = new HashMap<String, String>();map.put("123", "John Smith");map.put("111", "George Smith");map.put("123", "Steve Yao");map.put("222", "Steve Yao");}}
After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao".
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;elsereturn f2(n - 1, n + result);}}
0
What is the printout of invoking xfunction(6)?public static int xfunction(int n) {if (n >= 1)return 1;elsereturn n + xfunction(n - 2);}
1
What is the return value for xMethod(4) after calling the following method?static int xMethod(int n) {if (n == 1)return 1;elsereturn n + xMethod(n - 1);}
10
Consider the following recursive method.public static int m(int value) {if (value >= 0)return 5 * m(value - 2);elsereturn 1;}What value is returned when invoking m(5)?
125
What is the printout of invoking xfunction(6)?public static int xfunction(int n) {if (n <= 1)return 1;elsereturn n + xfunction(n - 2);}
13
How many times is the recursive moveDisks method invoked for 4 disks?
15
Suppose a list is {2, 9, 5, 4, 8, 1}. After the first pass of bubble sort, the list becomes ________.
2, 5, 4, 8, 1, 9
For an Integer object with value 20, what is its hashCode?
20
Suppose you choose the first element as a pivot in the list {5 2 9 3 8 4 0 1 6 7}. Using the partition algorithm in the book, what is the new list after the partition?
4 2 1 3 0 5 8 9 6 7
What is the printout of invoking xfunction(1234)?public static void xfunction(int n) {if (n > 0) {System.out.print(n % 10 + " ");xfunction(n / 10);}}
4 3 2 1
How many times is the recursive moveDisks method invoked for 3 disks?
7