What will the function Val ($165.30) return?
-
165
-
165.3
- $165.30
-
An error
The Val function in VBA is used to convert a string to a number. The string must contain only numeric characters, and the function will stop reading the string as soon as it encounters a non-numeric character.
In the case of Val(\$165.30)`, the function will encounter the dollar sign and the decimal point, which are not numeric characters. Therefore, the function will return an error.
Here is a table that summarizes the different options and their correctness:
| Option | Correctness | Reason |
|---|---|---|
| A. 165 | Incorrect | The function will stop reading the string after the dollar sign. |
| B. 165.3 | Incorrect | The function will stop reading the string after the decimal point. |
| C. \$165.30 | Incorrect | The function does not return the string itself. It returns the numeric value that the string represents. |
| D. Error | Correct | The function will encounter non-numeric characters and return an error. |
This question comes from a QTP/UFT bank, and QTP's scripting language is VBScript. VBScript has NO Val() function (Val exists only in VB6/VBA). Calling an undefined Val() in a QTP script raises a run-time error ('Type mismatch: Val'). On top of that, the argument as written, \$165.30, is not a legal literal in any VB dialect: the '\$' prefix is not valid VBScript/VB syntax, so it would fail to compile regardless. Either way the statement does not evaluate to a number, so it returns/produces an error rather than 165, 165.3, or the raw string. (Only the different case of VBA's Val("$165.30") on a quoted string would silently return 0 by stopping at the first non-numeric character — but that is not what is written here, and 0 is not offered.) Given the QTP context, 'An error' is the defensible answer.