Please help with these quesiotns they are all related thats why i didnt post eac
ID: 3940245 • Letter: P
Question
Please help with these quesiotns they are all related thats why i didnt post each one!
Explain what's wrong with the following code and show how to fix it.
i++;
What will be printed when the following statements are executed (3 questions have relationship)?
For the above Question 2, If they are 3 different questions (no relationship), then what are the results?
4. what is the value for q?
f = 23.4210
float f, q;
q = f – (long)f;
5. the following code is right?
byte b;
short s;
short i;
i = b + s;
Explanation / Answer
1. The problem in this code is that there are no braces after while statement. So, only one statement i.e the " System.out.println" line is considered inside yhe wile loop.So to get the desired output following changes should be done:
int i = 1;
while (i <= 10)
{
System.out.println("The square of " + i + " is " + i * i);
i++;
}
2. i = 6;
System.out.println(i--);
System.out.println(++i);
System.out.println(i);
If the above statements are executed the output will be(if no relationship):
a. is 6 (since its the post decrement operator so the value is not updated in the step in which the operator is used)
b. is 7 (its the pre increment operator so the value displayed will be one incremented to 6)
c. is 6 (it will simply display the value of i i.e. 6)
3.If there is relationship then the output will be:
a. is 6 (since its the post decrement operator so the value is not updated in the step in which the operator is used)
b. is 6 (after the previous step the value of i is 5 and now we have to increment 5 by 1 so the output is 6)
c. is 6 (after the previous step the val of i is 6 so it will simply print 6)
4.f = 23.4210
float f, q;
q = f – (long)f;
The value of "f" after converting to long will be 23. So the value of q after subtraction is ".4210".
5. byte b;
short s;
short i;
i = b + s;
The above code is incorrect as the size of byte is 1 byte and short is 2 bytes. So we cannot store short into a byte value.