CS3401 Quiz 1 - No answers yet will update
Question 1 (5
points)

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);
}
Question 1 options:
6
|
|
5
|
|
4
|
|
3
|
Save

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);
}
Question 2 options:
Save

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);
}
Question 3 options:
The
length of String a
|
|
the
length of String a concatenated with char b
|
|
the
number of times char b appears in String a
|
|
the
char which appears at location i in String a
|
Save

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);
}
Question 4 options:
an
array whose element type is Object
|
|
an
array whose element type is int
|
|
an
array whose element type is Integer
|
|
an
array whose element type is any superclass of Number
|
Save

True or False:
ArrayList<Comparable> is a subclass of ArrayList<E extends Date>
Question 5 options:
True
|
|
False
|
Save
Comments
Post a Comment