25 Nov 2012

Most Important Vb Script Interview Questions with Answers

Find the length of string without using Len function


Dim str,a,res
str = "I Love India"
a = right(str,1)
msgbox a
res = instrrev(str,a)
msgbox res

Swap 2 numbers without using temporary variable

Dim a,b
a=1055
b=155
a=a-b
b=a+b
a=b-a
msgbox a
msgbox b

Check whether the string is a POLYNDROM  
  
Dim oStr
oStr="bob"
fStr=StrReverse(oStr)  
msgbox fStr
If oStr=fStr Then
    msgbox "The Given String "&oStr&" is a Palindrome"
    else
    msgbox "The Given String "&oStr&" is not a Palindrome"
End If

Resuable Script for Arithematic operations(like addition, subtraction, multiplication division)

a=5
b=6
msgbox action(a,b,"*")
Function action(x,y,z)
c= x&z&y
action=eval(c)
End function

String in reverse order

a="VenkatReddy"
sum = ""
For i=1 to len(a)
b= mid(a,i,1)
sum = b+sum
Next
msgbox s

Find the how many number of specific character in a string

k="prime ki software solutions private limited"
temp=0
For i=1 to len(k)
If mid(k,i,1)="e" Then
temp=temp+1
else
temp=temp
End If
Next
msgbox temp

Find the factors of a given number

Dim i,a
a=10 
For i=1 to a/2
If a mod i=0 Then
print i
End If
Next

Find the Factorial of a given number

fvalue=1
x=10
For i=x to 1 step-1
fvalue=fvalue*i
Next
msgbox fvalue

Print the prime numbers between given range of number

Dim a,b,i,j 
a=1  
b=30  
For i=a to b
    For j=2 to round(i/2) 
            If i mod j=0 Then  
               Exit for  
            End If  
    Next  

    If j=round(i/2)+1 or i=1 Then  
            msgbox  i
    End If  
Next  

Function for Specific number count between 1 to100

x = GetCnt ("1")
msgbox x

Function GetCnt (byval SearchStr)
For i = 1 to 100
                x = Cstr(i)
                'len(x)
                For j = 1 to len(x)
                                If mid(x,j,1)  = SearchStr Then
                                                StrCnt = StrCnt + 1
                               End If
                Next
Next
GetCnt = StrCnt
End Function

Find whether given number is a odd number or not?


If 4 mod 2 <>0 Then
    msgbox  "The Number "& oNumber &" is an Odd Number"
else
    msgbox "The Number "& oNumber &" is not an Odd Number"
End If  

Find odd numbers between given range of numbers  
  
Dim a,b,i
a=10
b=20
For i=a to b
    If i mod 2 <>0 Then
        print i
    End If
Next

Find the length of a given string  


Dim oStr,L
oStr="venkatreddy"  
L=len(oStr)  
msgbox L

Find how many alpha characters present in a string.  


Dim oStr,L,i  
oStr="venkat01reddy08kandula1988"  
L=len(oStr)  
oAlphacounter=0  
For i=1 to L
    If not isnumeric (mid(oStr,i,1)) then  
        oAlphacounter=oAlphacounter+1   
    End if      
Next  
print oAlphacounter  

Find occurrences of a specific character in a string  

Dim oStr,oArray,ochr  

oStr="venkatkandula"  
ochr="a"  
oArray=split(oStr,ochr)  
print ubound(oArray)


Replace space with tab in between the words of a string




oStr="Quick Test Professional"  

fStr=replace(oStr," ",vbtab)

print fStr  

  


Write a program to return ASCII value of a given character  


ochr="A"
aVal=asc(ochr)
print aVal  
  
Write a program to return character corresponding to the given ASCII value  

aVal=65  
oChr=chr(aVal)
print oChr

Convert string to Upper Case  

oStr="QuickTest Professional"  
uStr=ucase(oStr)  
print uStr  

Convert string to lower case  

oStr="QuickTest Professional"  
lStr=lcase(oStr)  
print lStr  

Write a program to Replace a word in a string with another word  
  
oStr="Mercury Quick Test Professional"  
oWord1="Mercury"  
oWord2="HP"  
fStr=replace(oStr,oWord1,oWord2)  
print fStr


Verify whether given two strings are equal 

oStr1="qtp"
oStr2="qtp"
If  oStr1=oStr2 Then
        Print "The Given Strings are Equal"
        else
        Print "The Given Strings are not Equal"
End If


Print all values from an Array  

Dim oArray,i
oArray=array(1,2,3,4,"qtp","Testing")
For i=lbound(oArray) to ubound(oArray)
    print oArray(oCounter)
Next

Sort Array elements  

Dim oArray ,i,j,temp
oArray=array(8,3,4,2,7,1,6,9,5,0)
For i=lbound(oArray) to ubound(oArray)
        For j=lbound(oArray) to ubound(oArray)-1
                    If oArray(j)>oArray(j+1) Then
                        tmp=oArray(j)
                        oArray(j)=oArray(j+1)
                        oArray(j+1)=tmp
                    End If
        Next
Next
 
For i=lbound(oArray) to ubound(oArray)
    print oArray(i)
Next
   
Script for Add two 2X2 matrices  

Dim oArray1(1,1)
Dim oArray2(1,1)
Dim tArray(1,1)
 
oArray1(0,0)=8
oArray1(0,1)=9
oArray1(1,0)=5
oArray1(1,1)=-1
 
oArray2(0,0)=-2
oArray2(0,1)=3
oArray2(1,0)=4
oArray2(1,1)=0
 
tArray(0,0)=oArray1(0,0)+ oArray2(0,0)
tArray(0,1)=oArray1(0,1)+oArray2(0,1)
tArray(1,0)=oArray1(1,0)+oArray2(1,0)
tArray(1,1)=oArray1(1,1)+oArray2(1,1)


Multiply Two Matrices of size 2X2

Dim oArray1(1,1)
Dim oArray2(1,1)
Dim tArray(1,1)
 
oArray1(0,0)=8
oArray1(0,1)=9
oArray1(1,0)=5
oArray1(1,1)=-1
 
oArray2(0,0)=-2
oArray2(0,1)=3
oArray2(1,0)=4
oArray2(1,1)=0
 
tArray(0,0)=oArray1(0,0)* oArray2(0,0)+ oArray1(0,1)* oArray2(1,0)
tArray(0,1)=oArray1(0,0)* oArray2(0,1)+ oArray1(0,1)* oArray2(1,1)
tArray(1,0)=oArray1(1,0)* oArray2(0,0)+ oArray1(1,1)* oArray2(1,0)
tArray(1,1)=oArray1(1,0)* oArray2(0,1)+ oArray1(1,1)* oArray2(1,1)  
  
Convert a String in to an array  

Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr)
 
For iCounter=0 to ubound(StrArray)
        print StrArray(iCounter)
Next

Convert a String in to an array using ‘i‘ as delimiter  
  
Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr,"i")
 
For iCounter=0 to ubound(StrArray)

        print StrArray(iCounter)


Find number of words in string  
  
Dim oStr  
oStr="Quick Test Professional"  

StrArray=split(oStr," ")  
msgbox"There are "&ubound(StrArray)+1&" words in the string"  

Write a program to reverse the words of a given string.  
 
Dim oStr,i
oStr="Quick Test Professional"
StrArray=split(oStr," ")
 
For i=0 to ubound(StrArray)
        msgboxstrreverse(StrArray(i))
Next

Print the data as a Pascal triangle  
'The formulae for pascal triangle is nCr=n!/(n-r)!*r!  
 
Dim PascalTriangleRows
Dim nCr
Dim NumCount
Dim RowCount
 
PascalTriangleRows = 10
For NumCount = 0 To PascalTriangleRows
    toPrint= Space(PascalTriangleRows - NumCount)
    For RowCount = 0 To NumCount
            If (NumCount = RowCount) Then
                    nCr = 1
            Else
                nCr = Factorial(NumCount) / (Factorial(NumCount - RowCount) * Factorial(RowCount))
            End If
     toPrint=toPrint&nCr&" "
    Next
    print toPrint
Next
 
Function Factorial(num)
   Dim iCounter
    Factorial = 1
    If num <> 0 Then
        For iCounter = 2 To num
            Factorial = Factorial * iCounter
        Next
    End If
End Function  


Join elements of an array as a string  

Dim oStr
Dim iCounter
oStr="Quick Test Professional"
StrArray=split(oStr," ")
print join(StrArray," ")
  
Trim a given string from both sides  

Dim oStr
oStr="    QTP    "
print trim(oStr)
  
Write a program to insert 100values and to delete 50 values from an array  
  
Dim oArray()
Dim iCounter
 
ReDim oArray(100)
 
For iCounter=0 to ubound(oArray)
            oArray(iCounter)=iCounter
            'Print total 100 Values
            print(oArray(iCounter))
Next

ReDim preserve oArray(50)
 
For iCounter=0 to ubound(oArray)
    'Print Values after deleting 50 values
            print(oArray(iCounter))
Next


Write a program to force the declaration of variables  

Option explicit    ' this keyword will enforce us to declare variables
 
Dim x
x=10
'Here we get an error because i have not declared y,z
y=20
z=x+y
print z


Write a program to raise an error and print the error number.   

On Error Resume Next
Err.Raise 6   ' Raise an overflow error.
print  ("Error # " & CStr(Err.Number) & " " & Err.Description)
  
Finding whether a variable is an Array  
  
Dim oArray()
 
if  isarray(oArray) then
    print "the given variable is an array"
 else
    print "the given variable is not an array"
End if

Write a program to list the Timezone offset from GMT  

Dim objWMIService
Dim colTimeZone
Dim objTimeZone
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
 
For Each objTimeZone in colTimeZone
    print "Offset: "& objTimeZone.Bias  
Next


Retrieving Time Zone Information for a Computer  

Dim objWMIService
Dim colTimeZone
Dim objTimeZone
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone")
 
For Each objItem in colTimeZone
 
    print "Bias: " & objItem.Bias
    print "Caption: " & objItem.Caption
    print "Daylight Bias: " & objItem.DaylightBias
    print "Daylight Day: " & objItem.DaylightDay
    print "Daylight Day Of Week: " & objItem.DaylightDayOfWeek
    print "Daylight Hour: " & objItem.DaylightHour
    print "Daylight Millisecond: " & objItem.DaylightMillisecond
    print "Daylight Minute: " & objItem.DaylightMinute
    print "Daylight Month: " & objItem.DaylightMonth
    print "Daylight Name: " & objItem.DaylightName
    print "Daylight Second: " & objItem.DaylightSecond
    print "Daylight Year: " & objItem.DaylightYear
    print "Description: " & objItem.Description
    print "Setting ID: " & objItem.SettingID
    print "Standard Bias: " & objItem.StandardBias
    print "Standard Day: " & objItem.StandardDay
    print "Standard Day Of Week: " & objItem.StandardDayOfWeek
    print "Standard Hour: " & objItem.StandardHour
    print "Standard Millisecond: " & objItem.StandardMillisecond
    print "Standard Minute: " & objItem.StandardMinute
    print "Standard Month: " & objItem.StandardMonth
    print "Standard Name: " & objItem.StandardName
    print "Standard Second: " & objItem.StandardSecond
    print "Standard Year: " & objItem.StandardYear
     
Next  
  
Write a program to Convert an expression to a date  

Dim StrDate
Dim actualDate  
Dim StrTime
Dim actualTime
 
StrDate = "October 19, 1962"   ' Define date.
actualDate = CDate(StrDate)   ' Convert to Date data type.
print actualDate
StrTime = "4:35:47 PM"         ' Define time.
actualTime = CDate(StrTime)   ' Convert to Date data type.
print actualTime   
  
Display current date and Time  
  
print now


Find difference between two dates.  
  
'Date difference in Years
print DateDiff("yyyy","12/31/2002",Date)
 
'Date difference in Months
print DateDiff("m","12/31/2002",Date)
 
'Date difference in Days
print DateDiff("d","12/31/2002",Date)  


Add time interval to a date  

print DateAdd("m", 1, "31-Jan-95")
  
Print current day of the week  
Print day(date)
Find whether current month is a long month  

Dim oCurrentMonth
Dim ocurrentYear
Dim oDaysinMonths
 
oCurrentMonth = Month(date)
ocurrentYear = Year(date)
oDaysinMonths=Day(DateSerial(ocurrentYear, oCurrentMonth + 1, 0))
print oDaysinMonths&" Days in Current Month"
If oDaysinMonths=31 Then
    print "Current Month is a long month"
else
    print "Current Month is not a long month"
End If

Find whether given year is a leap year  
  
1st Method  
  
'The rules for leap year:
'1. Leap Year is divisible by 4    (This is mandotory Rule)
'2. Leap Year is not divisible by 100 (Optional)
'3. Leap Year divisble by 400 (Optional)
 
Dim oYear
 
oYear=1996
 
If ((oYear Mod 4 = 0) And (oYear Mod 100 <> 0) Or (oYear Mod 400 = 0)) then
    print "Year "&oYear&" is a Leap Year"
else
    print "Year "&oYear&" is not a Leap Year"
End If
  
2nd Method  

' Checking 29 days for February month in specified year
Dim oYear
Dim tmpDate
 
oYear=1996
tmpDate = "1/31/" & oYear
DaysinFebMonth = DateAdd("m", 1, tmpDate)
 
If  day(DaysinFebMonth )=29 then
    print "Year "&oYear&" is a Leap Year"
else
    print "Year "&oYear&" is not a Leap Year"
End If
  
Format Number to specified decimal places  

Dim oNum
Dim DecimaPlacestobeFormat
oNum = 3.14159
DecimaPlacestobeFormat=2
print Round(oNum , DecimaPlacestobeFormat)   
  
Write a program to Generate a Random Numbers  

This script will generate random numbers between 10 and 20  
Dim rStartRange
Dim rEndRange
 
rStartRange=10
rEndRange=20
 
For iCounter=1 to 10
    print Int((rEndRange - rStartRange + 1) * Rnd + rStartRange)
Next

Write a program to show difference between Fix and Int   
  
'Both Int and Fix remove the fractional part of number and return the resulting integer value.
'The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number,
'whereas Fix returns the first negative integer greater than or equal to number.  
'For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.
 
print Int(99.8)    ' Returns 99.
print Fix(99.2)    ' Returns 99.
print Int(-99.8)   ' Returns -100.
print Fix(-99.8)   ' Returns -99.
print Int(-99.2)   ' Returns -100.
print Fix(-99.2)   ' Returns -99.  


Write a program to  find subtype of a variable  

Dim oVar
Dim oDatatypes
oVar="QTP"
oVartype=Typename(oVar)
print oVartype



Write a program to print the decimal part of a given number  

Dim oNum
oNum=3.123
oDecNum=oNum- int(oNum)
print oDecNum

Print Numeric Values in a string

k="venkat123naresh7894329804328989342"
L=len(k)
temp=0
For i=1 to L
 m=mid(k,i,1)
  If IsNumeric(m) Then
      n=n&""&m
  End If
Next
msgbox n

Print Characters in a string

k="venkat123naresh7894329804328989342%$#@$%%"
L=len(k)
temp=0
For i=1 to L
 m=mid(k,i,1)
  If IsNumeric(m) Then
      temp=temp
  else
      n=n&""&m
   
  End If

Next
    msgbox n


'Print The Special Characters in a String

k="venkat123naresh7894329804328989342%$#@$%%"

L=len(k)

temp=0

For i=1 to L

    M=mid(k,i,1)

    Set r=new regexp

r.pattern="[A-Za-z 0-9]"

r.global=true

If   r.test (M) Then

    temp=temp

    else

msgbox M

End If

Next

'Print Triangle Form

Dim arr(5)

For i=1 to 5 step 1

    arr(i)=i

        str=arr(i)&"   "&str
   
 print str

Next

Print the Multiplication Table

Const ForReading=1

Set Fso=Createobject("scripting.filesystemobject")

    Set Fso1=Fso.CreateTextFile("C:\Table")

        x=inputbox("enter a number")

            For i=1 to 10

                Fso1.writeline  x&"*" &i& " ="&i*x

                print x&"*" &i&" ="&i*x

        Next




  


  

  






















1 comment: