|
few java questions
1. Arrays are:
a. variable-length entities.
b. fixed-length entities.
c. data structures that contain up to 10 related data items.
d. used to draw a sequence of lines, or “rays.”
2. Which of the following statements about arrays are true?
A. Arrays are a group of variables containing values that all have the same type.
B. Elements are located by index or subscript.
C. The length of an array c is determined by the expression c.length();.
D. The zeroth element of array c is specified by c[ 0 ].
a. A, C, D.
b. A, B, D.
c. C, D.
d. A, B, C, D.
3. Consider the array:
s[ 0 ] = 7
s[ 1 ] = 0
s[ 2 ] = -12
s[ 3 ] = 9
s[ 4 ] = 10
s[ 5 ] = 3
s[ 6 ] = 6
The value of s[ s[ 6 ] - s[ 5 ] ] is:
a. 0.
b. 3.
c. 9.
d. 0.
Ans: c.
4. A programmer must do the following before using an array:
a. declare then reference the array.
b. create then declare the array.
c. create then reference the array.
d. declare then create the array.
5. Consider the code segment below. Which of the following statements is not true?
int g[];
g = new int[ 23 ];
a. The first statement declares an array reference.
b. The second statement creates the array.
c. g is a reference to an array of integers.
d. The value of g[ 3 ] is -1.
6. Which of the following statements about creating arrays and initializing their elements is not true?
a. The new keyword should be used to create an array.
b. When an array is created, the number of elements must be placed in square brackets following the type of element being stored.
c. The elements of an array of integers have a value of null before they are initialized.
d. A for loop is an excellent way to initialize the elements of an array.
7. What do the following statements do?
double array[];
array = new double[ 14 ];
a. Creates a double array containing 13 elements.
b. Creates a double array containing 14 elements.
c. Creates a double array containing 15 elements.
d. Declares but does not create a double array.
8. Which of the following initializer lists would correctly set the elements of array n?
a. int n[] = { 1, 2, 3, 4, 5 };.
b. array n[ int ] = { 1, 2, 3, 4, 5 };.
c. int n[ 5 ] = { 1; 2; 3; 4; 5 };.
d. int n = new int( 1, 2, 3, 4, 5 );.
9. Constant variables also are called .
a. write-only variables.
b. finals.
c. named constants.
d. All of the above.
10. Which of the following will not produce a compiler error?
a. Changing the value of a constant after it is declared.
b. Changing the value at a given index of an array after it is created.
c. Using a final variable before it is initialized.
d. All of the above will produce compiler errors.
11. Consider the class below:
public class Test
{
public static void main( String args[] )
{
int a[];
a = new int[ 10 ];
for ( int i = 0; i < a.length; i++ )
a[ i ] = i + 1 * 2;
int result = 0;
for ( int i = 0; i < a.length; i++ )
result += a[ i ];
System.out.printf( "Result is: %d\n", result );
} // end main
} // end class Test
The output of this Java program will be:
a. Result is: 62.
b. Result is: 64.
c. Result is: 65.
d. Result is: 67.
12. Consider the class below:
public class Test
{
public static void main( String args[] )
{
int a[] = { 99, 22, 11, 3, 11, 55, 44, 88, 2, -3 };
int result = 0;
for ( int i = 0; i < a.length; i++ )
{
if ( a[ i ] > 30 )
result += a[ i ];
} // end for
System.out.printf( "Result is: %d\n", result );
} // end main
} // end class Test
The output of this Java program will be:
a. Result is: 280.
b. Result is: 154.
c. Result is: 286.
d. Result is: 332.
13. Which flag in a format specifier indicates that values with fewer digits than the field width should begin with a leading 0?
a. p.
b. l.
c. w.
d. 0.
14. Which of these statements can be used to complete the following sentence?
Invalid possibilities for array indices include .
a. Positive integers.
b. Negative integers.
c. Non-consecutive integers.
d. Zero.
15. Which expression adds 1 to the element of array arrayName at index i?
a. ++arrayName[ i ].
b. arrayName++[ i ].
c. arrayName[ i++ ].
d. None of the above.
16. Attempting to access an array element out of the bounds of an array, a(n) occurs.
a. ArrayOutOfBoundsException.
b. ArrayElementOutOfBoundsException.
c. ArrayIndexOutOfBoundsException.
d. ArrayException.
Ans: c.
17. Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4?
a.
values[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ];
b.
values[ 4 ] = values[ 3 ];
values[ 3 ] = values[ 4 ];
c.
int temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = temp;
d.
int temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ];
18. In this question, assume a class, Book, has been defined. Which set of statements creates an array of Book objects?
a.
Book books[];
books = new Book[ numberElements ];
b.
Book books[];
books = new Book()[ numberElements ];
c.
new Book() books[];
books = new Book[ numberElements ];
d. All of the above.
19. 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?
a.
for ( int i = 0; i < items.length; i++ )
System.out.prinf( "%d\n", items[ i ] );
b.
for ( int i : items )
System.out.prinf( "%d\n", items[ i ] );
c.
for ( int i : items )
System.out.prinf( "%d\n", i );
d.
for ( int i = 0 : items.length )
System.out.prinf( "%d\n", items[ i ] );
19. Which of the following tasks cannot be performed using an enhanced for loop?
a. Multiplying all the values in an array together.
b. Displaying all even elements in an array.
c. Comparing the elements in an array to a specific value.
d. Incrementing the value stored in each element of the array.
19. Which statement correctly passes array items to method takeArray? Array items contains 10 elements.
a. takeArray( items[] ).
b. takeArray( items ).
c. takeArray( items[ 9 ] ).
d. Arrays cannot be passed to methods—each item must be sent to the method separately.
20. Consider array items, which contains the values 0, 2, 4, 6 and 8. If method changeArray is called with the method call changeArray( items, items[ 2 ] ), what values are stored in items after the method has finished executing?
public static void changeArray( int passedArray[], int value )
{
passedArray[ value ] = 12;
value = 5;
} // end method changeArray
a. 0, 2, 5, 6, 12.
b. 0, 2, 12, 6, 8.
c. 0, 2, 4, 6, 5.
d. 0, 2, 4, 6, 12.
21. When an argument is passed by reference:
a. a copy of the argument’s value is passed to the called method.
b. changes to the argument do not affect the original variable’s value in the caller.
c. the called method can access the argument’s value in the caller directly and modify that data.
d. the original value is removed from memory.
22. In Java, multidimensional arrays:
a. are not directly supported.
b. are implemented as arrays of arrays.
c. are often used to represent tables of values.
d. All of the above.
23. In array items, which expression below retrieve the value at row 3 and column 5?
a. items[ 3 ].[ 4 ].
b. items[ 3[ 4 ] ].
c. items[ 3 ][ 4 ].
d. items[ 3, 4 ].
24. An array with m rows and n columns is not:
A. An m-by-n array.
B. An n-by-m array.
C. A two-dimensional array.
D. A dual-transcripted array.
a. A and C.
b. A and D.
c. B and D.
d. B and C.
25. Which statement below initializes array items to contain 3 rows and 2 columns?
a. int items[][] = { { 2, 4 }, { 6, 8 }, { 10, 12 } };.
b. int items[][] = { { 2, 6, 10 }, { 4, 8, 12 } };.
c. int items[][] = { 2, 4 }, { 6, 8 }, { 10, 12 };.
d. int items[][] = { 2, 6, 10 }, { 4, 8, 12 };.
26. For the array in the previous question, what is the value returned by items[ 1 ][ 0 ]?
a. 4.
b. 8.
c. 12.
d. 6.
27. Which of the following 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?
a. int items[][] = { { 1, null, null, null }, { 2, 3, 4, 5 }, { 6, 7, null, null } };.
b. int items[][] = { { 1 }, { 2, 3, 4, 5 }, { 6, 7 } };.
c. int items[][] = { { 1 }, { 2, 3, 4, 5 }, { 6, 7 }, {} );.
d. int items[][] = { { 1 }, { 4 }, { 2 } };.
28. 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?
a.
int items[][];
items = new int[ 3 ][ ? ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
b.
int items[][];
items = new int[ 3 ][ ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
c.
int items[][];
items = new int[ ? ][ ? ];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
d.
int items[][];
items[ 0 ] = new int[ 1 ];
items[ 1 ] = new int[ 4 ];
items[ 2 ] = new int[ 2 ];
29. can be used to traverse a two-dimensional array.
a. A do while statement.
b. A for statement.
c. Two nested for statements.
d. Three nested for statements.
30. Which set of statements totals the items in each row of two-dimensional array items, and displays each total?
a.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
total = 0;
for ( int column = 0; column < a[ row ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
b.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
for ( int column = 0; column < a[ row ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
c.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
for ( int column = 0; column < a[ column ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
d.
int total = 0;
for ( int row = 0; row < items.length; row++ )
{
total = 0;
for ( int column = 0; column < a[ column ].length; column++ )
total += a[ row ][ column ];
System.out.printf( "%d\n", total );
}
31. An argument type followed by a(n) in a method’s parameter list indicates that the method receives a variable number of arguments of that particular type.
a. square brackets ([]).
b. ellipsis ( ).
c. varargs keyword.
d. All of the above are acceptable to indicate a variable number of arguments.
32. Which command below runs TestProgram, and passes in the values files.txt and 3?
a. java TestProgram files.txt 3.
b. java TestProgram files.txt, 3.
c. java TestProgram "files.txt", "3".
d. java TestProgram (the arguments files.txt and 3 were passed in when the application was compiled).
33. Which method call converts the value in variable stringVariable to an integer?
a. Convert.toInt( stringVariable ).
b. Convert.parseInt( stringVariable ).
c. Integer.parseInt( stringVariable ).
d. Integer.toInt( stringVariable ).
|