Visual Basic Questions and Answers – Working with Classes

«
»

This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Working with Classes”.

1. _______ can be used instead of a Structure statement.
a) Class
b) Struct
c) StructDef
d) Sdef
View Answer

Answer: a
Explanation: Like the Structure statement, the Class statement groups related items into one unit. However, the unit is called a class rather than a structure.
advertisement

2. It is disadvantageous to use public variables in classes because ___________
a) Its values cannot be initialized
b) It results in compilation error
c) It cannot be accessed by other variables
d) It cannot control its values
View Answer

Answer: d
Explanation: The disadvantage of using Public variables in a class is that a class cannot control the values assigned to its Public variables. As a result, the class cannot validate the values to ensure they are appropriate for the variables. Furthermore, most classes contain not only attributes, but behaviors as well.

3. _______ variables of a class are not visible to applications that use the instance of that class.
a) Public
b) Private
c) Default
d) Static
View Answer

Answer: b
Explanation: Using OOP terminology, the Public members are “exposed” to the application, whereas the Private members are “hidden” from the application. For an application to assign data to or retrieve data from a Private variable in a class, it must use a Public property. In other words, an application cannot directly refer to a Private variable in a class. Rather, it must refer to the variable indirectly, through the use of a Public property.

4. You create a Public property using a _______ procedure.
a) SetProp
b) Property
c) Access
d) SetAccess
View Answer

Answer: b
Explanation: Property procedure is used to set Public or Private properties. The syntax is Public [ReadOnly | WriteOnly] Property propertyName[(parameterList)] As dataType.

5. The ________ keyword indicates that the property’s value can be retrieved (read) by an application.
a) Access
b) Get
c) Read
d) ReadOnly
View Answer

Answer: d
Explanation: The ReadOnly keyword indicates that the property’s value can be retrieved (read) by an application, but the application cannot set (write to) the property. The property would get its value from the class itself rather than from the application.
advertisement

6. The ________ keyword indicates that an application can set the property’s value, but it cannot retrieve the value.
a) Receive
b) Write
c) WriteOnly
d) ReceiveOnly
View Answer

Answer: c
Explanation: The WriteOnly keyword indicates that an application can set the property’s value, but it cannot retrieve the value. In this case, the value would be set by the application for use within the class.

7. The code contained in the ______ allows an application to retrieve the contents of the Private variable associated with the property.
a) Set Block
b) Retrieve
c) Get Block
d) Getter Block
View Answer

Answer: c
Explanation: Public members are visible to the application using the instance of a given class, private members are invisible. To access the private members, public member functions have to be used. If the header contains the ReadOnly keyword, you include only a Get block of code in the Property procedure The Get block is such a method, which is used to read private members.

8. The code contained in the ______ allows an application to retrieve the contents of the Private variable associated with the property.
a) Set Block
b) Retrieve
c) Get Block
d) Getter Block
View Answer

Answer: a
Explanation: Public members are visible to the application using the instance of a given class, private members are invisible. To access the private members, public member functions have to be used. If the header contains the WriteOnly keyword, you include only a Set block of code in the Property procedure. The Set block is such a method, which is used to change private members.

9. Is there an error in the below code?

advertisement
Private _intSide As Integer
Public Property Side As Integer
_intSide=_intSide+2
End Property

a) No error
b) Runtime Error
c) Syntax error
d) Compilation Error
View Answer

Answer: d
Explanation: Public members are visible to the application using the instance of a given class, private members are invisible. To access the private members, public member functions have to be used.

10. Is there an error in the below code?

Private _dblBonus As Double
Public ReadOnly Property Bonus As Double
Set(ByVal value As Decimal)
_dblBonus = value
End Set
End Property

a) No error
b) Runtime Error
c) Compilation Error
d) Syntax error
View Answer

Answer: c
Explanation: If the header contains the ReadOnly keyword, you include only a Get block of code in the Property procedure The Get block is such a method, which is used to read private members. If the header contains the WriteOnly keyword, you include only a Set block of code in the Property procedure. The Set block is such a method, which is used to change private members.
advertisement

Sanfoundry Global Education & Learning Series – Visual Basic.

To practice all areas of Visual Basic, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn