Chapter 7  Arrays

Section 7.1  Creating and Accessing Arrays

1.         After the following Dim statement is executed, how many subscripted variables called myvar(i) will be available?

Dim myVar(7) As Double

(A)  0

(B)  1

(C)  8

(D)  9

2.         Which statement is true regarding the following Dim statement?

Dim state(50) As String, population(50) 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 state() range from 1 to 50.

(D)  The subscripts of population() range from 0 To 50.

3.         What names are displayed in the list box when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim i As Integer, name(5) As String

  Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

  For i = 1 To 5

    name(i) = sr.ReadLine

  Next

  sr.Close()

  lstBox.Items.Clear()

  For i = 5 To 1 Step -2

    lstBox.Items.Add(name(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

4.         What is the output of the following program segment?

Dim num(9), i, k As Integer

Dim total As Double

For i = 1 To 9

  num(i) = i

Next

total = 0

For k = 1 To 4

  total += 10 ^ num(k)

Next

txtBox.Text = CStr(total)

(A)  10000

(B)  11110

(C)  1110

(D)  0

5.         What will be the value of phrase(2) after the following code is executed?

Dim place, i As Integer

Dim character, sentence, phrase(10) As String

sentence = "Every path hath a puddle."

place = 0

i = 1

character = sentence.Substring(place, 1)

Do

  If character = " " Then

    phrase(i) = sentence.Substring(0, place)

    i += 1

  End If

  place += 1

  character = sentence.Substring(place, 1)

Loop Until character = "."

(A)  Every

(B)  Every path

(C)  Every path hath

(D)  Every path hath a

(E)   Every path hath a puddle

6.         What will be displayed when the following program segment is executed?

Dim a(5) As Double, k As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

For k = 1 To 5

  a(k) = CDbl(sr.ReadLine)

Next

sr.Close()

txtBox.Text = CStr(a(4))

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

7.         What is the output of the following program segment?

Dim numbers(4) As Double, h As Double = 0

Dim i, k As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

For i = 1 To 4

  numbers(i) = CDbl(sr.ReadLine)

Next

sr.Close()

For k = 1 to 4

  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

8.         What is the output of the following program segment?

Dim numbers(5), x As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

Do While sr.Peek <> -1

  x = CInt(sr.ReadLine)

  numbers(x) += 2

Loop

sr.Close()

txtBox.Text = CStr(numbers(3))

Assume the six rows of the file DATA.TXT contain the following entries: 5, 3, 1, 3, 1, 3, 1

(A)  4

(B)  5

(C)  6

(D)  7

9.         What is the output of the following program segment?

Dim numbers(5) As Double, i, k As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

For i = 1 To 5

  numbers(i) = CDbl(sr.ReadLine)

Next

sr.Close()

For k = 1 To 3

  numbers(k + 1) += numbers(k)

Next

txtBox.Text = CStr(numbers(4))

Assume the five rows of the file DATA.TXT contain the following entries: 3, 6, 4, 8, 3.

(A)  10

(B)  21

(C)  18

(D)  11

10.     What is the output of the following program segment?

Dim values(3), num, i, j, k As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

num = 0

For i = 1 To 3

  values(i) = CInt(sr.ReadLine)

Next

For j = 1 To 3

  k = CInt(sr.ReadLine)

  num += values(k)

Next

txtBox.Text = CStr(num)

        Assume the six rows of the file DATA.TXT contain the following entries: 2, 1, 3, 1, 1, 2.

(A)  5

(B)  6

(C)  7

(D)  8

11.     What is the output of the following program segment?

Dim myArray(5) As Double, i, num, k As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

For i = 1 To 5

  myArray(i) = CDbl(sr.ReadLine)

Next

num = CInt(sr.ReadLine)

sr.Close()

k = 1

Do While (num > myArray(k)) And (k <= 5)

  k += 1

Loop

txtBox.Text = CStr(k)

Assume the six rows of the file DATA.TXT contain the following entries: 2, 4, 6, 8, 10, 3

(A)  1

(B)  2

(C)  3

(D)  4

(E)   None of the above

12.     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 = 0 To 100

         (i) = 100

       Next

(C)  For j = 0 to 100

         myArray(j) = 100

       Next

(D)  myArray() is already initialized to 100 by the Dim statement.

13.     What is the output of the following program segment?

Dim antonym(8) As String, i As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

For i = 1 To 7 Step 2

  antonym(i) = sr.ReadLine

  antonym(i + 1) = sr.ReadLine

Next

sr.Close()

txtBox.Text = antonym(4)

        Assume the twelve rows of the file DATA.TXT contain the following entries: big, small, black, white, old, young, fast, slow, tall, short, hard, soft.

(A)  black

(B)  white

(C)  small

(D)  Subscript out of range

14.     What is displayed in the list box by the following program segment?

Dim phoneDir(50) As String, i As Integer

Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

For i = 1 To 10 Step 3

  phoneDir(i) = sr.ReadLine

  phoneDir(i + 1) = sr.ReadLine

  phoneDir(i + 2) = sr.ReadLine

Next

sr.Close()

lstBox.Items.Clear()

i = 2

Do While phoneDir(i) <> ""

  lstBox.Items.Add(phoneDir(i))

  i += 3

Loop

Assume the twelve rows of the file DATA.TXT contain the following entries: Fischer, John, 797-3456, Robinson, Max, 535-8657, Stanberg, Jennifer, 544-9080, Thompson, Meg, 436-2345.

(A)  Fischer, Robinson, Stanberg, and Thompson 

(B)  John, Max, Jennifer, and Meg

(C)  797-3456, 535-8657, 544-9080, and 436-2345

(D)  No output

15.     What is the output of the following program segment?

Dim a(20), b(20), c As Double, k As Integer

Dim sr As IO.StreamReader

c = 0

sr = IO.File.OpenText("DATA1.TXT")

For k = 1 To 20

  a(k) = CDbl(sr.ReadLine)

Next

sr.Close()

sr = IO.File.OpenText("DATA2.TXT")

For k = 1 To 20

  b(k) = CDbl(sr.ReadLine)

Next

sr.Close()

For k = 1 To 20

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

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

17.     In the statement

Dim score(30) As Double

the number 30 designates which of the following?

(A)  the highest value of the subscripts of the elements for the array score().

(B)  the maximum value that can be assigned to any element in the array score().

(C)  the data type for the array score().

(D)  the value initially assigned to each element in the array score().

18.     If the following statement appears in a program, the array score() must have been declared using the String data type. (T/F)

score(1) = 87

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

20.     What is displayed in the message box by the following code?

Dim message As String

Dim teamName() As String = {"", "Packers", "Jets", "Seahawks"}

ReDim Preserve teamName(2)

message = teamName(2)

MsgBox(message)

(A)  Packers

(B)  Jets

(C)  Seahawks

(D)  2

21.     Like other variables, array variables can be declared and assigned initial values at the same time. (T/F)

22.     After an array has been declared, its type (but not its size) can be changed with a ReDim statement. (T/F)

23.     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)

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

25.     Unless otherwise specified, VB.NET assigns the value 0 to each element of a numeric array when it is declared with a Dim statement. (T/F)

26.     The following statements are valid. (T/F)'

x = CInt(InputBox("Enter number of items to be processed:"))

ReDim myArray(x)

27.     A fractional number such as 4.6 is not allowed as a subscript. (T/F)

28.     Use the following code to complete parts (a) and (b).

Dim i As Integer, a(100) As Double

For i = 1 To 100

  a(i) = i * i

Next

For i = 1 To 50

  a(i) = a(i + 49)

Next

(a)   What is the value of a(70) after this code executes?

(b)   What is the value of a(11) after this code executes?

29.     Consider the following VB.NET statements:

Dim nums(5) As Double

Dim index As Integer

For index = 1 To 5

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

30.     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)

 

Section 7.2  Using Arrays

1.         What is the output of the following program when the button is clicked?

Private Sub btnDisplay_Click(...) Handles btnDisplay.Click

  Dim result As Double

  Dim number(5) As Double

  FillArray(number)

  result = SumArray(number)

  txtBox.Text = CStr(result)

End Sub

Sub FillArray(ByRef anyArray() As Double)

  Dim i As Integer

  Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")

  For i = 1 To 5

    anyArray(i) = CDbl(sr.ReadLine)

  Next

  sr.Close()

End Sub

Function SumArray(ByVal anyArray() As Double) As Double

  Dim i As Integer, total As Double

  total = 0

  For i = 1 To 5 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

2.         The GetUpperBound(0) function 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)  The highest dimension of the array.

3.         What is the value of j after the following code segment has been executed?

Dim state(50) As String, j as Integer

'Assume that the data file STATES.TXT contains

'the names of the 50 states of the United States

'in the order they were admitted to the union.

Dim sr As IO.StreamReader = IO.File.OpenText("STATES.TXT")

j = 1

Do While sr.Peek <> -1

  state(j) = sr.ReadLine

  j += 1

Loop

sr.Close()

For j = 1 to state.GetUpperBound(0)

  lstBox.Items.Add( "State Number " & j & " is " & state(j))

Next

(A)  49

(B)  50

(C)  51

(D)  Unable to determine

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

5.         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)  num()

457

457

457

458

6.         Which of the following arrays could be searched most efficiently?

(A)  years()

4301

1944

2011

4445

(B)  cities()

Selah

Wapato

Yakima

Zillah

(C)  nbrhoods()

Hockinson

Brush Prairie

Dollars Corner

Battle Ground

(D)  num()

459

457

457

459

7.         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)  num()

459

457

457

458

8.         In the ordered array declared with the statement below, the value of the element namfriend(1) is equal to that of which of the other following elements?

Dim namfriend() As String = {"", "Fred", "Fred", "fred", "fred"}

(A)  namfriend(0)

(B)  namfriend(2)

(C)  namfriend(3)

(D)  namfriend(4)

9.         An application contains the lines

Select Case list1(m)

  Case Is < list2(n)

    newlist(r) = list1(m)

In this section of the program, the value of the mth element of the array list1() will be added into the array newlist() if which of the following is true?

(A)  the nth element of list2() is less than the mth element of list1()

(B)  the nth element of list2() is greater than the mth element of list1()

(C)  the nth element of list2() is equal to the mth element of list1()

(D)  the mth element of list1() is greater than the nth element of list2()

10.     In the line of code

For index = 1 to score.GetUpperBound(0)

the method GetUpperBound(0) 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)  declares a new array with the name GetUpperBound

11.     In the line of code

Dim score() As Integer = {0, 55, 33, 12}

the upper bound of the array score() is which of the following?

(A)  3

(B)  2

(C)  12

(D)  0

12.     Arrays are said to be ordered only if the values are in ascending order. (T/F)

13.     The statement

Dim newlist(10) As String

is used to declare a variable where each element has the value of 10. (T/F)

14.     An array that is declared within a procedure is unknown in all other procedures. (T/F)

15.     In the line of code

Function Sum(ByVal score() As Integer) As Integer

the pair of parentheses that follows score can be removed. (T/F)

16.     In the line of code

Dim score() As Integer = {0, 55, 33, 12}

the upper bound of the array score() is 12. (T/F)

17.     Searching successive elements of an ordered list beginning with the first element is known as a binary search. (T/F)

18.     Beginning with the first element of an ordered list and searching successive elements is known as a sequential search. (T/F)

19.     One disadvantage of using arrays is they cannot easily be passed to procedures. (T/F)

20.     A single array element may be passed to a Sub procedure. (T/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 structures 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

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

3.         Each of the following statements declares a control array except one. Which of the following statements does not create a control array?

(A)  Dim lblTitle(10) As Label

(B)  Dim btnAmount() As Button

(C)  Dim txtNumber(8) As TextBox

(D)  Dim score(5) As Integer

4.         A program contains a form (Form1) with three text boxes (TextBox1, TextBox2, and TextBox3) and a button. The line

Dim txtBox(3) As TextBox

is placed in the Declarations section of the Code window. This code makes txtBox(3) available to which of the following?

(A)  TextBox3’s event procedures only

(B)  the three textboxes’s event procedures only

(C)  all of the form's procedures

(D)  the button’s event procedures only

5.         A program contains a form (Form1) with three text boxes (TextBox1, TextBox2, and TextBox3). The line

Dim txtBox(3) As TextBox

appears in the Declarations section of the Code window. This line carries out which of the following tasks?

(A)  It creates a new text box containing the number 3.

(B)  It creates a variable array with the name TextBox.

(C)  It creates a control array with the name txtBox.

(D)  It establishes a maximum value of 3 for the value of any element in the txtBox() control array.

6.         A program contains a form (Form1) with three text boxes (TextBox1, TextBox2, and TextBox3). The line

Dim txtBox(3) As TextBox

is placed in the Declarations section of the Code window. The three lines

txtBox(1) = TextBox1

txtBox(2) = TextBox2

txtBox(3) = TextBox3

are placed in the Form1_Load procedure for this program. Which of the following statements are true about the three lines of code?

(A)  they assign the three text boxes to elements of the array txtBox()

(B)  they change the Text property for the three text boxes at runtime

(C)  they create a control array with the name TextBox

(D)  these lines of code could have been placed in the Declarations section of the Code window

7.         A program contains a form (Form1) with three textboxes (TextBox1, TextBox2, and TextBox3). The line

Dim txtBox(3) As TextBox

is placed in the Declarations section of the Code window. The three lines

txtBox(3) = TextBox1

txtBox(1) = TextBox2

txtBox(2) = TextBox3

are placed in the Form1_Load procedure for this program. This code will cause an error because the numbers of the subscripted control array elements do not match up to the numbers at the end of the default text box names. (T/F)

8.         A program contains a form (Form1) with three text boxes (TextBox1, TextBox2, and TextBox3) and a button The line

Dim txtBox(3) As TextBox

is placed in the Declarations section of the Code window. The three lines

txtBox(1) = TextBox1

txtBox(2) = TextBox2

txtBox(3) = TextBox3

are placed in the Form1_Load procedure for this program. The button’s click event procedure contains the code

Dim lpNum As Integer

For lpNum = 1 to 3

  txtBox(lpNum).Text = "Value is " & lpNum

Next

What output will be displayed in txtBox(2) when the button is clicked?

(A)  Value is 1

(B)  2

(C)  Value is lpNum

(D)  Value is 2

9.         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. Which of the following statements correctly declares a variable of type stateWA?

(A)  Dim stateUSA As stateWA

(B)  Dim stateWA As Structure

(C)  Dim stateWA As stateUSA

(D)  Dim stateWA As Member

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

MsgBox(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?

(A)  42 Olympia

(B)  Olympia42

(C)  42Olympia

(D)  Olympia 42

11.     The members of a structure must all be of the same data type. (T/F)

12.     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)

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

14.     A structure can contain members that are simple variables only; members cannot be arrays. (T/F)

15.     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)

Section 7.4 Sorting and Ordering

1.         How many comparisons will be made in the first pass of a bubble sort of 6 items?

(A)  4

(B)  5

(C)  6

(D)  Depends on the elements to be sorted.

2.         How many comparisons will be made in the first pass of a bubble sort of n items?

(A)  n - 1

(B)  n

(C)  n + 1

(D)  Depends on the elements to be sorted.

3.         The bubble sort compares

(A)  first and last items.

(B)  items separated by a variable gap.

(C)  adjacent items.

(D)  three items at once.

4.         From the following list of unsorted items, how many passes of a bubble sort are required to sort the list in ascending order?

Groucho     Harpo     Zeppo     Chico     Gummo

(A)  1

(B)  2

(C)  3

(D)  4

5.         The Shell sort is so named because

(A)  it was invented by Donald Shell.

(B)  it resembles the familiar carnival game that moves a pea back and forth under walnut shells.

(C)  it works through successive layers (shells) of unsorted data.

(D)  None of the above

6.         Which of following is not true of a sequential search?

(A)  The data need not be sorted.

(B)  It is usually slower than a binary search.

(C)  It starts either at the top or bottom of the data.

(D)  It is better than the binary search for large amounts of data.

7.         Assume a binary search is looking for "Mankato" in the following list. Which city would be compared with "Mankato" first?

Akron     Hartford     Ithaca     Seattle     Tulsa

(A)  Akron

(B)  Hartford

(C)  Ithaca

(D)  Seattle

(E)   Tulsa

8.         Each of the following represents a step in the bubble sort algorithm except one. Which of the following is not a step used in a pass through a list by the bubble sort algorithm?

(A)  On the final comparison of the first pass, the possible swap is between the next-to-last and last items.

(B)  Compare the first and second items.  If they are in the wrong order, swap them.

(C)  Compare the first and last items. If they are in the wrong order, swap them.

(D)  Compare the second and third items.  If they are in the wrong order, swap them.

9.         When using the bubble sort algorithm, how many passes will be required to correctly sort a list containing the names Pebbles, Barney, Wilma, Fred, and Dino?

(A)  5

(B)  3

(C)  2

(D)  4

 

10.     When using the bubble sort algorithm, after the first pass through the list, which of the following will be true?

(A)  the last item only is guaranteed to be in its proper position

(B)  the first item only is guaranteed to be in its proper position

(C)  the last two items are guaranteed to be in their proper positions

(D)  the first two items are guaranteed to be in their proper positions

11.     When using the bubble sort algorithm, after the second pass through the list, which of the following will be true?

(A)  the last item only is guaranteed to be in its proper position

(B)  the first item only is guaranteed to be in its proper position

(C)  the last two items are guaranteed to be in their proper position

(D)  the first two items are guaranteed to be in their proper position

12.     When using the bubble sort algorithm, which of the following is true about the number of passes required to sort a list?

(A)  the number of passes required is equal to the number of items to be sorted

(B)  the number of passes required is equal to twice the number of items to be sorted

(C)  the number of passes required is one less than the number of items to be sorted

(D)  the number of passes required is one more than the number of items to be sorted

13.     Which of the following describes a search method that begins with the first listed item and considers each successive item in order until a match is found?

(A)  bubble

(B)  binary

(C)  Shell

(D)  sequential

14.     Which of the following describes a sort algorithm that compares items that are next to each other in a list?

(A)  bubble

(B)  binary

(C)  Shell

(D)  sequential

15.     A binary search searches an ordered list by successively dividing the list in halves until the item searched for is found. (T/F)

16.     A binary search works just as effectively on an unordered list. (T/F)

17.     Both the bubble sort and the Shell sort require the interchanging of values stored in a pair of variables. (T/F)

18.     When using the bubble sort algorithm, each successive pass through a list requires one less comparison. (T/F)

19.     When using the bubble sort algorithm, only lists containing data of the String type can be correctly ordered. (T/F)

20.     The Shell sort algorithm, which compares distant items first and works its way down to nearby items, is more efficient than the bubble sort algorithm. (T/F)

21.     The number of comparisons required by a bubble sort depends only on the number of elements to be sorted. Applying the algorithm to an already sorted list will take as many steps as with an unsorted list of the same length. (T/F)

22.     The bubble sort is usually faster than the Shell sort. (T/F)

23.     The Shell sort compares distant items first and works its way down to nearby items. (T/F)

24.     A binary search may be used on any table, provided the contents are sorted in either ascending or descending order. (T/F)

25.     The Shell sort compares and possibly swaps elements such as x(1) and x(1+g), where g is the gap. (T/F)

26.     A binary search that begins in the middle of a list does not require the data to be in any order. (T/F)

27.     Although three assignment statements can be used to swap a pair of array elements, three assignment statements alone cannot swap all the items in one array with all of the items in another array. (T/F)

28.     Use the following array, age(), and assume the bubble sort is used to sort the array from smallest to largest: age(1)=16, age(2)=3, age(3)=21, age(4)=43,age(5)=2, age(6)=16, age(7)=32.

(a)   What is the contents of the array after one pass?

 (b)  What is the contents of the array after two passes?

 (c)  What is the contents of the array after three passes?

29.     Given an array of size 10 with the following ten values stored in order¾1.8, 2.9, 4.7, 9.3, 10.1, 13.6, 17.4, 19.2, 21.5, 23.3¾you want to perform a binary search for the value 4.7. What are the successive values of First, Last, and Middle as determined when performing this search?  (Fill in the table below until the search is completed.)

                       

First

Middle

Last

1

 

10

 

 

 

 

 

 

 

 

 

 

Section 7.5  Two-Dimensional Arrays

1.         Given the following array, what value will be assigned to num?

 

 

 

 

Myarray()

 

 

 

num = myArray(4,5)

 

1

2

3

4

5

 

1

0

1

2

3

4

 

2

5

6

7

8

9

 

3

10

11

12

13

14

 

4

15

16

17

18

19

 

5

20

21

22

23

24

(A)  19

(B)  4

(C)  24

(D)  0

(E)   None of the above

2.         Assume the array b() has been filled as shown. What is the output of the following program segment?

 

                                                  b()

                                            1     2     3     4

s = 0                                 |------------------------

For k = 1 To 3                  1     |     2     3     4     5

  s += b(k, k + 1)                    |

Next                            2     |     6     5     4     3

lstBox.Items.Add(s)                   |

                                3     |     2     3     4     5

                                      |

                                4     |     6     5     4     3

(A)  64

(B)  12

(C)  13

(D)  25

(E)   None of the above

3.         Assume the array num() has been filled as shown. What is the output of the following program segment?

For j = 1 To 3                                        num()

  For m = 1 To 2                                  1     2     3

    If num(j, m) > num(j, m + 1) Then           -----------------

      temp = num(j, m)                        1|  3     1     2

      num(j, m) = num(j, m + 1)                |

      num(j, m + 1) = temp                    2|  6     4     5

    End If                                     |

  Next                                        3|  7     9     8

Next

txtBox.Text = CStr(num(3, 2))

(A)  4

(B)  3

(C)  8

(D)  9

(E)   7

4.         How many elements are in the array declared by

Dim myArray(3, 2, 3) As Double

(A)  48

(B)  36

(C)  64

(D)  3

(E)   None of the above

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

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

7.         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) As TextBox

8.         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) As TextBox

9.         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)

10.     The statement

Dim nextVar(3, 5) As Double

declares a two-dimensional array that can store a table containing 3 columns and 5 rows of numeric data. (Note: Assume that only the non-zero subscripts will be used.) (T/F)

11.     A two-dimensional array can be declared and initialized at the same time. (T/F)

12.     In the two-dimensional array declaration

Dim newVar(,) As Double = {{0, 0, 0}, {0, 0, 2054}, {0, 802, 2786}}

the (,) can be removed. (T/F)

13.     The ReDim and Preserve keywords can be used with two-dimensional arrays. (T/F)

14.     Variable arrays can only be one- or two-dimensional. (T/F)

15.     ReDim statements cannot be used to change a one-dimensional array into a two-dimensional array. (T/F)

16.     ReDim statements can be used to change a one-dimensional array into a three-dimensional array. (T/F)

17.     Given the following statements, the first subscript in the second statement references the column. (T/F)

Dim myArray(15, 20) As Double 

myArray(5, 10) = 0

18.     Two-dimensional arrays are often referred to as tables or matrices. (T/F)

19.     The third statement below is not valid. (T/F)

row = 5

col = 3

someArray(col, row) = 25

20.     Write code using a For...Next loop to initialize a 5-by-5 matrix so only the two diagonals have an asterisk (*) in them. After the program runs, the matrix should be initialized as follows:

 

1

2

3

4

5

1

*

 

 

 

*

2

 

*

 

*

 

3

 

 

*

 

 

4

 

*

 

*

 

5

*

 

 

 

*