chapter 7 - arraysip204-5.sth.wku.edu/cs146web/quizzes/chapter07.pdf17. what two numbers are...
TRANSCRIPT
Chapter 7 Arrays
Section 7.1 Creating and Using Arrays
1. After the following Dim statement is executed, how many elements will the array myVar
have?
Dim myVar(7) As Double
(A) 0
(B) 1
(C) 8
(D) 9
C
2. In the line of code
Dim scores() As Integer = {55, 33, 12}
the upper bound of the array scores is which of the following?
(A) 2
(B) 1
(C) 11
(D) 0
A
3. Each individual variable in the list
student(0), student(1), student(2)
is known as a(n)
(A) subscript.
(B) dimension.
(C) element.
(D) type.
C
4. In the statement
Dim scores(30) As Double
the number 30 designates which of the following?
(A) the highest value of the subscripts of the elements for the array scores
(B) the maximum value that can be assigned to any element in the array scores
(C) the data type for the array scores
(D) the value initially assigned to each element in the array scores
A
5. Which statement is true regarding the following Dim statement?
Dim states(49) As String, populations(49) As Double
(A) It is invalid since more than one array is dimensioned by a single Dim statement.
(B) It is invalid since the two arrays must have the same data type.
(C) The subscripts of states range from 1 to 49.
(D) The subscripts of populations range from 0 To 49.
D
6. The Count method returns what information about an array?
(A) the highest number that can be used as a subscript for the array
(B) the largest value that can be assigned to an array element
(C) the number of elements in the array
(D)tThe highest dimension of the array
C
7. In the line of code
For index As Integer = 0 to (score.Count - 1)
the Count method is used to carry out which of the following tasks?
(A) determine the largest value for each of the elements
(B) determine the largest subscript in the array
(C) determine the smallest value for each of the elements
(D) declare a new array with the name Count
B
8. The ReDim statement causes an array to lose its current contents unless the word ReDim is
followed by the keyword
(A) CInt
(B) MyBase
(C) Preserve
(D) Add
C
9. Like other variables, array variables can be declared and assigned initial values at the same
time. (T/F)
T
10. After an array has been declared, its type (but not its size) can be changed with a ReDim
statement. (T/F)
F
11. If you use the ReDim statement to make an array smaller than it was, data in the eliminated
elements can be retrieved by using the Preserve keyword. (T/F)
F
12. What will be the size of the array stones after the following two lines of code are executed?
Dim stones() As String = {"Watts", "Jagger", "Wood", "Richards"}
ReDim Preserve stones(10)
11
13. The statement
Dim newlist(10) As String
is used to declare an array where each element has the value 10. (T/F)
F
14. In the line of code
Function Sum(scores() As Integer) As Integer
the pair of parentheses that follows scores can be removed. (T/F)
F
15. In the line of code
Dim scores() As Integer = {55, 33, 12}
the upper bound of the array scores is 12. (T/F)
F
16. What two names are displayed in the list box when the button is clicked on?
Dim krispies() as String
Private Sub frmCereal_Load(...) Handles MyBase.Load
krispies(0) = "snap"
krispies(1) = "crackle"
krispies(2) = "pop"
End Sub
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
lstBox.Items.Add(krispies.Max)
lstBox.Items.Add(krispies.Last)
End Sub
(A) crackle and pop
(B) crackle and snap
(C) snap and crackle
(D) snap and pop
D
17. What two numbers are displayed in the list box when the button is clicked on?
Dim nums() as Integer
Private Sub frmNumbers_Load(...) Handles MyBase.Load
nums(0) = 5
nums(1) = 3
nums(2) = 4
End Sub
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
lstBox.Items.Add(nums.Average)
lstBox.Items.Add(nums.Max)
End Sub
(A) 4 and 5
(B) 4 and 4
(C) 3 and 5
(D) 3 and 4
A
18. Either a For...Next loop or a For Each loop can be used to display every other value from an
array in a list box. (T/F)
F
19. An array can contain both numeric and string values. (T/F)
F
20. A Function procedure can return a number, but cannot return an array of numbers. (T/F)
F
21. What names are displayed in the list box when the button is clicked on?
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim names() As String = IO.File.ReadAllLines("Data.txt")
lstBox.Items.Clear()
For i As Integer = (names.Count - 1) To 0 Step -2
lstBox.Items.Add(names(i))
Next
End Sub
Assume the five lines of the file Data.txt contain the following entries: Bach, Borodin,
Brahms, Beethoven, Britain.
(A) Bach, Brahms, and Britain
(B) Britain, Beethoven, Brahms, Borodin, and Bach
(C) Bach, Borodin, Brahms, Beethoven, and Britain
(D) Britain, Brahms, and Bach
D
22. What names are displayed in the list box when the button is clicked on?
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim file As String = "Ships.txt"
Dim ships() As String = FillArray(file)
lstBox.Items.Add(ships(2))
lstBox.Items.Add(ships.Min)
End Sub
Function FillArray(file As String) As String()
Dim names() As String = IO.File.ReadAllLines(file)
Return names
End Function
Assume the three lines of the file Ships.txt contain the following entries: Pinta, Nina, Santa
Maria.
(A) Pinta and Nina
(B) Santa Maria and Pinta
(C) Nina and Santa Maria
(D) Santa Maria and Nina
D
23. What numbers are displayed in the list box when the button is clicked on?
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim file As String = "Beatles.txt"
Dim fabFour() As String = FillArray(file)
lstBox.Items.Add(Array.IndexOf(fabFour, "Ringo")
lstBox.Items.Add(fabFour.Count - 1)
End Sub
Function FillArray(file As String) As String()
Dim names() As String = IO.File.ReadAllLines(file)
Return names
End Function
Assume the four lines of the file Beatles.txt contain the following entries: John, Paul, Ringo,
George.
(A) 3 and 3
(B) 3 and 4
(C) 2 and 3
(D) 2 and 4
C
24. What names are displayed in the list box by the following program segment?
Dim newYork As String = "Manhatten,Bronx,Brooklyn,Queens,Staten Island"
Dim boroughs() As String = newYork.Split(","c)
lstBox.Items.Add(boroughs(0))
lstBox.Items.Add(borought.Min)
(A) Brooklyn and Queens
(B) Manhatten and Staten Island
(C) Bronx and Manhatten
(D) Manhatten and Bronx
D
25. What numbers are displayed in the list box by the following program segment?
Dim numbers As String = "1492,1776,1945"
Dim temp() As String = numbers.Split(","c)
Dim nums(2) As Integer
For i As Integer = 0 to 2
nums(i) = CInt(temp(i))
Next
lstBox.Items.Add(nums(1))
lstBox.Items.Add(nums.First)
(A) 1776 and 1492
(B) 1776 and 1776
(C) 1492 and 1492
(D) 1945 and 1492
A
26. What is the output of the following program segment?
Dim nums(8) As Integer
Dim total As Double = 0
For i As Integer = 0 To 8
nums(i) = i
Next
For k As Integer = 1 To 4
total += 10 ^ nums(k)
Next
txtBox.Text = CStr(total)
(A) 10000
(B) 11110
(C) 1110
(D) 0
B
27. What will be displayed when the following program segment is executed? Dim temp() As String = IO.File.ReadAllLines("Data.txt")
Dim n As Integer = temp.Count - 1
Dim a(n) As Double
For k As Integer = 0 To n
a(k) = CDbl(temp(i))
Next
txtBox.Text = CStr(a(3))
Assume the five rows of the file Data.txt contain the following entries: 3, 2, 5, 1, 4.
(A) 1
(B) 2
(C) 3
(D) 4
A
28. What is the output of the following program segment?
Dim temp() As String = IO.File.ReadAllLines("Data.txt")
Dim n As Integer = temp.Count - 1
Dim numbers(n) As Double, h As Double = 0
For i As Integer = 0 To n
numbers(i) = CDbl(temp(i))
Next
For k As Integer = 0 to n
h += numbers(k)
Next
txtBox.Text = CStr(h)
Assume the four rows of the file Data.txt contain the following entries: 2, 4, 2, 3
(A) 11
(B) 2
(C) 7
(D) 4
A
29. Given the Dim statement below, which set of statements will initialize all elements of
myArray to 100?
Dim myArray(100) As Double
(A) myArray = 100
(B) For i As Integer = 0 To 100 (i) = 100
Next
(C) For j As Integer = 0 to 100 myArray(j) = 100
Next
(D) myArray() is already initialized to 100 by the Dim statement.
C
30. What is the output of the following program segment?
Dim c As Double
Dim temp() As String = IO.File.ReadAllLines("Data1.txt")
Dim n As Integer = temp.Count - 1
Dim a(n) As Double
For i As Integer = 0 To n
a(i) = CDbl(temp(i))
Next
temp() = IO.File.ReadAllLines("Data2.txt")
Dim b(n) As Double
For i As Integer = 0 To n
b(i) = CDbl(temp(i))
Next
For k As Integer = 0 To n
If a(k) = b(k) Then
c += 1
End If
Next
lstBox.Items.Add(c)
Assume the twenty rows of the file Data1.txt contain the following entries: 3, 2, 5, 1, 7, 8, 3,
5, 6, 2, 3, 6, 1, 6, 5, 5, 7, 2, 5, 3.
Assume the twenty rows of the file Data2.txt contain the following entries: 5, 3, 3, 4, 8, 2, 3,
5, 9, 5, 3, 7, 3, 7, 6, 3, 2, 1, 3, 4.
(A) 2
(B) 3
(C) 4
(D) 5
(E) 6
B
31. What is displayed in the message box by the following code?
Dim message As String
Dim teamNames() As String = {"Packers", "Jets", "Seahawks"}
ReDim Preserve teamName(1)
message = teamNames(1)
MessageBox.Show(message)
(A) Packers
(B) Jets
(C) Seahawks
(D) 2
B
32. If the following statement appears in a program, the array scores must have been declared
using the String data type. (T/F)
scores(1) = 87
F
33. Consider the following Dim and assignment statements for myArray(). The assignment
statement will cause a "Subscript out of range" error. (T/F)
Dim myArray(50) As Double
myArray(34) = 51
F
34. Unless otherwise specified, Visual Basic assigns the value 0 to each element of a numeric
array when it is declared with a Dim statement. (T/F)
T
35. The following pair of statement is valid. (T/F)
x = CInt(InputBox("Enter number of items (must be a positive integer)"))
ReDim myArray(x - 1)
T
36. A fractional number such as 4.6 is not allowed as a subscript. (T/F)
T
37. Consider the following Visual Basic statements:
Dim nums(4) As Double
For index As Integer = 0 To 4
nums(index) = 1 + (index * 2)
Next
What values are placed in the array by the above statements? (Show each element of the
array and the value for that element.)
nums(0)=1, nums(1)=3, nums(2)=5, nums(3)=7, nums(4)=9
38. What is the output of the following program when the button is clicked on?
Private Sub btnDisplay_Click(...) Handles btnDisplay.Click
Dim result As Double
Dim number(4) As Double
FillArray(number)
result = SumArray(number)
txtBox.Text = CStr(result)
End Sub
Sub FillArray(ByRef anyArray() As Double)
Dim temp() As String = IO.File.ReadAllLines("Data.txt")
For i As Integer = 0 To 4
anyArray(i) = CDbl(temp(i))
Next
End Sub
Function SumArray(anyArray() As Double) As Double
Dim total As Double
total = 0
For i As Integer = 0 To 4 Step 2
total += anyArray(i)
Next
Return total
End Function
Assume the five rows of the file Data.txt contain the following entries: 1, 3, 5, 7, 9
(A) 0
(B) 25
(C) 15
(D) None of the above
C
39. Which of the tasks is the Join function used to carry out in the following statement?
Dim line As String
line = Join(strArrData, ",")
(A) Join concatenates the values of all elements of the array strArrData, and adds a comma
delimiter between successive values.
(B) Join concatenates the values of all elements of line, and adds a comma to the end of the
line.
(C) Join parses or separates out all items of text that are delimited by a comma in
strArrData.
(D) Join parses or separates out all items of text that are delimited by a comma in line.
A
Section 7.2 Using LINQ with Arrays
1. What names are displayed in the list box by the following lines of code?
Dim oceans() As String = {"Atlantic", "Pacific", "Indian", "Arctic",
"Antartic"}
Dim query = From ocean in oceans
Where ocean.Length = 6
Select ocean
For Each ocean As String In query
lstBox.Items.Add(ocean)
Next
(A) Pacific and Indian
(B) Indian and Arctic
(C) Indian
(D) Atlantic and Pacific
B
2. What numbers are displayed in the list box by the following program segment?
Dim numbers() As Integer = {4, 7, 9, 3, 1}
Dim query = From number in numbers
Where number > 6
Select number
lstBox.Items.Add(query.Count)
lstBox.Items.Add(query.Average)
(A) 5 and 12
(B) 2 and 12
(C) 2 and 8
(D) 5 and 8
C
3. What numbers are displayed in the list box by the following program segment?
Dim numbers() As Integer = {4, 7, 9, 3, 1, 9, 7}
Dim query = From number in numbers
Where number > 6
Select number
lstBox.Items.Add(query.Count)
lstBox.Items.Add(query.Average)
(A) 7 and 12
(B) 4 and 8
(C) 2 and 12
(D) 7 and 8
B
4. What numbers are displayed in the list box by the following program segment?
Dim numbers() As Integer = {4, 7, 9, 3, 1, 7, 7}
Dim query = From number in numbers
Where number > 6
Select number
Distinct
lstBox.Items.Add(query.Count)
lstBox.Items.Add(query.Average)
(A) 5 and 12
(B) 2 and 12
(C) 2 and 8
(D) 5 and 8
C
5. What states are displayed in the list box by the following program segment?
Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"}
Dim query = From state in states
Where ContainsE(state)
Select state
For Each state in query
lstBox.Items.Add(state)
Next
Function ContainsE(word As String) As Boolean
If word.IndexOf("E") <> -1 Or word.IndexOf("e") <> -1 Then
Return True
Else
Return False
End If
End Function
(A) Colorado
(B) New Mexico
(C) Colorado, New Mexico, Arizona, Utah
(D) No states
B
6. What states are displayed in the list box by the following program segment?
Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"}
Dim query = From state in states
Where state.length < 5
Select state.ToUpper
For Each state in query
lstBox.Items.Add(state)
Next
(A) Utah
(B) COLORADO, NEW MEXICO, ARIZONA, UTAH
(C) UTAH
(D) No states
C
7. What numbers are displayed in the list box by the following program segment?
Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"}
Dim query = From state in states
Where state.EndsWith("o")
Select state.Length
For Each number in query
lstBox.Items.Add(number)
Next
(A) 8 and 10
(B) 8, 10, 7, 4
(C) 8
(D) 29
A
8. What names are displayed in the list box by the following program segment?
Dim tonightShow() As String = {"Allen", "Parr", "Carson", "Leno",
"O'Brien", "Leno"}
Dim query = From host in tonightShow
Where host.Length = 4
Select host
Distinct
For Each host in query
lstBox.Items.Add(host)
Next
(A) Parr, Leno, Leno
(B) Parr, Leno
(C) Leno
(D) No names
B
9. What numbers are displayed in the list box by the following program segment?
Dim numbers() As Double = {.5, 1, 2, 2.5}
Dim query = From number in numbers
Where number < 2
Let FormattedPer = number.ToString("P")
Select FormattedPer
For Each percent In query
lstBox.Items.Add(percent)
Next
(A) .5 and 1
(B) 50% and 100%
(C) 50.00%, 100.00%, 200.00%, 250.00%
(D) 50.00% and 100.00%
D
10. What years are displayed in the list box by the following program segment?
Dim years() As Integer = {1492, 1776, 1840, 1929, 1945, 2005}
Dim query = From year in years
Where Is20thCentury(year)
Select year
For Each year in query
lstBox.Items.Add(year)
Next
Function Is20thCentury(num As Integer) As Boolean
If (num >= 1900) and (num < 2000) Then
Return True
Else
Return False
End IF
End Function
(A) 1929 and 1945
(B) 1929
(C) 1492, 1776, 1840, 1929, 1945, 2005
(D) No years
A
11. What states are displayed in the list box by the following program segment?
Dim states() As String = {"Colorado", "New Mexico", "Arizona", "Utah"}
Dim query = From state in states
Order By state Ascending
Select state
lstBox.Items.Add(query.First)
lstBox.Items.Add(query.Min)
(A) Arizona and Colorado
(B) Arizona and Utah
(C) Colorado and Arizona
(D) Arizona and Arizona
D
12. What words are displayed in the list box by the following program segment?
Dim deadlySins() As String = {"pride", "greed", "anger", "envy",
"lust", "gluttony", "sloth"}
Dim query = From sin in deadlySins
Order By sin Ascending
Where sin.StartsWith("g")
Select sin
lstBox.Items.Add(query.First)
lstBox.Items.Add(query.Max)
(A) gluttony and greed
(B) gluttony and gluttony
(C) pride and gluttony
(D) greed and greed
A
13. What words are displayed in the list box by the following program segment?
Dim deadlySins() As String = {"pride", "greed", "anger", "envy",
"lust", "gluttony", "sloth"}
Dim query = From sin in deadlySins
Order By sin.Length Descending
Select sin.ToUpper
lstBox.Items.Add(query.First)
lstBox.Items.Add(query.Min)
(A) GLUTTONY and GLUTTONY
(B) GLUTTONY and SLOTH
(C) GLUTTONY and ANGER
(D) PRIDE and ENVY
C
14. What colleges are displayed in the list box by the following program segment?
Dim ivies() As String = {"Harvard", "Princeton", "Yale", "Dartmouth",
"Brown", "Columbia", "Univ. of PA", "Cornell"}
Dim query = From college in ivies
Order By college Descending
Select college
lstBox.Items.Add(query.First)
lstBox.Items.Add(query.Min)
(A) Yale and Brown
(B) Yale and Yale
(C) Brown and Brown
(D) Harvard and Brown
A
15. What colleges are displayed in the list box by the following program segment?
Dim ivies() As String = {"Harvard", "Princeton", "Yale", "Dartmouth",
"Brown", "Columbia", "Univ. of PA", "Cornell"}
Dim query = From college in ivies
Where college.Length <= 9
Order By college.Length Descending, college Descending
Select college
lstBox.Items.Add(query.Last)
lstBox.Items.Add(query.Min)
(A) Dartmouth and Princeton
(B) Yale and Brown
(C) Yale and Cornell
(D) Dartmouth and Yale
B
16. What colleges are displayed in the list box by the following program segment?
Dim ivies() As String = {"Harvard", "Princeton", "Yale", "Dartmouth",
"Brown", "Columbia", "Univ. of PA", "Cornell"}
Dim query = From college in ivies
Where college.Length <= 9
Order By college.Length Descending, college Ascending
Select college
lstBox.Items.Add(query.First)
lstBox.Items.Add(query.Max)
(A) Dartmouth and Princeton
(B) Yale and Brown
(C) Yale and Cornell
(D) Dartmouth and Yale
D
17. What words are displayed in the list box by the following program segment?
Dim dimensions() As String = {"width", "height", "depth"}
Dim query = From dimension in dimensions
Order By dimension.Length Descending, dimension Ascending
Select dimension
lstBox.DataSource = query.ToList
lstBox.SelectedIndex = Nothing
(A) width, height, depth
(B) height, depth, width
(C) height, width, depth
(D) depth, width, height
B
18. What numbers are displayed in the list box by the following program segment?
Dim numbers() As Integer = {5, 79, 8, 33, 27}
Dim query = From number in numbers
Let formattedNum = number.ToString("N0")
Order By formattedNum Ascending
Select formattedNum
lstBox.DataSource = query.ToList
lstBox.SelectedIndex = Nothing
(A) 5, 8, 27, 33, 79
(B) 79, 33, 27, 8, 5
(C) 27, 33, 5, 79, 8
(D) 5, 79, 8, 33, 27
C
19. In an ascending ordered array, the value of each element is
(A) less than or equal to the value of the next element.
(B) greater than or equal to the value of the next element.
(C) equal to the value of the next element.
(D) greater than the value of the next element.
A
20. Which of the following is NOT an example of an ordered array?
(A) years
1877 1944 2011 4301
(B) cities
Selah Wapato Yakima Zillah
(C) nbrhoods
Hockinson Brush Prairie Dollars Corner Battle Ground
(D) nums
457 457 457 458
C
21. Which of the following is an ordered descending array?
(A) years
1011 1112 1112 1377
(B) cities
Selah Wapato Yakima Zillah
(C) nbrhoods
Parkrose Hockinson Dollars Corner Battle Ground
(D) nums
459 457 457 458
C
22. Arrays are said to be ordered only if the values are in ascending order. (T/F)
F
23. Searching successive elements of an ordered list beginning with the first element is known
as a binary search. (T/F)
F
Section 7.3 Some Additional Types of Arrays
1. Consider the following structure definition. Which Dim statement would correctly declare
an array of this structure for elements having subscripts from 0 through 30?
Structure carType
Dim yr As Integer
Dim make As String
Dim model As String
End Structure
(A) You cannot have an array of structures.
(B) Dim carType(30)
(C) Dim car(30) As carType
(D) Dim carType(30) As car
C
2. Consider the following Structure definition and declaration. Which assignment statement
would correctly record that player number 13 had three home runs so far this season?
Structure playerType
Dim fullname As String
Dim team As String
Dim position As String
Dim homerun As Double
Dim average As Double
Dim rbi As Double
End Structure
Dim player(45) As playerType
(A) player(13) = 3
(B) player(13).homerun(3)
(C) playerType(13).homerun = 3
(D) player(13).homerun = 3
(E) None of the above
D
3. Suppose a structure is created with the code
Structure stateUSA
Dim capCity As String
Dim stateNum As Integer
End Structure
in the Declarations section of the Code Editor. Which of the following statements correctly
declares a variable of type stateUSA?
(A) Dim stateUSA As stateWA
(B) Dim stateWA As Structure
(C) Dim stateWA As stateUSA
(D) Dim stateWA As Member
C
4. Suppose a structure is created with the code
Structure stateUSA
Dim capCity As String
Dim stateNum As Integer
End Structure
in the Declarations section of the Code window. The code
Dim stateWA As stateUSA
Dim message As String
stateWA.stateNum = 42
stateWA.capCity = "Olympia"
message = stateWA.capCity & " " & stateWA.stateNum
MessageBox.Show(message)
is placed in the Click event procedure for one of the program’s buttons. What output will be
displayed in the message box when the button is clicked on?
(A) 42 Olympia
(B) Olympia42
(C) 42Olympia
(D) Olympia 42
D
5. The members of a structure must all be of the same data type. (T/F)
F
6. Suppose a structure is created with the code
Structure stateUSA
Dim capCity As String
Dim yearFounded As Integer
End Structure
in the Declarations section of the Code window. In this structure, the variable capCity is
known as a member. (T/F)
T
7. Suppose a structure is created with the code
Structure stateUSA
Dim capCity As String
Dim stateNum As Integer
End Structure
in the Declarations section of the Code window. The following statement correctly makes
use of the stateNum member of this structure. (T/F)
Dim stateWA As stateUSA
StateWA.stateNum = 42
T
8. A structure can contain members that are simple variables only; members cannot be arrays.
(T/F)
F
9. When a variable with a user-defined data type is passed to a procedure, the corresponding
parameter in the Sub or Function statement must be declared to be of the same data type.
(T/F)
T
10. Suppose a structure and an array are created with the code
Structure Nation
Dim name As String
Dim continent As String
Dim population As Double 'in millions
Dim area As Double 'in square miles
End Structure
Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192
member nations of the UN. What will be displayed in the list box when the following code
is executed?
Dim query = From country In nations
Where country.continent = "Africa"
Order By country.population Descending
Select country.name
lstBox.DataSource = query.ToList
lstBox.SelectedIndex = Nothing
(A) the names of the countries in Africa in alphabetical order
(B) the names of the countries in Africa beginning with the name of the least populous
country
(C) the names of the countries in Africa beginning with the name of the most populous
country
(D) the names of the countries in Africa in reverse alphabetical order
C
11. Suppose a structure and an array are created with the code
Structure Nation
Dim name As String
Dim continent As String
Dim population As Double 'in millions
Dim area As Double 'in square miles
End Structure
Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192
member nations of the UN. What will be displayed in the list box when the following code
is executed?
Dim query = From country In nations
Order By country.area Descending
Select country.name, country.population
lstBox.Items.Add(query.First.name)
lstBox.Items.Add(1000000 * query.First.population)
(A) the name and population of the smallest country in the UN
(B) the name and population of the largest country in the UN
(C) the name and population of the least populous country in the UN
(D) the name and population of the most populous country in the UN
B
12. Suppose a structure and an array are created with the code
Structure Nation
Dim name As String
Dim continent As String
Dim population As Double 'in millions
Dim area As Double 'in square miles
End Structure
Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192
member nations of the UN. What will be displayed in the DataGridView control when the
following code is executed?
Dim query = From country In nations
Where country.population > 200
Order By country.area Descending
Select country.name, country.continent
dgvNations.DataSource = query.ToList
dgvNations.CurrentCell = Nothing
(A) Two columns, with headers name and continent. The grid will display the countries
(along with their continents) whose populations are greater than 200 million people.
The countries will be displayed in order to their population, beginning with the most
populous country.
(B) Four columns, with each row containing data about a single country whose population
is greater than 200 million people. The countries will be displayed in order of their area,
beginning with the largest country.
(C) Two columns, with headers country.name and country.continent. The grid will display
the countries (along with their continents) whose populations are greater than 200
million people. The countries will be displayed in order to their area, beginning with the
largest country.
(D) Two columns, with headers name and continent. The grid will display the countries
(along with their continents) whose populations are greater than 200 million people.
The countries will be displayed in order to their area, beginning with the largest
country.
D
13. Suppose a structure and an array are created with the code
Structure Nation
Dim name As String
Dim continent As String
Dim population As Double 'in millions
Dim area As Double 'in square miles
End Structure
Dim nations(191) As Nation
in the Declarations section of the Code editor and the array is filled with data for the 192
member nations of the UN. What will be displayed in the DataGridView control when the
following code is executed?
Dim query = From country In nations
Where (country.continent = "Europe") And
country.name.StartsWith("S")
Let countryUC = country.name.ToUpper
Let pop = (1000000 * country.population).ToString("N0")
Let area = (country.area).ToString("N0")
Order By countryUC Ascending
Select countryUC, pop, area
(A) Three columns, with headers country, population, and area. The grid will display the
countries in Europe (along with their populations and areas) whose names begin with
the letter S. The countries will be displayed capitalized in alphabetical order and their
populations and areas will be formatted with commas as thousands separators.
(B) Four columns, with headers countryUC, continent, pop, and area. The grid will display
the countries in Europe (along with their continents, populations and areas) whose
names begin with the letter S. The countries will be displayed capitalized in alphabetical
order and their populations and areas will be formatted with commas as thousands
separators.
(C) Three columns, with headers countryUC, pop, and area. The grid will display the
countries in Europe (along with their populations and areas) whose names begin with
the letter S. The countries will be displayed capitalized in alphabetical order and their
populations and areas will be formatted with commas as thousands separators.
(D) Three columns, with headers countryUC, pop, and area. The grid will display the
countries in Europe (along with their populations and areas) whose names begin with
the letter S. The countries will be displayed capitalized in reverse alphabetical order and
their populations and areas will be formatted with commas as thousands separators.
C
14. Which of the following methods is used to change the words in a column header of a
DataGridView control?
(A) HeaderText
(B) HeaderName
(C) HeaderContent
(D) HeaderCaption
A
Section 7.4 Two-Dimensional Arrays
1. Given the following array, what value will be assigned to num?
myArray num = myArray(3,4) 0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
4 20 21 22 23 24
(A) 19
(B) 4
(C) 24
(D) 0
(E) None of the above
A
2. Assume the array nums has been filled as shown. What is the output of the following
program segment?
nums 0 1 2 3
s = 0 |------------------------
For k As Integer = 0 To 2 0 | 2 3 4 5
s += nums(k, k + 1) |
Next 1 | 6 5 4 3
lstBox.Items.Add(s) |
2 | 2 3 4 5
|
3 | 6 5 4 3
(A) 64
(B) 12
(C) 13
(D) 25
(E) None of the above
B
3. How many elements are in the array declared by
Dim myArray(3, 2) As Double
(A) 12
(B) 8
(C) 6
(D) 2
(E) None of the above
A
4. Which of the following types of variables is capable of holding the information contained in
a table that has four rows and four columns?
(A) one-dimensional arrays
(B) simple variables
(C) single-subscripted variables
(D) double-subscripted variables
D
5. Which of the following types of variables can only hold a single item of data?
(A) two-dimensional arrays
(B) simple variables
(C) single-subscripted variables
(D) double-subscripted variables
B
6. Which of the following declarations creates a two-dimensional array?
(A) Dim newVar(2, 2) As Double
(B) Dim newVar As Integer
(C) Dim newVar(2) As Double
(D) Dim newVar(2)
A
7. Which of the following declarations can be used to create a three-dimensional array?
(A) Dim newVar(3, 3) As Double
(B) Dim newVar(2, 2, 2) As Double
(C) Dim newVar(3) As Double
(D) Dim newVar(3)
B
8. Arrays that are capable of holding the contents of a table with several rows and columns, are
known as two-dimensional arrays or double subscripted variables. (T/F)
T
9. The statement
Dim nextVar(3, 5) As Double
declares a two-dimensional array that can store a table containing 4 columns and 6 rows of
numeric data. (T/F)
F
10. A two-dimensional array can be declared and initialized at the same time. (T/F)
T
11. If the two-dimensional array nums has three rows and six columns, then the value of
nums.GetUpperBound(0) is 3 and the value of nums.GetUpperBound(1) is 6. (T/F)
F
12. In the two-dimensional array declaration
Dim newVar(,) As Double = {{1, 2, 3}, {4, 5, 2054}, {6, 802, 2786}}
the comma in newVar(,) can be removed. (T/F)
F
13. The ReDim and Preserve keywords can be used with two-dimensional arrays. (T/F)
T
14. Variable arrays can only be one- or two-dimensional. (T/F)
F
15. ReDim statements cannot be used to change a one-dimensional array into a two-dimensional
array. (T/F)
T
16. ReDim statements can be used to change a one-dimensional array into a three-dimensional
array. (T/F)
F
17. Given the following statements, the first subscript in the second statement references a
column. (T/F)
Dim myArray(15, 20) As Double
myArray(5, 10) = 0
F
18. Two-dimensional arrays are often referred to as tables or matrices. (T/F)
T
19. The third statement below is NOT valid. (T/F)
row = 5
col = 3
someArray(col, row) = 25
F
20. Write code using a For...Next loop to fill a 5-by-5 matrix so that the two diagonals have
asterisks (*) in them as shown.
0 1 2 3 4
0 * *
1 * *
2 *
3 * *
4 * *
Ans: For i As Integer = 0 to 4 matrix(i, i) = "*"
matrix(i, 4 - i) = "*"
Next