Inheritance & Polymorphism
Iteration & Recursion
Objects & Values
Searching & Sorting
Syntax & Types
100

Given the following classes:

class A {}

class B extends A {}

class C extends B {}

On what line does the following code throw an error, if any?


A obj1 = new C();
C obj2 = obj1;
Object obj3 = obj1;

2

100

What is the output of the following code?

int[] arr = { 1, 2, 3, 4, 5 };
for (int i = 0; i <= arr.length; i++)
  System.out.println(arr[i]);

ArrayIndexOutOfBoundsException

100

What is the value of the following expression?

"abc".compareTo("abcd")

-1

100

How many times will the while loop be executed when calling binarySearch(new int[]{1, 2, 3, 4, 5, 6}, 10)?

public static int binarySearch(int[] arr, int target) {
    int left = 0;
    int right = arr.length - 1;
   
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] < target)
            left = mid + 1;
        else if (arr[mid] > target)
            right = mid;
        else if (arr[mid] == target)
            return mid;
    }
    return -1;
}

3 times

100

What is the value of the expression 5 / 2?

2

200

What problems are there with the following code?

interface A {
  int data = 3;
  void doSomething();
}

class B extends A {
  public static void main(String[] args) {
    System.out.println("Hello World");
    this.doSomething();
  }
}


  • A is an interface, so the keyword "extends" is wrong
  • B does not implement doSomething()
  • `this` cannot be referenced inside the static method main
200

What is the missing expression in this function that allows the code to function?

public int sum(int[][] arr) {
  int s = 0;
  for (int r = 0; r < arr.length; r++)
    for (__________)
      s += arr[r][c];
  return s;
}

(int c = 0; c < arr[c].length; c++)

200

What is the value of the following expression?

Integer.MIN_VALUE - 1

2147483647 (Integer.MAX_VALUE)

200

What sorting algorithm is implemented in the following code?

public static void sort(int arr[]) {
  int n = arr.length;
  for (int i = 0; i < n-1; i++) {
    int idx = i;
    for (int j = i+1; j < n; j++)
      if (arr[j] < arr[idx])
        idx = j;
    int temp = arr[idx];
    arr[idx] = arr[i];
    arr[i] = temp;
  }
}

Selection sort

200

What is the range of possible values that can be printed as a result of executing the following statements?

int x = (int) Math.random() * 10;
System.out.println(x);

0 only

300

A obj0 = new A();
A obj1 = new B();
A obj2 = new C();

obj0.print();
obj1.print();
obj2.print();

What will the previous code output, given the following classes?

interface A {
  void print();
}

class B {
  void print() {
    System.out.println("Printing from B");
  }
}

class C {
  void print() {
    System.out.println("Printing from C");
  }
}

Cannot instantiate the type A

300

What will this function do when it is called on 10x10 array of values?

public void run(int[][] arr) {
  for(int[] rows : arr) {
    for(int n : arr) {
      n = n + 1;
    }
  }
}

Nothing.

300

What is the output of the following code?

String str = "Hello World";   System.out.println(str.substring(4, 5) == str.substring(7, 8));

false

300

How many times will the inner while loop of the following insertion sort implementation be called when sorting the array { 7, 2, 4, 6, 1, 9 }.

static void insertionSort(int[] arr) {
  for (int i = 1; i < arr.length; i++) {
    int n = arr[i];
    int j = i;

    while (--j >= 0 && arr[j] > n)
      arr[j + 1] = arr[j];

    arr[++j] = n;
  }
}

7 times

300

On which line is there a compile-time error in the code?

class Main {
    void run(String... strs) {
        for (String s : strs)
            System.out.println(s);
        break;
    }
}

Line 4

400

A obj1 = new C();
A obj2 = new A();
C obj3 = new C();
B obj4 = new B();
obj1.print(0);
obj2.print(0);
obj3.print(0);
obj4.print(0);

What is the output of the previous code, based on the following classes?

 
static class A {
    void print(double x) {
        System.out.println("1");
    }
}

static class B extends A {
    void print(double y) {
        System.out.println("2");
    }
}

static class C extends B {
    void print(int z) {
        System.out.println("3");
    }
}

2
1
3
2

400

What is the output of executing run(2)?

public static int run(int x) {
  if (x + 1 == run(x + 1))
    return x;
  else
    return x - 1;
}

StackOverflowError

400

What will be printed as a result of executing the following code?


String str = "abcdef";
str.substring(2, str.indexOf("d"));
System.out.println(str);

abcdef

400

What will this array look like after 2 passes of merge-sort?

{7, 0, 2, 6, 5, 3, 1, 4}

{0, 2, 6, 7, 1, 3, 4, 5}

400

What will be printed to the console after executing the following code?

int[] arr = { 1, 2, 3, 4 };
int c = 0, s = 0;

for (int n : arr)
  s += n;
  c++;

System.out.println("Sum: " + s + ", count: " + c);

Sum: 10, count: 1

500

A obj = new C();
System.out.println(obj.getString());

Based on the following classes, what will the previous code output?

class A {
    int x = 3;

    String getString() {
        return "a" + x;
    }
}

class B extends A {
    String getString() {
        String str = super.getString();
        x *= 2;
        return "B" + x;
    }
}

class C extends B {
    String getString() {
        return (double) x / 10 + super.toString();
    }
}

0.3Ba36

500

What is the value of the expression recurse(2, 7)?

public static int recurse(int a, int b) {
  int x = b + a;
  int y = b - a;
  if (x > y)
    y = recurse(x, y);
  return b + y;
}

-58

500

What is the output of the following code, assuming java.util.* has been imported?

Collection<int> list = new ArrayList<>();

list.add(1);
list.add(2);

list.remove(0);

System.out.println(list);

Error: unexpected type (required 'reference', found 'int')

500

What is the average-case O(n) time complexity of the following sorting algorithms?

Bubble, insertion, selection, merge, and quick sorts

Bubble, insertion, selection: O(n2)

Merge, quick: O(n log n)

500

What is the result of executing the following code?

int x = 0;
int y = 4;

System.out.println(++x == y-- || ++x < y && x++ == --y ? x : y);

3

M
e
n
u