This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Scope and Lifetime of a Variable”.
1. A variable’s ____________ indicates where the variable can be used in an application code.
a) Scope
b) Lifetime
c) Place
d) Position
View Answer
Explanation: A variable’s scope indicates where the variable can be used in application scope. Variables can have class scope, procedure scope or block scope. However, most of the variable used in the application will have procedure scope. This is because fewer unintentional errors occur in applications where the variables are declared using the minimum scope needed which usually is the procedure scope.
2. A variable’s _______________ indicates how long the variable stays in the computer’s internal memory.
a) Scope
b) Lifetime
c) Place
d) Position
View Answer
Explanation: A variable’s lifetime indicates how long the variable stays in the computer’s internal memory. A variable’s lifetime is declared by the place where you declare the variable, in other words where you enter the variable’s declaration statement.
3. Variables declared in a form’s declaration section have _____________ scope.
a) Class
b) Procedure
c) Block
d) Object
View Answer
Explanation: A form’s declaration section is not the same as general declaration section. The general declaration section is located above the public class clause in the code editor window, whereas the form’s declaration section is located between the public class and end class clauses. Variables declared in the form’s declaration section have class scope.
4. Variable declared in a procedure have ______________ scope.
a) Class
b) Procedure
c) Block
d) Object
View Answer
Explanation: Variables declared in a procedure, can only be accessed within it. It cannot be accessed outside the procedure i.e. it cannot be accessed anywhere in the class except within the procedure. Hence it has a procedure scope.
5. Variables declared in a block have _____________ scope.
a) Class
b) Procedure
c) Block
d) Object
View Answer
Explanation: Variables declared within a block can be accessed only within the given block. It cannot be accessed from any other block or from anywhere outside the block. Hence it has a block scope.
6. When you declare a variable in the form’s declaration section, it is called as ______________
a) Class-level variable
b) Object variable
c) Procedure-level variable
d) Auto variable
View Answer
Explanation: When you declare a variable in the form’s declaration section it is called the class-level variable and has class scope. Class-level variables can be used by all the procedure in the form, including the procedures associated with the controls contained on the form.
7. Lifetime of a class-level variable is till ________________
a) A procedure ends
b) An application ends
c) A block ends
d) Within the control section
View Answer
Explanation: Class-level variables can be used by all the procedures in the form, and they retain their values and remain in the computer’s memory until the application ends. In other words the lifetime of a class-level variable is same as that of the application itself.
8. A class-level variable is declared using ______________ keyword.
a) Private
b) Public
c) Dim
d) Static
View Answer
Explanation: A class-level variable is declared using private keyword. You typically use the class-level variable when more than one procedure in the same form need to use the same variable. However, a class level variable also can be used when a procedure wants to retain its value after the procedure ends.
9. A procedure-level variable is declared using ____________ keyword.
a) Private
b) Public
c) Dim
d) Static
View Answer
Explanation: A procedure-level variable is declared using Dim keyword. The scope of the procedure-level variable ends after the procedure ends as it is declared within the procedure. The value of the variable is not retained after the procedure ends.
10. Two procedures can have procedure-level variable whose names are same.
a) True
b) False
View Answer
Explanation: Two procedures can have procedure-level variable whose names are same, but they will have different values in both the procedures, since a procedure-level variable’s value is not retained after the procedure ends i.e. its scope and lifetime is within the procedure. So these two variables will be treated as different variables.
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.
- Check Visual Basic Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Computer Science Books