Download Ders 6. Problemler

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Control table wikipedia , lookup

Bloom filter wikipedia , lookup

Comparison of programming languages (associative array) wikipedia , lookup

Array data structure wikipedia , lookup

Transcript
Ders 6. Problemler
1. Arrays may have
a) one
b) two
c) more than two
d) All of the above.
Ans: d
dimensions.
2. Arrays are
data structures.
a) constant
b) dynamic
c) static
d) None of the above.
Ans: c
3. Arrays are data structures consisting of data items of different types.
Ans: False. Arrays are data structures consisting of data items of the same type.
4. The number positioned in square brackets after an array name is the
a) value
b) position
c) size
d) None of the above.
Ans: b
5. 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.
Ans: c
6. 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, C, D.
b) A, B, D.
c) C, D.
d) A, B, C, D.
Ans: d
of an item.
7. 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
Ans: c
8. An array is a group of contiguous memory locations that all have the same name and
type.
Ans: True.
9. The first element in every array is the 0th element.
Ans: True.
10. The index of an array must be an integer; it cannot include variables or expressions.
Ans: False. The index of an array can be an integer, or any integer expression.
11. An array must be counted manually to determine how many items it holds.
Ans: False. Every array knows its own length, which can be accessed by the Length
property.
12. The position number in parentheses is formally called an index.
Ans: False. The position number must be in square brackets.
13. Arrays are allocated with the keyword
a) new
b) array
c) mem
d) None of the above.
Ans: a
.
14. 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.
Ans: c
15. A programmer must do the following before using an array:
a) declare then reference the array
b) create then declare the array
c) create then reference the array
d) declare then create the array
Ans: d
16. 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 second statement creates the array.
c) g is a reference to an array of integers.
d) The value of g[ 3 ] is -1.
Ans: d
17. An array must be declared and allocated in the same statement.
Ans: False. An array can be declared and allocated in separate statements.
18. In an array of reference types, each element may be a reference to a different type.
Ans: False. In an array of reference types, each element is a reference to an object of the
same type.
19. Arrays can be declared to hold only primitive data types.
Ans: False. An array can be declared to hold almost any data type.
20. Arrays are only designed to hold primitive data types and Strings.
Ans: False. Arrays may contain variables of any type.
21. Constants are declared using keyword
a) static
b) const
c) dynamic
d) None of the above.
Ans: b
.
22.Which of the following statements about creating arrays and initializing their elements
is not true?
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.
Ans: c
23.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.
Ans: b
24. 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 );
Ans: a
25. Constant variables also are called
a) write-only variables
b) finals
c) named constants
d) All of the above
Ans: c
.
26. 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.
Ans: b
27. 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
Ans: c
28. 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: 154
c) Result is: 286
d) Result is: 332
Ans: c
29.Which of these statements can be used to complete the following sentence?
Invalid possibilities for array indices include
.
a) positive integers
b) negative integers
c) non-consecutive integers
d) zero
Ans: b
30.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.
Ans: a
31. When values are provided upon declaration of an array, the new keyword is not
required.
Ans: True.
32. A constant must be initialized in the same statement where it is declared and cannot
be modified.
Ans: True.
33. Values in an array can be totaled by using the ArrayTotal method.
Ans: False. If the values of an array need to be totaled, a repetition structure may be used
to traverse the array and add up each element.
34. Programs sometimes use a series of counter variables to summarize data.
Ans: True.
35. C# automatically performs bounds checking to ensure the program doesn’t access
data outside the bounds of an array.
Ans: True.
36. Constants are usually denoted by variable names in all capital letters.
Ans: True.
37. Using arrays can be an elegant way to avoid using sets of counter variables and switch
statements.
Ans: True.
38. Suppose that class Book has been defined. Which set of statements creates an array of
Book objects?
a)
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.
Ans: a
39. When accessing an element of an array, operations (calculations) are not allowed
inside the brackets of an array.
Ans: False. Assuming example is an array defined with 10 elements, example[1+3] is
allowed.
40. What can foreach statements iterate through?
a) arrays
b) collections
c) databases
d) a and b
Ans: d
41. 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.
Ans: c
42. 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.
Ans: b
43. Local variables can be implicitly typed by replacing their type with keyword ______.
a) type
b) unk
c) dim
d) var
Ans: d
44. Depending on the way one implements a foreach loop, it can behave just like a
regular for loop.
Ans: True.
45. foreach loop is barely used because it is more complicated to use than the for loop.
Ans: False. foreach is used by many because of its the simplicity.
46. The foreach repetition structure is a simple structure used to traverse an array.
Ans: True.
47. The foreach repetition structure is useful when the indices of the elements in an
array are not important.
Ans: True.
48. Instance variables can be implicitly typed if they are initialized in the constructor.
Ans: False. Only local variables (declared in a method) can be implicitly typed.
49. There are two types of multidimensional arrays:
a) quadrangular and rectangular
b) rectangular and jagged
c) quadrangular and jagged
d) None of the above
Ans: b
50. 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
Ans: c
51. 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.
Ans: a
52. 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 ]
Ans: d
53. 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. A dual-transcripted array.
a) A and C
b) A and D
c) B and D
d) B and C
Ans: c
54. 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 };
Ans: a
55. For the array in the previous question, what is the value returned by items[ 1, 0 ]?
a) 4
b) 8
c) 12
d) 6
Ans: d
56.
can be 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
Ans: c
57. 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 ];
}
Ans: a
59. Multi-dimensional arrays require two or more indices to identify particular elements.
Ans: True.
60. When dealing with multi-dimensional arrays, each “row” must be the same size.
Ans: 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.
61. Tables are often represented with rectangular arrays.
Ans: True.
62. By convention, the first set of brackets of a two-dimensional array identifies an
element’s column and the second identifies the row.
Ans: False. The first set of brackets identifies the row and the second set of brackets
identifies the column.
63. Jagged arrays are maintained as arrays of arrays.
Ans: True.
64. Write C# statements to accomplish each of the following tasks:
a) Display the value of the element of character array f with index 6.
ANS: Console.Write( f[ 6 ] );
b) Initialize each of the five elements of one-dimensional integer array g to 8.
ANS: int[] g = { 8, 8, 8, 8, 8 );
public static void Main( string[] args )
{
int[ , ] a = { { 1, 2 }, { 3, 4 } };
a[ 1, 1 ] = 5;
} // end Main
} // end PartCCorrect
c) Total the 100 elements of floating-point array c.
ANS: for ( int k = 0; k < c.Length; k++ )
total += c[ k ];
d) Copy 11-element array a into the first portion of array b, which contains 34 elements.
ANS: for ( int j = 0; j < a.Length; j++ )
b[ j ] = a[ j ];
e) Determine and display the smallest and largest values contained in 10-element
floatingpoint
array w.
ANS:
// Exercise 64c Solution: PartC.cs
public class PartC
{
public static void Main( string[] args )
{
double[] c = new double[ 100 ];
double total = 0;
// c)
for ( int k = 0; k < c.Length; k++ )
total += c[ k ];
} // end Main
} // end class PartC
// Exercise 64d Solution: PartD.cs
public class PartD
{
public static void Main( string[] args )
{
double[] a = new double[ 11 ];
double[] b = new double[ 34 ];
// d)
for ( int j = 0; j < a.Length; j++ )
b[ j ] = a[ j ];
} // end Main
} // end class PartD
// Exercise 64e Solution: PartE.cs
using System;
public class PartE
{
public static void Main( string[] args )
{
double[] w = new double[ 10 ];
Random randomNumbers = new Random();
// initialize the elements of w with random doubles
65. Consider the two-by-three rectangular integer array t.
a) Write a statement that declares t and creates the array.
ANS: int[ , ] t = new int[ 2, 3 ];
b) How many rows does t have?
ANS: two.
c) How many columns does t have?
ANS: three.
d) How many elements does t have?
ANS: six.
e) Write the access expressions for all the elements in row 1 of t.
ANS: t[ 1, 0 ], t[ 1, 1 ], t[ 1, 2 ]
f) Write the access expressions for all the elements in column 2 of t.
ANS: t[ 0, 2 ], t[ 1, 2 ]
g) Write a single statement that sets the element of t in row 0 and column 1 to zero.
ANS: t[ 0, 1 ] = 0;
h) Write a sequence of statements that initializes each element of t to 1. Do not use a
repetition
statement.
ANS: t[ 0, 0 ] = 1;
t[ 0, 1 ] = 1;
t[ 0, 2 ] = 1;
t[ 1, 0 ] = 1;
t[ 1, 1 ] = 1;
t[ 1, 2 ] = 1;
i) Write a nested for statement that initializes each element of t to 3.
ANS: for ( int j = 0; j < t.GetLength( 0 ); j++ )
for ( int k = 0; k < t.GetLength( 1 ); k++ )
t[ j, k ] = 3;
for ( int i = 0; i < w.Length; i++ )
w[ i ] = 10 * randomNumbers.NextDouble();
// display the elements of w
Console.WriteLine( "Contents of array w:" );
for ( int i = 0; i < w.Length; i++ )
Console.Write( "{0:F} ", w[ i ] );
// e)
double small = w[ 0 ];
double large = w[ 0 ];
for ( int i = 0; i < w.Length; i++ )
if ( w[ i ] < small )
small = w[ i ];
else if ( w[ i ] > large )
large = w[ i ];
Console.WriteLine( "\nsmallest: {0:F} largest: {1:F}",
small, large );
} // end Main
} // end class PartE
Contents of array w:
6.71 3.95 4.28 3.13 0.31 7.51 3.12 9.95 2.44 2.40
smallest: 0.31 largest: 9.95
j) Write a nested for statement that inputs values for the elements of t from the user.
ANS: for ( int j = 0; j < t.GetLength( 0 ); j++ )
for ( int k = 0; k < t.GetLength( 1 ); k++ )
t[ j, k ] = Convert.ToInt32( Console.ReadLine() );
k) Write a sequence of statements that determines and displays the smallest value in t.
ANS: int small = t[ 0, 0 ];
for ( int j = 0; j < t.GetLength( 0 ); j++ )
for ( int k = 0; k < t.GetLength( 1 ); k++ )
if ( t[ j, k ] < small )
small = t[ j, k ];
Console.WriteLine( small );
l) Write a statement that displays the elements of row 0 of t.
ANS: Console.WriteLine( "{0} {1} {2}", t[ 0, 0 ], t[ 0, 1 ], t[ 0, 2 ] );
m) Write a statement that totals the elements of column 2 of t.
ANS: int total = t[ 0, 2 ] + t[ 1, 2 ];
n) Write a sequence of statements that displays the contents of t in tabular format. List the
column indices as headings across the top, and list the row indices at the left of each row.
ANS: Console.WriteLine( "\t0\t1\t2\n" );
for ( int e = 0; e < t.GetLength( 0 ); e++ )
{
Console.Write( e );
for ( int r = 0; r < t.GetLength( 1 ); r++ )
Console.Write( "\t{0}", t[ e, r ] );
Console.WriteLine();
} // end for
66. (Sales Commissions) Use a one-dimensional array to solve the following problem: A
company
pays its salespeople on a commission basis. The salespeople receive $200 per week plus
9% of
their gross sales for that week. For example, a salesperson who grosses $5000 in sales in
a week receives
$200 plus 9% of $5000, or a total of $650.Write an application (using an array of
counters)
that determines how many of the salespeople earned salaries in each of the following
ranges (assume
that each salesperson’s salary is truncated to an integer amount):
a) $200–299
b) $300–399
c) $400–499
d) $500–599
e) $600–699
f) $700–799
g) $800–899
h) $900–999
i) $1000 and over
Summarize the results in tabular format.
ANS:
// Exercise 66 Solution: Sales.cs
// Application calculates the amount of pay for a salesperson and counts
// the number of salespeople that earned salaries in given ranges.
using System;
public class Sales
{
// counts the number of people in given salary ranges
public void CountRanges()
{
int[] total = new int[ 9 ]; // totals for the various salaries
// read in values and assign them to the appropriate range
Console.Write( "Enter sales amount (negative to end): " );
decimal dollars = Convert.ToDecimal( Console.ReadLine() );
while ( dollars >= 0M )
{
decimal salary = dollars * 0.09M + 200M;
int range = ( int ) ( salary / 100M );
if ( range > 10 )
range = 10;
++total[ range - 2 ];
Console.Write( "Enter sales amount (negative to end): " );
dollars = Convert.ToDecimal( Console.ReadLine() );
} // end while
// display chart
Console.WriteLine( "Range\t\tNumber" );
for ( int range = 0; range < total.Length - 1; range++ )
Console.WriteLine( "${0}-${1}\t{2}", ( 200 + 100 * range ),
( 299 + 100 * range ), total[ range ] );
// special case for the last range
Console.WriteLine( "$1000 and over\t{0}",
total[ total.Length - 1 ] );
} // end method CountRanges
} // end class Sales
67. Write statements that perform the following one-dimensional-array operations:
a) Set the three elements of integer array counts to 0.
ANS: for ( int u = 0; u < counts.Length; u++ )
counts[ u ] = 0;
b) Add 1 to each of the four elements of integer array bonus.
ANS: for ( int v = 0; v < bonus.Length; v++ )
++bonus[ v ];
c) Display the five values of integer array bestScores in column format.
ANS: for ( int w = 0; w < bestScores.Length; w++ )
Console.WriteLine( "{0,6}{1,6}", w, bestScores[ w ] );