# Review Exercises (Set B1)

1. In the method below, how many parameters (implicit and explicit) are there?

public double CountMyParameters(int a) {
double x = a * 2.0;
double y = x * 3.0;
double z = y * 4.0;
return x+y+z;
}

2. The method defined below has how many local variables?

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

3. Given the code below, what should one type to invoke the myMethod() method at the "?????????" in the main method given? (You may assume the Number class is visible to the main method.)

public class Number {
int num = 1;

Number(int n) {
num = n;
}

public void myMethod() {
System.out.println("Howdy!");
}
}

public class Main{

public static void main(String[] args) {
Number n = new Number(3);
?????????
}
}

4. What keyword references the implicit parameter?

5. Explain the error in the code below:

public boolean isGoodInvestment(double principal) {
double balance = principal;
for (int year = 0; year < 7; year++) {
principal *= 1.10;
boolean isEnough = (balance > 2*principal);
}
return isEnough;
}

6. Explain the error in the code below:

public class Square {

private double sideLength_;

public Square(double sideLength) {
sideLength_ = sideLength;
}

public double getArea() {
return sideLength_*sideLength_;
}
}

public class Main {

public static void main(String[] args) {
Square s = new Square(3.0);
System.out.println(Square.getArea());
}

}

7. What will the following code print?

public class Main {
public static void main(String[] args) {
int[] a = new int;
for (int i = 0; i < 3; i++) {
System.out.print(a[i]);
a[i] = 3 - i;
}
System.out.println();
for (int i = 0; i < 3; i++) {
System.out.print(a[i]);
}
}
}

8. What will the following code print?

public class Main {
public static void main(String[] args) {
int[][] a = new int;
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
a[i][j] = (i+2*j) % 3;
}
}
for (int k=0; k<3; k++) {
System.out.print(a[k]);
}
System.out.println(a.length);
}
}

9. What will the following code print?

public class Main {

public static void doSomething(int[] a, int b) {

for (int i = 0; i < 5; i++) {
a[i]++;
}

b++;
}

public static void main(String[] args) {

int[] list = {1, 2, 3, 4, 5};
int num = 6;

doSomething(list, num);

for (int i = 0; i < 5; i++) {
System.out.print(list[i]);
}
System.out.println();

System.out.println(num);
}
}

10. What single line of code should replace the comment below:

public class Circle {

public Circle() {
}

//return true if the circle's radius equals
//the one given, and false otherwise
}
}

11. First, consider the following two classes:

public class Paint {

int red_;
int green_;
int blue_;

public Paint() {
}

public Paint(int red, int green, int blue) {
red_ = red;
green_ = green;
blue_ = blue;
}

public void setGreen(int green) {
green_ = green;
}

public String toString() {
return (red_ + "," + green_ + "," + blue_);
}
}

public class ColorManipulator {

public static void increaseGreen(int r, int g, int b) {
g = 255;
}

public static void main(String[] args) {
Paint paintA = new Paint(255,0,0);
Paint paintB = new Paint();
System.out.println(paintB.toString());
paintB = paintA;
paintA.setGreen(255);
System.out.println(paintB.toString());

int red = 255;
int blue = 0;
int green = 0;
increaseGreen(red, blue, green);
System.out.println(red + "," + blue + "," + green);

int[] colA = {255,0,0};
int[] colB = colA;
colB = 255;
System.out.println(colA+","+colA+","+colA);
}
}


1. What four lines does the main method of the ColorManipulator class print?
2. The constructor for the Paint class has been _____________. (Fill in the blank with a single appropriate word.)
12. Constructors should always have a _____________ visibility modifier, while instance fields should generally have a _____________ visibility modifier to allow for maximum flexibility in changing the the internal structure of the class in the future, without impacting other classes. (Fill in the blank.)

13. What does the following code print?

int[] a = {2,3,6,8,5,9,1,7};
int p = 0;
for (int i=0; i < a.length; i++) {
if (a[p] > a[i]) {
p = i;
}
}
System.out.println(p);

14. The selection sort orders list or array of numbers (or some other comparable type) by performing a number of "swaps". Given the list: 3,5,9,8,4,6,1 -- 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: 3,5,9,8,4,6,1 -- 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. What type of "special method" has no return type declared (not even void)?

17. Examine the following method. If there is an error in it, explain what it is. If no such error exists, what will the method return if invoked?

public boolean isItZero() {
}

18. What does the following code print?

public class CharWrapper {

char c_ = 'a';

CharWrapper(char c) {
c_ = c;
}

public void setChar(char c) {
c_ = c;
}

public char getChar() {
return c_;
}
}

public class WrapperTester {

public static void main(String[] args) {
CharWrapper c1 = new CharWrapper('b');
CharWrapper c2 = new CharWrapper('c');
c1 = c2;
c2.setChar('d');
System.out.println(c1.getChar());
}
}

19. Suppose one uses an iterative version of a binary search to determine if 13 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. 1 implicit, 1 explicit
2. 2
3. n.myMethod();
4. this
5. isEnough is out of scope
6. the getArea() method is not static,
but was called in a static way
7.
000
321

8.
0213
9.
23456
6

10.
return this.radius == radius;

11.
1.
0,0,0
255,255,0
255,0,0
255,255,0

1. public
2. private
1.
6
2. assuming maximums are swapped to the end,
we would have the following (of course, it is equally
valid to do a selection sort where minimums are
swapped to the beginning)
3,5,9,8,4,6,1
3,5,1,8,4,6,9
3,5,1,6,4,8,9
3,5,1,4,6,8,9
3,4,1,5,6,8,9
3,1,4,5,6,8,9
1,3,4,5,6,8,9

3.
3,5,9,8,4,6,1
3,5,8,9,4,6,1
3,4,5,8,9,6,1
3,4,5,6,8,9,1
1,3,4,5,6,8,9

4. a constructor does not declare a return type
(as opposed to a method that declares a return
type of void which indicates it won't return
anything)
5. the local variable someNumber was not initialized
6.
d
7.
low   mid   high
0     5     10
6     8     7
7     6     6
7


returns -8