Computer Science : Primitive Data Types

年代tudy concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

← Previous 1 3 4 5 6 7

Example Question #1 :年代tandard Data Structures

int x = 10;

int y = 4;

int z = x / y;

What is the value ofz?

Possible Answers:

Correct answer:

Explanation:

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 anint。年代olve 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 ifwas configured as suggested in step 1, and returned 2.5, the value 2 would be the value stored inz。年代olve this value by declaringzas a float.

Example Question #2 :Primitive Data Types

Consider the following code:

int i = 55, j = 3;

年代ystem.out.println((i / j + 3) / 5);

What is the output for the code above?

Possible Answers:

There is an error in the code.

Correct answer:

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 getas your result.

Example Question #1 :年代tandard Data Structures

下列哪一项保障你的军n does not lose its decimal portion?

Possible Answers:

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

double d = 1.0 * 55 / 3;

double d = 55 / 3;

None of the answers are correct.

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

Correct answer:

double d = 1.0 * 55 / 3;

Explanation:

Remember that so long asoneelement 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...

Example Question #1 :年代tandard Data Structures

Consider the following code:

int i = 100;

double d = 4.55, d2 = 3.75;

int j = (int)(d * 100 + d2);

What is the value ofjat the end of the code's execution?

Possible Answers:

459

411

403

399

458

Correct answer:

458

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.

Example Question #1 :Primitive Data Types

Consider the following code:

int i = 3;

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

i += i;

}

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

Possible Answers:

96

48

243

729

24

Correct answer:

96

Explanation:

The loop in question executes forjvalues 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!

Example Question #1 :Primitive Data Types

Consider the code below:

int i = 5, p = 27;

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

i *= (l - 22);

}

What is the value foriat the end of the code above?

Possible Answers:

0

150

75

5

120

Correct answer:

120

Explanation:

You could always trace the loop in the code manually. You know that it is going to run froml = 23tol = 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 calculateifor 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

Example Question #2 :Primitive Data Types

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 variablevalin the code above?

Possible Answers:

25

12

6

6.40625

5.90225

Correct answer:

6

Explanation:

Recall that the operator/=could be rewritten:

val = val / 2;

Now, recall also that integer division drops the decimal portionalways。Therefore, this program will loop 5 times, doing the division ofvalby 2 each time. This gives you:

Example Question #51 :Computer Science

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

Possible Answers:

a

d, c

b, c

a, d

d

Correct answer:

a, d

Explanation:

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.

Example Question #1 :Primitive Data Types

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

Possible Answers:

func method->Int{}

func() -> Int{}

method->Int {}

func method() -> Int {}

Correct answer:

func method() -> Int {}

Explanation:

In Swift, all methods must first say what they are. They are functions, so they are prefixed withfunc.Next, methods must have a name. In this case, we named itmethod。所有方法需要指定参数,即使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.

Example Question #1 :Primitive Data Types

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

Possible Answers:

func method->Int{}

func() -> Int{}

method->Int {}

func method() -> Int {}

Correct answer:

func method() -> Int {}

Explanation:

In Swift, all methods must first say what they are. They are functions, so they are prefixed withfunc.Next, methods must have a name. In this case, we named itmethod。所有方法需要指定参数,即使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.

← Previous 1 3 4 5 6 7
Learning Tools by Varsity Tutors