DOC

CS Java 1

By Emma Alexander,2014-04-24 13:44
6 views 0
CS Java 1

Name __________________________ Date: ___/___/____

    AP CS Java - Mr. Merlis Block ___________

    Arrays and ArrayLists

INSTRUCTIONS: The questions below are “AP Style” questions.

1. Consider the following declaration.

    int[] A = new int[l0];

    Which of the following choices describes the state of array A after this statement is executed?

    a. The elements A [1] . . . A [ 10] are not yet initialized and contain unknown int values. b. The elements A[1] . . . A[10] are initialized to 0.

    c. The elements A [0] . . . A [9] are not yet initialized and contain unknown int values. d. The elements A [ 0] . . . A [ 9] are initialized to 0.

    e. The elements A [ 0] . . . A [10] are initialized to 0.

Questions 2 and 3 refer to the Coin class whose incomplete definition is below.

    public class Coin

    {

    double value;

    private String name;

// Constructor

    public Coin(double aValue, String aName) {...} // Returns coin value.

     public double getValue() {...} //Returns coin name.

    public String getName() { } // Returns true if this and other have same values, // false otherwise.

    public boolean equals (Object other)

    }

2. Assume that the variable money has been initialized as follows;

    ArrayList money = new ArrayList () ; and that 10 Coins were added to money.

    Which of the following statements or pair of statements would print the name of the sixth Coin in money?

a. String temp = (String)money[5].getName(); System.out.println(temp);

b. System.out.println (money[5].getName());

c. Coin temp = Coin (money. get (5)) ; System.out.println(temp) ;

d. Coin temp = (Coin)money.get(5); System.out.println(temp.getName());

e. System.out.println(money.get(5) .getName());

3. Assume that the variable money has been initialized as follows~

    ArrayList money = new ArrayList(); and that several Coins were added to money.

The following segment of code is intended to find the total value of the Coins in money. 1 public double getTotal()

    2 {

    3 double total = 0;

    4 int i = 0;

    5 while (i < money.size())

    6 {

    7 Coin tempCoin = (Coin)money.get(i);

    8 total += tempCoin.getValue();

    9 i++;

    10 }

    11 return total;

    12 }

Which of the following statements about this method is true?

a. There will be a compile-time error because the cast is line 7 is incorrect.

    b. There will be an indexOutOfBoundsException thrown during runtime because line 7 will try to access an element not in

    bounds of money.

    c. The method will compile and run but the total returned will be incorrect.

    d. There will be a run-time error when money contains no Coins, otherwise the method works as intended.

    e. This method will execute as intended.

4. Consider the method doTask defined below. public static int doTask(int[] a)

    {

    int index = 0;

    int soFar = 1;

    int count = 1;

    for (int k ~ 1; k < a.length; k++)

    {

    if (a[k] == a[k - 1])

    {

    count++;

    if (count> soFar)

    {

    soFar = count;

    index = k;

    }

    }

    else

    {

    count = 1;

    }

    }

    return a[index];

    }

When the following code segment is executed, int[] arr = {1, 2, 3, 3, 3, 3, 4, 2, 2, 2, 2, 2, 2, 2, 5, 6, 6, 6, 6, 6, 6, 4, 7,8};

    System.out.println(doTask(arr));

    What is printed to the screen?

    a. 1

    b. 2

    c. 6

    d. 7

    e. 24

     2

5. Assume that the following declarations have been made. int[] A = {1, 2, 3, 4, 5, 6, 7, 8};

    int[] B = {4, 5, 6, 7, 8};

    Also assume that the method printContents is defined with the following header and works as specified in the comment. // Prints the contents of array arr to the screen.

    public static void printContents(int[] arr)

If the following segment of code is executed, what will be printed to the screen? B = A;

    B[3] = 0;

    printContents(A);

    printContents(B);

a.

    1 2 3 4 5 6 7 8

    4 5 6 0 8

b.

    1 2 3 0 5 6 7 8

    1 2 3 0 5 6 7 8

c.

    4 5 6 0 8

    4 5 6 0 8

d.

    1 2 3 0 5 6 7 8

    4 5 6 7 8

e.

    1 2 3 4 5 6 7 8

    4 5 6 7 8

6. Which of the following statements about arrays and ArrayLists is not true?

    a. A method can change the length of an array that is passed as a parameter.

    b. A method can change the length of an ArrayList that is passed as a parameter. c. A method can reverse the order of elements in its array parameter.

    d. A method can reverse the order of elements in its ArrayList parameter.

    e. A method can remove all of the elements in an ArrayList.

7. Consider the following method doTask. public static boolean doTask(int[] arr)

    {

    boolean found = true;

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

    {

    found = found && (arr[i] % 2 == 1);

    }

    return found; }

    Which of the following statements is true about method doTask?

    a. Method doTask returns true if all elements in its array parameter arr are even, false otherwise. b. Method doTask returns true if all elements in its array parameter arr are odd, false otherwise. , c. Method doTask returns true if all elements in its array parameter arr are positive, false otherwise. d. Method doTask returns true if all elements in its array parameter arr are equal to 1, false otherwise. e. Method diask will always return true.

     3

8. Consider the following method definitions intended to find the smallest integer value in its Arraylist parameter.

     I. public static int findSmallest(ArrayList nums)

    {

    Integer small = (Integer)nums.get(0);

    for (int i = 1; i < nums.size(); i++)

    {

    Integer tempSmall = (Integer)nums.get(i);

    if (tempSmall.compareTo(small) < 0)

    {

    small = tempSmall;

    }

    }

    return small.intValue();

    }

II. public static int findSmallest(ArrayList nums)

    {

    Integer small = (Integer)nums.get(0);

    for (int i = 1; i < nums.size(); i++)

    {

    Integer tempSmall = (Integer)nums.get(i);

    if (tempSmall.intValue() < small.intValue())

    {

    small = tempSmall;

    }

    }

    return small.intValue();

    }

III. public static int findSmallest(ArrayList nums)

    {

    Integer small = (Integer)nums.get(0).intValue();

    for (int i = 1; i < nums.size(); i++)

    {

    int tempSmall = ((Integer)nums.get(i).intValue();

    if (tempSmall < small)

    {

    small = tempSmall;

    }

    }

    return small;

    }

Which of the method definitions above will work as intended?

    a. I only

    b. II only

    c. I and II only

    d. II and III only

    e. I, II, and III

     4

Report this document

For any questions or suggestions please email
cust-service@docsford.com