Interview Questions
1) Write a Do-While loop that is executed at least once ?
Do
statements
Loop While boolean-expression = True
2) Write a Do-Until loop that has the condition at the top ?
Do Until boolean-expression = False
statements
Loop
3) Can you explain why the last subroutine call does not increment the amount variable ?
There are only two ways to call a subroutine.
The first is to not use the Call method in which case the arguments do not appear in parentheses.
The second is to use the Call method in which case the arguments have to appear in parentheses.
In the last subroutine call the argument (or expression) is being evaluated before being passed to the mySub2 subroutine.
You can enclose expressions inside there own set of parentheses to force them to be evaluated first.
Sub MySubroutine()
Dim iAmount As Integer
iAmount = 50
Debug.Print iAmount '50
MySub2 iAmount
Debug.Print iAmount '100
Call MySub2(iAmount)
Debug.Print iAmount '150
MySub2 (iAmount)
Debug.Print iAmount '150
End Sub
Sub MySub2(ByRef iArgAmount As Integer)
iArgAmount = iArgAmount + 50
End Sub
4) What is displayed in the Immediate Window ?
Public Sub MySubroutine()
Debug.Print VBA.TypeName(MyFunction1) 'displays Empty
Debug.Print VBA.TypeName(MyFunction2) 'displays Empty
Debug.Print VBA.TypeName(MyFunction3) 'displays Empty
End Sub
Public Function MyFunction1()
End Function
Public Function MyFunction2() As Variant
End Function
Public Function MyFunction3() As Variant
Dim myVariant As Variant
MyFunction3 = myVariant 'defaults to Empty
End Function
A Function always has a value returned from it.
If the data type is not explicitly provided then a Variant data type will be used.
5) What is displayed in the Immediate Window ?
Public Sub MySubroutine()
Debug.Print MyFunction1 'displays 100
Debug.Print MyFunction2 'displays 0
Dim myObject As Object
Set myObject = MyFunction3
Debug.Print (myObject Is Nothing) 'displays True
End Sub
Public Function MyFunction1() As Long
MyFunction1 = 100
End Function
Public Function MyFunction2() As Long
Exit Function
MyFunction2 = 100
End Function
Public Function MyFunction3() As Object
End Function
The first function returns 100 because this is the value that is assigned to the function name.
The second function returns 0 because if a value has not been assigned to the function name (before an Exit Function or End Function) the default value for the return data type is passed back.
The third function returns Nothing because when a value is not assigned to the function name the default value for the return data type is passed back. In this case it is Object whose default value is Nothing.
When a function returns a reference data type it must be assigned to a variable using the Set statement.
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrev