Visual c how to program 6th edition deitel test bank

Page 1

Visual C# How to Program 6th Edition Deitel Test Bank Visit to download the full and correct content document: https://testbankdeal.com/dow nload/visual-c-how-to-program-6th-edition-deitel-test-bank/


Visual C# How to Program, Sixth Edition

1

Chapter 8—Arrays; Introduction to Exception Handling Test Item File

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

dimensions.

2. Arrays are data structures. a) constant b) dynamic c) static d) None of the above. Answer: c 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. 4. (True/False) 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 a) value b) position c) size d) None of the above. Answer: b

of an item.

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

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


Visual C# How to Program, Sixth Edition

2

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. (True/False) The first element in every array is the 0th element. Answer: True. 6. (True/False) 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. (True/False) 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 a) new b) array c) table d) None 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

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 4. (True/False) An array must be declared and allocated in the same statement. Answer: False. An array can be declared and allocated in separate statements. 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#. 6. (True/False) 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. 7. (True/False) Each reference in an array of references is set to null by default when the array is allocated. Answer: True. 8. (True/False) 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 a) initializer list b) index c) array allocation d) None 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

2. Constants are declared using keyword a) static b) const c) fixed d) None of the above. Answer: b

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 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. c) Using a final variable before it is initialized. d) All of the above will produce compiler errors. Answer: b

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


Visual C# How to Program, Sixth Edition

8. Consider the class below: class Test { static void Main() { int[] 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: {result}");

}

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

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

5


Visual C# How to Program, Sixth Edition

6

9. Consider the class below: class Test { static void Main() { 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]; } } }

Console.WriteLine($"Result is: {result}");

}

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.

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


Visual C# How to Program, Sixth Edition

7

13. What can foreach statements iterate through? a) arrays b) collections c) databases d) a and b Answer: d 14. 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 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. 19. (True/False) C# automatically performs bounds checking to ensure the program doesn’t access data outside the bounds of an array. Answer: True. 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

Section 8.5 Using Arrays to Analyze Survey Results; Intro to Exception Handling 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 2. Which of the following statements is false? a) An exception indicates a problem that occurs while a program executes. b) Exception handling helps you create fault-tolerant programs. c) When an exception is handled, the program continues executing as if no problem was encountered. d) The compiler does not detect exceptions. Answer: c) When an exception is handled, the program continues executing as if no problem was encountered. Actually, more severe problems might prevent a program from continuing normal execution, instead causing the program to terminate. 3. Which of the following statements is false? a) When the runtime or a method detects a problem, such as an invalid array index or an invalid method argument, it throws an exception—that is, an exception occurs. b) Exceptions are always thrown by the runtime. c) To handle an exception, place any code that might throw an exception in a try statement. d) The try block contains the code that might throw an exception, and the catch block contains the code that handles the exception if one occurs. Answer: b) Exceptions are always thrown by the runtime. Actually, you also can throw your own exceptions.

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

8


Visual C# How to Program, Sixth Edition

9

4. Which of the following statements is false? a) You can have many catch blocks to handle different types of exceptions that might be thrown in the corresponding try block. b) The runtime performs array bounds checking. c) When an exception is thrown, the try block in which it occurs terminates and a corresponding catch block, if there is one, begins executing—if you declared any variables in the try block, they are accessible in the catch block. d) The catch block declares an exception parameter’s type and name. The catch block can handle exceptions of the specified type. Answer: c) When an exception is thrown, the try block in which it occurs terminates and a corresponding catch block, if there is one, begins executing—if you declared any variables in the try block, they are accessible in the catch block. Actually, if you declared any variables in the try block, they no longer exist, so they’re not accessible in the catch block. 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

8.6 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

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


Visual C# How to Program, Sixth Edition

10

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 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 5. [C#6] Which of the following statements is false? a. Prior to C# 6, auto-implemented properties required both a get and a set accessor. b. Client code can use C# 6 getter-only auto-implemented properties only to get each property’s value. c. C#6 getter-only auto-implemented properties are read only. d. C#6 getter-only auto-implemented properties can be initialized only in their declarations. Answer: d. Getter-only auto-implemented properties can be initialized only in their declarations. Actually these properties can also be initialized in all of the type's constructors. © Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.


Visual C# How to Program, Sixth Edition

11

6.[C#6] Initializing an auto-implemented property in its declaration is a C# 6 feature known as auto-property initializers. Which of the following is the general syntax for a read-write auto-implemented property with an initializer? a. Type PropertyName {get, set;} = initializer; b. Type PropertyName {get | set} = initializer; c. Type PropertyName {get; set} = initializer; d. Type PropertyName {get; set;} = initializer; Answer: d. Type PropertyName {get; set;} = initializer; 7. (True/False) 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. 8. (True/False) Arrays can hold simple types and reference types. Answer: True.

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

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


Visual C# How to Program, Sixth Edition

12

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

a) 0, 2, 5, 6, 12 b) 0, 2, 12, 6, 8 c) 0, 2, 4, 6, 5 d) 0, 2, 4, 6, 12 Answer: d 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. 5. (True/False) Individual elements of arrays are passed to methods by value. Answer: True. 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.

8.8 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.9 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 © Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.


Visual C# How to Program, Sixth Edition

13

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


Visual C# How to Program, Sixth Edition 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

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

14


Visual C# How to Program, Sixth Edition

15

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

© 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; for (int row = 0; row < items.Length; ++row) { total = 0; for (int column = 0; column < a[row].Length; ++column) { total += a[row][column]; } }

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

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

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


Visual C# How to Program, Sixth Edition

17

12. (True/False) Multi-dimensional arrays require two or more indices to identify particular elements. Answer: True. 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. 14. (True/False) Tables are often represented with rectangular arrays. Answer: True. 15. (True/False) 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. (True/False) Jagged arrays are maintained as arrays of arrays. Answer: True.

8.10 Case Study: Class GradeBook Using a Rectangular Array 1. (True/False) The foreach statement can be used only with one-dimensional arrays. Answer: False. The foreach statement can be used with multiple-dimension arrays. 2. (True/False) One could iterate through multi-dimensional arrays by using nested for loops. Answer: True.

8.11 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? © Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.


Visual C# How to Program, Sixth Edition

18

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.

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

8.13 (Optional) Passing Arrays by Value and by Reference © Copyright 1992-2017 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.


Visual C# How to Program, Sixth Edition

19

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. (True/False) Passing a reference with keyword ref gives the called method control over the passed reference itself. Answer: True. 4. (True/False) 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. (True/False) To protect the reference of an array, it should be passed to methods with keyword val. 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.

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


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.