Int
Help Questions
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.
Explanation
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.
Explanation
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.
Explanation
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
Explanation
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
Explanation
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
Explanation
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
Explanation
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
Explanation
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
Explanation
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
?
Explanation
There are two reasons why this will not produce the expected output, 2.5:
- When dividing by two integer operands, the result will be returned as an
int
. Solve this by casting either of the operands (or the entire expression) to the float or double type. - When storing a floating-point value in an integer variable, the decimal is truncated (discarded, cut off, not considered). Even if
was configured as suggested in step 1, and returned 2.5, the value 2 would be the value stored in
z
. Solve this value by declaringz
as a float.