Chapter 8 - Sequential Files

advertisement
Chapter 8 Text Files
Section 8.1 Managing Text Files
1.
What method is used to copy the contents of a string array or LINQ query into a text file?
(A)
(B)
(C)
(D)
2.
Which of the following methods is used to change the words in a column header of a
DataGridView control?
(A)
(B)
(C)
(D)
3.
CreateTextFile
WriteAllLines
CopyToTextFile
ReadAllLines
B
HeaderText
HeaderName
HeaderContent
HeaderCaption
A
What property is set to False in order to remove the row header column on the left side of a
DataGridView control?
(A)
(B)
(C)
(D)
ShowRowHeader
DisplayRowHeader
RowHeaderVisible
RowHeaderEnabled
C
4.
A statement of the form IO.File.WriteAllLines("fileName.txt", numArray) copies
the contents of the numeric array numArray into a text file. (T/F)
F
5.
The acronym CSV stands for "Comma Separated Values". (T/F)
T
6.
Each record of the file UN.txt contains four pieces of information (name, continent,
population in millions, area) about one of the 192 member countries of the United Nations.
The first two lines of the file are
Afghanistan,Asia,28.2,251772
Albania,Europe,3.2,11100
What is the first line of the file created by the following code?
Dim query = From line In IO.File.ReadAllLines("UN.txt")
Let data = line.Split(","c)
Let country = data(0)
Let continent = data(1)
Select country & " is in " & continent
IO.File.WriteAllLines("NewFile.txt", query)
(A)
(B)
(C)
(D)
7.
Afghanistan is in Asia
Albania is in Europe
country is in continent
The new file will be empty.
A
Each line of the file UN.txt contains four pieces of information (name, continent, population
in millions, area) about one of the 192 member countries of the United Nations. The first
two lines of the file are
Afghanistan,Asia,28.2,251772
Albania,Europe,3.2,11100
What is the first line of the file created by the following code?
Dim query = From line In IO.File.ReadAllLines("UN.txt")
Let data = line.Split(","c)
Let country = data(0)
Let population = 1000000 * CDbl(data(2))
Select country & "," & population
IO.File.WriteAllLines("NewFile.txt", query)
(A)
(B)
(C)
(D)
Afganistan,28.2
Afganistan,28,200,000
Afganistan,28200000
country,population
C
8.
Each line of the file UN.txt contains four pieces of information (name, continent, population
in millions, area) about one of the 192 member countries of the United Nations. The first
two lines of the file are
Afghanistan,Asia,28.2,251772
Albania,Europe,3.2,11100
What is the first line of the file created by the following code?
Dim query = From line In IO.File.ReadAllLines("UN.txt")
Let data = line.Split(","c)
Let country = data(0)
Let population = 1000000 * CDbl(data(2))
Let area = CDbl(data(3))
Let density = population / area
Select country & "," & density.ToString("N1")
IO.File.WriteAllLines("NewFile.txt", query)
(A)
(B)
(C)
(D)
Afganistan,28200000/251772
Afganistan,112.0
Afganistan,112.006100757829
country,density
B
9.
What elements are in the array newArray after the following code is executed?
Dim firstArray() As Integer = {1, 2, 3}
Dim secondArray() As Integer = {3, 4, 5, 6}
Dim newArray() As Integer = firstArray.Intersect(secondArray).ToArray
(A)
(B)
(C)
(D)
1, 2, 3, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
3
1, 2
C
10. What elements are in the array newArray after the following code is executed?
Dim firstArray() As Integer = {1, 2, 3}
Dim secondArray() As Integer = {3, 4, 5, 6}
Dim newArray() As Integer = firstArray.Union(secondArray).ToArray
(A)
(B)
(C)
(D)
1, 2, 3, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
3
1, 2
B
11. What elements are in the array newArray after the following code is executed?
Dim firstArray() As Integer = {1, 2, 3}
Dim secondArray() As Integer = {3, 4, 5, 6}
Dim newArray() As Integer = firstArray.Except(secondArray).ToArray
(A)
(B)
(C)
(D)
1, 2, 3, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
3
1, 2
D
12. How many elements are in the array newArray after the following code is executed?
Dim firstArray() As Integer = {1, 2, 3}
Dim secondArray() As Integer = {3, 4, 5, 6}
Dim newArray() As Integer = firstArray.Concat(secondArray).ToArray
(A)
(B)
(C)
(D)
7
6
3
none
A
13. The following statement causes the Open dialog box to become visible on the form.
OpenFileDialog1.ShowDialog()
After a file has been selected and the Open button is pressed, the value of
OpenFileDialog1.FileName
will be the file’s filespec; this will include all of the following except
(A)
(B)
(C)
(D)
the file’s drive.
the file’s date of creation.
the file’s path.
the file’s extension.
B
14. When you place the OpenFileDialog control on a form, it will not be visible on the form.
(T/F)
T
15. In order to use the OpenFileDialog control, the programmer must first change its Name
property to something other than the default OpenFileDialog1. (T/F)
F
16. The following statement could be used to set the Filter property of the OpenFileDialog
control so that it displays files with the .txt extension. (T/F)
Text Files (*.txt)|(.txt)
F
17. The following statement could be used to set the Filter property of the OpenFileDialog
control so that it displays files with the .txt extension. (T/F)
Text Files (*txt)|*.txt
T
Section 8.2 StreamReaders, StreamWriters, and Structured Exception Handling
1. Opening a file for output means that
(A) data may be stored onto the disk.
(B) data may be read from the disk.
(C) anything that appears as output on the screen is written to the disk.
(D) data may be read from and stored onto the disk.
A
2. Opening a file for input means that
(A) data may be stored onto the disk.
(B) data may be read from the disk.
(C) anything that appears as output on the screen is written to the disk.
(D) data may be read from and stored onto the disk.
B
3. When a file is opened for Append,
(A) data may be read starting at the beginning of the file.
(B) data written to the file replaces the data already there.
(C) data written to the file are inserted at the beginning of the file.
(D) data written to the file are added at the end of the file.
D
4. Which of the following statements is used when you want to add data to the end of an existing
text file?
(A) Dim
(B) Dim
(C) Dim
(D) Dim
D
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")
5. When an existing file is opened for Output
(A) new data are added at the end of the existing file.
(B) the file is erased completely and new data are added at the beginning of the file.
(C) Visual Basic generates an error message and stops program execution.
(D) data may be read from it.
B
6. 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 Not sr.EndOfStream
Dim sw As IO.StreamWriter = IO.File.CreateText("Target.txt")
letter = sr.ReadLine
sw.WriteLine(letter)
sw.Close()
Loop
sr.Close()
(A) The alphabet in descending order.
(B) The alphabet in ascending order.
(C) a
(D) z
D
7. 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 Not sr.EndOfStream
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.
B
8. Select the example statement below that would open a text file for output.
(A) Dim
(B) Dim
(C) Dim
(D) Dim
B
sw
sw
sr
sr
As
As
As
As
IO.StreamWriter
IO.StreamWriter
IO.StreamReader
IO.StreamReader
=
=
=
=
IO.File.OpenText("IncomeData.txt")
IO.File.CreateText("IncomeData.txt")
IO.File.OpenText("IncomeData.txt")
IO.File.AppendText("IncomeData.txt")
9. If a text file is opened for input, then this allows
(A) data to be placed into the file by the Visual Basic application.
(B) data that is in the file to be used in the Visual Basic application.
(C) data to be appended to the end of the file by the Visual Basic application.
(D) data to be deleted from the file.
B
10.The line of code
sw.Close()
when used with the line
sw = IO.File.CreateText(fileName)
completes which of the following tasks?
(A) it closes the current application
(B) it closes the current form
(C) it closes the currently opened window of the application
(D) it closes the file referred to by the variable name sw
D
11. Which one of the following is NOT an example of a bug?
(A) viruses
(B) accessing the wrong property value
(C) using an incorrect formula
(D) typos
A
12. When an exception is generated by a Visual Basic application, the default exception handler
will
(A) restart the program.
(B) terminate the program.
(C) continue to execute the program.
(D) display a dialog box that asks the user if they wish to restart the computer.
B
13. Which of the following exceptions will be generated by trying to access a disk drive that
doesn’t contain a disk?
(A) NullReferenceException
(B) IndexOutOfRangeException
(C) IO.DirectoryNotFoundException
(D) OverflowException
C
14. 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
C
15. 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
A
16. 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."
MessageBox.Show(message)
newNum = 0
Finally
finalNum = newNum + 1
message1 = "Your age next year will be " & finalNum & "."
MessageBox.Show(message1)
End Try
End Sub
(A)
(B)
(C)
(D)
one
0
3000000000
*
B
17. The catch block:
Catch exc As IO.IOException
handles 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
B
18. The code in a Try block refers to a file and the first catch block is written as follows:
Catch exc As IO.IOException
The second catch block is written with the following clause:
Catch
What type of exception will this second Catch block handle?
(A)
(B)
(C)
(D)
any exception that hasn’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
A
19. Which exception will the following code generate?
Dim str As String
str = "Visual Basic".Substring(14,2)
(A)
(B)
(C)
(D)
NullReferenceException
ArgumentOutOfRangeException
OverflowException
InvalidCastException
B
20. To get rid of an existing closed file, execute a statement of the form
IO.File.Delete(filespec). (T/F)
T
21. Text files are updated by opening them for Input and then entering the new data. (T/F)
F
22. The value of sr.EndOfStream is True when the end of file accessed by the StreamReader is
reached. (T/F)
T
23. 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)
T
24. 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)
F
25. 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")
T
26. If an existing text file is opened for output, the computer will append any new data to the
end of this existing file. (T/F)
F
27. If the value of IO.File.Exists(filespec) is True, then the specified file exists. (T/F)
T
28. An individual item of a text file cannot be changed or deleted directly. (T/F)
T
29. Placing the statement Imports System.IO near the top of the Code Editor (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)
F
30. In a Try-Catch-Finally block, the code in the Finally block is always executed, regardless of
whether an exception was thrown. (T/F)
T
31. In a Try-Catch-Finally block, the code in the first listed Catch block is executed if an
exception is thrown in the Try block. (T/F)
F
32. 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)
F
33. In the line of code
sw = IO.File.CreateText(fileName)
sw
is a variable that holds the value of a filespec for an existing text file. (T/F)
F
34. A Catch block of code that is initiated by the keyword Catch and doesn’t contain any
additional terms will execute regardless of whether an exception occurred. (T/F)
F
35. 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)
T
36. When using Try-Catch-Finally blocks, the programmer is limited to three Catch blocks of
code. (T/F)
F
37. If an attempt is made to access a file within a missing folder using the OpenText method,
the IO.DirectoryNotFoundException will be thrown. (T/F)
T
38. Give three different file modes that Visual Basic allows you to use when accessing a file.
Input, Output, and Append
39. A text file cannot be open both for input and for output at the same time. (T/F)
T
40. Before a text file can be opened for input, it must already exist on a disk. (T/F)
T
40. Before a text file can be opened for append, it must already exist on a disk. (T/F)
F
41. If any record in a text file is to be changed, the entire file must be rewritten. (T/F)
T
Section 8.3 XML
1. The XML standard was recommended by the World Wide Web Consortium. (T/F)
T
2. XML stands for
(A)
(B)
(C)
(D)
extreme markup language
excessive markup language
extraordinary markup language
extensible markup language
D
3. Which of the following terms is NOT used to refer to a part of an XML document?
(A)
(B)
(C)
(D)
content
element
start tag
title
D
4. Comment statements in XML begin with <!-- and end with --!> (T/F)
F
5. Element names are case sensitive. (T/F)
T
6. End tags always begin with a less than symbol followed by a forward slash. (T/F)
T
7. Each element of an XML document must contain both a start tag and an end tag. (T/F)
T
8. Elements in an XML document can contain other elements. (T/F)
T
9. Which of the following terms is NOT used to refer to elements in an XML document?
(A)
(B)
(C)
(D)
child
basis
sibling
parent
B
10. An XML file can contain several root elements. (T/F)
F
11. XML files must be loaded into XElement objects before being accessed with LINQ. (T/F)
T
12. The characters < and & cannot appear in the content of an element of an XML file. (T/F)
T
Download