Java quiz 20 MCQS
Question 1(5 points)
[removed]
Method A invokes method A itself. This is called _________.
Question 1 options:
[removed] | indirect recursion |
[removed] | explicit recursion |
[removed] | direct recursion |
[removed] | one-step recursion |
Save
[removed]
What are the base cases in the following recursive method?
public static void recurse(int n)
{
if (n > 0)
{
System.out.print(n % 10);
recurse(n / 10);
}
}
Question 2 options:
[removed] | n < 0 |
[removed] | n <= 0 |
[removed] | n > 0 |
[removed] | no base cases |
Save
[removed]
What will be displayed by the method call recurse(1234)?
public static void recurse(int n)
{
if (n <= 0)
{
System.out.print(n % 10);
recurse(n / 10);
}
}
Question 3 options:
[removed] | 1234 |
[removed] | 4 3 2 1 |
[removed] | Nothing |
[removed] | 4321 |
Save
[removed]
Analyze the following recursive method and indicate which of the following will be true.
public static long factorial(int n)
{
return n * factorial(n - 1);
}
Question 4 options:
[removed] | Invoking factorial(2) returns 2. |
[removed] | Invoking factorial(1) returns 1. |
[removed] | The method runs infinitely and causes a StackOverflowError. |
[removed] | Invoking factorial(0) returns 0. |
Save
[removed]
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)
return 1;
else
return _____________;
}
Question 5 options:
[removed] | n * (n - 1) |
[removed] | n |
[removed] | n * factorial(n - 1) |
[removed] | factorial(n - 1) * (n - 1) |
Save
[removed]
What will be displayed by the method call recurse(6)?
public static intrecurse(int n)
{
if (n <= 1)
return 1;
else
return n + recurse(n - 2);
}
Question 6 options:
[removed] | 13 |
[removed] | 14 |
[removed] | 11 |
[removed] | 12 |
Save
[removed]
The base case _________ the recursion.
Question 7 options:
[removed] | stops |
[removed] | breaks |
[removed] | pauses |
[removed] | starts |
Save
[removed]
What is the output of the following program?
{
public static void main(String[] args)
{
System.out.println(countDown(2, 0));
}
public static intcountDown(int n, int result)
{
if (n == 0)
return 0;
else
return countDown(n - 1, n + result);
}
}
Question 8 options:
[removed] | 1 |
[removed] | 3 |
[removed] | 0 |
[removed] | 2 |
Save
[removed]
Which of the following statements about recursive methods is accurate?
Question 9 options:
[removed] | They must have exactly 1 base case and exactly 1 recursive case |
[removed] | They must have at least 1 base case and at least 1 recursive case |
[removed] | They must have exactly 1 base case and at least 1 recursive case |
[removed] | They must have at least 1 base case and exactly 1 recursive case |
Save
[removed]
Which of the following is a possible disadvantage of recursion?
Question 10 options:
[removed] | Recursive solutions can be less efficient than their iterative counterparts |
[removed] | Recursive solutions tend to have more local variables than their iterative counterparts |
[removed] | Recursive solutions tend to be longer than their iterative counterparts |
[removed] | Recursive solutions are more likely to go into infinite loops than their iterative counterparts |
Save
[removed]
To declare an interface named A with two generic types, use
Question 11 options:
[removed] | public interface A(E) { ... } |
[removed] | public interface A<E, F> { ... } |
[removed] | public interface A<E> { ... } |
[removed] | public interface A(E, F) { ... } |
Save
[removed]
Will the following code have a runtime error?
Comparable date = new Date();
inti = date.compareTo("time");
Question 12 options:
[removed] | Always |
[removed] | Never |
[removed] | Only when date contains an invalid date |
[removed] | Only when date is not the current date |
Save
[removed]
To create a list to store integers, use
Question 13 options:
[removed] | ArrayList<Object> list = new ArrayList<Integer>(); |
[removed] | ArrayList<Number> list = new ArrayList<Integer>(); |
[removed] | ArrayList<int> list = new ArrayList(); |
[removed] | ArrayList<Integer> list = new ArrayList(); |
Save
[removed]
To create a generic type bounded by Number, use
Question 14 options:
[removed] | <E extends Object> |
[removed] | <E extends Number> |
[removed] | <E> |
[removed] | <E extends Integer> |
Save
[removed]
Which of the following describes the benefit of using generic classes to implement collections?
Question 15 options:
[removed] | It eliminates the need to downcast objects when they are removed from a collection |
[removed] | It eliminates the need to upcast objects when they are inserted into a collection |
[removed] | It eliminates the need to downcast objects when they are inserted into a collection |
[removed] | It eliminates the need to upcast objects when they are removed from a collection |
Save
[removed]
Suppose list list1 is [1, 2, 5] and list list2 is [2, 3, 6]. After list1.addAll(list2), list2 is __________.
Question 16 options:
[removed] | [1, 5] |
[removed] | [1, 2, 3, 5, 6] |
[removed] | [2, 3, 6] |
[removed] | [1, 2, 2, 3, 5, 6] |
Save
[removed]
Suppose a list contains {red, red, red, red}. What is the list after the following code?
String element = "red";
for (inti = list.size() - 1; i>= 0; i--)
if (list.get(i).equals(element))
list.remove(element);
Question 17 options:
[removed] | [red] |
[removed] | [red, red] |
[removed] | [] |
[removed] | [red, red, red] |
Save
[removed]
Which of the following is correct to sort the elements in a list aList?
Question 18 options:
[removed] | Arrays.sort(aList) |
[removed] | new LinkedList<String>(new String[]{red, green, blue}) |
[removed] | Collections.sort(aList) |
[removed] | aList.sort() |
Save
[removed]
Which of the following best describes all objects of type List?
Question 19 options:
[removed] | They define an ordered collection that prohibits duplicates |
[removed] | They define an ordered collection that allows duplicates |
[removed] | They define an unordered collection that allows duplicates |
[removed] | They define an unordered collection that prohibits duplicates |
Save
[removed]
Which of the following problems would be a good candidate for using a stack?
Question 20 options:
[removed] | A print spooler that dispatches jobs based on shortest job first |
[removed] | An inventory system that processes product records by product number |
[removed] | A program that is designed to evaluate expressions |
[removed] | A task scheduler that schedules tasks in the order that they are received |
10 years ago
Purchase the answer to view it
- answer.docx