Chapter 8
Which expression adds 1 to the element of array arrayName at index i, assuming the array is of type int? ++arrayName[i] arrayName++[i] arrayName[i++] None of the above.
++arrayName[i]
For the array in the previous question, what is the value returned by items[1, 0]. 4 8 12 6
6
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: 0 3 9 0
9
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, C, D A, B, D C, D A, B, C, D
A, B, C, D
Arrays may have __________ dimensions. one two more than two All of the above.
All of the above.
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.
B and D
Which of the following correctly accesses element 13 of array Book? Book[0] + 13 Book[13] Book[12] None of the above.
Book[13]
[C#6] Which of the following statements is false? Prior to C# 6, auto-implemented properties required both a get and a set accessor. Client code can use C# 6 getter-only auto-implemented properties only to get each property's value. C#6 getter-only auto-implemented properties are read only. C#6 getter-only auto-implemented properties can be initialized only in their declarations.
C#6 getter-only auto-implemented properties can be initialized only in their declarations.
Which of the following will not produce a compiler error? Changing the value of a constant after it is declared. Changing the value at a given index of an array after it's created. Using a final variable before it is initialized. All of the above will produce a compiler errors
Changing the value at a given index of an array after it's created.
Arrays can be declared to hold only non-class data types. True False
False
Changes made to an entire array that has been passed to a method will not affect the original values of the array. True. False.
False
The foreach statement can be used only with one-dimensional arrays. True False
False
The index of an array must be an integer; it cannot include variables or expressions. True False
False
The position number in parentheses is formally called an index. True. False
False
When a method receives a reference-type object parameter by value, the object is actually passed by value. True False
False
When accessing an element of an array, operations (calculations) are not allowed inside the brackets of an array. True False
False
An array must be declared and allocated in the same statement. True. False.
False.
By convention, the first set of brackets of a two-dimensional array identifies an element's column and the second identifies the row. True. False.
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. True. False.
False.
Attempting to access an array element out of the bounds of an array causes a(n) ________. ArrayOutOfBoundsException. ArrayElementOutOfBoundsException IndexOutOfRangeException. ArrayException.
IndexOutOfRangeException
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) ________. IndexRangeError SubcriptException IndexOutOfRangeException SubscriptRangeError
IndexOutOfRangeException
When an exception is caught, the program can access the exception object's built-in ________ property to get the error message and display it. Error Fault Message Note
Message
In rectangular array items, which expression below retrieve the value at row 3 and column 5? items[3. 4] items[3][4] items[3, 4] None of the above.
None of the above.
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: Result is: 62 Result is: 64 Result is: 65 Result is: 67
Result is: 65
Which of the following statements about creating arrays and initializing their elements is false? The new keyword should be used to create an array. When an array is created, the number of elements must be placed in square brackets following the type of element being stored. The elements of an array of integers have a value of null before they are initialized. A for loop is an excellent way to initialize the elements of an array.
The elements of an array of integers have a value of null before they are initialized.
Consider the code segment below. Which of the following statements is false? int[] g; g = new int[23]; The first statement declares an array reference. g is a reference to an array of integers. The value of g[3] is -1.
The value of g[3] is -1.
Which function is called when an object is used where a string should be? TranslateToString() String() ConvertToString() ToString()
ToString()
A constant must be initialized in the same statement where it is declared and cannot be modified. True False
True
Arrays can hold simple types and reference types. True False
True
C# automatically performs bounds checking to ensure the program doesn't access data outside the bounds of an array. . True False
True
Command-line arguments allow the user to pass information into an app as it begins executing. True False
True
Individual elements of arrays are passed to methods by value. True False
True
Jagged arrays are maintained as arrays of arrays. True False
True
One could iterate through multi-dimensional arrays by using nested for loops. True False
True
Passing a reference with keyword ref gives the called method control over the passed reference itself. True False
True
The first element in every array is the 0th element. True False
True
To protect the reference of an array, it should be passed to methods with keyword val. True. False.
True
Values in an array can be totaled by using the ArrayTotal method. True False
True
When values are provided upon declaration of an array, the new keyword is not required. True False
True
Variable-length argument lists allow you to create methods that receive an arbitrary number of arguments. True. False.
True.
[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? Type PropertyName {get, set;} = initializer; Type PropertyName {get | set} = initializer; Type PropertyName {get; set} = initializer; Type PropertyName {get; set;} = initializer;
Type PropertyName {get; set;} = initializer;
If you want to pass an array element into a method by reference, what will you need to do? It always passes the element as a reference automatically. .Use the keyword ref and/or out. All of the above. None of the above, passing in by reference of an array element is only possible if the array type is a reference type.
Use the keyword ref and/or out.
Which of the following statements is false? An exception indicates a problem that occurs while a program executes. Exception handling helps you create fault-tolerant programs When an exception is handled, the program continues executing as if no problem was encountered. The compiler does not detect exceptions.
When an exception is handled, the program continues executing as if no problem was encountered.
What can foreach statements iterate through? arrays collections databases a and b
a and b
What is the naming convention for the parameter in the Main header? par prm args str
args
Rectangular arrays are often used to represent tables of values consisting of information arranged in: rows columns . both a and b none of the above
both a and b
Which foreach header represents iterating through an array of int named numbers? foreach (numbers) foreach (number in numbers) foreach (int number in int[] numbers)
foreach (int number in numbers)
What is the proper foreach header format? (foreach type_identifer in arrayName) (foreach (arrayname) foreach (type_identifer in arrayName) None of the above.
foreach (type_identifer in arrayName)
Consider integer array values, which contains 5 elements. Which statements successfully swap the contents of the array at index 3 and index 4? values[3] = values[4]; values[4] = values[3]; values[4] = values[3]; values[3] = values[4]; int temp = values[3]; values[3] = values[4]; values[4] = temp;
int temp = values[3]; values[3] = values[4]; values[4] = temp;
Which statement below initializes array items to contain 3 rows and 2 columns?
int[,] items = {{2, 4}, {6, 8}, {10, 12}};
Which of the following correctly declares and initializes a two-dimensional rectangular array of integers? int[,] sum = new int[3, 4]; int[] sum = new int[2, 4]; int[] sum = new int[2, 2]; None of the above.
int[,] sum = new int[3, 4];
Which of the following initializer lists would correctly set the elements of array n? int[] n = {1, 2, 3, 4, 5}; array n[int] = {1, 2, 3, 4, 5}; int n[5] = {1, 2, 3, 4, 5}; int n = new int {1, 2, 3, 4, 5};
int[] n = {1, 2, 3, 4, 5};
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? int[][] items = {new int {1, null, null, null}, new int {2, 3, 4, 5}, new int {6, 7, null, null}}; int[][] items = {new int {1}, new int {2, 3, 4, 5}, new int {6, 7}}; int[][] items = {new int {1}, new int {2, 3, 4, 5}, new int {6, 7}, new int {}); int[][] items = {new int {1}, new int {4}, new int {2}};
int[][] items = {new int {1}, new int {2, 3, 4, 5}, new int {6, 7}};
What is the method header for passing in the variable that holds a reference to an array of Strings? method_name(ref String[] array) method_name(String[] ref array) method_name(String[]) None of the above
method_name(ref String[] array)
Constant variables also are called write-only variables write-only named constants All of the above
named constants
Invalid possibilities for array indices include positive integers negative integers non-consecutive integers zero
negative integers
What kinds of arrays can variable-length argument lists work with? one-dimensional arrays multi-dimensional arrays All of the above None of the above
one-dimensional arrays
The keyword _________ overrides an existing method with the same signature. replace override overrule supersede
override
What is the keyword associated with variable-length argument lists? arg params var vla
params
Arrays are _________ data structures. constant dynamic static None of the above.
static
Passing a reference type by value is done to protect: the original object from being modified the reference itself from being modified data outside the bounds of an array All of the above.
the reference itself from being modified
The parameter in the Main header allows for ________? the use of strings the use of strings command-line arguments input and output capacity All of the above
the use of strings command-line arguments