Name : Comp 240 Quiz Written 3

ID # : Apr 21, 2005

3/3

State clearly all of your assumptions.

Good luck.

Q1. (60 pt)

In the following questions, check all that apply.

1.a. (6 pt)

Consider the array declaration:

int s[] = {7, 0, -12, 9, 10, 3, 6};

What is the value of

s[ s[ 6 ] - s[ 5 ] ]

o 0

o 3

þ 9

o -12

1.b. (6 pt)

A programmer must do the following before using an array:

o declare then reference the array.

o create then declare the array.

o create then reference the array.

þ declare then create the array.

1.c. (6 pt)

Which expression adds 1 to the element of array arrayName at index i?

þ ++arrayName[ i ]

o arrayName++[ i ]

o arrayName[ i++ ]

1.d. (6 pt)

Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4?

o values[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ];

o values[ 4 ] = values[ 3 ];
values[ 3 ] = values[ 4 ];

þ int temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = temp;

o int temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ];

1.e. (6 pt)

Assume a class, Book, has been defined. Which set of statements creates an array of Book objects?

þ Book books[];
books = new Book[ numberElements ];

o Book books[];
books = new Book()[ numberElements ];

o new Book() books[];
books = new Book[ numberElements ];

1.f. (6 pt)

Assume array items contains the values 0, 2, 4, 6 and 8. Which of the following set of statements uses the enhanced for loop to display each value in array items?

o for ( int i : items )
System.out.printf( "%d\n", items[ i ] );

þ for ( int i : items )
System.out.printf( "%d\n", i );

o for ( int i = 0 : items.length )
System.out.printf( "%d\n", items[ i ] );

1.g. (6 pt)

Which statement below initializes array items to contain 3 rows and 2 columns?

þ int items[][] = { { 2, 4 }, { 6, 8 }, { 10, 12 } };

o int items[][] = { { 2, 6, 10 }, { 4, 8, 12 } };

o int items[][] = { 2, 4 }, { 6, 8 }, { 10, 12 };

o int items[][] = { 2, 6, 10 }, { 4, 8, 12 };

1.h. (6 pt)

Which of the following sets of statements creates a multidimensional array with 3 rows, where the first row contains 1 value, the second row contains 4 items and the final row contains 2 items?

o int items[][];
items = new int[ 3 ][ ? ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];

þ int items[][];
items = new int[ 3 ][ ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];

o int items[][];
items = new int[ ? ][ ? ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];

o int items[][];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];

1.i. (6 pt)

When an argument is passed by reference:

o a copy of the argument’s value is passed to the called method.

o changes to the argument do not affect the original variable’s value in the caller.

þ the called method can access the argument’s value in the caller directly and modify that data.

o the original value is removed from memory.

1.j. (6 pt)

In array items, which expression below retrieve the value at row 3 and column 5?

o items[ 3 ].[ 4 ]

o items[ 3[ 4 ] ]

þ items[ 3 ][ 4 ]

o items[ 3, 4 ]

Q2. (40 pt)

What it the output of the following program?

public class TestApp {

public static void main(String[] args) {

int items[] = {0, 2, 4, 6, 8};

outputArray( items );

changeArray( items, items[ 2 ] );

outputArray( items );

} // end method main

public static void outputArray( int passedArray[] )

{

for ( int item : passedArray )

System.out.printf("%d ", item);

System.out.println();

} // end method outputArray

public static void changeArray( int passedArray[], int value )

{

passedArray[ value ] = 12;

value = 5;

} // end method changeArray

} // end class TestApp

0 2 4 6 8

0 2 4 6 12