This set of Visual Basic Multiple Choice Questions & Answers (MCQs) focuses on “Static Variables and Named Constants”.
1. __________ is a variable that remains in memory, and retains its value even after the procedure declaring it ends.
a) Procedure-level variable
b) Class-level variable
c) Static variable
d) Dynamic variable
Explanation: A static variable is a procedure-level variable that remains in memory, and also retains its value, even when the procedure in which it is declared ends. Like a class-level variable, a static variable is not removed from the computer’s internal memory until the application ends.
2. Static variable can only be used in the __________ in which it is declared.
b) Any procedures in the form
c) Any form
d) Only the Procedure it is declared in
Explanation: Unlike a class-level variable, which can be used by all of the procedures in a form, a static variable can be used only by the procedure in which it is declared. In other words, a static variable has a narrower scope than does a class-level variable.
3. A __________ cannot change its value while the application is running.
a) Constant variable
b) Class variable
c) Named constant
d) Static constant
Explanation: Unlike a variable’s value, a named constant’s value cannot be changed while the application is running. This is because its value is set at compile time. It is exactly like a literal constant, except that it has a name.
4. Named constants are used because __________
a) Because they take up less space in memory
b) Because they make documentation and code modification easier
c) Because they can be easily accessed by procedures
d) Because they are easily declared
Explanation: Named constant are used because they make documentation and code modification easier. Named constants are used to declare names to values that are otherwise confusing. For example, dblPi can be used to demote the value of Pi which is 3.141593.
5. What is wrong with the expression “const dblPi As Double = 3.141593”?
a) Double is written
b) Name should be DBLPi
c) “const” starts with a “C”
d) Value is too large
Explanation: Syntax of a constant variable declaration is “Const constantName As dataType = expression”, that is the word const is a keyword and starts with a capital C.
6. What is wrong with the expression “static dblPi AsDouble ”?
a) Double is written
b) Nothing is wrong
c) “Static” starts with a “S”
d) Value is not given
Explanation: Syntax of a constant variable declaration is “Static variableAs data-type= expression”. That is the word static is a keyword and starts with a capital S.
7. What happens when a procedure containing the following statements ends?
Dim decSales As Decimal=12.2 Static decTotal As Decimal=13.5
a) Both values lose their values
b) Dim variable loses its value
c) Static variable loses its value
d) Both variables retain their values
Explanation: A static variable is a procedure-level variable that remains in memory, and also retains its value, even when the procedure in which it is declared ends. Thus as the procedure ends the variable declared using Dim loses its value whereas the variable declared using Static retains its value.
8. Using a named constant is advantageous because to change a value in future, change __________
a) The value in the Const statement
b) The value of the variable wherever it appears in the program
c) The value of the variable anywhere in the program
d) The value of the variable in one of the procedures in the program
Explanation: Using a named constant is advantageous because, to change a value in future, we need not change the value wherever it appears in the program; rather we need to change only in the Const statement in the program, which makes our task easier.
9. Unintentional errors in application by declaring variables can be reduced by __________
a) Giving them Small names
b) Declaring them with a small sized data type
c) Giving them minimum scope possible
d) Making them class variables
Explanation: Unintentional errors in application by declaring variables can be reduced by giving them minimum scope possible. Variable names use up the name space. Variables may hide similar variables in larger scopes, and lead to errors.
10. To declare a class-level named constant you declare it as __________
Explanation: Private variables and methods are those that are meant to be directly accessed by the class that owns them. Thus giving class-level named constant, all the procedures within the class can access it, and its value is retained till the application is running.
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!