Bad interview question

D

Dann Corbit

A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);
}


I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.

Except that shifing left of signed integers is implementation defined.

Except that there is a varadic function (printf) with no prototype in
scope.

We also have an integer function (main) with no return value.

We also have implicit declaration of main, which is no longer allowed.

It's hard to imagine such a bad question written in such a compact way.
 
B

Ben Bacarisse

Dann Corbit said:
A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);
}


I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.

Except that shifing left of signed integers is implementation defined.

No, it is either well-defined or undefined. When the left operand is
negative it is undefined (at least in C99).
Except that there is a varadic function (printf) with no prototype in
scope.

We also have an integer function (main) with no return value.

We also have implicit declaration of main, which is no longer allowed.

You can't have these last two at the same time. In C99 the first is OK
and the second is an error. In C90 the first is an error and the second
is just poor style.
It's hard to imagine such a bad question written in such a compact
way.

One could add that int main(void) is better yet; that the printf
format should end with a newline; and that %x requires an unsigned
argument, not a signed one.
 
I

izlesa

Dann said:
A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);
}
Can you post url of that questionaire?
 
N

Nick Keighley

A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);

}

I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.

I bet they assumed 32 bit ints

Except that shifing left of signed integers is implementation defined.

Except that there is a varadic function (printf) with no prototype in
scope.

We also have an integer function (main) with no return value.

We also have implicit declaration of main, which is no longer allowed.

failure to fflush or output a newline
 
V

Vincenzo Mercuri

Richard said:
By "best", do you mean "least bad", or "funniest"?

If I said the "best" I would mean that both the question
and the answer are fine. But they're not.

If I said the "funniest" I would mean that the question
is well-posed and the answer ridiculous, but not even
this is the case.

It's the "least bad", because it is a good compromise
between a good answer and a ill-posed question!
 
K

Keith Thompson

Nick Keighley said:
A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);

}

I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.
[list of criticisms snipped]

failure to fflush or output a newline

It's implementation-defined whether an output text stream must
be terminated by a newline. If it's not required, fflush isn't
necessary; if it's required, fflush doesn't help.
 
J

John Bode


I particularly enjoyed this one (Question 141):
main()
{
int i, j;
scanf("%d %d"+scanf("%d %d", &i, &j));
printf("%d %d", i, j);
}

A) Compile error
B) 0, 0
C) Runtime error.
D) the first two values entered by the user
Explanation: two values entered, 3rd will be null pointer assignment

If someone asked me this in an interview and expected an answer that
*wasn't* "the author should be shot on sight", that tells all I need
to know about their code.
 
E

Eric Sosman


Good Lord! These are simply *awful*! I went to the first
question and started paging through them:

Q1: Explanation is wrong
Q2: Explanation omits a crucial point
Q3: Explanation is wrong (on a technicality)
Q4: Explanation does not actually answer the question, but
suggests an incorrect answer
Q5: The question isn't about C *and* the explanation is wrong
Q6: The question is worded ambiguously. No answer or explanation
is offered, but it's necessarily wrong (because one can always
interpret the question "the other way")
Q7: Explanation is wrong
Q8: Explanation omits a crucial point
Q9: ANSWER IS CORRECT! A RED-LETTER DAY!
Q10: Explanation is silly, very nearly useless
Q11: Explanation is stupid *and* typographically inept
Q12: Explanation *and* question's nomenclature are wrong
Q13: Explanation *and* question's nomenclature are wrong
Q14: Explanation is wrong
Q15: Explanation is sort of right, but questionable
Q16: Explanation is sort of right, but sort of questionable
Q17: Explanation is outdated
Q18: ANOTHER RED-LETTER DAY! WHOOPEE!
Q19: The question isn't about C, and the explanation is simplistic
Q20: The question isn't about C, the explanation is simplistic,
and the first piece of advice is pretty bad
...

Some of the errors are "minor," involving misuse of terminology
that could be corrected fairly easily (e.g., Q3 implying that it's
not possible to store into mallocated memory). Some errors are more
serious (for example, Q8 forgetting to mention the use of `volatile'
in connection with setjmp). Some are just plain sloppy ...

Of the first twenty questions/explanations, only two were right
(or if erroneous, I didn't catch it). We might also allow Q17 on the
grounds that the explanation used to be true, ten years ago. Then
we might double the accuracy rate on the grounds that only a pedant
would care about the inaccuracies -- oh, hell, lets triple the rate
to spite the pedants -- and there are still more wrong than right
answers in this sample.

Where do you suppose these half-baked things come from?
 
B

Ben Pfaff

Eric Sosman said:
Where do you suppose these half-baked things come from?

I suspect that people who know C don't spend time writing or
reading interview questions on these sites, so that the questions
and answers are only written by people who don't know C.
 
V

Vincenzo Mercuri

John said:
I particularly enjoyed this one (Question 141):


If someone asked me this in an interview and expected an answer that
*wasn't* "the author should be shot on sight", that tells all I need
to know about their code.

You should be careful then, they may happen to take
a video of your interview...just turning away denying the answer
and saying "Not very pleased to C you" would be fine (laughs)
 
V

Vincenzo Mercuri

Eric said:
Good Lord! These are simply *awful*! I went to the first
question and started paging through them:

Q1: Explanation is wrong
Q2: Explanation omits a crucial point
Q3: Explanation is wrong (on a technicality)
Q4: Explanation does not actually answer the question, but
suggests an incorrect answer
Q5: The question isn't about C *and* the explanation is wrong
Q6: The question is worded ambiguously. No answer or explanation
is offered, but it's necessarily wrong (because one can always
interpret the question "the other way")
Q7: Explanation is wrong
Q8: Explanation omits a crucial point
Q9: ANSWER IS CORRECT! A RED-LETTER DAY!
Q10: Explanation is silly, very nearly useless
Q11: Explanation is stupid *and* typographically inept
Q12: Explanation *and* question's nomenclature are wrong
Q13: Explanation *and* question's nomenclature are wrong
Q14: Explanation is wrong
Q15: Explanation is sort of right, but questionable
Q16: Explanation is sort of right, but sort of questionable
Q17: Explanation is outdated
Q18: ANOTHER RED-LETTER DAY! WHOOPEE!
Q19: The question isn't about C, and the explanation is simplistic
Q20: The question isn't about C, the explanation is simplistic,
and the first piece of advice is pretty bad
...

Some of the errors are "minor," involving misuse of terminology
that could be corrected fairly easily (e.g., Q3 implying that it's
not possible to store into mallocated memory). Some errors are more
serious (for example, Q8 forgetting to mention the use of `volatile'
in connection with setjmp). Some are just plain sloppy ...

Of the first twenty questions/explanations, only two were right
(or if erroneous, I didn't catch it). We might also allow Q17 on the
grounds that the explanation used to be true, ten years ago. Then
we might double the accuracy rate on the grounds that only a pedant
would care about the inaccuracies -- oh, hell, lets triple the rate
to spite the pedants -- and there are still more wrong than right
answers in this sample.

Where do you suppose these half-baked things come from?

AHA....actually I don't know...but all I am wondering about
is how these websites pretend to show any kind of credibility.
I kinda hope they just offer some sort of catering service
so customers can enjoy their free time and get fun with some
half-baked things before going back to work!
 
E

Ersek, Laszlo

Good Lord! These are simply *awful*! I went to the first
question and started paging through them:

Q1: Explanation is wrong
Q2: Explanation omits a crucial point
Q3: Explanation is wrong (on a technicality)
Q4: Explanation does not actually answer the question, but
suggests an incorrect answer
Q5: The question isn't about C *and* the explanation is wrong
Q6: The question is worded ambiguously. No answer or explanation
is offered, but it's necessarily wrong (because one can always
interpret the question "the other way")
Q7: Explanation is wrong
Q8: Explanation omits a crucial point
Q9: ANSWER IS CORRECT! A RED-LETTER DAY!
Q10: Explanation is silly, very nearly useless
Q11: Explanation is stupid *and* typographically inept
Q12: Explanation *and* question's nomenclature are wrong
Q13: Explanation *and* question's nomenclature are wrong
Q14: Explanation is wrong
Q15: Explanation is sort of right, but questionable
Q16: Explanation is sort of right, but sort of questionable
Q17: Explanation is outdated
Q18: ANOTHER RED-LETTER DAY! WHOOPEE!
Q19: The question isn't about C, and the explanation is simplistic
Q20: The question isn't about C, the explanation is simplistic,
and the first piece of advice is pretty bad

I didn't look at the questions at all, but your summary above was very
enjoyable. The crescendo over Q14-Q16 is priceless. Thanks.

lacos
 
N

Nick Keighley

It's implementation-defined whether an output text stream must
be terminated by a newline.  If it's not required, fflush isn't
necessary; if it's required, fflush doesn't help.

I've found fflush() to change the behaviour of programs. Are you
talking abou the case where the program is terminating anyway?
 
K

Keith Thompson

Nick Keighley said:
I've found fflush() to change the behaviour of programs.

Certainly; there wouldn't be much point in having it in the language if
it didn't.
Are you
talking abou the case where the program is terminating anyway?

I'm talking about the case quoted above, so yes. When the
program terminates, stdout is implicitly flushed and closed;
if the implementation requires a terminating new-line and the
program doesn't provide one, it's not clear what's going to happen.
(I've argued that the behavior is undefined, but I won't get into
that now.)
 
T

Tim Rentsch

Keith Thompson said:
Nick Keighley said:
A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);

}

I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.
[list of criticisms snipped]

failure to fflush or output a newline

It's implementation-defined whether an output text stream must
be terminated by a newline. If it's not required, fflush isn't
necessary; if it's required, fflush doesn't help.

What I think you mean is that fflush() isn't guaranteed to
help. It _may_ help, and I expect it does help in some
implementations, even if it isn't guaranteed to. To say
that another way, using fflush() won't hurt, and although
it isn't guaranteed to help it might. Do you agree?
 
K

Keith Thompson

Tim Rentsch said:
Keith Thompson said:
Nick Keighley said:
A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);

}

I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.
[list of criticisms snipped]

failure to fflush or output a newline

It's implementation-defined whether an output text stream must
be terminated by a newline. If it's not required, fflush isn't
necessary; if it's required, fflush doesn't help.

What I think you mean is that fflush() isn't guaranteed to
help. It _may_ help, and I expect it does help in some
implementations, even if it isn't guaranteed to. To say
that another way, using fflush() won't hurt, and although
it isn't guaranteed to help it might. Do you agree?

I'd be surpised if adding a call to fflush() just before the end
of main() made any difference.

When the program terminates, "all open streams with unwritten
buffered data are flushed, all open streams are closed, and all
files created by the tmpfile function are removed".

I'm not saying that it *can't* make a difference (in the presence
of undefined behavior, all bets are off), but I can't think of a
plausible scenario where it would.

Under what circumstances would you expect a call to fflush() to do
anything that the subsequent implicit flushing of all open streams
wouldn't have done anyway?
 
T

Tim Rentsch

Keith Thompson said:
Tim Rentsch said:
Keith Thompson said:
A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);

}

I guess that the answer they were looking for would be "the letter 'f'
for the width of the integer type on the system with the bottom nybble
cleared'.

[list of criticisms snipped]

failure to fflush or output a newline

It's implementation-defined whether an output text stream must
be terminated by a newline. If it's not required, fflush isn't
necessary; if it's required, fflush doesn't help.

What I think you mean is that fflush() isn't guaranteed to
help. It _may_ help, and I expect it does help in some
implementations, even if it isn't guaranteed to. To say
that another way, using fflush() won't hurt, and although
it isn't guaranteed to help it might. Do you agree?

I'd be surpised if adding a call to fflush() just before the end
of main() made any difference.

When the program terminates, "all open streams with unwritten
buffered data are flushed, all open streams are closed, and all
files created by the tmpfile function are removed".

I'm not saying that it *can't* make a difference (in the presence
of undefined behavior, all bets are off), but I can't think of a
plausible scenario where it would.

Under what circumstances would you expect a call to fflush() to do
anything that the subsequent implicit flushing of all open streams
wouldn't have done anyway?

I guess it depends on whether one believes that the semantics of
'fflush()' and the semantics of flushing on 'exit()' must be
identical. Normally I would expect that they would be, but in
the presence of an implementation that requires a newline for the
last line (already a strange bird as C implementations go), I
imagine that they might be different. (Incidentally, I'm of a
different opinion about whether omitting the final newline in
such implementations yields undefined behavior, but my comments
don't depend on that either way.) I expect there to be
implementations where a final fflush() makes a difference even
though I wasn't thinking of any specific circumstances where it
would, because I believe (perhaps wrongly, I admit) that
implementations on systems that "require a final newline" may
choose to behave in peculiar ways besides just that one decision.
 
R

Ralph Malph

A web search accidentally took me to a C interview questionaire.
Here is one of the questions:

98. ajz at his interviews asks what will be printed out when the
following code is executed:
main()
{ printf("%x",-1<<4);
}
I have never really been asked this sort of inane
puzzle question. Indeed, I almost have stopped
believing that they exist in real life at all.
Who the hell does this?
Did it list a company name?
I just cannot see how knowing the answer
to little puzzles is a sign of programming skill.
Is their codebase full of integer shifts or something?
This is a terrible question in that it does not require
any test of algorithmic skill, just a knowledge of
the seldom used shift operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top