25 AnswersNew Answer
As Simon Sauter points out the answer is that the value of x after excuting the above code is 11. Although there is no output in the example code snippet you could have simply added a printf statement at the end to verify this yourself. If you got a job as a programmer without knowing this then either the competition was incompetent or IBM has p1$$ poor standards these days. It used to be IBM would not hire anyone without a degree. int x = 1, y = 2; // initial values // The if statement has no braces so consists of a single line if (x==y) // <- evaluates to false y = x + 10; // <- not executed x = y + 8; // <- x is now 10 x++; // <- x is now 11 after executing this statement //add a printf("%d\n", x); statement here to verify. No compiler could possibly output 0 using the code fragment because there is no output statement whatsoever. I don't know which frightens me more. The fact that you claim that you got a job with IBM without knowing this, or the fact that the question still haunts you. I suspect that it's a "do my homework question" but I'll answer it anyway because you had the cajones to claim you got a job with IBM without knowing something this basic.
Mr. E , the code does not anything output. may be you want to show us the complete code?
Mr. E in that case better questions would be: "What's wrong with this code?" "Why does this output 0?" Although I don't see how it can output anything without any kind of output statement. ¯\_(ツ)_/¯
So my guess, as someone who knows virtually nothing about C, is that the value of x is 11 but the program has no output.
Mr. E. I'm not bent out of shape I just think your full of it. No compiler will generate 0 as output unless you program it to generate 0 as the output. If you don't know the difference between console output and a return code then you know even less than I feared. A full scholarship would explain a lot. You must have got your scholarship, degree, and job due to some form of affirmative action quota.
Run it and find out.
I don't know C, but I'm pretty sure that this is not a complete program. So I'm not sure if that's relevant. But I notice that you switch back and forth between "what is the value of x" (which seems to be their question) and "what is the output" (which seems to be the question you answered). Maybe that's the entire issue? I don't know.
@Martin Taylor: I aced all the other interview questions which were mainly writing various programs in C, so, obviously I do know quite a lot about the "printf("%d\n", x);" statement, and I also knew the correct value of x = 11, but just like @Simon Sauter pointed out, I must've thought about the output of the program, instead of the value of x (my mistake - at least I had the guts to admit I was wrong, unlike other people), that's why that was the only question that was marked as "Incorrect", all the other ones being "Correct", so there's no need to get all bent out of shape about it, @Martin Taylor. And, mind you & take note, @Martin Taylor: all the compilers in the world will always output 0 on that given code, on any day (you can even verify it, with any compiler you want, if you have any kind of doubts)! And IBM's standards are still the highest of all. P.S. I do have a Software Engineering university degree (B.A.) from CalTech (full-scholarship), by the way. Adios!
I already did that, but the point is, it outputs 0 in any compiler. So, what's the answer to my question, then, seeing as how the value of x can't be 0?
@Lothar, there are no other lines of code! That's what I was suprised about, myself. It would output 0. It's a programming question that was given by IBM in one of their interviews. The full question is (quote): What is the value of “x” after the execution of the code below? int x = 1, y = 2; if (x == y) y = x +10; x = y + 8; x++; No any other info provided.
Notice that they don't use the word "output".
@Lothar, this was the only question that I didn't get right in their interview, due to the lack of any other information: I said it outputs 0 (nothing), they said that's not the correct answer. I got all the other questions right (they were all correct), I got the job, but this question still haunts me today.
I thought someone would say otherwise, but apparently everyone on here agrees with me on that (you - directly and others - indirectly). If anyone has the correct answer to that, let me know by answering this question.
@Simon Sauter: well then, what's the correct answer?
@Simon Sauter: yes, but they still marked the only question I got wrong as "Incorrect". Is there a "correct" answer to their question?!
Sugandha Das Thierry Andriamihaja maybe in the future you could read the answers that have already been given before posting. Then instead of repeating information that has already been provided by others you could move on to questions that haven't been resolved yet.
int x = 1, y = 2; //x=1 and y=2 if (x == y) //x==y-->false and 0-->y=2 y = x +10; // not executed x = y + 8; // x is now 10 becuse y=2 and 2+8=10 x++; // x=10+1 the value of x is 11
play some C chalanges in Sololearn to see it's common to ask value of a variable after some code😁
Mr. E, you should expect all kinds of criticisms when asking a question. I think your question has gotten many useful feedbacks as to how to ask a question and how your program doesn't output the same as you expect, you didn't really have to give them negative feedbacks by verbally attacking and trying to compare your self-worth with others, Martin Taylor just has the guts to say what everyone else is thinking.