The exam will cover all of the review material, and the basics of how to design classes (everything we covered up through last Tuesday). This includes all or part of the following chapters (which might have different chapter numbers, depending on the version you're using):
- Data and Expressions
- Using Classes and Objects
- Writing Classes
- Conditionals and Loops
- Arrays
- Recursion
The best place to start reviewing is doing exercises from those chapters. Also, don't forget to make your page of notes (front and back).
Example exam problems
Define a class called
Point2D
that represents a point in the x-y plane. Ensure that instances of the class are immutable by making all member variablesprivate
. Define the following methods:dist(Point q)
:p.dist(q)
should return the distance betweenp
andq
. Recall that the distance between two points is and thatMath.sqrt
provides the square root function.closest(Point[] points)
: should return the closest point in the input array.
What does the following code print?
int x = 5; int y = 10; if (x > y) { System.out.println("a"); x += 1; else { System.out.println("b"); x += 2; } System.out.println(x);
What is the value of
g(12, 9)
ifg
is the following method?public static int g(int a, int b) { if (b == 0) { return a; } return g(b, a % b); }
What does the following code print?
public class Counter { private int count = 0; public Counter() { } public void add() { count++; } public int getCount() { return count; } public static void main(String[] args) { Counter c = new Counter(); System.out.println(c.getCount()); c.add(); c.add(); System.out.println(c.getCount()); } }
Define a static method called
max
that takes 3int
inputs and returns the largest.What is the value of
z
at the end of the following program?int[] x = { 1, 2, 3, 4, 5 }; int[] y = x; x[3] = 11; int q = y[3]; int z = 5 * q;
Define a method that takes an array of integers as input, and returns a new array with elements double that of the input. For example, with input
{2, 5, 3}
the output should be{4, 10, 6}
.
This post will be updated with additional practice questions later.