Visual Basic Questions and Answers – Creating a Class

«
»

This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Creating a Class”.

1. Class is defined using ______________
a) Class statement
b) Object statement
c) Access specifier statement
d) Instance statement
View Answer

Answer: a
Explanation: You define a class using the Class statement, which you enter in a class file. The steps for adding a class file to an open project are:
• Click Project on the menu bar and then click Add Class. The Add New Item dialog box opens with Class selected in the middle column of the dialog box.
• Type the name of the class followed by a period and the letters vb in the Name box, and then click the Add button.
The syntax of the class statement is as follows:

advertisement
Public Class className
   attributes section
   behaviors section
End Class

2. The convention is to use ________________ case for classname.
a) Upper
b) Lower
c) Pascal
d) Mixed
View Answer

Answer: c
Explanation: Although it is not a requirement, the convention is to use Pascal case for the class name. The names of Visual Basic classes (for example, Integer and TextBox) also follow this naming convention.

3. Objects are represented by _____________ variables and ______________ properties.
a) Private, Public
b) Public, Private
c) Protected, Public
d) Protected, Private
View Answer

Answer: a
Explanation: Within the Class statement, you define the attributes and behaviors of the objects the class will create. In most cases, the attributes are represented by Private variables and Public properties. The behaviors are represented by methods, which can be Sub or Function procedures.

4. The behavior are represented by ______________
a) Methods
b) Attributes
c) Values
d) Numbers
View Answer

Answer: a
Explanation: Within the Class statement, you define the attributes and behaviors of the objects the class will create. In most cases, the attributes are represented by Private variables and Public properties. The behaviors are represented by methods, which can be Sub or Function procedures.
advertisement

5. After defining a class you can instantiate one or more ______________
a) Objects
b) Classes
c) Attributes
d) Methods
View Answer

Answer: a
Explanation: After you define a class, it then can be used to instantiate one or more objects. The syntax is:

Syntax – Version 1

{Dim | Private} variableName As className
variableName = New className

Syntax – Version 2

{Dim | Private} variableName As New className

In both versions, className is the name of the class, and variableName is the name of a variable that will represent the object.

advertisement

6. The computer creates the object when it processes the statement containing ____________ keyword.
a) Class
b) Object
c) New
d) Private
View Answer

Answer: c
Explanation: After you define a class, it then can be used to instantiate one or more objects. The syntax is:

Syntax – Version 1

{Dim | Private} variableName As className
variableName = New className

Syntax – Version 2

advertisement
{Dim | Private} variableName As New className

In both versions, className is the name of the class, and variableName is the name of a variable that will represent the object. The difference between both versions relates to when the object is actually created. The computer creates the object only when it processes the statement containing the New keyword.

7. The class statement groups _______________ item in one unit.
a) Related
b) Different
c) Same access specifier items
d) Different access specifier items
View Answer

Answer: a
Explanation: In its simplest form, the Class statement can be used in place of the Structure statement. Like the Structure statement, the Class statement groups related items into one unit. However, the unit is called a class rather than a structure.

8. The name of the class file ends with _____________
a) .cla
b) .class
c) .cla
d) .vb
View Answer

Answer: b
Explanation: The name of class file ends with .class extension. A class file contains the attributes and methods of the class; and if any instance of that class is created, it can use the attributes and methods of the class file. The class file contains the definition of the methods. The class is a blueprint of an object.

9. A constructor is a _____________
a) A function procedure
b) A sub procedure
c) Either a function procedure or a sub procedure
d) A property procedure
View Answer

Answer: c
Explanation: A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.

10. Which of the following statements is false?
a) A class can have only one constructor
b) A class can have multiple constructors
c) An object created from a class is referred to as instance of the class
d) An instance of a class is considered an object
View Answer

Answer: a
Explanation: A class can have multiple constructors. A constructor can be empty, can contain arguments; and the attributes of the class are defined, by the way the objects are created using the different constructors. For example, while creating an object, if we pass arguments, then the constructor with arguments are called and the attributes are initialized to some value, else if no arguments are passed while creating objects, then an empty constructor is called by default, and the attributes are initialized to zero.

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