Category 1
Category 2
Category 3
100

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

100

public class Book {

    private String title;

    public Book(String title) {

        // what goes here?

    }

}

A. this.title = title;

B. title = this.title;

C. title = title;

D. this(title) = title;

A

100

/** 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

200

public class Vehicle {

    public Vehicle(int wheels, int speed) { /* ... */ }

}

public class Car extends Vehicle {

    public Car(String` model, int wheels, int speed) { /* ... */ }  

}

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;

Name the 3 errors here

(1) Syntax error with backtick, (2) Cannot assign Vehicle type to Car, (3) Car must explicitly call super(wheels, speed) because Vehicle does not have a no-argument constructor

200

import java.util.*;

public class Test {

    public static void main(String[] args) {

        ArrayList<Integer> list = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {

            list.add(i);

        }  

        for (int i = list.size() - 1; i >= 0; i -= 2) {

            list.remove(i);

        }

        System.out.println(list);

    }}

What is the output?

A) [1, 2, 3]
B) [1, 3, 5]
C) [2, 4]
D) [1, 3]

C) [2, 4]

200

public class Shape {

    public Shape(int sides) {}

}

public class Square extends Shape {

    public Square() {

        System.out.println("Square created");

    }

}

What is wrong?

A. Shape cannot be extended because it has a constructor

B. Square must call super(sides) in its constructor

C. System.out.println cannot be used in constructors

D. Square must override toString()

B. Square must call super(sides) in its constructor

300

What is returned by this method call: digitSum(732)?

public static int digitSum(int n) {

    if (n == 0) return 0;

    return (n % 10) + digitSum(n / 10);

}

What is the output?

A. 12

B. 10

C. 3

D. 732

A. 12

300

class A {

    void print() { System.out.print("A "); }

}

class B extends A {

    void print() { System.out.print("B "); }

}

class C extends B {

    void print() { System.out.print("C "); }

}

public class Test {

    public static void main(String[] args) {

        A obj = new C();

        obj.print();

    }}

What is the output?

C

300

public class Test {

public static int mystery(int n) {

    if (n <= 1) {

        return 1;

    } else if (n % 2 == 0) {

        return n / 2 + mystery(n - 1);

    } else {

        return n * mystery(n - 2);

    }}

public static void main (String[] args) {

    System.out.println(mystery(8));

}}

What is the output?

109

M
e
n
u