Sunday, November 9, 2014

VBScript Interview Questions Part-1

How to display the first 3 letters in these "ABCDEFGH” using VB script?

Dim str : str = "ABCDEFGH"
MsgBox "First 3 letters from String "& str & " = " & LEFT(str,3)





X=10, Y=2- how to swap the numbers without using a third variable?

Dim x: x=10
Dim y: y=2
Result = "Before swap X = "& x & " and Y = "& y & vbcrlf
x = x+y
y= x-y
x= x-y
Result = Result & "After swap X = "& x & " and Y = "& y & vbcrlf
MsgBox result





How to remove the spaces in a string Ex "Welcome to QTP World"?

Dim x : x= "Welcome to QTP World"
x = replace(x," ","")
MsgBox x




Without using any VB or QTP function reverse the string "OH MY GOD".

Dim Str: Str="OH MY GOD"
Dim Result: Result =""

For x =  1 To Len(str)
       Result = Mid(str,x,1)& Result
Next

MsgBox Result




Consider the following scenario:
·         str1=Milan is a automation tester
·         str2=AAP is going t0 build government in Delhi
·         Take str2 as base. Read each character from str1, find it in str2 and remove the matched character from str2. Once this is done print all the characters left in str2.

Dim str1: str1 = "Milan is a automation tester"
Dim str2: str2 ="AAP is going to build government in Delhi"

SrchChar = " "

For x =  1 To Len(str1)

SrchChar = Mid(str1,x,1)



       For y = 0 To Len(str2)

              str2= Replace(str2,SrchChar,"")

       Next

Next

Result= ""


MsgBox str2




Say you have arr(2) as bellow:
Arr(0)=a
Arr(1)=b
Arr(2)=c
Now you have to assign a new value at Arr(2) and assign the value of Arr(2) i.e. value c to Arr(3), how you will do that?
Dim ArrLimit : ArrLimit =2
Dim Result,x
ReDim arr(ArrLimit)

arr(0)="a"
arr(1)="b"
arr(2)="c"

Result = " Before:" & vbcrlf
x=0

For Each i In arr

       Result = Result & " arr("& x &") = "& i & vbcrlf

Next

ArrLimit=3

ReDim Preserve arr(Arrlimit)

arr(3)=arr(2)

arr(2)="New"

Result = Result & vbcrlf & " After:" & vbcrlf

For Each i In arr

       Result = Result & " arr("& x &") = "& i & vbcrlf

Next

MsgBox Result




Str="Google" how to reverse the string without using STRreverse Option?

Dim Str: Str="Google"
Dim Result: Result =""

For x =  1 To Len(str)
       Result = Mid(str,x,1)& Result
Next

MsgBox Result




Write a script to print bellow pattern:
54321
5432
543
54
5
Dim Result 

For x =1 To 5

       For y=5 To x Step -1

       Result = Result & y

       Next

       Result = Result & vbcrlf

Next

MsgBox Result





Reverse each word in a sentence.

Dim Str: Str="OH MY GOD,I Love You."
Dim result :result=""
Word=""
WordStart=1
Wordlength=0
RevWord=""
For x =  1 To Len(str)
       Char = Mid(str,x,1)
       If char=" " Or char="." Or char=","Then

              WordEnd=x-1

              Word= Mid(Str,WordStart,Wordlength)

              For i=1 To Len(Word)
                     RevWord= Mid(Word,i,1) & RevWord
              Next
             

              Result =  Result & RevWord & Char
              RevWord=""
              WordStart= x+1

              Wordlength = 0            

       Else
      
      
              Wordlength = Wordlength +1

       End IF

Next

MsgBox Result





How to sort an array in descending order?

Dim arr(6)

arr(0) =2
arr(1) =15
arr(2) =1
arr(3) =19
arr(4) =20
arr(5) =8

For x =  0 To UBound(arr)

       For y = 0 To UBound (arr)

              If(arr(x) > arr (y) ) Then

              temp= arr(x)
              arr(x)=arr(y)
              arr(y)= temp
                    
              End If

       Next

Next

Result= ""

For Each i In arr

       result = result & i & vbcrlf
      
Next

MsgBox Result





How to find an entered number is prime number or not?

Dim i: i = Int(InputBox(" Plese Enter the Whole Number"))

IsPrime= "Prime Number"

For x = 2 To i-1

       If i Mod x = 0 Then

              IsPrime= "Not Prime Number"

              Exit For

       End If

Next

If i<1 Then

       IsPrime= "Not Prime Number"

End If


MsgBox " Entered Number ("& i &") is " & IsPrime





Explain Dim,  RrDim and preserve with example.

Dim ArrLimit : ArrLimit =2
Dim Result,x
ReDim arr(ArrLimit)

arr(0)="a"
arr(1)="b"
arr(2)="c"

Result = " Before:" & vbcrlf
x=0

For Each i In arr

       Result = Result & " arr("& x &") = "& i & vbcrlf

Next

ArrLimit=3

ReDim Preserve arr(Arrlimit)

arr(3)=arr(2)





Write a script for factorial number.

Dim x,fact

x= Int ( InputBox(" Please enter the Value to get Factorial "))

fact=1

For i = x To 1 Step -1

       fact=fact * i
      
Next

MsgBox "Factoral of "& x &" = "& fact






How to find the number of spaces in a given string?

Dim Str: Str="Google is a search engine."

Counter =0

For x =  1 To Len(str)
       If( Mid(str,x,1) = " ") Then
      
       Counter= counter+1

       End If
      
Next


MsgBox " Number of spaces in the Givven String = " & Counter