Primitive Data Types - Computer Science

Card 0 of 20

Question

Consider the code below:

int[] vals = {841,-14,1,41,49,149,14,148,14};

boolean[] bools = new boolean[vals.length];

for(int i = 0; i < vals.length; i++) {

bools[i] = vals[i] > 20 && vals[i] % 2 == 0;

}

for(int i = 0; i < bools.length; i++) {

if(bools[i]) {

System.out.println(vals[i]);

}

}

What is the output of the code above?

Answer

This code uses a pair of parallel arrays, one being a boolean containing the result of the logic

vals[i] > 20 && vals[i] % 2 == 0

as applied to each element in the vals array.

The logic will evaluate to true when a given value is greater than 20 and also is even. (Remember that the % is the modulus operator, giving you the remainder of the division by 2 in this case. When this is 0, the division is an even division without remainder. When the divisor is 2, this means the number is divisible by 2—it is even.)

Now, in the second loop, it merely prints the values for which this was true. (This isn't the most efficient algorithm in the world. It is merely trying to test your ability to use parallel arrays and boolean values!) There is only one number in the entire list that fits: 148. Notice, however, that it does not output the boolean values. Those answers are traps that are trying to see if you are not paying attention.

Compare your answer with the correct one above

Question

int j=6;

int k=0;

int l=2;

int c = (j|k) & l;

What is the value of c?

Answer

The parenthesis indicate which operations need to be completed first. J or'd with k gives an answer of 6. Remember that these boolean operations are done by using the binary representtion of the numbers. 6 in binary is 110 and 0 in binary is 000. 6 anded with 2 is 2.

Compare your answer with the correct one above

Question

In the following equation, considering the boolean variables x, y, and z, which of the following combinations of values will evaluate to true?

(x\,\&\&\,!y)\,||\,((!x\,||\,z)\,\&\&\,!(y\,||\,z))

Answer

When evaluated with x == true, y == false, z == false_,_ the equation comes out to be

All other combinations of values produce false.

Compare your answer with the correct one above

Question

#include

using namespace std;
bool bigger(int x, int y)
{
if (x>y)
return true;
else
return false;
}
int main()
{

bool test;
test!=bigger(10,7);
cout<<test;
}

What will be the value of test after the previous code is run?

Answer

The function "bigger" defined before main returns a value of type bool and takes in two integer inputs. It compares the first value to the second. If it it greater, it returns true. Otherwise, it returns false. In main, a new variable test is called, and it is of type bool as well. This automatically eliminates 3 out of the 5 possibilities.

"test" is defined as the opposite of the outcome of the bigger function with the two inputs are 10 and 7. 10 is bigger than 7, so the function returns true. Since test is the opposite of that, it is false.

Compare your answer with the correct one above

Question

Convert the decimal number 67 to binary.

Answer

In a regular number system, also known as decimal, the position furtherest to the right is , then one over to the left would be , then . These are the ones, tens and hundreds place. Binary is a base 2 number system. Therefore, the digit to the furthest right is , then to the left , then , and so on.

Explanation

1000011 The bolded number has a value of 1

1000011 The bolded number has a value of 2

1000011 The bolded number has a value of 64

The positions that are marked true (as 1) in the binary number 1000011 corresponds to the numbers 64, 2, and 1 which added up equals 67

Step By Step

  • To convert the number 67 to binary, first find the digit place number that has the largest number possible that is less than or equal to 67. In this case it would be the position holding .
  • The number for 64 is 1000000
  • To get 67, we need to add 3
  • Again, find the digit place number that has the largest number possible less than or equal to 3. In this case, it would be
  • The number for 66 (64 + 2) is 1000010
  • To get from 66 to 67, repeat the steps from before.
  • The number for 67 (66 + 1) is 1000011

Compare your answer with the correct one above

Question

In Swift (iOS), define an unwrapped boolean.

Answer

In Swift, every variable must be declared with var before the name of the variable. This automatically knocks out two of the answers. After we eliminate those two, we see var variable and var variable: Bool. var variable would be a correct answer if the prompt had not asked for an unwrapped boolean. var variable: Bool performs the unwrapping by declaring the variable to be a boolean specifically.

Compare your answer with the correct one above

Question

How do we set a method to return a Boolean 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 a boolean. So we set the return type using -> Bool.

Compare your answer with the correct one above

Question

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?

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;

Compare your answer with the correct one above

Question

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)

Answer

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

Question

What is the size of the float data type in Java?

Answer

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

Question

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?

Answer

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

Question

How do we declare a method to return a Double 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 a Double. So we set the return type using -> Double.

Compare your answer with the correct one above

Question

int currentYear = 2016;

bool leapYear;

``

if (currentYear % 4 == 0) {

leapYear = true;

} else {

leapYear = false;

}

``

febDays = leapYear ? 28 : 29

Based on the code above, what value will febDays have?

Answer

The ternary operator works as follows:

(condition to be checked) ? (value to return if true) : (value to return if false)

The answer is purposely logically incorrect to force you to consider the structure of the code. While 2016 is obviously a leap year where February should have 29 days, the ternary operator used is set to return 28 days if true and 29 if false. So in this case, febDays will be 28.

Compare your answer with the correct one above

Question

Consider the following code:

int\[\] a = {8,4,1,5,1,5,6,2,4};

int b = 1;

boolean\[\] vals = new boolean\[a.length\];

for(int i = 0; i < a.length; i++) {

vals\[i\] = (a\[i\] - 1 > 4);

}

for(int i = 0; i < vals.length; i++) {

String s = "Duns Scotus";

if(!vals\[i\]) {

s = "Mithrandir";

}

System.out.println(s);

}

What is the output for the code above?

Answer

Begin by getting a general idea of the loop logic being executed. For each execution of the first loop, you are assigning a boolean to the vals array. Now, you could rewrite the code a little to make the math easier:

a\[i\] - 1 > 4

really is the same as:

a\[i\] > 5

Now, the only values for which this is true are:

8 and 6 (the first and the 7th values in a).

Now, looking at the second loop, the value s will be "Duns Scotus" for every value that is true in vals. This is because the if statement checks for !vals\[i\]. Thus, the first and the seventh values alone will be "Duns Scotus". Everything else will be "Mithrandir".

Compare your answer with the correct one above

Question

Consider the following code:

int a = 14;

int b = -15;

int c = 22;

int d = 11;

if(a > 12 && b < -14 && c < d) {

System.out.println("YAY!!!!");

} else if (d - 20 < b) {

System.out.println("GO DO MORE PROGRAMMING!");

} else if(a + 12 >= c) {

System.out.println("This is vexing......");

} else {

System.out.println("This is very fun!");

}}

What is the output for the code above?

Answer

Consider each condition:

The first if statement has: a > 12 && b < -14 && c < d

While it is true that 14 > 12 and -15 < -14, it is not true that 11 < 22. Therefore, the conjunction of all of these cannot be true. For &&, all of the expressions must be true.

Next, consider: d - 20 < b

This is really: 11 - 20 < -15 or -9 < -15. This is not true either.

Next, consider: a + 12 >= c

This is really: 12 + 12 >= 22 or 24 >= 22. This is true. Therefore, your output is "This is vexing......"!

Compare your answer with the correct one above

Question

Consider the code below:

int i = 45, j = -12;

String s = "This is great!";

String s2 = "Where are you?";

if(s2.charAt(4) == ' ' && (i < 4 || j > -43) && !s2.equals("WOW!!!")) {

System.out.println("Happy days are here again!");

} else if (s2.charAt(5) == 's') {

System.out.println("Well, we are here at least.");

} else if (j + 12 != 0) {

System.out.println("The stock market is up today!");

} else if(i - 20 < 100) {

System.out.println("Where will we be having lunch tomorrow?");

} else {

System.out.println("But here you go!");

}

What is the output for the code above?

Answer

The easiest way to begin is by looking at the general form of the first if statement. Notice that it is made up of three expressions that have a boolean AND between them. This means that if any of those members is false, the whole thing will be false. Well, consider the very first term:

s2.charAt(4) == ' '

This is asking about the fifth character in the string s2. (Remember, the index starts at 0.) Well, this is true for s but not for s2, which has 'e' for its fifth member. Thus, we know that the first if must be false.

Now, to the second:

s2.charAt(5) == 's'

This also is not true, for the sixth character in s2 is ' '.

The third if is also false. j + 12 is equal to 0!

However, it is indeed the case that (45 - 20 < 100) is true. Therefore, the fourth if statement will execute, giving you the output:

Where will the group be having lunch tomorrow?

Compare your answer with the correct one above

Question

Consider the code below:

for(int i = 0; i < 10; i++) {

if(i % 2 == 0) {

System.out.println("This is my favorite case!");

} else if(i + 6 > 12 && i % 3 == 1) {

System.out.println("There once was a pool in the back yard!");

} else {

System.out.println("Well, at least it is something...");

}

}

What is the output for the code above?

Answer

First of all, note that your loop will run for values from 0 to 9, inclusive.

Now, the first condition is relatively easy:

i % 2 == 0

This is true when the number i is even. When it is divisible by 2, the remainder will be 0. (Recall that the modulus gives you the remainder.)

Now, the next two else conditions will execute only for odd numbers. The first one can be rewritten:

i > 6 && i % 3 == 1

This means that we need all of the numbers from 7 to 9 that have a remainder of 1 when divided by 3. This only applies to i = 7. This will be the eighth line output. Therefore, you know that you will have alternating lines saying, "This is my favorite case!" All of the other ones will be "Well, at least it is something...", except for the eighth line, which will be "There once was a pool in the back yard!"

Compare your answer with the correct one above

Question

Consider the code below:

int i = 12, j = -5, k = 103;

if(i < 15 && j >= -4) {

System.out.println("You should take more philosophy classes!");

} else if(!(i < 100 && k > 4)) {

System.out.println("I love medieval studies!");

} else if(!(i == 3 && k == 4)) {

System.out.println("My favorite philosopher is Radulphus Brito, of course!");

} else if(i != 4 && k == 102) {

System.out.println("My favorite philosopher is Johann Fichte.");

} else {

System.out.println("I am going to cut the grass tomorrow.");

}

What is the output for the code above?

Answer

Let us consider each conditional in turn. When we reach one that is true, we know our output.

First Conditional: i < 15 && j >= -4

Recall that you must have both of these true in order for the whole expression to be true. Well, while it is the case that , it is not true that . Thus, this conditional is false.

Second Conditional: !(i < 100 && k > 4)

For this, you must first attempt the expression in parentheses. Both of these are true. However, !(true) is false. (It makes sense in English: "That is not true!" This means, "That is false!")

Third Conditional: !(i == 3 && k == 4)

Well, here, we know that both 12 == 3 and 103 == 4 are both false. Therefore, the expression is !(false). Therefore, it is true! This is your output.

Compare your answer with the correct one above

Question

Consider the code below:

char c = 'M';

String s = "James of Metz";

String s2 = "Albert of Sazony";

int i = 14;

if(i > 34 || c < 'B') {

System.out.println("I love Duns Scotus!");

} else if(s.charAt(9) == c) {

System.out.println("I read William of Ockham yesterday!");

} else if(!(i < 100 || s.equals("James of Metz"))) {

System.out.println("Well, I call William of Ockham 'Bill' because we're BFFs.");

} else if(s2.equalsIgnoreCase("Al")) {

System.out.println("I was reading Simon of Faversham, and he is very logical.");

} else {

System.out.println("There is nobody like Peter of Spain for a good logic read.");

}

What is the output for the code above?

Answer

Let's consider the cases in order until we find our matching true expression.

Conditional 1: i > 34 || c < 'B'

All we need is for one of these to be true for the whole expression to be true (given the nature of the logical OR represented by ||. However, neither of these are true.

Conditional 2: s.charAt(9) == c

If you carefully count the letters in the String s, you will see that index 9 (i.e. the tenth character) is M. Therefore, this is equal to c. Thus, you know that your program will output I read William of Ockham yesterday!

Compare your answer with the correct one above

Question

What does the expression (7 != 14) evaluate to?

Answer

The "!" symbol means not. Therefore, 7 does not equal 14 is true.

Compare your answer with the correct one above

Tap the card to reveal the answer