Primitive Data Types - AP Computer Science A
Card 0 of 627
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?
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?
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;
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;
Compare your answer with the correct one above
What line number makes the following Python Code not execute? (1:, 2:, 3:,... represents line numbers)
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is "+z\[3\]
10: print(string)
What line number makes the following Python Code not execute? (1:, 2:, 3:,... represents line numbers)
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is "+z\[3\]
10: print(string)
When the code is executed it will produce a type error with the variable string in line 9. The variable string is trying to concatenate a string and an integer from the z array. To make the code executable, we would need to make the integer from the z array a string. We would do this by simply wrapping it in the method str(), which converts it into a string. After making the change, the following code will execute correctly.
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is"+str(z\[3\])
10: print(string)
When the code is executed it will produce a type error with the variable string in line 9. The variable string is trying to concatenate a string and an integer from the z array. To make the code executable, we would need to make the integer from the z array a string. We would do this by simply wrapping it in the method str(), which converts it into a string. After making the change, the following code will execute correctly.
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is"+str(z\[3\])
10: print(string)
Compare your answer with the correct one above
What is the size of the float
data type in Java?
What is the size of the float
data type in Java?
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.
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.
Compare your answer with the correct one above
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?
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?
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.
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.
Compare your answer with the correct one above
How do we declare a method to return a Double in Swift (iOS)?
How do we declare a method to return a Double in Swift (iOS)?
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 a Double. So we set the return type using -> Double.
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 a Double. So we set the return type using -> Double.
Compare your answer with the correct one above
int a = 49;
int b = 6;
``
int c = 49 % 6;
What is the value of c
?
int a = 49;
int b = 6;
``
int c = 49 % 6;
What is the value of c
?
The modulus (or modulo) operator returns the remainder of a division statement. It only works with integer operands. Thus, it will only return an integer result. Six is able to divide forty-eight a maximum of eight times.

And then

Thus,

The modulus (or modulo) operator returns the remainder of a division statement. It only works with integer operands. Thus, it will only return an integer result. Six is able to divide forty-eight a maximum of eight times.
And then
Thus,
Compare your answer with the correct one above
int a = 5;
a += 3.14;
Using the language C++ or Java, what will the value of a
be after the above code snippet?
int a = 5;
a += 3.14;
Using the language C++ or Java, what will the value of a
be after the above code snippet?
When using the plus-equals (+=) operator, the answer is cast to the type of the left-hand argument. Since a
was an int
, the value returned by the expression is converted to int
before it is stored in a
. Thus, the decimal point and any information behind the decimal is truncated.
When using the plus-equals (+=) operator, the answer is cast to the type of the left-hand argument. Since a
was an int
, the value returned by the expression is converted to int
before it is stored in a
. Thus, the decimal point and any information behind the decimal is truncated.
Compare your answer with the correct one above
Consider the following code:
int i = 85, j = 2, k = 4;
i--;
j++;
k -= 6;
i = i / j + i / k;
What is the value of i in the code above?
Consider the following code:
int i = 85, j = 2, k = 4;
i--;
j++;
k -= 6;
i = i / j + i / k;
What is the value of i in the code above?
It is probably easiest to give a running commentary on the code in order to understand it. See the comments in bold below:
int i = 85, j = 2, k = 4;
i--;
// After this, i = 84
j++;
// After this, j = 3
k -= 6;
// After this, k = k - 6 or k = -2
/*
Consider this in several steps:
i / j = 84 / 3 = 28
i / k = 84 / -2 = -42
Thus, the final expression is:
i = 28 - 42 = -14
*/
i = i / j + i / k;
It is probably easiest to give a running commentary on the code in order to understand it. See the comments in bold below:
int i = 85, j = 2, k = 4;
i--;
// After this, i = 84
j++;
// After this, j = 3
k -= 6;
// After this, k = k - 6 or k = -2
/*
Consider this in several steps:
i / j = 84 / 3 = 28
i / k = 84 / -2 = -42
Thus, the final expression is:
i = 28 - 42 = -14
*/
i = i / j + i / k;
Compare your answer with the correct one above
Consider the code below:
int a = 24, b = 17, c = 5, d = 7;
b = b % d + a / b;
a %= b;
a -= 12;
What is the value of a at the end of the code above?
Consider the code below:
int a = 24, b = 17, c = 5, d = 7;
b = b % d + a / b;
a %= b;
a -= 12;
What is the value of a at the end of the code above?
The easiest way to trace code like this is to provide running commentary in bold comments. See these below:
int a = 24, b = 17, c = 5, d = 7;
/*
The modulus (%) is the remainder of a division. So . . .
b % d = 17 % 7 = 3 (17 divided by 7 is 2 remainder 3.)
a / b = 1 (Remember, integer division truncates the decimal portion.)
Thus, we have:
b = 3 + 1 = 4
*/
b = b % d + a / b;
a %= b;
// This is the same as a = a % d, which is a = 24 % 4 = 0 (No remainder)
a -= 12;
// Remember that this is the same as a = a - 12, which is a = 0-12 or -12.
The easiest way to trace code like this is to provide running commentary in bold comments. See these below:
int a = 24, b = 17, c = 5, d = 7;
/*
The modulus (%) is the remainder of a division. So . . .
b % d = 17 % 7 = 3 (17 divided by 7 is 2 remainder 3.)
a / b = 1 (Remember, integer division truncates the decimal portion.)
Thus, we have:
b = 3 + 1 = 4
*/
b = b % d + a / b;
a %= b;
// This is the same as a = a % d, which is a = 24 % 4 = 0 (No remainder)
a -= 12;
// Remember that this is the same as a = a - 12, which is a = 0-12 or -12.
Compare your answer with the correct one above
What is the Unicode value for the letter "W"?
What is the Unicode value for the letter "W"?
The Unicode Standard is Java's attempt to simplify the translation of characters and symbols to input the computer can understand, while being able to work on all Java machines. The first 128 numbers (0-127, 0x0000-0x007F) are for basic Latin. Under this character encoding scheme, the letter 'W' is denoted by U+0057.
The standard for writing Unicode values is U+####, where the number is in hexadecimal format, not a base 10 number.
The Unicode Standard is Java's attempt to simplify the translation of characters and symbols to input the computer can understand, while being able to work on all Java machines. The first 128 numbers (0-127, 0x0000-0x007F) are for basic Latin. Under this character encoding scheme, the letter 'W' is denoted by U+0057.
The standard for writing Unicode values is U+####, where the number is in hexadecimal format, not a base 10 number.
Compare your answer with the correct one above
#include
using namespace std;
int main()
{
bool bin=true;
int var=bin*2 +1;
int triple=5;
int final= (bin&var) &triple;
cout<<final;
return 0;
}
What is the output of this code?
#include
using namespace std;
int main()
{
bool bin=true;
int var=bin*2 +1;
int triple=5;
int final= (bin&var) &triple;
cout<<final;
return 0;
}
What is the output of this code?
Taking a look at the first line in main, the variable bin is defined as true, which we know to be 1. We see in the next line that the variable var is 2 * bin +1. The trick here is that you can actually do math with boolean values, so the value of var is 3.
The value of triple is 5.
The the variable final asks us to AND bin and var first, then AND triple.
To do this, we can convert all three numbers into binary notation to make it easier.
1 in binary is 001.
3 in binary is 011.
5 in binary is 101.
1&3 =1.
1&5=1.
Final answer is 1.
Taking a look at the first line in main, the variable bin is defined as true, which we know to be 1. We see in the next line that the variable var is 2 * bin +1. The trick here is that you can actually do math with boolean values, so the value of var is 3.
The value of triple is 5.
The the variable final asks us to AND bin and var first, then AND triple.
To do this, we can convert all three numbers into binary notation to make it easier.
1 in binary is 001.
3 in binary is 011.
5 in binary is 101.
1&3 =1.
1&5=1.
Final answer is 1.
Compare your answer with the correct one above
int a;
a= 1+1 * 2 + 4 / 5;
System.out.println(a);
What would the code output?
int a;
a= 1+1 * 2 + 4 / 5;
System.out.println(a);
What would the code output?
The expression 1+1 * 2 + 4 / 5 can be rewritten as
1 + (1*2) + (4/5) = 1 + 2 + 0
4/5 = 0 because a is an int and when dividing ints, there are no decimals and since 4/5=0.8 we keep the ones digit which is zero.
The expression 1+1 * 2 + 4 / 5 can be rewritten as
1 + (1*2) + (4/5) = 1 + 2 + 0
4/5 = 0 because a is an int and when dividing ints, there are no decimals and since 4/5=0.8 we keep the ones digit which is zero.
Compare your answer with the correct one above
int x = 2893;
- What does the expression " x / 10 " evaluate to?
- What does the expression " x % 10 " evaluate to?
int x = 2893;
- What does the expression " x / 10 " evaluate to?
- What does the expression " x % 10 " evaluate to?
- 2893 / 10 = 289, since x is an int, it does not store the decimal value
- 2893 % 10 = 3 because the remainder of 2893/10 is 3
- 2893 / 10 = 289, since x is an int, it does not store the decimal value
- 2893 % 10 = 3 because the remainder of 2893/10 is 3
Compare your answer with the correct one above
Evaluate the mathematical equation?

Evaluate the mathematical equation?
You have to remember PEMDAS.

Multiplication and Division take precedence first
(double multiplied by an int equal a double)

Addition and Subtraction after

You have to remember PEMDAS.
Multiplication and Division take precedence first
(double multiplied by an int equal a double)
Addition and Subtraction after
Compare your answer with the correct one above
int x = 10;
int y = 4;
``
int z = x / y;
What is the value of z
?
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:
- 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 declaring z
as a float.
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.
Compare your answer with the correct one above
Consider the following code:
int i = 55, j = 3;
System.out.println((i / j + 3) / 5);
What is the output for the code above?
Consider the following code:
int i = 55, j = 3;
System.out.println((i / j + 3) / 5);
What is the output for the code above?
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.
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
Which of the following gurantees that your division does not lose its decimal portion?
Which of the following gurantees that your division does not lose its decimal portion?
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...
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
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?
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?
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
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
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?
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?
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:





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
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?
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?
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.
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