The Hardest Test I Ever Gave (C++)
So while attending the University of Florida, I was a TA for a variety of classes. One summer in particular I took on an extra job of teaching an intro to programming course for incoming engineering freshman. The course wasn’t for credit so it let me be a little, shall we say, creative. Since the class was taught in a computer lab, the course was really me giving a tiny lesson and them then doing some assignment in class. I wanted them to have left the course having written many successful programs that solved simple problems.
But I also had to give a test. Ok, then. I sat down and made up a test. I had no intention of using the test in their “grades” (again, not for credit), so I wasn’t worried too much about the difficulty. I should have been. It turned out way too hard. The test had the unintended side effect of causing the high-school-programmed kids to have a collective aneurysm (you see because 90% and above is an A, and so they were obviously failing, hard).
I was able to find the old course website. I’ve chosen some of the more entertaining questions so you can try it yourself…
Question #1 — Syntax
1. Which of the following will give a syntax/parse error? (circle all that apply) (note, x is an integer) *5 points* 1a) y = x++3; 1b) y = x+++3; 1c) cout >> "Hello"; 1d) if (x+3) x=x+3; 1e) x = (x++ == 3 + x % (x=x=3));
Not on the original test, but for bonus points, if x is 1 before 1e, what is x after that line?
Question #2 — Evaluating Expressions
2. What is the value of each of the following... *5 points* 2a. 11 % 3 _____ 2b. 2 - 6 * 2 _____ 2c. 3 / 1 + 1 _____ 2d. 5//3 _____ 2e. (34 > 19) _____
This should be straightforward. Except for 2d. That is just plain mean.
Question #5 — Why? Seriously, why?
5. What is the output of the following code?
*2 points*
int x = 0;
int y = 1;
int z = 2;
x = x + 1;
z = z + 1;
y = y + y - x;
if (x != z)
{
if (y = z)
cout << "This is the answer.";
else if (y != z)
cout << "This is not the answer.";
}
Output: ___________________________________________
This problem screams “trick-question” but to no avail. Every single one of them got this wrong. Every. Single. One. That is quite frankly amazing considering it’s essentially a 50-50 guess and I had about 30 students. I’ll go ahead and put this challenge out. Try to come up with a 50-50 question that every single student in your class gets wrong.
Bonus Question #1 — Words do not describe…
B1) What is the _EXACT_ output of the following code:
*2 points*
void main(int)
{
int counter1, counter2, j, k;
j = k = counter1 = counter2 = 0;
while (counter1++ != 10)
while (counter2++ < 10)
j = counter1 + (k = k + counter2);
cout << 'j' << 'k';
}
Output: ______________
I am a horrible person.
November 24th, 2008 at 9:28 am
The “if(y=z)” is an assignment statement, so it should return true. So #5 should print “This is the answer”.
The bonus almost had me until I saw the little quote marks at which point I had to have a little laugh. Output: jk
I am curious to find out the answers to #1 and #2d…my C syntax is not that good. Are you going to post the answers?
November 24th, 2008 at 9:45 am
No one caught the single = in question 5? That’s pretty sad…being able to recognize single =’s in c code is extremely important if you want to avoid stupid errors.
Also, the bonus question makes you a horrible person…jk
.
November 24th, 2008 at 9:53 am
I have an even harder question: Why do we keep giving classes and tests in C++ and then not using a single feature of C++? No classes and inheritance, no overloading, no reason whatsoever that that class shouldn’t have been taught in C.
As an aside- what did your test prove? That you know how to write a test that fools people? Everyone has done #5 at least once when writing programs in C. You test it, get the wrong output, check your code and fix it. Then you don’t make that mistake again. Trick questions don’t measure a persons knowledge, they measure a persons ability to detect stupid test questions given by TA’s with too much time on their hands. It’s questions like that that make people run away from Computer Science as fast as they can.
I understand the class wasn’t for credit- but did you stop to ask yourself if what you were doing was in the best interest of education? This was an introductory class for engineers. Instead of easing students fears about computer programming and making it interesting to them, you probably made a lot of them feel like they never want to take a Computer Science class again. The sorts of questions you asked are amusing to someone that’s been programming for a while. To someone just starting out it would likely make them want to gouge their eyes out. Why do so many TA’s seem to feel it’s their job to torture people for their own amusement?
November 24th, 2008 at 9:59 am
2d. 5//3 _____
the // is a comment, so it evaluates to 5. That’s the trick.
November 24th, 2008 at 10:03 am
@Grant :
1.
The following generate syntax / parse error : 1a (“x+++3″ is “x++” + “3″, but “x++3″ is nothing), 1c (>> is not a cout method, only <<), and that’s it. Only two
2.
2a = 2
2b = -10
2c = 4
2d is not valid
2e = 1
Pretty funny test. It’s funny to see how some people really are “formated” to “not think”, just “retrieve and apply knowledge”.
November 24th, 2008 at 10:11 am
@Naixn:
As jonnii already pointed out, 2d is valid and the answer is 5. the //3 evaluates to a comment.
November 24th, 2008 at 10:13 am
1c is not a syntax error. cout could be a local variable of any type, not necessarily std::cout
November 24th, 2008 at 10:16 am
> The “if(y=z)” is an assignment statement, so it should return true.
> So #5 should print “This is the answer”.
Careful, here. Assignment statements return the result of the assignment. Here, it assigns z (3 at this point) to y, and so returns 3, which is non-zero, thus true. If z had been zero, it would have returned zero, thus false, and it wouldn’t have mattered that it was an assignment.
November 24th, 2008 at 10:26 am
@ Mark Richards:
I agree. A lot of the engineering majors require classes that could be helpful someday, but aren’t critical to your future job. As with C++ at my school, every single engineering major needs to take the introduction to C++ class all the way from Computer Engineering to Environmental Engineering. If some 18 year old prospective Environmental Engineering kid came across this exam, it would further reinforce their idea that they’re going to fail anything to do with programming.
November 24th, 2008 at 10:41 am
Actually, 5//3 would generate a syntax error. I take it that it is implied that each of these would have a semicolon at the end of the line. The semicolon would become part of the comment. Even if it were part of a larger expression, anything after the // would be part of the comment and the only way to correct the syntax would be to have a semicolon on the next line.
November 24th, 2008 at 12:00 pm
It’s tests like these that prove how useless tests can be.
The only things that these questions could prove is that some people might notice some of the hard-to-find yet common programming errors when writing a C or C++ program. That does NOT show the difference between a good and bad programmer.
What you SHOULD have been doing is testing their knowledge of programming theory rather than crushing their spirit.
Oh, and question 5 is not 50/50; it’s 1:3. There are three possible paths in that program.
Also, it contains unnecessary code. You should have been teaching them how to write efficient and readable code that doesn’t have side effects. We’ve got enough hackers already.
November 24th, 2008 at 12:18 pm
Instructional opportunity FAIL.
November 24th, 2008 at 12:47 pm
Quite a good test. Most of the problems in today’s world is not programming per se, but rather program maintenance.
Being able to identify poorly written code is a large benefit in doing so, in addition, identifying _your own_ poorly written code 6 months later is always good. Many errors are language independent (unclosed case statements, non-broken loops, assignment errors) and if not quashed early will run rampant.
Crushing their spirits was a good call, as many people have this romantic notion of programming (Oh – I’m gonna become a game developer!) and bringing them down to the real world. These students probably suffered from that notion where programming is easy, 3D is fun, and money is around the corner. The less there are of these people in CS, the better.
November 24th, 2008 at 12:49 pm
Boggle at some of you.
Those of you whose “insight” consists of pointing out how poor this test is aren’t very “insightful”. You see, when I select the worst test I ever gave, and I select the worst questions on it, I’m already admitting to the world that this test sucked and that I royally screwed the pooch. Trying to “explain” to me why this test is a terrible example of testing methodology is a bit redundant. That’s -why- I selected to show it to -you- in the first place.
November 24th, 2008 at 12:57 pm
Actually,
5//3
;
Would be equivalent to 5
November 24th, 2008 at 1:06 pm
> Not on the original test, but for bonus points, if x is 1 before 1e, what is x after that line?
I believe the correct answer to this question is “I don’t know”: the C standard doesn’t say.
November 24th, 2008 at 1:07 pm
@ louis:
But you didn’t say it was the “worst” test you ever gave. You said it was the “hardest”, and that the non-credit nature of the class allowed you to be “creative”. It’s easy to mis-interpret those statements as being vaguely, if guiltily, pleased with yourself.
November 24th, 2008 at 1:20 pm
@lemur,
Fair enough. So let me state it here: the test was bad. This is, by far, the single biggest failure I ever had as a teacher (the test, not the class). Keep in mind that this is tempered by the other 8 questions not reproduced above, all of which most people would find more palatable. I hoped to make that clear but it’s obvious the self-deprecation can be interpreted as smugness.
I was a first-time teacher (as far as writing tests went), dealing with an audience I never dealt with (incoming freshmen), and I made a terrible test. I learned alot from this experience and moved on. I posted it so we can all laugh at the younger me.
For those worried about the students, don’t be. They knew the class was about the in-class work of actual programming. I felt that actually doing programming was the best way to learn it. I still feel that way. So I’m proud of that class. The test, however, wasn’t so good.
November 24th, 2008 at 1:43 pm
@a on 1c: even if it really is std::cout – it would not be syntax error. (Don’t know what the type of error is called though.)
November 24th, 2008 at 2:24 pm
Since no one else has taken a stab a the bonus for problem #1 I’ll have a go.
The answer is implementation dependent since x is assigned to more than once in the statement.
November 24th, 2008 at 2:34 pm
The last (bonus) question invokes undefined behavior because main() returns void, instead of int.
November 24th, 2008 at 2:56 pm
Why not use computers as compilers instead of students.
November 24th, 2008 at 2:56 pm
About half of those, in the old days, would be answered:
“depends on the compiler”
November 24th, 2008 at 8:39 pm
5//3 is a great example of why we need syntax highlighting.
November 24th, 2008 at 9:40 pm
How is Question #5 a 50-50 guess? There are three possible answers: “This is the answer.”, “This is not the answer.”, or nothing at all.
November 24th, 2008 at 11:13 pm
@Charles:
It’s true that you can make 5//3 work if you put a semicolon on the next line. However, you can also change the values of many of the other expressions by putting stuff on the next line. For example:
x =
3 / 1 + 1
* 3;
sets x to 6. If 5 is a valid answer to 2d, why isn’t 6 a valid answer to 2c?
November 24th, 2008 at 11:16 pm
ouch man! poor students…
November 24th, 2008 at 11:25 pm
Ow. My brain went pop on one or two of those, and I’ve been doing this proffesionally for at least 10 years.
That said, I’ve spent most of the day tracking down who’s sending my code a packet of spurious data. Stupy event driven systems with no origin information.
November 25th, 2008 at 12:26 am
@Tim: ‘5//3′ is as valid an expression as ‘5 /* blah */’ or as ‘6*4′. The semicolon is not part of the expression. The rest of the line would be ignored but that’s not relevant for it being an expression or not. The value of that expression is 5. Think e.g. #define n 5//3. So the answer to 2d is 5, regardless of how you “make it work”.
November 25th, 2008 at 12:58 am
Question (1e) is undefined, since it has the expression “x = x++”.
November 25th, 2008 at 3:09 am
You might be interested in my collection of these sort of questions as well:-)
http://www.gowrikumar.com/c/index.html
November 25th, 2008 at 3:52 am
I agree with Mark Richards in that I don’t see the use of these kind of tests (either in education or in job tests). It’s the equivalent of those spelling tests where you have to spell words that are never used in real live (except when you do spelling contests). Some people might enjoy figuring out what the result of 3x+++%12^–45 is, but it proves nothing except that those people have nothing better to do.
November 25th, 2008 at 8:01 am
I think you’ve confused puzzles with education.
I’ve been programming since 1965, and I wouldn’t waste two minutes on trying to unravel these conundrums.
First, they will render differently depending on the compiler.
Second, why rack your brain when you can just compile them and let the compiler do the work for you.
Third, anyone writing like this in production code would get [A] a stern warning and then [B] fired.