Re: CORRECTED TEST AND ANSWERS.



On Mon, 05 Dec 2005 18:47:43 GMT, richmann@xxxxxxxxxxxx (Richard
Jalbert) wrote:

Thing promised, thing due.

Thanks to all who helped put on this, I really appreciated your
efforts. I learned a couple of new things which in itself is a good
thing.

Now it's your tunr to have fun. Here is the new corrected test.

=========================================================
QUESTIONS
=========================================================
---------------------------------------------------------------------
What is wrong with this code?

Option base 0
...
Dim n as Integer
Dim anArray(255) as integer

For n=1 to 255 step 1
anArray(n) = n
Next n

---------------------------------------------------------------------
Can you do this?

Option Base 0

Dim anArray() as integer

Redim anArray(255)
Redim anArray(0)

---------------------------------------------------------------------
What will happen and why?

Dim n as integer
Dim m as integer
Dim L as long

n = 32767
m = 32767
L = n + m

---------------------------------------------------------------------
In a project, you have a class named c_myClass.
Write code to instantiate that class.

---------------------------------------------------------------------
What is the difference between those two objects?

Dim oneObj as New c_oneObjClass

Dim anotherObj as c_oneObjClass
Set anotherObj= New c_oneObjClass

---------------------------------------------------------------------
Do VB Collections support the "For Each <object> IN <collection>
statement ?
In a more general way: For Each <item> *In* <container>

---------------------------------------------------------------------
What is the type of mVar1?

Dim mVar1, nVar2 as integer

---------------------------------------------------------------------
What is wrong with this code?

Dim s as String
s = "Test"
MyFunction n
...
Private Function MyFunction(byval nVar as integer) as integer
<Do something fun>
End Function

---------------------------------------------------------------------
What is wrong with this code?

Private Sub MySub(byval nMat as integer) as long

---------------------------------------------------------------------
What is a recursive function?

---------------------------------------------------------------------
What is version compatibility option useful for?

---------------------------------------------------------------------
A form is Unloaded then specifically set to Nothing while one control
on
it is still referenced somewhere. What will happens?

---------------------------------------------------------------------
Explain the differences between early bound and late bound. What are
some advantages of each ?

---------------------------------------------------------------------
When creating an ActiveX server, what is the difference between
MultiUse and SingleUse for the instancing property.

---------------------------------------------------------------------
What will be the value of aColl(2) after this code runs and why?

Dim anotherColl as Collection
aColl(2)=25

---------------------------------------------------------------------
Give at least two ways to connect to the Excel application object.

---------------------------------------------------------------------
With VB in process and out of process COM servers can be created.
Which
kinds of project one has to use for in process and out of process COM
servers and why?

---------------------------------------------------------------------
Why can't an in-process ActiveX server be compiled as an EXE.

---------------------------------------------------------------------
Name 4 uses of the Common dialog Activex.

---------------------------------------------------------------------
Dim foo as Object
Is foo an early-bound or late bound variable ?

---------------------------------------------------------------------
Open "foo.txt" For Input As #1
For n=1 to 5 step 1
if (n mod 2) <> 0 then
Write #1,Cstr(n),n
else
Write #1,Cstr(n*2),n*3
endif
Next n
Close #1

What bad VB coding practices appear in the above code.

---------------------------------------------------------------------
ProgressForm.Show vbModal
ProgressForm.ProgressBar.Min = 0
ProgressForm.ProgressBar.Max = aColl.Count

For nCounter = 1 To aColl.Count Step 1
ProgressForm.ProgressBar.Value = ProgressForm.ProgressBar.Value +
1
Next nCounter

What's wrong with the above code ?

---------------------------------------------------------------------
Create a IF ... ELSEIF ... THEN equivalent to this statement

Value = IIf(Len(TextString) > 0, "'" & TextString & "'", "NULL")

---------------------------------------------------------------------
Create a different one-liner equivalent to this statement

Value = IIf(Len(TextString) > 0, "'" & TextString & "'", "NULL")

---------------------------------------------------------------------
What is the difference in scope of a module-level variable declared as
Public, Friend and Private.

---------------------------------------------------------------------
====================================================
ANSWERS
====================================================
---------------------------------------------------------------------
What is wrong with this code?

Option base 0
...
Dim as Integer
Dim anArray(255) as integer

For n=1 to 255 step 1
anArray(n)=n
Next n

---ANSWER
Nothing.
Option 0 fixes the lower index at 0, the redim fixes the top index as
255.
That means there would be 256 elements in the array.
VB is not C/C++: 255 would not be the number of elements.

As good practice, always write out the dimensions wanted:
Dim anArray(0 to 254) As Integer (TX to Ulrich Korndoerfer)

---------------------------------------------------------------------
Can you do this?

Option Base 0

Dim anArray() as integer

Redim anArray(255)
Redim anArray(0)

---ANSWER

Yes, this will resize the array to one value with the default value.

---------------------------------------------------------------------
What will happen and why?

Dim n as integer
Dim m as integer
Dim L as long

n = 32767
m = 32767
L = n + m

---ANSWER
Overflow error
VB will try to accumulates both numbers into an integer variable THEN
transfer it to a Long.
The repair is to force one variable to a Long like this:
L = Clng(n) + m
or like this
L = 0& + n + m (TX to Dmitriy Antonov)

---------------------------------------------------------------------
In a project, you have a class named c_myClass.
Write code example to instantiate that class.

---ANSWER
Dim anObject as New c_myClass
or
Dim anObject as c_myClass
Set anObject = New c_myClass
or
Dim anObject as c_myClass
Set anObject = CreateObject(<ProjectName>.c_myClass) (TX to Dmitriy
Antonov)

---------------------------------------------------------------------
What is the difference between those two objects?

Dim oneObj as New c_oneObjClass

Dim anotherObj as c_oneObjClass
Set anotherObj= New c_oneObjClass

---ANSWER
Setting anotherObj to Nothing destroys it.

However, setting anotherObj to nothing not always destroys it.
Precisely it decrements
internal counter and only, when it (counter) reaches 0, then object
destroys
itself. So if there is another reference to the same object, then
setting
one of the references to nothing will not lead to object's
destruction. (TX Dmitriy Antonov)

Setting oneObj to Nothing terminates (destroys)
the instance that oneObj currently references too (if any).
>From then on there is no instance in memory. But when you *use* oneObj
later (using
its properties or methods or using it as a source for object
reference)
*then* VB looks if it currently is Nothing. If it is Nothing, then VB
silently will create a new instance as if Set oneObj = New
c_oneObjClass
had been called.

---------------------------------------------------------------------
Do VB Collections support the "For Each <object> IN <collection>
statement ?
In a more general way: For Each <item> *In* <container>

---ANSWER
YES, provided each are loaded in the proper type variable or in a
Variant.

(full explanation follows)
The For Each statement is a generalization usable for all container
kind
types that support the IEnum interface. Among those containers are
arrays too. So for me, as For Each enumerates the items in a
container,
my naming seems quite natural.

Regarding the type <item> must have:

when the container items are object references, then the type of
<item>
may be a variant or an object type. This object type must be such that

the references can be casted to this type. So normally the generic
type
"Object" would suffice. It could also be the default interface or any
other interface the referenced objects support.

In all other cases <item> must be of type variant.

So For Each <object> In <a VB collection> is not quite correct, as
Variants in this case are allowed too. And it does not reflect the
more
general aspect of the For Each construct.


---------------------------------------------------------------------
What is the type of mVar1?

Dim mVar1, nVar2 as integer

---ANSWER
mVar1 is a Variant.

---------------------------------------------------------------------
What is wrong with this code?

Dim s as String
s = "Test"
MyFunction n
...
Private Function MyFunction(byval nVar as integer) as integer
<Do something fun>
End Function

---ANSWER
n is a String, the function calls for an Integer.
An error "Type mismatch" will occurs when running.
But the code will compile without error.

---------------------------------------------------------------------
What is wrong with this code?

Private Sub MySub(byval nMat as integer) as long

---ANSWER
A Sub cannot have a return value.

---------------------------------------------------------------------
What is a recursive function?

---ANSWER
A function that calls itself, usually with condition checking to
prevent infinite looping.

---------------------------------------------------------------------
What is version compatibility option useful for?

---ANSWER
It keeps the different compilations of software (EXEs or DLLs)
compatible
between themselves. New ones still have all old functionality intact
while they are free to offer new functionality to newer clients.

Citing from MSDN documentation...
8. Project v Binary Compatibility

The fundamental difference between 'Project' and 'Binary'
compatibility
is that 'Binary' compatibility retains both CLSID's and interface
ID's
while 'Project' compatibility retains only CLSID's.

This results in a design time client project being able to update its
reference to a component built using either option, but an already
compiled application will only work against a 'binary' compatible
component if its references to that component are early bound.

As an overview, 'Project' compatibility is provided by Visual basic
for developer convenience only and should not be used after a build
of your component has been released to the user community if all
client applciations are not to be recompiled when a new component
release is made.

---------------------------------------------------------------------
A form is Unloaded specifically set to Nothing while one control on
it is still referenced somewhere. What will happens?

---ANSWER
The form will be reloaded the instant that control is actively
referenced and will be loaded with default values instead of the
last values it contained.

---------------------------------------------------------------------
Explain the differences between early bound and late bound. What are
some advantages of each ?

---ANSWER
Early bound explicitly set the object library at design-time. If the
exact object is not found, error will occurs.
Early bound is very fast.

Late bound does not explicitly set the object library as desing-time
but let the application set it as an undefined Object and will
query the
object each time a method or property is accessed as to its
existence.
Late bound are slower but safer since if an earlier version of an
object
is found, the program will run.

---------------------------------------------------------------------
When creating an ActiveX server, what is the difference between
MultiUse and SingleUse for the instancing property.

---ANSWER
MultiUse allows other applications to create objects from the class.
One instance of your component can provide any number of objects
created in this fashion

SingleUse allows other applications to create objects from the class,
but every object of this class that a client creates starts a new
instance of your component. Not allowed in ActiveX DLL projects

---------------------------------------------------------------------
What will be the value of aColl(2) after this code runs and why?

Dim anotherColl as Collection
aColl(2) = 25

---ANSWER
Collection items are read only. They can be added or removed but
their values cannot be changed.
The line aColl(2) = 25 will cause an error 424: object required.

---------------------------------------------------------------------
Give at least two ways to connect to the Excel application object.

---ANSWER
Set anApp = Getobject(, "Excel.Application")
or
Set anApp = Getobject("c:\filepath\...\MYTEST.XLS")

---------------------------------------------------------------------
With VB in process and out of process COM servers can be created.
Which
kinds of project one has to use for in process and out of process COM
servers and why?

---ANSWER

To create an in process server an ActiveX DLL project type has to be
used.
To create an out of process server, an ActiveX EXE project type
has to be used.

An in process server is characterized by having its code and data
mapped
into the using applications process space. As this can be done under
Windows only by using Dlls, so it is in the COM world and therefore
for
VB too. It is a requirement imposed from Windows.

Out of process servers have code and data running in their own
process,
which is separate from the application process that uses it (hence out

of process). Processes under Windows can be created only by Exes, so
VB
has to too. (TX Ulrich Korndoerfer)
---------------------------------------------------------------------
Why can't an in-process ActiveX server be compiled as an EXE.

---ANSWER
An in-process ActiveX can only be compiled as a DLL.
Activex can be compiled as EXE but can only
run out-of-process.

---------------------------------------------------------------------
Name 4 uses of the Common dialog Activex.

---ANSWER
Set colors
Open files
Save/SaveAS files
Print files
Set Color
Show Help

---------------------------------------------------------------------
Dim foo as Object
Is foo an early-bound or late bound variable ?

---ANSWER
foo is late bound as the Object is not specifically defined.

---------------------------------------------------------------------
Open "foo.txt" For Input As #1
For n=1 to 5 step 1
if (n mod 2) <> 0 then
Write #1,Cstr(n),n
else
Write #1,Cstr(n*2),n*3
endif
Next n
Close #1

What bad VB coding practices appear in the above code.

---ANSWER
Opening a file for Input (from file to computer)
and attempting to Write (from computer to file) to itwill cause a
crash.

---------------------------------------------------------------------
ProgressForm.Show vbModal
ProgressForm.ProgressBar.Min = 0
ProgressForm.ProgressBar.Max = aColl.Count

For nCounter = 1 To aColl.Count Step 1
ProgressForm.ProgressBar.Value = ProgressForm.ProgressBar.Value +
1
Next nCounter

---ANSWER
The progress form is show as Modal, which means the progress bar will
never increment.
The porgram just sticks there.

---------------------------------------------------------------------
Create a IF ... ELSEIF ... THEN equivalent to this statement

Value = IIf(Len(TextString) > 0, "'" & TextString & "'", "NULL")

---ANSWER
If len(TextString) > 0 Then
Value = "'" & TextString & "'"
ElseIf
Value = "NULL"
EndIf
---------------------------------------------------------------------
Create a different one-liner equivalent to this statement

Value = IIf(Len(TextString) > 0, "'" & TextString & "'", "NULL")

---ANSWER
Value = Format$(Len(TextString), Trim$(Left$(Join(Split(StrConv( _
" " & TextString, vbUnicode), Chr$(0)), "\"), _
2 * Len(TextString) + 1)) & ";;\N\U\L\L")

---------------------------------------------------------------------
What is the difference in scope of a module-level variable declared as
Public, Friend and Private.

---ANSWER
Scope Availability
Public Everywhere
Use when you need to give access to the whole project,
or expose the code to other projects.

Friend In the declaring project
Friend is required when building projects that expose an interface
to other projects. Use Friend to keep other projects from accessing
the code.
This can only be used in classes, not modules.

Private In the declaring class/module
Use for implementation procedures, all data,
and enum/user-defined type definitions required only inside the
class/module.

---------------------------------------------------------------------
=========================================================
HAVE FUN
=========================================================

.



Relevant Pages

  • Re: Troubles with ActiveX-EXE (Out-Of-Process Server)
    ... If all the client connections are terminated properly, and the server isn't ... continuing to hold references on its own objects, then the ActiveX EXE ... EXE's own Collection is keeping references to those objects, ...
    (microsoft.public.vb.enterprise)
  • Re: CORRECTED TEST AND ANSWERS.
    ... > Dim n as Integer ... > Why can't an in-process ActiveX server be compiled as an EXE. ... > the instance that oneObj currently references too. ...
    (microsoft.public.vb.general.discussion)
  • Re: Range Names convert to Cell References
    ... but it seems to happen on either the Server or the PC that connects ... maybe it's not excel. ... but it is reproducible on the work laptop. ... persistently selecting the "Update remote references" and "Save external link ...
    (microsoft.public.excel.misc)
  • RE: Pre-Win 2000 logon remains in "Log onto" field after W2K3 conv
    ... Our DHCP Server still references the Server Option ... > able to update your profile and access the the partner newsgroups. ...
    (microsoft.public.windows.server.migration)
  • Re: ActiveX control and Authorization issue
    ... security settings the way you do in standalone apps. ... > I am running a DCOM server on Win XP Pro, developed with VC++ 6, MFC. ... My typical client setup makes the SAME CALL to ... This has always worked until I wrote an ActiveX ...
    (microsoft.public.win32.programmer.ole)