CS3401 Quiz 1
1) How many times is the following
factorial method invoked for complicatedSum(6)?
public int complicatedSum(int n) { if (n <= 3) return n; else if (n % 3 == 0) return n * 3 + complicatedSum(n-1); else if (n % 3 == 2) return n * 2 + complicatedSum(n-1); else return n + complicatedSum(n-1); }
Answer: 4
2) What does recursiveString("abcd")
return if that method is defined by the following?
public String
recursiveString(String str) {
if (str.length() < 2)
return str;
return recursiveString(str.substring(1)) +
str.substring(str.length()-1);
}
if (str.length() < 2)
return str;
return recursiveString(str.substring(1)) +
str.substring(str.length()-1);
}
Answer: dddd
3) What does the following method
compute? Assume the method is called initially with i = 0.
public int recursiveEx2(String a, char b, int i) { if (i = = a.length( )) return 0; if (b = = a.charAt(i)) return recursiveEx2(a, b, i+1) + 1; return recursiveEx2(a, b, i+1); }
Answer: the number of times char b appears in String a
4)
What type of argument can the following generic method be passed?
public static <E extends Number> void displayArray(E[] array) {
for (E element : array)
System.out.println(element);
}
Answer: an array whose element type is Integer
5) True or False:
ArrayList<Comparable> is a subclass of ArrayList<E extends
Date>
Answer: False
Comments
Post a Comment