Chapter 8 - Sequential Files

advertisement
Chapter 8 Sequential Files
Section 8.1 Sequential Files
1.
Opening a file for output means that
(A)
(B)
(C)
(D)
2.
When a file is opened for Append,
(A)
(B)
(C)
(D)
3.
data may be read starting at the beginning of the file.
data written to the file replaces the data already there.
data written to the file are inserted at the beginning of the file.
data written to the file are added at the end of the file.
Which of the following statements is used when you want to add data to the end of an
existing sequential file.
(A)
(B)
(C)
(D)
4.
data may be stored onto the disk.
data may be read from the disk.
anything that appears as output on the screen is written to the disk.
data may be read from and stored onto the disk.
Dim
Dim
Dim
Dim
sr
sr
sw
sw
As
As
As
As
IO.StreamReader
IO.StreamReader
IO.StreamWriter
IO.StreamReader
=
=
=
=
IO.File.OpenText("DATA.TXT")
IO.File.AddText("DATA.TXT")
IO.File.CreateText("DATA.TXT")
IO.File.AppendText("DATA.TXT")
When an existing file is opened for Output
(A)
(B)
(C)
(D)
new data are added at the end of the existing file.
the file is erased completely and new data are added at the beginning of the file.
Visual Basic generates an error message and stops program execution.
data may be read from it.
5.
Assume the file ALPHABET.TXT contains 26 records, the lowercase letters of the alphabet
in ascending order. What is stored in TARGET.TXT after the following code is executed?
Dim letter As String
Dim sr As IO.StreamReader = IO.File.OpenText("ALPHABET.TXT")
Do While sr.Peek <> -1
Dim sw As IO.StreamWriter = IO.File.CreateText("TARGET.TXT")
letter = sr.ReadLine
sw.WriteLine(letter)
sw.Close()
Loop
sr.Close()
(A)
(B)
(C)
(D)
6.
The alphabet in descending order.
The alphabet in ascending order.
a
z
Assume the file ALPHABET.TXT contains 26 records, the lowercase letters of the alphabet
in ascending order. What happens when the following code is executed?
Dim letter As String
Dim sr As IO.StreamReader = IO.File.OpenText("ALPHABET.TXT")
Do While sr.Peek <> -1
letter = sr.ReadLine
Dim sw As IO.StreamWriter = IO.File.CreateText(letter.ToUpper & ".TXT")
sw.WriteLine(letter.ToUpper & ".TXT")
sw.Close()
Loop
sr.Close()
(A) A "too many files open" error is produced.
(B) Twenty-six files are created and named with one of the uppercase letters of the alphabet
and each containing only its own file name.
(C) A file called z is created containing all of the letters of the alphabet in lowercase.
(D) An "invalid file name" error is produced.
7.
Which of the following is an example of how a filespec for a sequential file would be written
within a Visual Basic application?
(A)
(B)
(C)
(D)
C:\TEXT FILES\INCOME DATA.TXT
"C\TEXT FILES\INCOME DATA.TXT"
"C:\TEXT FILES\INCOME DATA.*"
"C:\TEXT FILES\INCOME DATA.TXT"
8.
Which of the terms in the following line of code are named variables that could be replaced
as long as all other references were also updated?
sr = IO.File.OpenText(fileName)
(A)
(B)
(C)
(D)
9.
fileName
IO
File
OpenText
Select the example statement below that would open a sequential file for output.
(A)
(B)
(C)
(D)
Dim
Dim
Dim
Dim
sw
sw
sr
sr
As
As
As
As
IO.StreamWriter
IO.StreamWriter
IO.StreamReader
IO.StreamReader
=
=
=
=
IO.File.OpenText("INCOME DATA.TXT")
IO.File.CreateText("INCOME DATA.TXT")
IO.File.OpenText("INCOME DATA.TXT")
IO.File.AppendText("INCOME DATA.TXT")
10. If a sequential file is opened for input, then this allows
(A)
(B)
(C)
(D)
data to be placed into the file by the Visual Basic application.
data that is in the file to be used in the Visual Basic application.
data to be appended to the end of the file by the Visual Basic application.
data to be deleted from the file.
11. The line of code
sw.Close()
when used with the line
sw = IO.File.CreateText(fileName)
completes which of the following tasks?
(A)
(B)
(C)
(D)
it closes the current application
it closes the current form
it closes the currently opened window of the application
it closes the file referred to by the variable name sw
12. Which one of the following is NOT an example of a bug?
(A)
(B)
(C)
(D)
viruses
accessing the wrong property value
using an incorrect formula
typos
13. When an exception is generated by a Visual Basic application, the default exception handler
will
(A)
(B)
(C)
(D)
restart the program.
terminate the program.
continue to execute the program.
display a dialog box that asks the user if they wish to restart the computer.
14. Which of the following exceptions will be generated by trying to access a disk drive that
doesn’t contain a disk?
(A)
(B)
(C)
(D)
NullReferenceException
IndexOutOfRangeException
IO.DirectoryNotFoundException
OverflowException
15. If included in a program as written here, which exception will the following code generate?
Dim num1 As Integer, num2 As Integer = 1000000
num1 = num2 * num2
(A)
(B)
(C)
(D)
NullReferenceException
ArgumentOutOfRangeException
OverflowException
InvalidCastException
16. Which of the following statements will result in an IndexOutOfRangeException if placed
after the following line of code?
Dim arr(2) As Integer
(A)
(B)
(C)
(D)
arr(3)
arr(2)
arr(1)
arr(0)
=
=
=
=
2
2
2
2
17. Consider the following code. The Catch block of code will not be executed if the user enters
which of the following when prompted?
Private Sub btnCalc_Click(...) Handles btnCalc.Click
Dim newNum, finalNum As Integer
Dim message, message1 As String
Try
newNum = CInt(InputBox("How old are you?"))
Catch
message = "That answer is not an Integer value."
MsgBox(message)
newNum = 0
Finally
finalNum = newNum + 1
message1 = "Your age next year will be " & finalNum & "."
MsgBox(message1)
End Try
End Sub
(A)
(B)
(C)
(D)
one
0
3000000000
*
18. The code for an application tests to see if a file exists using the following line in the Try
block:
IO.File.Exists(fileName)
The first catch block is written as follows:
Catch exc As IO.IOException
This catch block is meant to handle which of the following exceptions?
(A)
(B)
(C)
(D)
any exception that hasn’t been handled by the Try block
an exception generated by deleting or renaming an open file
an exception generated by attempting to access a file that is missing
an exception generated by attempting to access a file within a folder that is missing
19. The code for an application tests to see if a file exists using the following in the Try block:
IO.File.Exists(fileName)
The first catch block is written as follows:
Catch exc As IO.IOException
The second catch block is written with the following code:
Catch
What type of exception will this second Catch block handle?
(A)
(B)
(C)
(D)
any exceptions that haven’t already been handled
an exception generated by deleting or renaming an open file
this block isn’t designed to catch any exceptions
this block will catch all cases where the file specified by fileName does exist
20. The code for an application tests to see if a file exists using the following in the Try block:
IO.File.Exists(fileName)
The first catch block is written as follows:
Catch exc As IO.IOException
The second catch block is written with the following code:
Catch
The Finally block of this code contains the line
sr.Close()
Which of the following statements is true in regards to the Finally block of code?
(A)
(B)
(C)
(D)
the Finally block of code will never execute
the Finally block of code will only execute if an exception is thrown and handled
the Finally block of code will execute regardless of whether an exception occurred
the Finally block of code will execute only if no exceptions are thrown
21. To get rid of an existing file, execute a statement of the form IO.File.Delete(filespec).
(T/F)
22. Sequential files are updated by opening them for Input and then entering the new data. (T/F)
23. The value of sr.Peek is -1 when the end of file accessed by the StreamReader is reached.
(T/F)
24. The following statements are valid. (T/F)
Dim filespec As String
filespec = InputBox("Enter name of file to process:")
Dim sr As IO.StreamReader = IO.File.OpenText(filespec)
25. When a text file is created in Visual Basic using a StreamWriter, the extension .TXT is
automatically added to the file name if no other extension is specified. (T/F)
26. In the following statement, sw is the name of a variable. (T/F)
Dim sw As IO.StreamWriter = _
IO.File.CreateText("C:\TEXT FILES\INCOME DATA.TXT")
27. If an existing sequential file is opened for output, the computer will append any new data to
the end of this existing file. (T/F)
28. If the value of IO.File.Exists(filespec) is True, then the specified file exists. (T/F)
29. An individual item of a sequential file cannot be changed or deleted directly. (T/F)
30. Placing the statement Imports System.IO near the top of the Code window (before the
Class frmName statement) simplifies file handling by eliminating the need to use the words
StreamReader, StreamWriter, and File in your code. (T/F)
31. In a Try-Catch-Finally block, the code in the Finally block is always executed, regardless of
whether an exception was thrown. (T/F)
32. In a Try-Catch-Finally block, the clause in the first listed Catch block is considered if an
exception is thrown in the Try block. (T/F)
33. In a Try-Catch-Finally block, if there are multiple Catch blocks, the code in the last listed
Catch block is always executed. (T/F)
34. In the line of code
sw = IO.File.CreateText(fileName)
sw
is a variable that holds the value of a filespec for an existing sequential file. (T/F)
35. A Catch block of code that is initiated by the keyword Catch and doesn’t contain any
additional clause will execute regardless of whether an exception occurred. (T/F)
36. In a Try-Catch-Finally block, the Catch clauses are considered one at a time, in the order in
which they appear in the code, until the proper exception is located. (T/F)
37. When using Try-Catch-Finally blocks, the programmer is limited to three Catch blocks of
code. (T/F)
38. If an attempt is made to access a missing file within an existing folder using the OpenText
method, the IO.DirectoryNotFoundException will be thrown. (T/F)
39. Give three different file modes that Visual Basic allows you to use when accessing a file?
Section 8.2 Using Sequential Files
1.
Which of the tasks is the Split function used to complete in the following code?
Dim words() As String
words = line.Split(","c)
(A) Split adds a comma delimiter between all items of text in the given line.
(B) Split adds a comma followed by a c to the end of the given line.
(C) Split parses or separates out all items of text that are delimited by a comma in the given
line.
(D) Split parses or separates out all items of text that are delimited by a c in the given line.
2.
Which of the tasks is the Join function used to complete 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.
3.
When merging sequential files without duplications, one possible algorithm calls for
opening two ordered files for input and a third file for output. Then, an item of data is
retrieved from each ordered file and the item with precedence is written out to the third file.
If, however, the two retrieved items are identical, which of the following should take place?
(A) both items are written out to the third file and a new item is retrieved from each of the
ordered files
(B) both items are deleted and a new item is retrieved from each of the ordered files
(C) neither item is written out to the third file and a new item is retrieved from each of the
ordered files
(D) one item is written into the third file and a new item is retrieved from each of the
ordered files
4.
In an application that merges sequential files without duplications, which of the following
lines of code would be used to open a file for output?
(A)
(B)
(C)
(D)
5.
Dim sr1 As IO.StreamReader = IO.File.OpenText(file3)
file3 = txtDataMerged.Text
data1 = CDbl(sr1.ReadLine)
Dim sw As IO.StreamWriter = IO.File.CreateText(file3)
When merging sequential files without duplications, the last step in any algorithm should be
to complete which of the following tasks?
(A) Close all opened files.
(B) After there is no longer data in one of the ordered files, write out all remaining data in
the other file.
(C) Delete all items of data that remain in the ordered files once the files have been merged.
(D) Index all opened files.
6.
Control break processing means which of the following?
(A)
(B)
(C)
(D)
A program that processes data based on a variable type.
A program that processes data based on a variable that maintains a constant value.
A program that processes data based on a change to the value of a variable.
A program that processes data based on a variable scope.
7.
Identify the control variable in the following code block.
Dim loopStart As Integer = 1
Dim message As String
Dim loopContinue As Integer = 1
Do While loopContinue <= 3
message = "The number is " & loopContinue
MsgBox(message)
loopContinue += loopStart
Loop
(A)
(B)
(C)
(D)
8.
loopStart
message
loopContinue
Integer
In which line of the following code does the break occur?
Dim loopStart As Integer = 1
Dim message As String
Dim loopContinue As Integer = 1
Do While loopContinue <= 3
message = "The number is " & loopContinue
MsgBox(message)
loopContinue += loopStart
Loop
(A)
(B)
(C)
(D)
9.
Dim message As String
loopContinue += loopStart
End While
Dim loopStart As Integer
What is displayed in the text box by the following two lines of code?
Dim language() As String = {"V", "B", " ", "2", "0", "0", "5"}
txtBox.Text = Join(language, "")
(A)
(B)
(C)
(D)
V,B, ,2,0. 0. 5
"V","B"," ","2","0","0","5"
"V","B","","2","0","0","5"
VB 2005
10. Sorting the records in a sequential file can be accomplished by first reading the data into an
array of structures and then sorting on an individual member. (T/F)
11. The following code produces the output one two. (T/F)
Dim strArrData() As String = {"one", "two"}
Dim strData As String
strData = Join(strArrData, ",")
MsgBox(strData)
12. The Trim function is often used with the output of a Split function to delete extraneous
spaces. (T/F)
13. In the following statement, the term Join is used to concatenate the data contained in the
array outputLine() into one comma delimited line that can be placed in a sequential file.
(T/F)
sw.WriteLine(Join(outputLine, ","))
14. A control variable is a variable that is used to determine (by its value) the flow of a
program. (T/F)
15. A break is the value of a control variable that is being modified. (T/F)
16. In control break processing, a break occurs when the value of the control variable is
changed. (T/F)
17. Sequential files must always be opened, processed, and closed within a single procedure.
(T/F)
Download