# Review Exercises (Set B2)

1. In the class below, find the number of: a) parameters; b) instance variables; and c) local variables:
public class MyClass {

double d1, d2;
float f;
boolean flag;

public MyClass() {};

public double myMethod(int num1, int num2, int num3) {
int sum = num1 + num2 + num3;
int product = num1 * num2 * num3;
return sum + product;
}
}

2. What does the following program print?
public class Stuff {

public static void f(int x) {
x = 444;
}

public static void g(int[] x) {
x[0] = 777;
}

public static void main(String[] args) {
int[] a = {1, 2, 3};
System.out.println(a[0]);
f(a[0]);
System.out.println(a[0]);
g(a);
System.out.println(a[0]);
}
}

3. What does the following code print?
public class Useful {
public static double dp(double[] a, double[] b) {
double r = 0;
for (int i = 0; i < a.length; i++)
r = r + a[i]*b[i];
return r;
}

public static void main(String[] args) {
double[] c = {2, 4, 3};
double[] d = {3, 2, 5};
System.out.println(dp(c,d));
}
}

4. What does the following code print?
public class ItsAMystery {

public static void main(String[] args) {
int[] a = {1,2,3,4};
mystery(a, 1);
for (int i=0; i < a.length; i++) {
System.out.print(a[i]);
}
}

public static void mystery(int[] a, int m) {
int n = a.length;

for(int i=0; i<n; i++) {
int tmp = a[i];
int j = (i+m)%n;
a[i] = a[j];
a[j] = tmp;
}
}
}

5. Given the code below answer the questions that follow:
public class Number {

public int x;

public Number(int x) {
this.x = x;
}

public static int cube(int x) {
return x*x*x;
}

public int squared() {
return x*x;
}
}
public class Main {
public static void main(String[] args) {
//Comment A
//Comment B
//Comment C
}
}

1. What single statement can replace //Comment A above to create a new object of type Number named "myNumber" with an x-value of 4?
2. What single statement can replace //Comment B above to invoke the method named squared for the object created in part (a) and print its output?
3. What single statement can replace //Comment C above to invoke the method named cube to cube the value 5 and print its output?
6. Overloaded methods can be distinguished by different _____________? (Fill in the blank)
7. Identify the error in the following code:
public class Cling {

int a = 1;

public static void main(String[] args) {
int b = 2;
f(b);
}

public static void f(int b) {
a = b;
b++;
}
}

8. Identify the error in the following code:
public int f(int x) {
int y = 1;
if (y < x) {
int z = 2;
}
else {
int z = 3;
}
return y + z;
}

9. What will the following code print?
public class ArrayFun {

public static void main(String[] args) {
int[] a = {1,2,3};
int[] b = {4,5,6};
int[] c = {7,8,9};
int[][] d = {a,b,c};
int[][] e = new int[3][3];

for (int i = 0; i < 3; i++) {
e[i][2] = d[i][2];
}

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(d[i][j] + e[i][j] + " ");
}
System.out.println();
}
}
}

10. Name two distinguishing characteristics of constructors.
11. Explain what it means when a method declares its return type as void
12. What does the following code print?
public class MoreArrayFun {

public static void main(String[] args) {
int[] a = {1,2,3};
int[] b = {4,5,6};

int[] t = a;
a[0] = 9;
a = b;
b = t;

for (int i = 0; i < 3; i++) {
System.out.println(a[i] + " " + b[i]);
};
}
}

13. What does the following print?
public class Sandbox {

private int lbsOfSand_;
private int length_;
private int width_;

public Sandbox(int width, int length, int lbsOfSand) {
width_ = width;
length_ = length;
lbsOfSand_ = lbsOfSand;
}

public int[] getDimensions() {
int[] dimensions = new int[2];
dimensions[0] = width_;
dimensions[1] = length_;
return dimensions;
}
}

public class SandboxFun {
public static void main(String[] args) {
Sandbox s1 = new Sandbox(3,4,5);
Sandbox s2 = new Sandbox(6,7,8);
s1 = s2;
s2 = s1;
System.out.println(s1.getDimensions()[0]);
System.out.println(s2.getDimensions()[0]);
}
}

14. The selection sort orders list or array of numbers (or some other comparable type) by performing a number of "swaps". Given the list: 1,9,8,3,6,2 -- put the list in order using a selection sort. Show the state of the list after each "swap", from the first swap made to the last, in that order.

15. The insertion sort orders a list or array of numbers (or some other comparable type) by performing a number of "insertions". Given the list: 1,9,8,3,6,2 -- put the list in order using an insertion sort. Show the state of the list after each "insertion", from the first insertion made to the last, in that order.

16. Suppose one uses a binary search to determine if 6 is present in the following array {1, 2, 4, 5, 7, 9, 12, 15, 18, 19, 23}. List the values of the low, mid, and high variables seen over the course of the algorithm (in the order that they are seen), as well as the return value for the method.

public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;

while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key > list[mid])
low = mid + 1;
else
return mid;
}

return -1 - low;
}


1. There are 4 parameters: num1, num2, num3, and this (the implicit parameter). There are 4 instance variables: d1, d2, f, flag. There are 2 local variables: sum and product
2.
1
1
777

3.
29.0

4.
1342

1. Number myNumber = new Number(4);
2. System.out.print(myNumber.squared());
3. System.out.printl(Number.cube(5));
5. method signatures
6. In the static method f, there is a reference to an instance variable a, but static variables are tied to the class, not any particular object (i.e., an instance of the class). Thus, the static method wouldn't know which object's value of a to use.
7. The local variable z used in the expression returned was never declared or initialized (the other two z's are out of scope).
8.
1 2 6
4 5 12
7 8 18

9. Constructors have the same name as the class to which they apply. Also, in the declaration of a constructor no return type (not even "void") is specified.
10. Methods with return type void do not return any output.
11.
4 9
5 2
6 3

12.
6
6

13.
1 9 8 3 6 2
1 2 8 3 6 9
1 2 6 3 8 9
1 2 3 6 8 9

14.
1 9 8 3 6 2
1 8 9 3 6 2
1 3 8 9 6 2
1 3 6 8 9 2
1 2 3 6 8 9

15.
lo   mid   high
0    5     10
3    2     4
4    3     3
4

return value:
-5