• Post Reply Bookmark Topic Watch Topic
  • New Topic

output  RSS feed

 
Tom Petty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Petty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ____.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try compiling it, that will tell you why, where is the link for "show some effort"?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still prefer the paper-and-pencil technique, Wendy. That requires more effort ← here’s the link.
 
Tom Petty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still prefer pencil and paper. But then I always do, don’t I? Also an eraser. Preferably a big one
 
Tom Petty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!