Monday, March 23, 2009

VBScript Interview Questions

1. What is VBScript?

http://qtp.blogspot.com/2007/05/vbscript-in-qtp.html

2. What is difference between VBScript and VBA?

VBScript is a subset of Visual Basic for Applications, but there are still many differences:

VBScript doesn't have a debugger like Visual Basic or you can say that VBScript does not provide any debugging features. You'll resort to using lots of message boxes, instead.

Unlike Visual Basic and Visual Basic for Applications, in which the developer can define the data type of a variable in advance, all variables in VBScript are variants.

There is no integrated development environment for VBScript that parallels the IDE for Visual Basic and Visual Basic for Applications.

Because variables are untyped and code is not compiled, all external objects instantiated in VBScript code are necessarily late-bound. This has a number of implications. First, late binding typically entails a substantial performance penalty in comparison to early binding. Second, while the properties and methods of early-bound objects can be examined in Visual Basic or hosted VBA environments using the Object Browser, this is not the case with late-bound objects. Finally, the help facilities available for early-bound objects in VB and VBA (like Auto List Members and Auto Quick Info) are not available, making syntax errors more likely and ready access to good documentation all the more necessary.

3. What is the difference between function and procedure?

A function, which is called differently from a subroutine, returns a value to the code that calls it, while a procedure (subroutine) does not. Functions must be set equal to a return value:

Variable_1 = My_Function(A, B, C)

A subroutine, on the other hand, isn't set to a variable. It can be called like

My_Subroutine A, B, C
Or
Call My_Subroutine(A, B, C)

4. Is VBScript a case-sensitive or case-insensitive?

VBScript is a case-insensitive programming language; i.e. case is ignored when reading VBScript scripts. So myVar, MyVar, MYvar all refer to the same variable.

5. What are the Data Types supported by VBScript?

VBScript has only one data type called a Variant. There are different categories of information that can be contained in a Variant, called subtypes. These subtypes are:

Empty, Null, Boolean, Byte, Integer, Long Single, Double, Date/Time, Currency , String , Object and Error.

6. By default in VBScript the arguments passed to functions and subroutines are by reference or by value?

By default, arguments are passed to functions and subroutines by reference; that is, the address of the argument is provided to the function or subroutine

7. Which function allows you to instantiate an object given its programmatic identifier or ProgID?

CreateObject function

8. Discuss all the ways you know to create an Array in VBScript?

http://qtp.blogspot.com/2007/05/vbscript-arrays.html

9. What is Dictionary object in VBScript? Explain.

The Dictionary object stores name / value pairs (referred to as the key and item respectively) in an array.

Write the below code in a notepad and save it with a .vbs extension and run it from Command Prompt by just typing the name of the file.

Dim Beers
Set Beers = CreateObject("Scripting.Dictionary")
Beers.Add "a", "Strauss"
Beers.Add "b", "Kingfisher"
Beers.Add "c", "Budweiser"
Msgbox ("The value corresponding to the key 'b' is " & Beers.Item("b"))

The Dictionary object has many properties like Count, Item, CompareMode etc and many methods like Exists, Add, and Keys etc.

10. What is the difference between a dictionary and an array?

A dictionary can't be multidimensional but an array can be.

A dictionary has extra methods to add new items and check for existing items.

When you delete a particular item from a dictionary, all the subsequent items automatically shift up. For example, if you delete the second item in a three-item dictionary, the original third item automatically shifts up into the second-item slot.

11. What is the purpose of On Error Resume Next statement?

The On Error Resume Next statement provides you some degree of error handling by preventing program interruptions from runtime errors.

When an error occurs, by using this statement, the line of code containing the error is simply skipped over and the program continues.

On Error Resume Next statement does not correct an error, just ignore it, without even displaying the error message.

The Statement essentially has two ways it is used. First to turn off the script engine error checking:

On Error Resume Next

The second is to turn it on, thereby allowing the script engine to stop execution of a script when an error is encountered. Like so:

On Error Goto 0

12. Which object provide information about a single runtime error in a VBScript?

Err Object

13. How do you declare a class in VBScript?

http://qtp.blogspot.com/2007/06/vbscript-classes.html

14. What's the difference between VBScript and VB.NET?

VBScript is script - a text-based code that gets interpreted by a "host", like the Windows Script Host or IE. VB.Net is a semi-compiled language for writing java-like software.

15. Which command is used for writing text on a page?

Document.Write (text)

16. What aspects of VBScript make it safe so that a Web page using VBScript cannot destroy or corrupt information on a user's computer?

The VBScript code that comes along in a Web page cannot directly access any files on the client's computer. This prevents a Web page from making any modifications to sensitive files on the user's computer. VBScript is also very safe in that it doesn't give programmers the ability to create unrecoverable crashes by virtue of its language syntax.

17. Write the code required within a procedure to create a variable for storing your name with VBScript code. Assign your name to that variable.

Dim Name
Name = "Harry Manis"

18. Assume you have a variable called Age that contains an age provided by the user. Write the code that ensures that the user has entered a number as her age. If the user hasn't entered a number, tell her she needs to enter her age correctly.

If IsNumeric(Age) = False Then
MsgBox "The size should be a number. Please type it again."
End If

19. Write a VBScript procedure that converts feet to inches. Hint: There are 12 inches in a foot.

Option Explicit

Sub feet_to_inch()
Dim Inches, Feet
Feet=inputbox("Enter feet value ")
Inches = Feet * 12
MsgBox "There are " & Inches & " inches in " & Feet & " feet."
End Sub

Call feet_to_inch()

20. Why is the use of Exit Do or Exit For statements within loops discouraged?

These statements are acceptable for specific conditions, but they often make code less readable, which makes it difficult for others who look at your code to know what's really going on. If you make sure the only way out of a loop is to not satisfy the loop's condition, it's much easier to follow your code. Often, breaking out of a loop by force with an exit statement is a sign of a poorly constructed loop with a condition that does not meet your goals. Take a look at the loop structure and condition again to make sure you're not leaving something out.

21. Give an example of if...then...elseif statement.

http://qtp.blogspot.com/2007/05/vbscript-conditional-statements.html

22. What is the use of Property Let and Property Get procedure?

http://qtp.blogspot.com/2007/08/vbscript-property-let-property-get.html

23. Explain about VBScript coding conventions?

http://msdn.microsoft.com/hi-in/library/ektke1b0(en-us,VS.85).aspx

24. What is the difference between VB Debugger and the Script Debugger?

Difference between VB Debugger and the Script Debugger:

No "on the fly" editing
Because the scripting window is read-only, you cannot edit the code during execution, as you can most of the time with VB.

No Instant Watch (Shift-F9)
The VB debugger's instant watch facility, which allows you to highlight a variable in your code, press Shift-F9, and see the value of the variable, is not available in the Script Debugger.

Cannot set watches
Watches do not exist in the Script Debugger.

Cannot set the next statement
Using the VB Debugger, you can place the cursor on a line of code and, by clicking CTRL-F9, have program execution resume at that line. This is particularly useful to backtrack or to re-execute a section of code. Unfortunately, this feature is not available in the Script Debugger.


Also download the above questions, print them and read in your free time.

Also see: Top 25 VBScript Interview Questions & Answers

Subscribe to QTP RSS Feed to get more useful & interesting stuff.