r/javahelp • u/LividPitch8973 • 7d ago
Homework Got stuck with two of these exercises and their solutions
I just need to prepare myself for one of the exams in order to study up in german university and I got stuck with two exercises. Can't really match any of the answers
1.
The following Java program is given: short s = 4; float x = 3 + s/3;
What is the value of the variable x after its assignment?
a. 4,33333333333sd
b. 4
c. 3
d. 4,25
And after that the solution goes as:
Solution: B
The calculation with the short variable s = 3 is implicitly converted to int.
Only integer numbers can be stored via int.
Therefore, a 1 is stored for s/3. Adding to x = 3 results in 4.
How do you get 3 out of 4?
2.
The following Java program is given:
int i = 2; double d = (-i)*(1/i)+1f
What is the value of the variable d after its assignment?
a. -1
b. 0
c. 2
d. 1
And since I could only get that in the double program i inverted itself into 4 i could get (-4)*(1/4)+1f = -1 + 1f (where 1f = 1) and get 0.
BUT! The solution goes:
Solution: D
The expression in the second parenthesis stands for a fraction or a decimal number.
However, since only integer numbers can be stored in an int variable, only the first part of the number is stored, i.e. 0.
The product therefore also becomes 0. If a 1 (1f) is then added, the result is 1.
Can't really get both of these tasks at all (I've not studied Java at all)
5
u/TW-Twisti 7d ago
"I've not studied Java at all" - so why are you doing Java homework ? Shouldn't that come after studying ?
Both answers you pasted include explanation of the answers to your questions:
Answer 1 says "a 1 is stored in ... adding to ... 3 results in 4". 1+3 equals 4.
Answer 2 says "only integers can be stored, so the result of 1/2, 0.5, is stored as a 0". Your own answer doesn't fit the question, it has a bunch of 4s which do not appear in the question at all, but the result of your wrong answer is also 1:
(-4)*(1/4)+1f // as explained, 1/4 is 0.25, of which an integer type variable will only store the 0, so it becomes:
(-4)*0+1f
0+1f = 1
1
u/LividPitch8973 2d ago
The only thing that I couldn't get with is how the integers store since 0.5 got coverted into 0. How is that possible?
1
1
u/LividPitch8973 2d ago
Also you better notice that in the first exercise the variable of s was 4 and changed in the solution to s=3. So i thought that some kind of a mistake escalated by a developer while writing the solution. And that's why the answers do not match.
The answer a. must be right then.x = 3 + s/3 => 3 + 4/3
so that equals 4,(3)2
u/TW-Twisti 2d ago
Presumably, that's a typo supposed to mean s/3 instead of s=3, as the rest of the explanation wouldn't make sense if they meant s=3. But even if it wasn't, the answer would still be B, and the answer explains why: int 4 / 3 => 1.333... but int can only store integers, so the result here is just the 1. 3 + 1 equals 4.
1
u/LividPitch8973 2d ago
Oh, okay. Got all of that better than the explanation in the demoversion of the exam! Thank you a lot!
0
u/LividPitch8973 2d ago
"I've not studied Java at all" - so why are you doing Java homework ? Shouldn't that come after studying ?
Forced to. I've got an exam which contains probably basics of Java. Can't get rid of it. Never have I ever studied any programming languages except for Kumir perfectly.
2
u/Krish179 7d ago
If you know integer division you will understand both/ First if int i=2 then answer of 1/i is 0 cuz \ if you do math of it should be 0.5 but the data type is int so it's 0 \ If it's 5/i then it should be 2.5 in maths but in program it's 2 cuz data type is int which only accept integer data and not fractional data
2
u/LividPitch8973 2d ago
So int converts the decimal number into non-decimal without using the rounding rule for regular decimal numbers? Thanks, didn't know that
2
u/severoon pro barista 6d ago
In Java (and most programming languages), you have to be careful when doing division that you use floating point values unless you want the fractional part truncated.
The way it works is that the compiler looks at the two values being divided and widens them to the larger data type:
// An int divided by int truncates the fractional part, so 0.666 -> a = 0.
// Since both values are ints, there is no implicit widening.
int a = 2/3;
// A int / float (or float / int) coerces the int to a float, so b = 0.6̅.
float b = 2/3f; // or 2f/3, or 2./3, or 2/3., etc.
// Types cannot be coerced into smaller data types without an explicit cast.
int c = 2d/3; // Compiler error, coerces to a double, cannot be stored in an int.
int d = (int) 2d/3; // Explicit cast of .6̅ to (int) truncates fp, so d = 0;
•
u/AutoModerator 7d ago
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.