Forums Register Login

output

+Pie Number of slices to send: Send
Can someone explain why the answer to the following M/C question is, "The code will not compile"?

if (x > 5 )
if ( y < 10 )
System.out.print(“1”);
System.out.print(“2”);
else
System.out.print(“3”);

Why is it not 12?

Thanks
+Pie Number of slices to send: Send
Welcome to the Ranch

What’s M/C? Is it multiple choice? Where is the question from?
You need to write the question on paper, leaving spaces between successive lines, adding {} so as to retain the flow of control unchanged. Then you will be able to see whether that code will compile. I presume x and y are already declared and initialised.
+Pie Number of slices to send: Send
This was from a quiz I had taken. Yes it is a multiple choice qestion. Here is the exact wording of the question.

Consider the following code fragment:



Assuming that x has a value of 10 and y has a value of 5, the output of this code fragment would be ____.
+Pie Number of slices to send: Send
try compiling it, that will tell you why, where is the link for "show some effort"?
+Pie Number of slices to send: Send
Adding code tags shows how you indented the code when you copied it. Is that the same indentation they used in the quiz (please quote the link, book title, or whatever) originally? I have not edited your first post, so as to remove indentation. Remember, in some quizzes, indentation is used to mislead confuse or lead astray. You need to indent it correctly, with added {}, as per the Java™ grammar. Then you can see why it says, “doesn’t compile.” Please post your indented version.
+Pie Number of slices to send: Send
I still prefer the paper-and-pencil technique, Wendy. That requires more effort ← here’s the link.
+Pie Number of slices to send: Send
Wow.....I thought this was a forum for beginners to get help. You don't know what I have done before I posted to this forum so how can you judge me?? I have complied it and I don't see why the answer is not 12.
+Pie Number of slices to send: Send
No need to be sarcastic, thank you very much.

You get a lot more help by writing it down on paper and showing us what you have than by our giving you the solution. If we simply do that, you learn nothing. If you write it on paper (or a word-processor) you will learn how to do it now and next time. I wrote it on paper myself to confirm that the website was giving the correct answer, and it too about 2 minutes.
+Pie Number of slices to send: Send
 

Tom Petty wrote:Wow.....I thought this was a forum for beginners to get help.



It is. Did you read the "Show Some Effort" link?

Help means you give it your best shot, post what you tried and what happened, or what you think the answer should be, and then we nudge you in the right direction toward figuring out your problem yourself. It doesn't mean just giving you the answer.

You don't know what I have done before I posted to this forum



Of course not. We can only go by what you've posted here, which shows no evidence of effort, nor any indication of what your thoughts on the matter are.

so how can you judge me??



Nobody is judging you. We just want you to be a more active participant in solving your problem. :-)

I have complied it and I don't see why the answer is not 12.



The answer is not 12 because that would require running it, and you can't run it, because you can't compile it, because it has a syntax error.

What error message did you get when you tried compiling it? What do you think that error means?
+Pie Number of slices to send: Send
I still prefer pencil and paper. But then I always do, don’t I? Also an eraser. Preferably a big one
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:No need to be sarcastic, thank you very much.

You get a lot more help by writing it down on paper and showing us what you have than by our giving you the solution. If we simply do that, you learn nothing. If you write it on paper (or a word-processor) you will learn how to do it now and next time. I wrote it on paper myself to confirm that the website was giving the correct answer, and it too about 2 minutes.



There is no sarcasim there. If I new what my mistake was there would be no reason to post on this forum. I took the quiz, I missed the probelm and I am curious as to why. The more I look at it I am thinking it is because there are no braces separating the if statements.
+Pie Number of slices to send: Send
write it down on paper, and indent it, or put the {} in.

if there aren't any {} how many lines of the source code are included in the if statement?
+Pie Number of slices to send: Send
 

Tom Petty wrote:There is no sarcasim there. If I new what my mistake was there would be no reason to post on this forum. I took the quiz, I missed the probelm and I am curious as to why. The more I look at it I am thinking it is because there are no braces separating the if statements.


Well, as has been suggested several times now - don't think about it; do it.

The likelihood is that the error message will explain exactly what the problem is.

Winston
+Pie Number of slices to send: Send
 

Tom Petty wrote: If I new what my mistake was there would be no reason to post on this forum.



Nobody has suggested otherwise. Not even close.

The more I look at it I am thinking it is because there are no braces separating the if statements.



So what happened when you added braces and tried again?

Then, once you get it working, do you unerstand why you originally got the error message you did?
+Pie Number of slices to send: Send
 

Winston Gutkowski wrote:
The likelihood is that the error message will explain exactly what the problem is.



Technically, yes, but in this case I'd say it's one step removed from the actual coding mistake. A tiny bit of reasoning is all it should take to bridge that gap though.
1
+Pie Number of slices to send: Send
 

Tom Petty wrote:


Hint: The indentation of this code is misleading and makes you think that it's structured in a certain way, while the structure is really something else.
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 675 times.
Similar Threads
The hashCode() e equals() methods was overriden
substring problem
question
Please explain the output
Constructor
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:19:28.