public class Vehicles{
public void drive(){
System.out.print(“I can drive!!”);}}
public class Cars extends Vehicles {
public void drive(){
System.out.print(“I can drive a car!);
}
}
public class Toyota extends Cars{
}
public class Sienna extends Toyota{
}
The following code segment appears in another class
rob.drive();
Under which of the following conditions will the code segment print “I can drive a car!”
a) If rob is an instance of Cars or Toyota
b) If rob is an instance of Cars, Toyota, or Sienna
c) If rob is an instance of Vehicles
d) If rob is an instance of Vehicles, Cars, Toyota or Sienna
b
In order to use the binary search algorithm, the data must be _____.
Sorted
A recursive function will always produce a correct result if the base case is not defined explicitly but is instead assumed to be handled by the recursive calls themselves.
False
In Java, constructors are inherited.
False
A recursive method must have a ____ ____, which stops the recursion from infinitely continuing.
base case
public static int mystery(int n) {
if (n <= 0) {
return 1;
}
return n * mystery(n - 2);
}
What happens if mystery(-1) is called?
returns 1
Consider the following classes:
public class Vehicle {
public Vehicle(int wheels, int speed) { /* ... */ }
}
public class Car extends Vehicle {
public Car(String` model, int wheels, int speed) { /* ... */ }
}
And the following declaration:
Vehicle[] garage = new Vehicle[3];
garage[0] = new Vehicle(4, 120);
garage[1] = new Car("Sedan", 4, 150);
Car sportsCar = new Vehicle(2, 200);
garage[2] = sportsCar;
Why does this code not compile?
A Car cannot reference a Vehicle.
What is the function/purpose of this code?
public static int recursionUnit(int n) {
If (n == 0) {
return 0;
}
return (n % 10) + recursionUnit (n / 10);
}
a) It returns the sum of digits of n
b) It returns the number of digits in n
c) It returns the binary length of n
d) It returns the factorial of n
a
/** Precondition: a > 0 */
public static void something(int a)
{
System.out.print(a + “*”);
if (a>1)
{
something(a-1);
}
}
Which of the following options best describes the output produced by the method call, something(num)?
a) Prints all integers from 1 to num followed by an asterisk, with no spaces and all on the same line
b) Prints all integers from num down to 1 followed by an asterisk, with no spaces and all on the same line
c) Prints all integers from 2 to num followed by an asterisk, with no spaces and all on the same line
d) Prints all integers from num down to 2 followed by an asterisk, with no spaces and all on the same line
b