Professional Documents
Culture Documents
8.1 Introduction
1. Arrays may have dimensions.
a) one
b) two
c) more than two
d) All of the above.
Answer: d
3. (True/False) 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.
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.
Answer: b
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 2
a) A, C, D.
b) A, B, D.
c) C, D.
d) A, B, C, D.
Answer: d
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 3
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
3. 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. (True/False) 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#.
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 4
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
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
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 5
int result = 0;
for (int i = 0; i < a.Length; ++i)
{
result += a[i];
}
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 6
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.
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 7
15. 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
16. (True/False) When values are provided upon declaration of an array, the new
keyword is not required.
Answer: True.
17. (True/False) A constant must be initialized in the same statement where it is declared
and cannot be modified.
Answer: True.
18. (True/False) 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.
20. (True/False) 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.
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 8
1. When a C# program executes, the runtime checks array element indices for validity—
all indices must be greater than or equal to 0 and less than the length of the array. Any
attempt to access an element outside that range of indices results in a runtime error
known as a(n) ________.
a) IndexRangeError
b) SubscriptException
c) IndexOutOfRangeException
d) SubscriptRangeError
Answer: c) IndexOutOfRangeException
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 9
5. When an exception is caught, the program can access the exception object’s built-in
________ property to get the error message and display it.
a) Error
b) Fault
c) Message
d) Note
Answer: c) Message
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
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 10
4. Suppose that class Book has been defined. Which of the following creates an array of
Book objects?
a)
Book[] books = new Book[numberElements];
b)
Book[] books = new Book()[numberElements];
c)
new Book() books[];
books = new Book[numberElements];
d) All of the above.
Answer: a
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 11
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 12
4. (True/False) 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.
6. (True/False) 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.
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 13
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.
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
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 14
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
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 15
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];
Answer: b
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 16
11. Which set of statements totals the items in each row of two-dimensional array items,
and displays each total?
a)
int total = 0;
b)
int total = 0;
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;
Answer: a
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
Visual C# How to Program, Sixth Edition 17
13. (True/False) 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.
2. (True/False) One could iterate through multi-dimensional arrays by using nested for
loops.
Answer: True.
a) one-dimensional arrays
b) multi-dimensional arrays
c) All of the above
d) None of the above
Answer: a
3. (True/False) Variable-length argument lists allow you to create methods that receive an
arbitrary number of arguments.
Answer: True.
4. (True/False) 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.
3. (True/False) 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. (True/False) Command-line arguments allow the user to pass information into an app
as it begins executing.
Answer: True.
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. (True/False) Passing a reference with keyword ref gives the called method control
over the passed reference itself.
Answer: True.
© Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.