Quarter 1 Review
Quarter 2 Review
2D Arrays
Array Lists
100

4.0 - 1/2

4.0

100

Declare and preallocate a one-dimensional array of Student objects, whose length is 5, stored in a variable called roster.

Student[] roster = new Student[5];

100

Declare and preallocate a two-dimensional array of boolean values that has 2 rows and 8 columns, stored in variable mat.

boolean[][] mat = new boolean[2][8];

100

Declare an empty array list of doubles in the variable myList.

ArrayList<Double> myList = new ArrayList<Double> ();

200
The Weather class has a single instance variable called temp, intended to store the temperature (a double). Write a constructor for the class that takes in a value for temperature and stores it in the already-declared instance variable.

public Weather(double temp)

     this.temp = temp;

200

x = false, y = true, z = false

((x & y) & (y & w)) || (x & z)

false

200

What is stored in arr after the following code segment is executed?

int[][] arr = {{6,5},{7,8}};
arr[1][0] = arr[0][1];
arr[0][0] += arr[1][0];

{{11, 5},
  {5, 8}}

200

Suppose that numbers is the already-declared array list containing the integers
     {10, 15, 18}
What is stored in numbers after the code segment has been executed?

numbers.add(1,20);
numbers.add(numbers.remove(2));

{10, 20, 18, 15}

300

(int) 11.0/2;

5

300

What value is printed after the following code segment is executed?

String word = "computer";
String s = "abcde";
int x = 0;
for (int i = 0; i < word.length(); i++)
     if (s.indexOf(word.substring(i,i+1)) > -1)
          x++;
     else
          x--;
System.out.print(x);

-4

300

Daily double!

Consider a two-dimensional array of String objects last. Write a line of code that stores the String object in the bottom-right corner of the array in the variable last

String last = mat[mat.length - 1][mat[0].length - 1];

300

Suppose that "arr" is an ArrayList<Double>. Write a line of code that will store the last value in "arr" in the variable "last".

double last = arr.get(arr.size() - 1);

400
Evaluate the following:


"baseball".substring(5).equals("ball");

false
400

Consider the array of integers
     arr = {5, 10, 11, 12}
What is printed after running the code?

for(int i = 0; i < arr.length; i++)
     if(i%2 == 0 || arr[i]%2 == 0)
          System.out.print(i + " " arr[i] + " ");

0 5 1 10 2 11 3 12

400

What is stored in arr after the following code segment is executed?

String s = "abc";
int i = 0, j = 0;
String[][] arr = new String[3][2];
for (int r = 0; r < arr.length; r++)
     for (int c = 0; c < arr[0].length; c++)
          j = i % s.length()
          arr[r][c] = s.substring(i, i+1);
          i++;

{{a, b},
  {c, a},
  {b, c}}

400

Daily double!

Suppose listA is an array list of doubles, and listB is an empty array list of doubles. Using no more than three lines of code, write an algorithm that keeps all positive numbers (and zero) in listA, but moves all negative numbers to listB.

for (int i = 0; i < listA.size(); i++)
     if (listA.get(i) >= 0)
          listB.add(listA.remove(i));

500

String word = "beethoven";

String newWord = word.substring(word.length()/2)                        + word.substring(0, word.length()/2);

"hovenbeet"

500

For the following code segment, what will be the final value of n after it is executed?

int n = 0;
for (int i = 0; i < 4; i++)
     for (int j = i; j >=0; j--)
          n++;

n = 10

500

What is stored in x after the code segment is executed?

int[][] mat = {{2, 5, 4}, {3, 3, 6}};
int x = 0;
for (int r = 0; r < mat.length; r++)
     for (int c = 0; c < mat[0].length; c++)
          if (mat[r][c] > mat[r][c-1])
               x += mat[r][c];

11

500

If words is an ArrayList<String>
     words = {"computer", "science"}
what is returned after running the code below?

ArrayList<String> result = ArrayList<String>();
for (String word : words)
    result.add(word.substring(1)+word.substring(0,1)                                          + "ay");


{"omputercay", "ciencesay"}

M
e
n
u