Wednesday, June 3, 2009

Arrays

. Which of the following statements about creating arrays and initializing their elements is not true?
A) The elements of an array of integers have a value of null before they are initialized.
B) A for loop is an excellent way to initialize the elements of an array.
C) When an array is created, the number of elements must be placed in square brackets following the type of element being stored.
D) The new keyword should be used to create an array.

Points Earned: 1.0/1.0
Correct Answer(s): A




2. 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 ];

Console.WriteLine( "Result is: {0}", result );
} // end Main
} // end class Test

The output of this C# program will be:


A) Result is: 62
B) Result is: 64
C) Result is: 65
D) Result is: 67

Points Earned: 1.0/1.0
Correct Answer(s): C




3. Arrays may have ____ dimensions.
A) one
B) two
C) more than two
D) All of the above.

Points Earned: 1.0/1.0
Correct Answer(s): D




4. Which of the following correctly declares and allocates an array of double values?
A) double A[15];
B) double() A = new double[15];
C) double[] A = new double[25];
D) All of the above.

Points Earned: 1.0/1.0
Correct Answer(s): C




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 value of g[ 3 ] is -1.
C) The second statement creates the array.
D) g is a reference to an array of integers.

Points Earned: 1.0/1.0
Correct Answer(s): B




6. Arrays are ____ data structures.
A) constant
B) dynamic
C) static
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): C




7. 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

Console.WriteLine( "Result is: {0}", result );
} // end Main
} // end class Test

The output of this C# program will be:


A) Result is: 280
B) Result is: 286
C) Result is: 154
D) Result is: 332

Points Earned: 1.0/1.0
Correct Answer(s): B




8. Constant variables also are called ____.
A) write-only variables
B) finals
C) named constants
D) All of the above

Points Earned: 1.0/1.0
Correct Answer(s): C




9. The number positioned in square brackets after an array name is the ____ of an item.
A) value
B) position
C) size
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): B




10. Which of the following correctly accesses the 13th element of array Book?
A) Book[0] + 13
B) Book[13]
C) Book[12]
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): C




11. Arrays are allocated with the keyword .
A) new
B) array
C) mem
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): A




12. An array can be supplied values upon declaration by providing an ____.
A) initializer list
B) index
C) array allocation
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): A




13. Invalid possibilities for array indices include ____.
A) positive integers
B) non-consecutive integers
C) zero
D) negative integers

Points Earned: 1.0/1.0
Correct Answer(s): D




14. 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.

Points Earned: 1.0/1.0
Correct Answer(s): B




15. 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 );

Points Earned: 1.0/1.0
Correct Answer(s): A




16. Constants are declared using keyword ____.
A) static
B) const
C) dynamic
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): B




17. 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) 3
B) 0
C) 0
D) 9

Points Earned: 1.0/1.0
Correct Answer(s): D




18. A programmer must do the following before using an array:
A) create then reference the array
B) create then declare the array
C) declare then create the array
D) declare then reference the array

Points Earned: 0.0/1.0
Correct Answer(s): C




19. What do the following statements do?

double[] array;
array = new double[ 14 ];
A) Creates a double array containing 14 elements.
B) Declares but does not create a double array.
C) Creates a double array containing 13 elements.
D) Creates a double array containing 15 elements.

Points Earned: 1.0/1.0
Correct Answer(s): A




20. Which of the following statements about arrays are true?

A. Arrays are a group of variables 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, B, D.
B) A, B, C, D.
C) A, C, D.
D) C, D.

Points Earned: 1.0/1.0
Correct Answer(s): B




21. Which expression adds 1 to the element of array arrayName at index i, assuming the array is of type int?
A) ++arrayName[ i ]
B) arrayName++[ i ]
C) arrayName[ i++ ]
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): A




22. Attempting to access an array element out of the bounds of an array, causes a(n) ____ to occur.
A) IndexOutOfRangeException.
B) ArrayElementOutOfBoundsException.
C) ArrayOutOfBoundsException.
D) ArrayException.

Points Earned: 1.0/1.0
Correct Answer(s): A




23. Rectangular arrays are often used to represent tables of values consisting of information arranged in:
A) rows
B) columns
C) both rows and columns
D) None of the above

Points Earned: 1.0/1.0
Correct Answer(s): C




24. The foreach repetition structure requires the programmer provide an array and a variable for the purpose of:
A) preventing the structure from going past the end of the array
B) storing the value of each element that is traversed
C) acting as a counter to traverse the array
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): B




25. What is the proper foreach header format?
A) ( foreach type_identifer in arrayName )
B) foreach ( arrayName )
C) foreach ( type_identifer in arrayName )
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): C




26. 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, 12, 6, 8
B) 0, 2, 4, 6, 12
C) 0, 2, 5, 6, 12
D) 0, 2, 4, 6, 5

Points Earned: 1.0/1.0
Correct Answer(s): B




27. Which method call does the method header void ModifyArray(double[] B) represent? Assume that the array being passed in is already defined and is called list.
A) ModifyArray( double list[] )
B) ModifyArray( double[] list )
C) ModifyArray( double[] : list )
D) ModifyArray( list )

Points Earned: 1.0/1.0
Correct Answer(s): D




28. What can foreach statements iterate through?
A) databases
B) both arrays and collections
C) collections only
D) arrays only

Points Earned: 1.0/1.0
Correct Answer(s): B




29. The keyword _______ overrides an existing method with the same signature.
A) overrule
B) replace
C) override
D) supersede

Points Earned: 1.0/1.0
Correct Answer(s): C




30. What is the method header for passing in the variable that holds a reference to an array of Strings?
A) method_name( ref String[] array)
B) method_name( String[] ref array)
C) method_name( String[] )
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): A




31. There are two types of multidimensional arrays:
A) quadrangular and rectangular
B) rectangular and jagged
C) quadrangular and jagged
D) None of the above

Points Earned: 1.0/1.0
Correct Answer(s): B




32. Which function is called when an object is used where a string should be?
A) TranslateToString()
B) ConvertToString()
C) String()
D) ToString()

Points Earned: 1.0/1.0
Correct Answer(s): D




33. If you want to pass an array element into a method by reference, what will you need to do?
A) It always passes the element as a reference automatically.
B) Use the keyword ref and/or out.
C) All of the above.
D) None of the above, passing in by reference of an array element is only possible if the array type is a reference type.

Points Earned: 1.0/1.0
Correct Answer(s): B




34. Passing a reference type by value is done to protect:
A) the original object from being modified
B) the reference itself from being modified
C) data outside the bounds of an array
D) All of the above.

Points Earned: 1.0/1.0
Correct Answer(s): B




35. In rectangular 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 ]

Points Earned: 1.0/1.0
Correct Answer(s): D




36. 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)
int temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = values[ 3 ];
C)
int temp = values[ 3 ];
values[ 3 ] = values[ 4 ];
values[ 4 ] = temp;
D)
values[ 4 ] = values[ 3 ];
values[ 3 ] = values[ 4 ];

Points Earned: 1.0/1.0
Correct Answer(s): C




37. Suppose that class Book has been defined. Which set of statements creates an array of Book objects? Assume numberElements holds a positive integer.
A)
Book[] books;
books = new Book[ numberElements ];
B)
Book[] books;
books = new Book()[ numberElements ];
C)
new Book() books[];
books = new Book[ numberElements ];
D)
All three achieve the desired result.

Points Earned: 1.0/1.0
Correct Answer(s): A




38. Which foreach header represents iterating through an array of int named numbers?
A) foreach ( number in numbers )
B) foreach ( int number in int[] numbers )
C) foreach ( numbers )
D) foreach ( int number in numbers )

Points Earned: 1.0/1.0
Correct Answer(s): D




39. Which of the following correctly declares and initializes a two-dimensional array of integers?
A) int[,] sum = new int[3, 4];
B) int[] sum = new int[2, 4];
C) int sum[] = new int[2, 2];
D) None of the above.

Points Earned: 1.0/1.0
Correct Answer(s): A




40. Local variables can be implicitly typed by replacing their type with keyword ______.
A) var
B) unk
C) type
D) dim

Points Earned: 1.0/1.0
Correct Answer(s): A




41. Values in an array can be totaled by using the ArrayTotal method.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




42. Arrays remain the same size once they are created.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




43. A constant must be initialized in the same statement where it is declared and cannot be modified.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




44. The first element in every array is the 0th element.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




45. Each reference in an array of references is pointed to null by default when the array is allocated.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




46. The position number in parentheses is formally called an index.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




47. Arrays are data structures consisting of data items of different types.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




48. Using arrays can be an elegant way to avoid using sets of counter variables and switch statements.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




49. An array must be declared and allocated in the same statement.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




50. Arrays are only designed to hold primitive data types and Strings.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




51. Arrays can be declared to hold only primitive data types.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




52. When values are provided upon declaration of an array, the new keyword is not required.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




53. An array must be counted manually to determine how many items it holds.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




54. In an array of reference types, each element may be a reference to a different type.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




55. C# automatically performs bounds checking to ensure the program doesn’t access data outside the bounds of an array.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




56. Constants are usually denoted by variable names in all capital letters.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




57. Programs sometimes use a series of counter variables to summarize data.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




58. An array is a group of contiguous memory locations that all have the same name and type.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




59. The index of an array must be an integer; it cannot include variables or expres­sions.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




60. The number of elements in the array must be specified in brackets after the array name in the declaration.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




61. Changes made to an entire array that has been passed to a method will not affect the original values of the array.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




62. When a method receives a reference-type object parameter by value, the object is actually passed by value.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




63. Jagged arrays are maintained as arrays of arrays.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




64. Depending on the way one implements a foreach loop, it can behave just like a regular for loop.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




65. One could iterate through multi-dimensional arrays by using nested for loops.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




66. Instance variables can be implicitly typed if they are initialized in the constructor.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




67. When accessing an element of an array, operations (calculations) are not allowed inside the brackets of an array.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




68. The foreach repetition structure can only be used with single-dimensional arrays.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




69. Multi-dimensional arrays require two or more indices to identify particular elements.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




70. Tables are often represented with rectangular arrays.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




71. By convention, the first set of brackets of a two-dimensional array identifies an element’s column and the second identifies the row.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




72. To protect the reference of an array, it should be passed to methods with key­word val.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




73. The foreach repetition structure is useful when the indices of the elements in an array are not important.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




74. The foreach loop is rarely used because it is more complicated to use than the for loop.
A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




75. Passing a reference with keyword ref gives the called method control over the passed reference itself.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




76. The foreach repetition structure is a simple structure used to traverse an array.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




77. Individual elements of arrays are passed to methods by value.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




78. Arrays are not limited to just simple types, but can hold reference types as well.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): True




79. When dealing with multi-dimensional arrays, each “row” must be the same size.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False




80. To pass an array argument to a method, specify the name of the array followed by empty brackets.A) True
B) False

Points Earned: 1.0/1.0
Correct Answer(s): False

No comments:

Post a Comment