### All AP Computer Science A Resources

## Example Questions

### Example Question #22 : Standard Data Structures

int arr[4]={0};

arr[2]=arr[2]+2;

arr[1]=1;

Which of the following displays the correct elements of arr?

**Possible Answers:**

{1 2 3 4}

{0 1 2 3 4 5}

{1 2 0 0}

{0 1 2 3}

{0 1 2 0}

**Correct answer:**

{0 1 2 0}

The first line of code creates an integer array of size 4 filled with all 0s. The second line changes the third element to two. Remember that the first index actually starts at 0, so arr[2] really points to the third value.

The next line sets the second element to 1, so your final array is

{0 1 2 0}.

### Example Question #23 : Standard Data Structures

**Consider the following code:**

`double a = 4.5, d = 4;`

`int b = 10,c=5;`

`double e = a / b + c / d - d % c;`

What is the value of `e`

at the end of this code's execution?

**Possible Answers:**

There is a type-related error in the code.

**Correct answer:**

The easiest way to work through this code is to comment on its parts. See the comments below in **bold**.

`double a = 4.5, d = 4;`

`int b = 10,c=5;`

**/***

**a / b: This is a division that will maintain the decimal portion (since it has a double involved in it). 4.5 / 10 will evaluate to 0.45. **

**c / d: Since this has both an int and a double, it will evaluate to a double. That means that it will maintain its decimal portion as well. It is: 5 / 4 or 1.25.**

**d % c: Since d is 4, this kind of remainder division works just like having two integers for the modulus. 4 % 5 is just 4. (It is 0 remainder 4.)**

**Thus, the expression is: 0.45 + 1.25 - 4, which is -2.3.**

***/**

`double e = a / b + c / d - d % c;`

### Example Question #1 : Double

What is the size of the `float`

data type in Java?

**Possible Answers:**

1 byte (8 bits)

4 bytes (32 bits)

6 bytes (48 bits)

2 bytes (16 bits)

8 bytes (64 bits)

**Correct answer:**

4 bytes (32 bits)

A `float`

is represented by 4 bytes (32 bits). It's comprised of 1 sign bit, 8 exponent bits, and 23 mantissa bits. An example of a floating point number in binary would be `11111111111111111111111111111111`

. The breakdown would be `[1](sign bit) [11111111](exponent bits) [11111111111111111111111](mantissa bits)`

. All in all, that equals 32 bits.

In Java, the `byte`

is the only 8 bit data type. The `short`

and `char`

are 16 bits in size. There are no 48 bit data types, as everything is in a power of 2. The `double`

and `long`

are 64 bits in size.

Because `floats`

have half the number of bits as `doubles`

, it isn't as precise, so shouldn't be used when lots of precision is needed. It's better suited for when there are memory size concerns.

### Example Question #75 : Computer Science

Breanna wants to keep track of how many grapes she eats in a day over the course of a week.

Her brother Nathan, however, wants to keep track of the average amount of grapes he eats in a day over the course of a week.

What type of variable would best be fit for what Breanna wants to keep track of? What about Nathan?

**Possible Answers:**

Breanna should use double

Nathan should use double

Breanna should use int

Nathan should use int

Breanna should use int

Nathan should use double

Breanna should use double

Nathan should use int

**Correct answer:**

Breanna should use int

Nathan should use double

Breanna should use int because she would be counting whole numbers.

Nathan however, should use double because he will be adding up how many grapes he eats and divide by the number of days in the week.

### All AP Computer Science A Resources

### Incompatible Browser

Please upgrade or download one of the following browsers to use Instant Tutoring: