Monday, November 10, 2014

VBScript Interview Questions Part-2

 Write a script to read and write data from file?
Dim fso,fol,fls

Set fso = createObject("scripting.fileSystemObject")

Set File = fso.OpenTextFile("D:\KT Sessions\Testing.txt")

BeforeChange = ""
AfterChange = ""
Const Readmode =1,WriteMode=2, AppendMode=8

FileName="D:\KT Sessions\Testing.txt"

'To  Write in the File
Set File = fso.OpenTextFile(FileName, WriteMode, True)

File.WriteLine "This is VB File Stream Script."
File.WriteBLankLines(1) ' For Blank Line
File.WriteLine "To Understand Read/write from File"
File.Close

'To  Read from the File
Set File = fso.OpenTextFile(FileName, ReadMode)

  
BeforeChange = BeforeChange & File.ReadAll


File.Close

'To  Append in the File
Set File = fso.OpenTextFile(FileName, AppendMode, True)

File.WriteLine "This is Append Data." & Now


File.Close


Set File = fso.OpenTextFile(FileName, ReadMode)

'Do until File.AtEndOfStream = True
   
'AfterChange = AfterChange & File.ReadLine

'Loop

AfterChange = AfterChange & File.ReadAll


File.Close

MsgBox "Before change: " & vbcrlf & BeforeChange & vbcrlf & vbcrlf & "After Append: " & vbcrlf & AfterChange





If we have 40 files in a folder, how to find the latest updated file in the folder?

Dim fso,Folders,Files,Temp

Set fso= createObject("scripting.FileSystemObject")

set Folders = fso.getFolder("D:\Software Testing\VB Script")

set files= Folders.Files

dim FileName : FileName=""



For each x in files

       For each y in Files
      
             
              if x.DateLastModified > y.DateLastModified then
      
                     FileName = x.Name
      
              Else

                     FileName = y.Name

              End IF



       Next

Exit For

Next


MsgBox "Last Updated File Name = " &Filename





Script to find number of hours in given date?
Dim CurDateVar,CurHour

CurDateVar = #2/29/2016 1:02:03 PM#

CurHour= HOur(CurDateVar)

'CurHour= HOur(NOW)


MsgBox "Hour from Date = " & CurHour


 There is an array with 1-100 randomly arranged, one integer is missing, find it efficiently.
Dim max,min,FNumber
Dim Arr(99)
max=100
min=1
Randomize

isFound = "Not Found"

For i =0 to 99

       Arr (i)= Int((max-min+1)*Rnd+min)

Next


Fnumber= int (inputbox(" Enter the number you want to search from 1-100 from Random number"))


For i =0 to 99

       If FNumber = Arr (i) then

              isFound="Found"

              exit for

       End IF

Next


MsgBox "Enter Number is " & isFound


Script to get the file name from the folder.
Dim fso,fol,fls

Set fso = createObject("scripting.fileSystemObject")

set fol=fso.getFolder("D:\KT Sessions")

Set fls= fol.Files

Result =""


For each i in fls

Result = Result & i.Name & VBCRLF
'Result = Result & " Type = " & i.type & VBCRLF
'Result = Result & " Size = " & i.size & VBCRLF
'Result = Result & " Attributes = " & i.Attributes & VBCRLF
'Result = Result & " DateLastModified = " & i.DateLastModified & VBCRLF & VBCRLF

Next


MsgBox Result



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