Chapter 19: How to work with inheritance

MULTIPLE CHOICE

1.When used correctly, inheritance can

a. / make an application run faster
b. / make it easier to write the code for the application
c. / simplify the overall design of an application
d. / eliminate the need for casting

ANS:C

2.In the System.Windows.Forms namespace, the Control class

a. / provides properties and methods that all controls in the namespace have in common
b. / inherits the properties and methods from all of the control classes in the namespace
c. / is the base class for the Button, TextBox, and Label classes, but not the Form class
d. / is a child class of the Form class

ANS:A

3.The ToString, Equals, and GetHashCode methods are available to all objects because

a. / they are defined in the System namespace
b. / they are inherited by the System.Object class, which all other objects are based on
c. / they are members of the System.Object class, which all objects inherit
d. / they are defined with Public access

ANS:C

4.A Protected method defined within a base class is available to

a. / only the base class
b. / only classes derived from the base class
c. / both the base class and classes derived from it
d. / neither the base class nor classes derived from it

ANS:C

5.If the Student class inherits the Person class and overrides a method named GetBirthday, what feature does the following code example illustrate?

Dim s As New Student("Albert Einstein")

Dim p As Person = s

p.GetBirthday()

a. / Inheritance / c. / Polymorphism
b. / Encapsulation / d. / Hiding

ANS:C

6.If the Student class inherits the Person class and overrides a method named GetBirthday, which method does the third statement in the following code example call?

Dim s As New Student("Albert Einstein")

Dim p As Person = s

p.GetBirthday()

a. / the GetBirthday method defined in the Person class
b. / the GetBirthday method defined in the Student class
c. / the GetBirthday method defined in the Object class
d. / none of the above

ANS:B

7.The GetType method of an object returns

a. / a Type enumeration that identifies the object’s type
b. / a Type object that contains information about the object’s type
c. / a string that contains the name of the object’s type
d. / an integer value that identifies the type of the object

ANS:B

8.If the Student class inherits the Person class and Option Strict is on, which of the following statements is true?

a. / You must explicitly cast a Person object to a Student object whenever a Student object is expected.
b. / You must explicitly cast a Student object to a Person object whenever a Student object is expected.
c. / You must explicitly cast a Person object to a Student object whenever a Person object is expected.
d. / You must explicitly cast a Student object to a Person object whenever a Person object is expected.

ANS:A

9.A class can

a. / have only one derived class
b. / be the base class for only one derived class
c. / be only a base class or a derived class
d. / be both a base class and a derived class

ANS:D

10.Which of the following method declarations overrides the method that is declared as follows?

Public Overridable Function CalculateMPG(ByVal speed as Double) As Double

a. / Public New Function CalculateMPG(ByVal s As Integer) As Double
b. / Public Overrides Function CalculateMPG(ByVal speed As Integer) As Double
c. / Public New Function CalculateMPG(ByVal speed As Double) As Double
d. / Public Overrides Function CalculateMPG(ByVal s As Double) As Double

ANS:D

Diagram 19-1

11.(Refer to diagram 19-1.) If the constructor for the WaterCraft class is called, the default constructors for what other class or classes are also called?

a. / Sailboat
b. / Canoe
c. / Sailboat and Canoe
d. / Vehicle

ANS:D

12.(Refer to diagram 19-1.) If the MotorVehicle class contains a Protected method named GetEngineType, what other class or classes can access this method?

a. / Vehicle, Automobile, and Motorcycle / c. / Automobile and Motorcycle
b. / Vehicle and WaterCraft / d. / Vehicle

ANS:C

13.If a method accepts an Object type, what types of objects can it accept?

a. / None
b. / All objects
c. / Only objects created from classes in the System namespace
d. / Only objects created from classes that explicitly inherit the Object class

ANS:B

14.Which of the following is not a reason to declare a class as sealed?

a. / To prevent others from inheriting the class
b. / To improve efficiency
c. / To prevent others from changing how the methods work
d. / To give a class more functionality

ANS:D

15.Which of the following can you not code in a derived class?

a. / A method that overrides a method in the base class
b. / A call to a constructor of the base class
c. / A new method that’s not defined by the base class
d. / A call to a private method of the base class

ANS:D

Chapter 20: How to work with interfaces and generics

MULTIPLE CHOICE

1.Which of the following statements about interfaces is not true?

a. / An interface can only declare abstract members.
b. / An interface can inherit other interfaces.
c. / A class can only implement a single interface.
d. / A class that implements an interface must provide an implementation for every member of the interface.

ANS:C

2.Which of the following is a difference between interfaces and abstract classes?

a. / None of the members of an interface can be implemented, but some of the members of an abstract class can be.
b. / An interface can declare shared members, but an abstract class can’t.
c. / An interface can’t inherit multiple classes, but an abstract class can.
d. / All of the above.

ANS:A

3.Given an interface named IWriteable, what type of object can be passed to the Save method that has the following declaration?

Public Function Save(ByVal obj As IWriteable) As Boolean

a. / any object
b. / any object that implements the IWriteable interface
c. / any object that implements the Save method
d. / only objects created from the IWriteable class

ANS:B

Code example 20-1

Public Interface IPrintable

Sub Print()

End Interface

Public Class Printer

Public Shared Sub Print(ByVal p As IPrintable)

MessageBox.Show("Print")

p.Print()

End Sub

End Class

Public Class Order Implements IPrintable

Public Sub Print()

MessageBox.Show("Order")

End Sub

End Class

Public Class Transaction

End Class

Public Class Rental Inherits Transaction Implements IPrintable

Public Sub Print()

MessageBox.Show("Rental")

End Sub

End Class

4.(Refer to code example 20-1.) Which of the following statements will definitely not compile?

a. / Dim p As IPrintable = New Order()
b. / Dim rental As New Rental()
Dim p As IPrintable = rental
c. / Dim p As New IPrintable()
d. / Dim t As Transaction = New Rental()

ANS:C

5.(Refer to code example 20-1.) What happens when the code that follows is executed?

Dim order As New Order()

order.Print()

a. / “Print” is displayed in a message box.
b. / “Order” is displayed in a message box.
c. / “Print” is displayed in a message box and then “Order” is displayed in a second message box.
d. / A runtime error occurs because the Print method can’t accept Rental objects.

ANS:B

6.(Refer to code example 20-1.) What happens when the code that follows is executed?

Dim r As New Rental()

Printer.Print(r)

a. / “Print” is displayed in a message box.
b. / “Rental” is displayed in a message box.
c. / “Print” is displayed in a message box and then “Rental” is displayed in a second message box.
d. / A runtime error occurs because the Print method can’t accept Rental objects.

ANS:C

7.A method that accepts an interface as an argument can accept any object that

a. / implements that interface
b. / defines the same methods as the interface
c. / implements the interface or defines the same methods as the interface
d. / is created from that interface

ANS:A

8.A variable that stores an object that implements an interface can be declared as which of the following data types?

a. / The class that defines the object
b. / The interface that the object implements
c. / Any subclass of the class that defines the object
d. / All of the above
e. / A and C only

ANS:D

9.The ICloneable interface defines a Clone method

a. / that returns an object type
b. / that accepts an object type as a parameter
c. / that returns a specific type
d. / that accepts a specific type as a parameter

ANS:A

10.The IComparable() interface defines a CompareTo method

a. / that returns an object type
b. / that accepts an object type as a parameter
c. / that returns a specific type
d. / that accepts a specific type as a parameter

ANS:D

11.If you implement the ICloneable interface for an Invoice that has a property that represents a Customer object,

a. / you must make sure that the clone refers to a different Customer object
b. / you must make sure that the clone refers to the same Customer object
c. / you must convert the Customer object to its composite value types
d. / you can decide whether the clone refers to the same or a different Customer object

ANS:D

Chapter 22: How to work with files and data streams

MULTIPLE CHOICE

1.The primary difference between a text file and a binary file is that a binary file

a. / stores data with different data types
b. / stores data as fields
c. / separates each record with a new line character
d. / uses input and output streams

ANS:A

2.Which of the following is not the cause of a common I/O exception?

a. / The directory can’t be found.
b. / The file can’t be found.
c. / The program tries to read beyond the end of a stream.
d. / The path is too long.

ANS:D

3.To read a text file, you need to use a FileStream object and a

a. / TextReader object / c. / RecordReader object
b. / StreamReader object / d. / FieldReader object

ANS:B

4.When you use a method to read data from a text file, you normally read

a. / one character
b. / one field
c. / one line
d. / the data from the current position to the end of the file

ANS:C

5.When you call the Write method of a binary output stream, you must specify

a. / the character to be written / c. / the record to be written
b. / the data to be written / d. / the data type to be written

ANS:B

6.To read data from a binary file, you need to use a FileStream object and a

a. / DataReader object / c. / BinaryReader object
b. / CharacterReader object / d. / TextReader object

ANS:C

7.You can call the PeekChar method from a binary input stream to determine

a. / what the data type of the next field is
b. / whether another character exists in the file
c. / whether the directory for the file exists
d. / whether the file is in the specified directory

ANS:B

8.What type of exception can be prevented by using the Exists method of the File class?

a. / IOException / c. / FileNotFoundExeption
b. / EndOfStreamException / d. / MethodNotFoundException

ANS:C

9.What type of exception can be prevented by using the Peek method?

a. / IOException / c. / FileNotFoundExeption
b. / EndOfStreamException / d. / MethodNotFoundException

ANS:B

10.If a string named path refers to a file that you want to delete, which of the following statements will delete that file?

a. / Directory.Delete(path) / c. / path.Delete()
b. / File.Delete(path) / d. / File.DeleteFile(path)

ANS:B

11.To allow data to flow from a file to an application’s internal memory, the .NET Framework uses

a. / an input stream / c. / a text stream
b. / an output stream / d. / a binary stream

ANS:A

12.What namespace does the .NET Framework use to store classes that work with input and output operations?

a. / System.FileIO / c. / System.IO
b. / System.File.IO / d. / System.File

ANS:C

13.If the path variable contains a string that refers to an existing file, what does the following code do?

Dim fs As New FileStream(path, FileMode.OpenOrCreate)

a. / Creates the file but doesn’t open it
b. / Creates the file and then opens it
c. / Opens the file
d. / Opens the file and truncates it so its size is zero bytes

ANS:C

14.If the path variable contains a string that refers to an existing file, what does the following code do?

Dim fs As New FileStream(path, FileMode.Open, FileAccess.Write)

a. / Opens the file so data can be read from it, but not written to it
b. / Opens the file so data can be written to it, but not read from it
c. / Opens the file and only allows other applications to open it for writing
d. / Opens the file and prevents other applications from opening it

ANS:B

15.Assume that you have a valid StreamWriter object named textOut. What does the following code do?

textOut.Write("Wizard of Oz")

textOut.Write(vbTab)

textOut.Write("1939")

textOut.WriteLine()

a. / Writes three strings to a binary file
b. / Writes four strings to a binary file
c. / Writes a record that consists of two tab-delimited fields to a text file
d. / Throws an IOException

ANS:C

16.Assume that you have a valid StreamReader object named textIn. What does the following code do?

Do While textIn.Peek > -1

Dim row As String = textIn.ReadLine

MessageBox.Show(row)

Loop

a. / Displays the first line of a binary file in a message box
b. / Displays each line of a binary file in successive message boxes
c. / Displays the first line of a text file in a message box
d. / Displays each line of a text file in successive message boxes

ANS:D

17.What type of file is opened in the window below?

a. / a text file / c. / a delimited file
b. / a binary file / d. / a console file

ANS:B

18.What type of exception is prevented by the use of the If statement in the Finally block that follows?

Finally

If fileStream IsNot Nothing Then

fileStream.Close()

End If

a. / DataException / c. / FileNotFoundExeption
b. / EOFException / d. / NullPointerException

ANS:D

19.If the numbersFile string refers to a file that contains three integers, how many integers will that file contain when the code that follows is executed?

Dim binaryOut As New BinaryWriter( _

New FileStream(numbersFile, FileMode.Create))

binaryOut.Write(4)

binaryOut.Close()

a. / 1 / c. / 4
b. / 3 / d. / 7

ANS:A

20.What method of the BinaryReader class do you use to read a decimal value from a binary file?

a. / Read / c. / ReadDecimal32
b. / ReadDecimal / d. / ReadDecimal64

ANS:B

21.What method of the BinaryReader class do you use to read an integer value from a binary file?

a. / Read / c. / ReadInt32
b. / ReadInteger / d. / ReadInt64

ANS:C

22.If dir is a valid directory and lstBox is a list box control, what does the following code do?

For Each item As String in My.Computer.FileSystem.GetFiles(dir)

lstBox.Items.Add(item)

Loop

a. / Lists the contents of the first file in the directory
b. / Lists the name of the first file in the directory
c. / Lists the contents of all the files in the directory
d. / Lists the names of all the files in the directory

ANS:D

Chapter 23: How to work with XML

MULTIPLE CHOICE

1.Which of the following statements is not true about XML?

a. / XML is used by the .NET Framework to store and exchange data.
b. / XML can be used to structure data that’s sent over the Internet.
c. / XML documents can be used as an alternative to text and binary files.
d. / XML consists of a set of pre-defined tags and elements.

ANS:D

2.The first tag in an XML document defines the

a. / parent element / c. / root element
b. / child element / d. / XML declaration

ANS:D

3.An end tag for an XML element is identified by

a. / a slash (/) / c. / a closing bracket (>)
b. / a backslash (\) / d. / an exclamation point (!)

ANS:A

4.The content for an XML element is coded

a. / between the opening bracket and the closing bracket for the tag
b. / within the start tag for the element
c. / after the end tag for the element
d. / between the start tag and end tag for the element

ANS:D

5.The start tag for an element can contain one or more

a. / parameters / c. / child elements
b. / attributes / d. / comments

ANS:B

6.Which of the following is not true when you’re using DataGrid view in the XML Editor window?

a. / The XML document is displayed in the form of a data table.
b. / You can add, modify, and delete data in the XML document without coding tags.
c. / You can modify tags in the XML document.
d. / You can use the Code command in the View window to switch back to XML view

ANS:C

7.When coding attributes for a parent element that contains a large number of child elements, many designers

a. / use one attribute to store a field that uniquely identifies the parent element
b. / store all of the child elements as attributes
c. / store all child elements that don’t contain additional child elements as attributes
d. / store as many child elements as possible as attributes

ANS:A

8.When using an XmlWriter object to write an XML document, which method or methods can you use to write a child element that contains content?

a. / WriteStartElement, WriteContent, and WriteEndElement
b. / WriteStartElement, WriteElementValue, and WriteEndElement
c. / WriteElementString
d. / WriteChildElement

ANS:C

9.When you use the XmlReader object to read an XML document, the document is treated as a series of

a. / elements / c. / nodes
b. / attributes / d. / records

ANS:C

10.To get the value of an attribute when you’re using an XmlReader object to read an XML document, you can use the

a. / ReadStartElement, ReadAttributeValue, and ReadEndElement methods
b. / ReadAttribute method
c. / Attribute property
d. / the Item property with a name argument

ANS:D

11.If an XML document is created with indented formatting, you should set a property of the XmlReader object that’s used to read the file

a. / so the indentation is removed before the elements are read
b. / so indented formatting is assumed
c. / so white space is ignored
d. / so the node types don’t include spaces

ANS:C

Code example 23-1

<?xml version="1.0" encoding="utf-8" ?>

<!--Episode data-->

<Episodes>

<Episode Number="I">

<Title>The Phantom Menace</Title>

<Year>1999</Year>

</Episode>

<Episode Number="II">

<Title>Attack of the Clones</Title>

<Year>2002</Year>

</Episode>

<Episode Number="III">

<Title>Revenge of the Sith</Title>

<Year>2002</Year>

</Episode>

<Episode Number="IV">

<Title>A New Hope</Title>

<Year>1977</Year>

</Episode>

<Episode Number="V">