Job Interview: What's Wrong With this Code?
by Curtis Krauskopf
The problem with the Gimpel Software Bug of the Month
#750 (repeated in Listing 1
for your convenience) is on line 11. Carefully look
at the ON_THE_R0CKS symbol. The O
of ROCKS is actually a zero (0). Gimpel's PC-lint tool
provides a hint to the problem by reporting that the
ON_THE_ROCKS symbol is not used in the program even
though it's defined.
The zero in the ON_THE_R0CKS symbol
causes the ice() function to compile with one line:
return "";
Many thanks to alert reader ZenJu for correcting my
original answer.
The next code sample is actually
in C#. You might think that it's not fair to use a C#
code sample in a C++ interview. I, as an interviewer,
would not do that but I've been in an interview where
the interviewer thought that JavaScript and Java were
the same thing. If you recognized that the program was
in C# and said something to the interviewer in a cordial
way, then points for you. Even though you might not
have ever programmed in C#, the syntax is close enough
that you should be able to provide constructive criticism
about the code sample.
Eric
Gunnerson's blog on msdn.com provides details about
what's wrong with the code from a C# programmer's perspective.
Assuming that I didn't know C# and wasn't familiar
with HTML, I would suspect that appending the input
string to "<h1>Values</h1>" would cause
the input string to be adjacent to the label. For example:
ValuesLike this. This is not the case for normal HTML
because the </h1> tag forces a new paragraph (caveat:
this might be overridable with CSS but I'm not that
much of a CSS expert to know).
The point of the first code sample was to provide an
example of a program that wasn't C++ but was similar
to C++ and to examine it from a C++ coder's perspective.
This is the C# code for your convenience:
The second code sample, shown in Listing 3, asks if
a number can be assigned to a structure. The trick in
Listing 3 is that the code looks like it's doing one
thing when it's really doing something else. When 7
is being assigned to the Fred structure, the compiler
looks for a conversion for 7 to the Fred structure.
One is available with Fred's constructor. By tracing
this code in C++ Builder, you'll be able to see what
happens. A std::vector with 7 empty elements is instantiated.
The std::vector::operator=() method copies the temporarily
instantiated empty vector to f. The value that is written
to cout is 0 because the temporary vector was instantiated
with 7 empty elements.
|