Using Objects and Methods
Selection and Iteration
Class Creation
Data Collections
Advanced Algorithms
100

What is printed to the console by the statement:

System.out.println("\\a\"");

\a"

100

Write code that will print out "Yay!" if an integer variable x is a multiple of 3.

if (x % 3 == 0)

    System.out.println("Yay!");

100

Write a setter method for the following instance variable in an unknown class:

private double score;

public void setScore(double s) {

    score = s;

}

100

Write code to swap the elements at indices 2 and 5 in int[] arr.

int temp = arr[2];

arr[2] = arr[5];

arr[5] = temp;

100

What would the following method call return?

int[] arr = { 13, 11, 6, 2, 13, 4, 3, 10, 9, 8, 4}

sequentialSearch(arr, 3);

6

200

What are the possible values generated by the call:

Math.random()*3+5

[5.0, 8.0)

200

Fill in the blank so that the following loop runs exactly 5 times:

for (int i = 0; i < 10; _______) {

   /* loop body not shown */

}

i += 2

200

Write a valid constructor for the following class:

public class Student {

   private String name; private int id; private double gpa;

    /* rest of class not shown */

}

public Student() {

    name = "bob"; id = 123; gpa = 3.2;

}

public Student(String n, int i, double g) {

    name = n; id = i, gpa = g;

}

200

Write code to create an ArrayList of Strings and add "hello" to it.

ArrayList<String> list = new ArrayList<String>();

list.add("hello");

200

Which elements (in order!) get examined by the following call?

int[] arr = { 1, 3, 5, 8, 9, 11, 14, 15, 17, 20, 21 };

binarySearch(arr, 3);

11, 5, 1, 3

300

What is returned by the following call:

"basketball".indexOf("ball");

6

300

What is printed out by the following code?

int count = 1;

while (count <= 10) {

   count++;

   System.out.print(count + " ");

}

2 3 4 5 6 7 8 9 10 11

300
How many mutator methods are in the following class:

public class Dice {

   private int currRoll; int numSides;

   public Dice(int n) { numSides = n; currRoll = 0; }

   public int getRoll() { return currRoll; }

   public void roll() { currRoll = Math.random()*numSides+1; }

   public void setSides(int n) { numSides = n; }

}

2

300

Consider list to be an empty ArrayList of Strings. What is output by the following code?

list.add(0, "one");

list.add(0, "two");

list.add(0, "three");

System.out.println(list);

["three", "two", "one"]

300

What would the following array look like after the 4th pass of insertion sort?

int[] arr = {13, 6, 11, 2, 5, 1, 17, 8, 9, 12}

[ 2, 5, 6, 11, 13, 1, 17, 8, 9, 12 ]

400

What is returned by the following call:

"hello".substring(3,3)

The empty string

400

What is printed when x has a value of .8?

if (x > .25) Sys.out.print("second quartile");

else if (x > .5) Sys.out.print("third quartile");

else if (x > .75) Sys.out.print("fourth quartile");

else Sys.out.print("first quartile");

second quartile

400

What makes a static variable different than an instance variable?

It's a variable shared by the whole class rather than unique to each instance of the class.

400

Write a method that will return true if a given integer array has all even numbers.

public boolean allEvens(int[] array) {

   for (int i = 0; i < array.length; i++) {

       if (array[i] % 2 != 0) return false;

   }

   return true;

}

400

How many calls to our merge/help method would it take to sort the following array using Merge Sort?

int[] arr = {16, 13, 5, 2, 12, 18, 4, 6, 9, 10, 11, 21}

11

500
Consider the following class definition and create an instance of the class called bob.

public class Thing {

    public Thing(String y, int x) {

         /* implementation not shown */

    }

}

Thing bob = new Thing("some string", 123);

500

Use DeMorgan's Law to simplify the following expression:

!(x<3 && y <=2)

x >= 3 || y > 2

500

QUIET CLASSES

*answers on other sheet*

500

Write a method that takes in an ArrayList of integers and removes all the multiples of 5 from it.

public void noFives(ArrayList<Integer> nums) {

    for (int i = nums.size()-1; i >=0; i--) {

         if (nums.get(i) % 5 == 0 ) nums.remove(i);

    }

}

500

MERGE SORT PASSES

int[] arr = {16, 13, 5, 2, 12, 18, 4, 6, 9, 10, 11, 21}

16 5 13 2 12 18 4 6 9 10 11 21

5 13 16 2 12 18 4 6 9 10 11 21

5 13 16 2 12 18 4 6 9 10 11 21

5 13 16 2 12 18 4 6 9 10 11 21

2 5 12 13 16 18 4 6 9 10 11 21

etc.

M
e
n
u