String[][] words = {{"A", "B"}, {"C", "D", "E"}}; System.out.println(words[1].length);
Question 11
What is printed?
int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int count = 0; for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { if (grid[r][c] % 2 == 0) { count++; } } } System.out.println(count);
Question 12
What does this code find?
int max = arr[0][0]; for (int[] row : arr) { for (int val : row) { if (val > max) { max = val; } } }
Inheritance (Questions 13-24)
Question 13
What keyword is used to inherit from a class?
Question 14
What is printed?
public class Animal { public void speak() { System.out.print("Animal"); } } public class Dog extends Animal { public void speak() { System.out.print("Woof"); } } // In main: Animal a = new Dog(); a.speak();
Question 15
What is printed?
public class Parent { public Parent() { System.out.print("P "); } } public class Child extends Parent { public Child() { System.out.print("C "); } } // In main: Child c = new Child();
Question 16
What does super() do in a subclass constructor?
Question 17
What is printed?
public class Vehicle { private String type = "Vehicle"; public String getType() { return type; } } public class Car extends Vehicle { private String type = "Car"; } // In main: Car c = new Car(); System.out.println(c.getType());
Question 18
What is method overriding?
Question 19
What is printed?
public class A { public void show() { System.out.print("A"); } } public class B extends A { public void show() { super.show(); System.out.print("B"); } } // In main: B b = new B(); b.show();
Question 20
Can a subclass access private fields of its superclass directly?
Question 21
What is printed?
public class Shape { public double getArea() { return 0; } } public class Rectangle extends Shape { private int w, h; public Rectangle(int w, int h) { this.w = w; this.h = h; } public double getArea() { return w * h; } } // In main: Shape s = new Rectangle(3, 4); System.out.println(s.getArea());
Question 22
What does the Object class provide?
Question 23
Which is true about inheritance in Java?
Question 24
What is printed?
public class Parent { public Parent(int x) { System.out.print("P" + x + " "); } } public class Child extends Parent { public Child() { super(5); System.out.print("C "); } } // In main: Child c = new Child();
Recursion (Questions 25-36)
Question 25
What is printed?
public static int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } // In main: System.out.println(factorial(5));
Question 26
What are the two essential parts of a recursive method?
Question 27
What is printed?
public static void countdown(int n) { if (n <= 0) { System.out.print("Go!"); return; } System.out.print(n + " "); countdown(n - 1); } // In main: countdown(3);
Question 28
What is printed?
public static void mystery(int n) { if (n <= 0) return; mystery(n - 1); System.out.print(n + " "); } // In main: mystery(3);
Question 29
What is printed?
public static int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } // In main: System.out.println(fib(6));
Question 30
What happens if a recursive method has no base case?
Question 31
What is printed?
public static int sum(int n) { if (n == 0) return 0; return n + sum(n - 1); } // In main: System.out.println(sum(4));
Question 32
What is printed?
public static String reverse(String s) { if (s.length() <= 1) return s; return reverse(s.substring(1)) + s.charAt(0); } // In main: System.out.println(reverse("hello"));
Question 33
How many times is mystery() called?
public static int mystery(int n) { if (n <= 0) return 0; return 1 + mystery(n - 2); } // In main: mystery(7);
Question 34
What is printed?
public static int power(int base, int exp) { if (exp == 0) return 1; return base * power(base, exp - 1); } // In main: System.out.println(power(2, 5));
Question 35
What is printed?
public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // In main: System.out.println(gcd(48, 18));
Question 36
What is printed?
public static boolean isPalindrome(String s) { if (s.length() <= 1) return true; if (s.charAt(0) != s.charAt(s.length() - 1)) return false; return isPalindrome(s.substring(1, s.length() - 1)); } // In main: System.out.println(isPalindrome("racecar"));
Searching & Sorting (Questions 37-50)
Question 37
What is the time complexity of binary search?
Question 38
What requirement must be met to use binary search?
Question 39
What is printed?
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16}; int target = 10; int low = 0, high = arr.length - 1; int steps = 0; while (low <= high) { steps++; int mid = (low + high) / 2; if (arr[mid] == target) break; else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } System.out.println(steps);
Question 40
What is the time complexity of selection sort?
Question 41
What sorting algorithm does this code implement?
for (int i = 0; i < arr.length - 1; i++) { int minIdx = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minIdx]) { minIdx = j; } } // swap arr[i] and arr[minIdx] }
Question 42
What sorting algorithm does this code implement?
for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; }
Question 43
After one pass of selection sort on {5, 2, 8, 1, 9}, what is the array?
Question 44
After one pass of insertion sort on {5, 2, 8, 1, 9}, what is the array?
Question 45
What is the time complexity of merge sort?
Question 46
Which search is better for a small unsorted array?
Question 47
How many comparisons does linear search need in the worst case for an array of 100 elements?
Question 48
How many comparisons does binary search need in the worst case for an array of 100 elements?
Question 49
What is returned when binary search doesn't find the target?
public static int binarySearch(int[] arr, int target) { int low = 0, high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return -1; }
Question 50
Which statement about sorting is FALSE?
🎉 Unit 4 Complete!
Review your answers above or click Reset to practice again.
Contact form
Choosing a selection results in a full page refresh.