Interview Questions/Programs of VBscript, UFT/QTP ?

VB-Script-Basic-Programmes

In this post we will learn the basic programs of VBScript which are commonly asked in a UFT QTP Interview.

1. Given Number is Prime or not?

Maths: A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Logic: Starting from 2 divide the given number till half of the number, if the remainder is zero then the number is not a prime number

userInput = Inputbox ("Enter any number")
i=2
Do While i < userInput/2 
    If userInput Mod i = 0 Then
        flag = 1
        Exit Do
    End If
    i=i+1
Loop
If flag = 1 Then
    Msgbox userInput & " is not a Prime number"
Else
    Msgbox userInput & " is a Prime number"
End If

Output:
 Given Number is Prime or not?

2. Find the factors of a given number?

Maths: The factors of a number are all those numbers that can divide evenly into the number with no remainder.

Logic: starting from 1 divide the given number till half of the number, if the remainder is zero it is a factor of the given number.

userInput = Inputbox ("Enter any number other than Prime number")
For i = 1 to userInput/2  
    If userInput Mod i = 0 Then  
        print i  
    End If  
Next  
print userInput

Output:
Find the factors of a given number?

3. Find factorial of given number?

Maths: Factorial is the product of all the numbers from 1 to n, where n is the user specified number.

factorialValue = 1
userInput = Inputbox ("Enter any number")
For i = 1 to userInput
    factorialValue = factorialValue * i  
Next  
Msgbox  factorialValue 

Output:
Find factorial of given number?

4. Find greatest of three numbers?

number1 = Inputbox ("First number: Enter any postive number")
number2 = Inputbox ("Second number: Enter any postive number")
number3 = Inputbox ("Third number: Enter any postive number")

If number1 > number2 AND number1 > number3 Then
    Msgbox "First number: " & number1 & " is greatest"

ElseIf number2 > number1 AND number2 > number3 Then
    Msgbox "Second number: " & number2 & " is greatest" 

ElseIf number3 > number1 AND number3 > number2 Then
    Msgbox "Third number: " & number3 & " is greatest" 

Else
    Msgbox "All numbers are equal"
End If

Output:
Find greatest of three numbers

5. Swap 2 numbers without a temporary variable

number1 = Inputbox ("First number: Enter any postive number")
number2 = Inputbox ("Second number: Enter any postive number")

Print "Before swapping number 1: " & number1
Print "Before swapping number 2: " & number2

number1 = number1 - number2
number2 = number1 + number2
number1 = number2 - number1

Print "After swapping number 1: " & number1
Print "After swapping number 2: " & number2

Output:
Swap 2 numbers without a temporary variable

6. Write a program to find sub datatype of a variable

userInput1 = 44
Msgbox TypeName(userInput1)

userInput2 = "44"
Msgbox TypeName(userInput2)

'Anything in the double quotes is considered as string.
'For integers Don't use double quotes

userInput3 = "TestNBug"
Msgbox TypeName(userInput3)

userInput4 = True
Msgbox TypeName(userInput4)

Output:
program to find sub datatype of a variable

7. Write a program to generate Random Number

We have a predefined formula for random number generation
You need to enter the minimum and maximum limits.

min = 1
max = 100
Randomize
rNumber = Int((max-min+1)*Rnd+min)
Msgbox rNumber

Output:
program to generate a Random Numbers