Int - Computer Science

Card 0 of 11

Question

int x = 10;

int y = 4;

``

int z = x / y;

What is the value of z?

Answer

There are two reasons why this will not produce the expected output, 2.5:

  1. 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.
  2. 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 declaring z as a float.

Compare your answer with the correct one above

Question

Consider the following code:

int i = 55, j = 3;

System.out.println((i / j + 3) / 5);

What is the output for the code above?

Answer

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.

Compare your answer with the correct one above

Question

Which of the following gurantees that your division does not lose its decimal portion?

Answer

Remember that so long as one element of a set of multiplications / divisions is a floating point value, the whole thing will be a floating point value. Thus, the correct answer is:

double d = 1.0 * 55 / 3;

You have to evaluate the right side:

1.0 * 55 will become 55.0. This will then finish out as a double in the division and store that value in d.

Note that the following does not work:

double d = (double)(55 / 3);

This code will first do the integer division 55 / 3. This will resolve to 18. Only then will the double cast occur. This will give you 18.0 but not 18.33333...

Compare your answer with the correct one above

Question

Consider the code below:

int i = 5, p = 27;

for(int l = 23; l < p; l++) {

i *= (l - 22);

}

What is the value for i at the end of the code above?

Answer

You could always trace the loop in the code manually. You know that it is going to run from l = 23 to l = 26. Recall that *= could be rewritten:

i = i * (l - 22)

Now, let's consider our first looping. For this, we would have:

i = 5 * (23 - 22) = 5 * 1

Now, let's calculate i for each looping from 23 to 26:

23: 5

24: 5 * (24 - 22) = 5 * 2 = 10

25: 10 * (25 - 22) = 10 * 3 = 30

26: 30 * (26 - 22) = 30 * 4 = 120

Compare your answer with the correct one above

Question

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?

Answer

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:

Compare your answer with the correct one above

Question

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?

Answer

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.

Compare your answer with the correct one above

Question

Consider the following code:

int i = 3;

for(int j = 5; j > 0; j--) {

i += i;

}

What will be the value of i at the end of this loop's iteration?

Answer

The loop in question executes for j values of 5 through 1. Thus, you will execute 5 times. For each looping, then you will have:

i = 3 + 3 = 6

i = 6 + 6 = 12

i = 12 + 12 = 24

i = 24 + 24 = 48

i = 48 + 48 = 96

The last value is your answer!

Compare your answer with the correct one above

Question

Which of the following variable assignments is NOT a valid assignment statement?

a. 15 = x - 15;

b. num = x * y;

c. x = y + 15

d. x + y = 15

Answer

a. 15 = x - 15;

You can not assign a value to an integer because they are not variables and do not store information.

d. x + y = 15

This statement does not assign a value to a single integer, therefore, it is not a valid variable assignment.

Compare your answer with the correct one above

Question

How do we set a method to return an Int in Swift(iOS)?

Answer

In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return an Int. So we set the return type using -> Int.

Compare your answer with the correct one above

Question

Refer to the following line of code:

double d = -4.73;

Which of the following correctly rounds d to the nearest integer?

Answer

The answer is (int) (d - 0.5) because you want roundNumber to be set to -5, and casting a negative number of type double to int will round the number up to the greater integer.

Math.abs(d) returns the absolute value of d, and does not typecast to int. This option will throw an incompatible types compiler error.

If you add the (int) cast to Math.abs(d), you will floor the number, or round down d to 4.

The statement int roundedNumber = (int) (d+.5) will add .5 to -4.67 resulting in (int) -4.17. This will result in roundedNumber being set to -4, since casting a negative double to an int will actually do a cieling operation ( for example (int) -4.99 is still -4).

Finally, int roundedNumber = Math.abs((int) (d)) casts d to -4 and the takes the absolute value of -4, resulting in 4.

Compare your answer with the correct one above

Question

Suppose you are given the following lines of code:

Integer a = new Integer(10);

Integer b = new Integer(4);

Which of the following lines will not generate an error?

I. if (a.intValue() == b.intValue())

II if ((a.toString()).equals(b.toString()))

III if ((a.intValue()).equals(b.intValue()))

Answer

The answer is I and II.

It is possible to compare primitive types (we are comparing int here) using the == operator, so statement I won't throw an error. Statement II also won't throw an error because the .equals method allows you to compare 2 strings to each other. However, you cannot invoke a method on an int, so statement III will result in an error.

Compare your answer with the correct one above

Tap the card to reveal the answer