Hmmmm... ok. Seems pretty easy. My Guess: 100
I like Why06's idea of the 'error of the day' so I figured i'd post some of the questions I have asked people while interviewing them for C++ Software Developer Positions.
These questions are usually written on a whiteboard (or printed out) and thus are meant to be answered WITHOUT the help of a compiler in an interview! So just read the code, don't copy/paste/build/run and just figure out what it does by reading.
I suggest you answer in a 'white font' so that others don't immediately see your answers
IQOD #1:
Consider the following program snippet:
Question: What output is generated / is the value of 'a'?Code:#define ABC 20 #define XYZ 10 #define XXX ABC-XYZ void main() { int a; a = XXX * 10; printf("%d\n",a); }
Last edited by B1ackAnge1; 09-17-2009 at 07:10 PM.
Hmmmm... ok. Seems pretty easy. My Guess: 100
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
B1ackAnge1 (09-18-2009)
And you sir would be: Incorrect
Thanks a lot for these Black Angel =D
My guess: 100
But: I'm not sure whether or not you should have put the abc-xxx in brackets hmm actually nvm
Last edited by zeco; 09-17-2009 at 07:54 PM.
You guys want the full answer with explanation now or tomorrow so the other guys can chime in? I can just write it in white I guess?
Last edited by zeco; 09-17-2009 at 08:05 PM.
B1ackAnge1 (09-18-2009)
Woops double post >_<
Highlight Post to see answer & explanation below
-----------------------------------------------
The Answer is: -80
#define works like a 'Find & Replace' before the program gets compiled ,thus the program actually sees:
Following the standard operator order the multiplication gets performed first so you end up with 20-100 = 80;Code:#define ABC 20 #define XYZ 10 #define XXX 20-10 void main() { int a; // a = XXX * 10; This is then seen as: a = 20 - 10 * 10; printf("%d\n",a); }
Zeco got close by mentioning the brackets. Had we wanted to output 100 instead then placing brackets in the definition of XXX would've taken care of that: #define XXX (20-10); a = (20-10) * 10; etc
This is why i fail and am stupid.
When i previously said i got it, i had the same solution as Black angel. Then a little thing in my brain said. Are you stupid, - and * have the same operator precendence(importantness thingy). Even though i do know that * goes before -. I need to like sterilize my brain. grrr.
But now i am really anticipating your next one. These are really cool. I have to wait an entire 12-24 hours T_T.
Last edited by zeco; 09-17-2009 at 08:06 PM.
OK. Before I see the answer last Guess: 200 or there something up with the #define statements
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Yeh. I noticed that. I have no idea how #define works, but now I do. That's actually pretty neat, but I guess since i failed I don't get the job xD.
Zeco you gotta lighten up. Your harder on urself then anyone else. I would tell you to stop calling urself stupid, but I'm not sure what weird side affect that might have with your split personality and all? o_O
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Usually they get to work on the whiteboard with a dry-erase marker. Depending on the problem I'd talk them through it if they get it wrong: "Are you sure?" give them a hint and see if they get it depending on how well they're doing.
Other times I may just move on and mark them as wrong if it's a simple problem.
If you're interviewing for a C++ position but don't know the difference between i++ and ++i then that says it all right there
20 -10*10 = 20 -100 = -80 <--answer
owned
why don't u guys put all these questions together so other people can try them to?
B1ackAnge1 (09-18-2009)