Primitive Data Types - AP Computer Science A

Card 0 of 627

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

Which is equivalent?

int a = 5

int b = 10

int c = 25

int d = 15

  1. (a + b) == c
  2. (a * b) == d
  3. (a + b) == d
  4. (a * c) == b

Answer

3 is the correct answer because a = 5 and b = 10 so 5 + 10 = 15 which is d.

Compare your answer with the correct one above

Question

Which is true?

a = 4

b = 2

c = 5

d = 6

a) a = c - d

b) a = b - d

c) a = d - b

d) a = c * d

Answer

c) is the correct choice because a = 6 - 2 since d = 6 and b = 2. Substitute the numbers for the variables in each answer choice. You will see that all of the answer choices are not equal to 4 except for c).

Compare your answer with the correct one above

Question

True or False.

This code snippet returns an error.

String s1 = "foo";

String s2 = "foo";

return s1 == s2;

Answer

While the code snippet doesn't return an error, it also doesn't return the answer that you want. Testing string equality requires the method String.equals(). This code snippet uses == to compare two strings. == is a pointer equality function. To get the expected answer (true), the code should return s1.equals(s2).

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

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

String s = "abcdefghijklmnop";

String s2 = "";

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

s2 += s.charAt(i+1);

s2 += s.charAt(i);

}

What is the value of s2 at the end of the code above?

Answer

String s = "abcdefghijklmnop";

String s2 = "";

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

s2 += s.charAt(i+1);

s2 += s.charAt(i);

}

The key logic of this, of course, occurs in the loop. This loop can be thought of as looking at each pair of characters (hence the use of i += 2 as your increment condition). Thus, i will be 0,2,4, . . . For 0, you will first place character 1 on s2 then character 0. Then, for 2, you will place 3 followed by 2. The pattern will continue. This is the same as saying that you will first do b, then a,next d, then c, and so forth. Therefore, for this code, you are flipping each pair of letters, thus giving you the answer badcfehgjilknmpo.

Compare your answer with the correct one above

Question

Consider the code below:

String s = "Logic!";

String s2 = "";

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

if(i != 0) {

s2 += " ";

}

s2 += s;

}

What is the value of s2 at the end of the code's execution?

Answer

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

if(i != 0) {

s2 += " ";

}

s2 += s;

}

Let us focus on the main for loop that is used in this code. Notice that the loop will run from 0 to the length of the string s. This string is 6 long. Therefore, the loop will run 6 times. The if statement adds a space to the string on every iteration except for the first. Therefore, the code copies the string 6 times, each copy being separated by a space.

Compare your answer with the correct one above

Question

string str = "Hello"

char newvar = str\[4\];

What is the value of newvar?

Answer

The line, char newvar = str\[4\] is declaring a new variable which is of the type character. Further more, when it declares it, it is saying that it is the same as the character at the 4th location in the string above.

The value 4 actually represents the 5th value, since indices start at 0.

Therefore, at str\[4\] we have the character 'o'.

Compare your answer with the correct one above

Question

What is the output of the following code?

System.out.println( (1+2) + (3-1) + new Integer(5));

Answer

When evaluating a string expression with integers, arithmatic addition is performed if the integers are not separated by string Objects. Since all of the integers and operations in this expression are not separated by String objects, arithmatic operations are performed on the integers and the final airthmatic result is printed to the console.

Compare your answer with the correct one above

Question

What is the output of the following code?

System.out.println((2*2) + " foo " + (3-1) + new Integer(5) + "3-2");

Answer

The presence of the "foo" String in the middle of the expression makes it such that arithmatic operations are performed separately on the left and right sides of the String. It causes each plus sign to perform concatenation instead of addition between terms.

Compare your answer with the correct one above

Question

Consider the following code:

Q2

What is the output of the method call mystery("Green eggs and ham")?

Answer

The method String.split() splits a String into an array of Strings separated according to the expression within the method argument. The expression String.split("\\s+") splits the String at every whitespace character. The "for" loop concatenates the elements of the String array together, separated by a comma.

Compare your answer with the correct one above

Question

What is the best way to print out a String variable into a sentence in Swift (iOS)?

Answer

The \(str) is the best way in Swift. This notation allows for a value of a variable to be input into the sentence. The "+" notation is also correct but is not the best way for Swift. The other two answers print out the words "str" and "Hello" instead of the value of the variable.

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[] 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

Tap the card to reveal the answer