Question:
Java code question... I'm confused >_
None
2008-05-22 01:46:43 UTC
Consider this statement for computing the average of x, y, and z.
*
double average = x + y + z / 3;

Which of the following statements is correct?

A) The code always computes the correct average.

B) The code works correctly provided x, y, and z are variables of type double

C) The code works correctly provided x, y, and z are variables of type int

D) The code only gives the right answer when x = -y


Won't it always give the wrong average? because its missing the ( ) around the numbers it needs to add?
Six answers:
go bubba go
2008-05-22 02:18:36 UTC
This question is testing your knowledge of operator precedence. the / will always get evaluated before the +. That means without any parens, z gets div'ed before getting added to y and x. Therefore, the only time this statement is correct is when x = -y, because:



(x + y) + (z / 3) == (x + y + z) / 3 when x = -y.
tidwell
2016-12-18 13:26:42 UTC
What’s the regular sort of start manage after 50? Nudity ~~~~~~~~~~~~~~~~~~~~~~~ What’s the replace between a woman chum and an important different? 40 5 lbs. ~~~~~~~~~~~~~~~~~~~~~~~ What’s the adaptation between a boyfriend and a husband? 40 5 minutes. ~~~~~~~~~~~~~~~~~~~~~~~ What’s the quickest answer to a individual’s coronary heart? by utilising skill of his chest with a pointy knife. ~~~~~~~~~~~~~~~~~~~~~~~ Why do men decide to marry virgins? they might’t stand grievance. ~~~~~~~~~~~~~~~~~~~~~~~ Why is it so problematical for women to hunt for out men which could be comfortable, being concerned, and purely applicable gazing? for the explanation that those men have already got boyfriends. ~~~~~~~~~~~~~~~~~~~~~~~ What’s the replace between a clean husband and a clean dogs? After a 365 days, the dogs remains excited to look you. ~~~~~~~~~~~~~~~~~~~~~~~ What makes men chase women they have have been given no aim of marrying? The equivalent urge that makes domestic canines chase autos they have no aim of using. ~~~~~~~~~~~~~~~~~~~~~~~ What do you call a sensible blonde? A golden retriever. ~~~~~~~~~~~~~~~~~~~~~~~ Why does the bride in any respect situations positioned on white? pondering the actuality that it’s marvelous for the dishwasher to envision the range and refrigerator. ~~~~~~~~~~~~~~~~~~~~~~~ How do you recognize mutually as you’re fairly grotesque? domestic canines hump your leg with their eyes closed. ~~~~~~~~~~~~~~~~~~~~~~~ How do you recognize once you’re maximum excellent a pathetic life? while a nymphomaniac tells you, “we could purely be friends.” ~~~~~~~~~~~~~~~~~~~~~~~ Why did God create alcohol? So grotesque human beings could have intercourse, too. ~~~~~~~~~~~~~~~~~~~~~~~ What did the blonde say while she got here across she replaced into pregnant? “Are you specific it’s mine?” ~~~~~~~~~~~~~~~~~~~~~~~ What’s the replace between Beer Nuts and Deer Nuts? Beer Nuts are $one million, and Deer Nuts are often below a dollar. ~~~~~~~~~~~~~~~~~~~~~~~ What could you call it while an Italian has one arm shorter than the different? A speech impediment. ~~~~~~~~~~~~~~~~~~~~~~~ Why do men to discover it perplexing to make eye touch? Breasts don’t have eyes. ~~~~~~~~~~~~~~~~~~~~~~~ What’s the adaptation between a Northern fairytale and a Southern fairytale? A Northern fairytale starts “as quickly as upon a time.” A Southern fairytale starts “Y’all ain’t gonna experience this rubbish.”
anonymous
2008-05-22 01:59:49 UTC
see the code will get u the right ans. provided this is what u give. (x+y+z)/3 for 3 mark avg. if u give this x+y+z/3 it will take z/3 first for which dou is necessary and this gives the wrong ans. this is the only right and systematic approach to get the right answer.
Alex
2008-05-22 01:55:57 UTC
The answer is D. If x is -y it means that:

average= -y + y + z / 3 = z / 3.
A J
2008-05-22 01:58:26 UTC
The answer must be D.



if x=-y then they disappear from the equation and make the formula correct. It would otherwise be wrong precisely because there are no brackets.



You are partly correct!
Petos
2008-05-22 01:52:01 UTC
Answer is D


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...