AP Computer Science A › Int
Consider the following code:
int i = 55, j = 3;
System.out.println((i / j + 3) / 5);
What is the output for the code above?
There is an error in the code.
Be careful! This is integer division. Therefore, for every division, you will lose your decimal place. Thus:
i / j is the same as , but it becomes
.
Then, you will have . Once more, you lose the decimal and get
as your result.
Consider the following code:
int i = 55, j = 3;
System.out.println((i / j + 3) / 5);
What is the output for the code above?
There is an error in the code.
Be careful! This is integer division. Therefore, for every division, you will lose your decimal place. Thus:
i / j is the same as , but it becomes
.
Then, you will have . Once more, you lose the decimal and get
as your result.
Consider the following code:
int i = 55, j = 3;
System.out.println((i / j + 3) / 5);
What is the output for the code above?
There is an error in the code.
Be careful! This is integer division. Therefore, for every division, you will lose your decimal place. Thus:
i / j is the same as , but it becomes
.
Then, you will have . Once more, you lose the decimal and get
as your result.
Consider the code below:
int val = 205;
for(int i = 0; i < 5; i++) {
val /= 2;
}
At the end of its execution, what is the value of the variable val
in the code above?
6
6.40625
5.90225
12
25
Recall that the operator /=
could be rewritten:
val = val / 2;
Now, recall also that integer division drops the decimal portion always
. Therefore, this program will loop 5 times, doing the division of val
by 2 each time. This gives you:
Consider the code below:
int val = 205;
for(int i = 0; i < 5; i++) {
val /= 2;
}
At the end of its execution, what is the value of the variable val
in the code above?
6
6.40625
5.90225
12
25
Recall that the operator /=
could be rewritten:
val = val / 2;
Now, recall also that integer division drops the decimal portion always
. Therefore, this program will loop 5 times, doing the division of val
by 2 each time. This gives you:
Consider the code below:
int val = 205;
for(int i = 0; i < 5; i++) {
val /= 2;
}
At the end of its execution, what is the value of the variable val
in the code above?
6
6.40625
5.90225
12
25
Recall that the operator /=
could be rewritten:
val = val / 2;
Now, recall also that integer division drops the decimal portion always
. Therefore, this program will loop 5 times, doing the division of val
by 2 each time. This gives you:
Consider the following code:
int i = 100;
double d = 4.55, d2 = 3.75;
int j = (int)(d * 100 + d2);
What is the value of j at the end of the code's execution?
458
399
403
459
411
Do not over think this. Begin by evaluating the expression:
d * 100 + d2
This is the same thing as:
Now, this value is then cast to an integer:
(int)(458.75)
Remember that when you type cast an integer from a double value, you drop the decimal place completely. You do not round up or down. You just truncate it off. Thus, the answer is 458.
Consider the following code:
int i = 100;
double d = 4.55, d2 = 3.75;
int j = (int)(d * 100 + d2);
What is the value of j at the end of the code's execution?
458
399
403
459
411
Do not over think this. Begin by evaluating the expression:
d * 100 + d2
This is the same thing as:
Now, this value is then cast to an integer:
(int)(458.75)
Remember that when you type cast an integer from a double value, you drop the decimal place completely. You do not round up or down. You just truncate it off. Thus, the answer is 458.
Consider the following code:
int i = 100;
double d = 4.55, d2 = 3.75;
int j = (int)(d * 100 + d2);
What is the value of j at the end of the code's execution?
458
399
403
459
411
Do not over think this. Begin by evaluating the expression:
d * 100 + d2
This is the same thing as:
Now, this value is then cast to an integer:
(int)(458.75)
Remember that when you type cast an integer from a double value, you drop the decimal place completely. You do not round up or down. You just truncate it off. Thus, the answer is 458.
int x = 10;
int y = 4;
``
int z = x / y;
What is the value of z
?
There are two reasons why this will not produce the expected output, 2.5:
int
. Solve this by casting either of the operands (or the entire expression) to the float or double type.z
. Solve this value by declaring z
as a float.