logo

APSU Notes


Quiz 2

Question 1

Given the following header for a method in the class Demo:

1
public static double smallest(double[] data)

Which of the following is the correct way to call this method, given an array of doubles called values?

1
double min = Demo.smallest(values);

Question 2

Given the following initial array:

1
{'P', 'A', 'U', 'S'}

Which of the following correctly illustrates the sequence of passes performed by selection sort?

1
2
3
4
{'P', 'A', 'U', 'S'}
{'A', 'P', 'U', 'S'}
{'A', 'P', 'U', 'S'}
{'A', 'P', 'S', 'U'}

Question 3

Assume there is a class called Trombone with a default constructor. Which of the following is the correct way to create and initialize an array of 76 Trombone objects?

1
2
3
Trombone[] marchingBand = new Trombone[101];
for(int i = 0; i < marchingBand.length; i++)
  marchingBand[i] = new Trombone();

Question 4

Given an array called items, what is the correct way of determining the number of elements in the array?

1
items.length

Question 5

Which of the following is the correct way to declare an array of 12 integers in Java?

1
int[] numbers = new int[12];

Question 6

Which of the following is the correct way to initialize a two-dimensional array of integers with 3 columns and 5 rows?

1
int[][] table = new int[5][3];

Question 7

Given the following class declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MyList
{
  private String[] list;
  private int numItems;

  /**
  Creates an empty list with the given capacity
  */
  public MyList(int capacity)
  {
    list = new String[capacity];
    numItems = 0;
  }

  /**
  Attempts to add an item to the next open spot in the list.
  Returns false if the list is full.
  */
  public boolean addItem(String item)
  {
    // YOUR CODE HERE
  }
}

Which of the following is a correct implementation of the addItem method?

1
2
3
4
5
6
7
8
9
10
11
public boolean addItem(String item)
{
  if(numItems == list.length)
    return false;
  else
  {
    list[numItems] = item;
    numItems++;
    return true;
  }
}

Question 8

If an array is sorted, the fastest way to search it is by using a sequential search.

False

Question 9

Arrays can be used as parameters for methods, but a method can not return an array.

False

Question 10

An array can be used as an instance variable in a class.

True

Question 11

Given two arrays array1 and array2, when does the expression array1 == array2 evaluate to true?

When array1 and array2 refer to the same array object.

Question 12

All of the items stored in an array must be of the same type.

True

Notes

Chapter 1/Arrays