Practice Test
1. What is wrong with the following code:
class Wrong { public static void main(String args[]) { int x = 3; printX(); }
public static void printX() { System.out.println(“x is ” + x); } } |
2.) Fix the above using parameters
3.) Why can’t you write a swap method in Java?
4.) Write a for loop that iterates 100 times.
5.) How would you, in Java, get a random number between 1 and 100?
6.) Write a method called coinToss that returns 0 for heads and 1 for tails.
7.) For each of the following, provide the recursive definition and the base case:
a.) Sum
b.) Factorial
c.) Power
8.) Pick one of the above and write a recursive method in Java to calculate it.
9.) What is Object Oriented Programming? What two classes of things make up an object?
10.) What does it mean for a program to be event-driven?
11.) Given the following method:
int find(int a[], int start, int value);
that returns the position in a that value first occurs at, starting at position a[start], write the code for Selection Sort.
void selectionSort(int[] data) {
}