Basic VB Interview Questions
Basic VB Interview Questions
A reader sent these in noting that these questions were used in his company for interviewing prospective VB
programmers.
18. Write a simple app with Encrypt and Decrypt buttons and Textbox where the user can enter text for
encryption and decryption.
This came in the mail from the reader who recently went through a job interview process. He didn’t mention the
company name.
COM interview questions
1. What is IUnknown? What methods are provided by IUnknown? It is a generally good idea to have
an answer for this question if you claim you know COM in your resume. Otherwise, you may consider
your interview failed at this point. IUnknown is the base interface of COM. All other interfaces must
derive directly or indirectly from IUnknown. There are three methods in that interface: AddRef, Release
and QueryInterface.
2. What are the purposes of AddRef, Release and QueryInterface functions? AddRef increments
reference count of the object, Release decrements reference counter of the object and QueryInterface
obtains a pointer to the requested interface.
3. What should QueryInterface functions do if requested object was not found? Return
E_NOINTERFACE and nullify its out parameter.
4. How can would you create an instance of the object in COM? Well, it all depends on your project.
Start your answer from CoCreateInstance or CoCreateInstanceEx, explain the difference between them.
If interviewer is still not satisfied, you’ll have to explain the whole kitchen behind the scenes, including a
difference between local server and inproc server, meaning and mechanism of class factory, etc. You
may also mention other methods of object creation like CoGetInstanceFromFile, but discussion will likely
turn to discussion of monikers then.
5. What happens when client calls CoCreateInstance? Again, all depends on the level of detail and
expertise of interviewer. Start with simple explanation of class object and class factory mechanism.
Further details would depend on a specific situation.
6. What the limitations of CoCreateInstance? Well, the major problems with CoCreateInstance is that it
is only able to create one object and only on local system. To create a remote object or to get several
objects, based on single CLSID, at the same time, one should use CoCreateInstanceEx.
7. What is aggregation? How can we get an interface of the aggregated object? Aggregation is the
reuse mechanism, in which the outer object exposes interfaces from the inner object as if they were
implemented on the outer object itself. This is useful when the outer object would always delegate every
call to one of its interfaces to the same interface in the inner object. Aggregation is actually a specialized
case of containment/delegation, and is available as a convenience to avoid extra implementation
overhead in the outer object in these cases. We can get a pointer to the inner interface, calling
QueryInterface of the outer object with IID of the inner interface.
8. C is aggregated by B, which in turn aggregated by A. Our client requested C. What will
happen? QueryInterface to A will delegate request to B which, in turn, will delegate request for the
interface to C. This pointer will be returned to the client.
9. What is a moniker ? An object that implements the IMoniker interface. A moniker acts as a name that
uniquely identifies a COM object. In the same way that a path identifies a file in the file system, a
moniker identifies a COM object in the directory namespace.
10. What’s the difference, if any, between OLE and COM? OLE is build on top of COM. The question is
not strict, because OLE was built over COM for years, while COM as a technology was presented by
Microsoft a few years ago. You may mention also that COM is a specification, while OLE is a particular
implementation of this specification, which in today’s world is not exactly true as well, because what
people call COM today is likely implementation of COM spec by Microsoft.
11. What’s the difference between COM and DCOM? Again, the question does not require strict answer.
Any DCOM object is yet a COM object (DCOM extends COM) and any COM object may participate in
DCOM transactions. DCOM introduced several improvements/optimizations for distributed environment,
such as MULTI_QI (multiple QueryInterface()), security contexts etc. DCOM demonstrated importance of
surrogate process (you cannot run in-proc server on a remote machine. You need a surrogate process to
do that.) DCOM introduced a load balancing.
12. What is a dual interface? Dual interface is one that supports both - IDispatch interface and vtbl-based
interface. Therefore, it might be used in scripting environment like VBScript and yet to use power and
speed of vtbl-based interface for non-scripting environment. Discussion then may easily transform into
analyzing of dual interface problems - be prepared to this twist.
13. Can you have two dual interfaces in one class? Yes. You may have two dual interfaces in one class,
but only one of them may be default. The bottom line is that you cannot work with two dual interfaces at
the same time due to nature of dual interface! To support two dual interfaces in VB you would write
something like:
14. dim d1 as IDualInterface1
15. dim d2 as IDualInterface2
16. set d1 = new MyClassWithTwoDuals
17. set d2 = d1
18. What is marshalling by value? Some objects can essentially be considered static: regardless of which
methods are called, the state of the object does not change. Instead of accessing such an object
remotely, it is possible to copy the static state of the object and create a new object with the same state
information on the caller side. The caller won’t be able to notice the difference, but calls will be more
efficient because they do not involve network round trips. This is called “marshaling by value".
19. What is a multi-threaded apartment (MTA)? Single-threaded apartment (STA)? This is pretty
difficult question to describe shortly. Anyway, apartments were introduced by Microsoft in NT 3.51 and
late Windows 95 to isolate the problem of running legacy non-thread safe code into multithreaded
environment. Each thread was “encapsulated” into so called single-threaded apartment. The reason to
create an object in apartment is thread-safety. COM is responsible synchronize access to the object even
if the object inside of the apartment is not thread-safe. Multithreaded apartments (MTA, or free
threading apartment) were introduced in NT 4.0. Idea behind MTA is that COM is not responsible to
synchronize object calls between threads. In MTA the developer is responsible for that. See “Professional
DCOM Programming” of Dr. Grimes et al. or “Essential COM” of Don Box for the further discussion on this
topic.
20. Let’s assume we have object B and aggregated object C (in-proc server), created by B. Can
you access any interface of B from C? What’s the difference between aggregated and
contained objects? Yes, you can. This is fundamental postulate of COM: “If you can get there from
here, you can get there from anywhere", i.e. QI’ing for IUnknown you may proceed and to get a pointer
to any other interface, supported by the object. Aggregated object exposes its interface directly, without
visible intervention of the object container. Contained object is created within the object container and its
interfaces might be altered or filtered by the object container.
21. What is ROT ? GIT ? Count pros and cons of both. By definition, running object table (ROT) is a
globally accessible table on each computer that keeps track of all COM objects in the running state that
can be identified by a moniker. Moniker providers register an object in the table, which increments the
object’s reference count. Before the object can be destroyed, its moniker must be released from the
table. Global Interface Table (GIT) allows any apartment (either single- or multi-threaded) in a process
to get access to an interface implemented on an object in any other apartment in the process.
22. If you have an object with two interfaces, can you custom marshal one of them? No! The
decision to use custom marshaling is an all-or-nothing decision; an object has to custom marshal all its
interfaces or none of them.
23. Is there a way to register in-proc server without regsvr32.exe? Yes. Call DllRegisterServer() from
the client. Do not forget to call DLLUnregisterServer() from the same client. You may also use Registrar
object for the same purpose or use direct manipulation of the windows registry.
24. What is VARIANT? Why and where would you use it? VARIANT is a huge union containing
automation type. This allows easy conversion of one automation type to another. The biggest
disadvantage of VARIANT is size of the union.
25. How can you guarantee that only remote server is ever created by a client? Create an object
(call CoCreateObjectEx()) with CLSCTX_REMOTE_SERVER flag.
26. What is __declspec(novtable)? Why would you need this? __declspec(novtable) is a Microsoft’s
compiler optimization. The main idea of this optimization is to strip the vtable initialization code from
abstract class (for abstract class the vtable is empty, while it is initialized in contructor) MSDN has an
article on this topic.
27. What is an IDL? IDL stands for Interface Definition Language. IDL is the language to describe COM
interfaces.
28. What is In-proc? In-proc is in-process COM object, i.e. COM object that implemented as DLL and
supposed to be hosted by a container. When you have to instantiate the in-proc object remotely, you
may use DLLHost.exe application that was design specially for this purpose.
29. What is OLE? OLE is an object and embedding first implementation of COM spec available from MS
before COM was officially named COM.
30. Give examples of OLE usage. The most famous examples are probably drag and drop and structured
storage implementations.
31. What are 2 storage types for composite document? Storage and Stream.
49. Which tool is used to configure the port range and protocols for DCOM communications?
Note: This set of questions was sent in by a recruiter with no answers provided. Usually that means that
the interview is conducted by developers themselves who do not need a cheat sheet for the upcoming
interview, and they prefer to ask the questions that they are intimately familiar with. Please consider
looking into several in-depth titles on Visual Basic to find out the answers. I don’t have them. If you’re
willing to send them or post them here, I would gladly publish them.
Visual Basic
1. How do you register a component? Expected answer: Compiling the component, running REGSVR32
MyDLL.dll
2. Name and explain the different compatibility types when creating a COM component. Expected
answer: No Compatibility ? New GUID created, references from other components will not workProject
Compatibility ? Default for a new component <Not as critical to mention this one>Binary Compatibility ?
GUID does not change, references from other components will work
3. Why iss it important to use source control software for source code? Expected answer: Modification
history.Code ownership: Multiple people can not modify the same code at the same time.
4. What two methods are called from the ObjectContext object to inform MTS that the transaction
was successful or unsuccessful? Expected answer: SetComplete and SetAbort.
5. What is the tool used to configure the port range and protocols for DCOM communications?
Expected answer: DCOMCONFIG.EXE
6. What does Option Explicit refer to? Expected answer: All variables must be declared before use. Their
type is not required.
7. What are the different ways to Declare and Instantiate an object in Visual Basic 6? Expected
answer: Dim obj as OBJ.CLASS with eitherSet obj = New OBJ.CLASS orSet obj = CreateObject(?
OBJ.CLASS?) orSet obj = GetObject( ,? OBJ.CLASS?)orDim obj as New OBJ.CLASS
8. Name the four different cursor types in ADO and describe them briefly. Expected answer: The cursor
types are listed from least to most resource intensive.Forward Only ? Fastest, can only move forward in
recordset Static ? Can move to any record in the recordset. Data is static and never changes.KeySet ?
Changes are detectable, records that are deleted by other users are unavailable, and records created by
other users are not detectedDynamic ? All changes are visible.
9. Name the four different locking type in ADO and describe them briefly. Expected answer:
LockPessimistic ? Locks the row once after any edits occur.LockOptimistic ? Locks the row only when Update
is called.LockBatchOptimistic ? Allows Batch Updates.LockReadOnly ? Read only. Can not alter the data.
10. Describe Database Connection pooling (relative to MTS ) Expected answer: This allows MTS to reuse
database connections. Database connections are put to ?sleep? as opposed to being created and destroyed
and are activated upon request.
11. What are the ADO objects? Explain them. Provide a scenario using three of them to return data
from a database. Expected answer: Connection ? Connects to a data source; contains the Errors
collectionCommand ? Executes commands to the data source. Is the only object that can accept parameters
for a stored procedure.Recordset ? The set of data returned from the database.Scenario: There are many
possibilities. The most likely is as follows:Dim conn As ADODB.ConnectionDim rs As ADODB.RecordsetDim
Cmd As ADODB.Commandconn.ConnectionString = ?CONNECTION STRING?conn.OpenSet
Cmd.ActiveConnection = connCmd.CommandText = ?SQL STATEMENT?Set rs = Cmd.ExecuteSet
rs.ActiveConnection = Nothingconn.Close
12. Under the ADO Command Object, what collection is responsible for input to stored procedures?
Expected answer: The Parameters collection.
13. What are some benefits of using MTS? Expected answer: Database Pooling, Transactional operations,
Deployment, Security, Remote Execution.
14. What is the benefit of wrapping database calls into MTS transactions? Expected answer: If database
calls are made within the context of a transaction, aborting the transaction will undo and changes that occur
within that transaction. This removes the possibility of stranded, or partial data.
15. Describe and In Process vs. Out of Process component. Which is faster? Expected answer:An in-
process component is implemented as a DLL, and runs in the same process space as its client app, enabling
the most efficient communication between client and component.Each client app that uses the component
starts a new instance of it.An out of process component is implemented as an EXE, and unlike a dll, runs in
its own process space. As a result, exe’s are slower then dll’s because communications between client and
component must be marshalled across process boundaries. A single instance of an out of process
component can service many clients.
16. What are the main components of the ADO object model? How are they used? Expected
answer:Connection: Used to make a connection between your app and an external data source, ie, sql
server.Command: Used to build queries, including user-specific parameters, to access records from a data
source (which are returned in a Recordset)Recordset:Used to access records returned from an SQL query.
With a recordset, you can navigate returned records. You can also add, modify or delete records.