100% found this document useful (17 votes)
109 views

Visual C 2012 How To Program 5th Edition Deitel Test Bank Download

This document contains a test bank with multiple choice questions for Chapter 8 on Arrays from the book Visual C# 2012 How to Program by Deitel. There are over 15 questions covering various topics related to arrays in C# including declaring and initializing arrays, accessing array elements, and using arrays in programs. The questions provide the correct answers.

Uploaded by

Laura Skinner
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (17 votes)
109 views

Visual C 2012 How To Program 5th Edition Deitel Test Bank Download

This document contains a test bank with multiple choice questions for Chapter 8 on Arrays from the book Visual C# 2012 How to Program by Deitel. There are over 15 questions covering various topics related to arrays in C# including declaring and initializing arrays, accessing array elements, and using arrays in programs. The questions provide the correct answers.

Uploaded by

Laura Skinner
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Visual C 2012 How to Program 5th Edition Deitel Test

Bank
Download full solution manual + test bank at:
https://testbankpack.com/

Visual C# 2012 How to Program, Fifth Edition

CHAPTER 8—Arrays

Test Item File

8.1 Introduction
1. Arrays may have dimensions.
a) one
b) two
c) more than two
d) All of the above.
Answer: d

2. Arrays are data structures.


a) constant
b) dynamic
c) static
d) None of the above.
Answer: c

3. Arrays are data structures that may consist of data items of different types.
Answer: False. Arrays are data structures consisting of data items of the same type.

4. Arrays remain the same size once they're created.


Answer: True.

8.2 Arrays
1. The number in square brackets after an array name is the of an item.
a) value
b) position
c) size
d) None of the above.

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Answer: b

2. Which of the following correctly accesses element 13 of array Book?


a) Book[0] + 13
b) Book[13]
c) Book[12]
d) None of the above.
Answer: b

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


A. An array is 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, C, D.
b) A, B, D.
c) C, D.
d) A, B, C, D.
Answer: d

4. 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
Answer: c

5. The first element in every array is the 0th element.


Answer: True.

6. The index of an array must be an integer; it cannot include variables or expressions.


Answer: False. The index of an array can be an integer, or any integer expression.

7. The position number in parentheses is formally called an index.


Answer: False. The position number must be in square brackets.

8.3 Declaring and Creating Arrays


1. Arrays are allocated with the keyword .

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
a) new
b) array
c) table
d) None of the above.
Answer: a

2. 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.
Answer: c

4. Consider the code segment below. Which of the following statements is false?
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.
Answer: d

5. An array must be declared and allocated in the same statement.


Answer: False. An array can be declared and allocated in separate statements.

6. The number of elements in an array must be specified in brackets after the array name
in the declaration.
Answer: False. The number is never specified in the brackets after the array name in
C#.

7. In an array of reference types, each element may be a reference to a different type.


Answer: False. In an array of reference types, each element is a reference to an object
of the same type.

8. Each reference in an array of references is set to null by default when the array is
allocated.
Answer: True.

9. Arrays can be declared to hold only non-class data types.


Answer: False. An array can be declared to hold any data type, including class types.

8.4 Examples Using Arrays


1. An array can be supplied values in its declaration by providing an .

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
a) initializer list
b) index
c) array allocation
d) None of the above.
Answer: a

2. Constants are declared using keyword .


a) static
b) const
c) fixed
d) None of the above.
Answer: b

3.Which of the following statements about creating arrays and initializing their elements is
false?
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.
Answer: c

4.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.
Answer: b

5. 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 );
Answer: a

6. Constant variables also are called .


a) write-only variables
b) finals
c) named constants
d) All of the above
Answer: c

7. 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’s created.

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
c) Using a final variable before it is initialized.
d) All of the above will produce compiler errors.
Answer: b

8. 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
Answer: c

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

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
} // 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: 154
c) Result is: 286
d) Result is: 332
Answer: c

10. Invalid possibilities for array indices include .


a) positive integers
b) negative integers
c) non-consecutive integers
d) zero
Answer: b

11.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.
Answer: a

12. Attempting to access an array element out of the bounds of an array causes a(n)
________.
a) ArrayOutOfBoundsException.
b) ArrayElementOutOfBoundsException.
c) IndexOutOfRangeException.
d) ArrayException.
Answer: c.

13. When values are provided upon declaration of an array, the new keyword is not
required.
Answer: True.

14. A constant must be initialized in the same statement where it is declared and cannot be
modified.
Answer: True.

15. Values in an array can be totaled by using the ArrayTotal method.


Answer: False. If the values of an array need to be totaled, a repetition statement may
be used to traverse the array and add up each element.

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
16. C# automatically performs bounds checking to ensure the program doesn’t access data
outside the bounds of an array.
Answer: True.

8.5 Case Study: Card Shuffling and Dealing Simulation


1. The keyword _______ overrides an existing method with the same signature.
a) replace
b) override
c) overrule
d) supersede
Answer: b

2. Which function is called when an object is used where a string should be?
a) TranslateToString()
b) String()
c) ConvertToString()
d) ToString()
Answer: d

3. 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 ];
Answer: c

4. Suppose that class Book has been defined. Which set of statements creates an array of
Book objects?
a)

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
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.
Answer: a

5. When accessing an element of an array, operations (calculations) are not allowed


inside the brackets of an array.
Answer: False. Assuming example is an array defined with 10 elements,
example[1+3] is allowed.

6. Arrays can hold simple types and reference types.


Answer: True.

8.6 foreach Statement


1. What can foreach statements iterate through?
a) arrays
b) collections
c) databases
d) a and b
Answer: d

2. 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.
Answer: c

3. The foreach repetition statement requires that you 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.
Answer: b

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
4. Local variables can be implicitly typed by replacing their type with keyword ______.
a) type
b) unk
c) dim
d) var
Answer: d

5. The foreach statement is preferred over the for statement when the indices of the
elements in an array will be used in the body of the repetition statement.
Answer: False. The foreach statement cannot access the indices of the elements.

6. Instance variables can be implicitly typed if they’re initialized in the constructor.


Answer: False. Only local variables (declared in a method) can be implicitly typed.

8.7 Passing Arrays and Array Elements to Methods


1. 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.
Answer: b

2. 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 )
Answer: d

3. Consider array items, which contains the values 0, 2, 4, 6 and 8. If method


changeArray is called with 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

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
b) 0, 2, 12, 6, 8
c) 0, 2, 4, 6, 5
d) 0, 2, 4, 6, 12
Answer: d

4. To pass an array argument to a method, specify the name of the array followed by empty
brackets.
Answer: False. To pass an array argument to a method, specify the name of the array
without using brackets.

5. Individual elements of arrays are passed to methods by value.


Answer: True.

6. Changes made to an entire array that has been passed to a method will not affect the
original values of the array.
Answer: False. Arrays are passed to methods by reference; therefore, changes made
within the method are direct changes to the array itself.

8.8 Passing Arrays by Value and by Reference


1. 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.
Answer: b

2. 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.
Answer: a

3. Passing a reference with keyword ref gives the called method control over the passed
reference itself.
Answer: True.

4. When a method receives a reference-type object parameter by value, the object is


actually passed by value.
Answer: False. The object is passed by reference; it is the reference itself that is passed
by value.

5. To protect the reference of an array, it should be passed to methods with keyword val.

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Answer: False. Arrays are by default passed by reference, however it’s the objects
that can be modified by the method, not the reference itself.

8.9 Case Study: Class GradeBook Using an Array to Store


Grades
1. Which foreach header represents iterating through an array of int named
numbers?
a) foreach ( numbers )
b) foreach ( number in numbers )
c) foreach ( int number in numbers )
d) foreach ( int number in int[] numbers )
Answer: c

8.10 Multidimensional Arrays


1. There are two types of multidimensional arrays:
a) quadrangular and rectangular
b) rectangular and jagged
c) quadrangular and jagged
d) None of the above
Answer: b

2. Rectangular arrays are often used to represent tables of values consisting of information
arranged in:
a) rows
b) columns
c) both a and b
d) None of the above
Answer: c

3. Which of the following correctly declares and initializes a two-dimensional rectangular


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.
Answer: a

4. 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 ]
c) None of the above.

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Answer: c

5. 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. An n times m dimensional array.
a) A and C
b) A and D
c) B and D
d) B and C
Answer: c

6. 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 };
Answer: a

7. For the array in the previous question, what is the value returned by items[ 1, 0 ].
a) 4
b) 8
c) 12
d) 6
Answer: d

8. Which of the following statements creates a multidimensional array with 3 rows, where
the first row contains 1 item, the second row contains 4 items and the final row contains 2
items?
a) int[][] items = { new int { 1, null, null, null },
new int { 2, 3, 4, 5 },
new int { 6, 7, null, null } };
b) int[][] items = { new int { 1 },
new int { 2, 3, 4, 5 },
new int { 6, 7 } };
c) int[][] items = { new int { 1 },
new int { 2, 3, 4, 5 },
new int { 6, 7 },
new int {} );
d) int[][] items = { new int { 1 },
new int { 4 },
new int { 2 } };
Answer: b

9. 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 ][ ? ];

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
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 ];
Answer: b

10. is (are) typically 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
Answer: c
11. 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 ];
}

b)

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
int total = 0;

for ( int row = 0; row < items. Length; ++row )


{
for ( int column = 0; column < a[ row ]. Length; ++column )
total += a[ row ][ column ];
}

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

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 ];
}

Answer: a

12. Multi-dimensional arrays require two or more indices to identify particular elements.
Answer: True.

13. When dealing with multi-dimensional arrays, each “row” must be the same size.
Answer: False. Each “row” in a multi-dimensional array does not have to be the same;
this functionality is described in the definition of a jagged array.

14. Tables are often represented with rectangular arrays.


Answer: True.

15. By convention, the first set of brackets of a two-dimensional array identifies an


element’s column and the second identifies the row.
Answer: False. By convention, the first set of brackets identifies the row and the
second set of brackets identifies the column.

16. Jagged arrays are maintained as arrays of arrays.


Answer: True.

8.11 Case Study: Class GradeBook Using a Rectangular Array

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
1. The foreach statement can be used only with one-dimensional arrays.
Answer: False. The foreach statement can be used with multiple-dimension arrays.

2. One could iterate through multi-dimensional arrays by using nested for loops.
Answer: True.

8.12 Variable-Length Argument Lists


1. What is the keyword associated with variable-length argument lists?
a) arg
b) params
c) var
d) vla
Answer: b

2. What kinds of arrays can variable-length argument lists work with?


a) one-dimensional arrays
b) multi-dimensional arrays
c) All of the above
d) None of the above
Answer: a

3. Variable-length argument lists allow you to create methods that receive an arbitrary
number of arguments.
Answer: True.

4. The params modifier can be used anywhere in the method’s header.


Answer: False. The params modifier can occur only in the last entry of the parameter
list.

8.13 Using Command-Line Arguments


1. What is the naming convention for the parameter in the Main header?
a) par
b) prm
c) args
d) str
Answer: c

2. The parameter in the Main header allows for ________?


a) the use of strings
b) command-line arguments
c) input and output capacity
d) All of the above
Answer: b

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
3. When an app is executed from the Command Prompt, the execution environment
passes the command-line arguments to the Main method as a string.
Answer: False. The command-line arguments are passes as a one-dimensional array
of strings.

4. Command-line arguments allow the user to pass information into an app as it begins
executing.
Answer: True.

© Copyright 1992-2014 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.

You might also like