Card 0 of 935
In Swift (iOS), define an unwrapped boolean.
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
Which is equivalent?
int a = 5
int b = 10
int c = 25
int d = 15
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
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
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
True or False.
This code snippet returns an error.
String s1 = "foo";
String s2 = "foo";
return s1 == s2;
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
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
Compare your answer with the correct one above
int foo[] = {1, 2, 3, 4, 5};
number = 100 + foo[4];
What is the value of number
?
The answer is 105 because arrays are zero indexed. This means that the first position has the subscript 0, the second subscript 1, and so on. 5 is in the fifth space, located at subscript 4. We would access it by saying foo\[4\]. As another example, if we wanted to access one, we would say foo\[0\].
Compare your answer with the correct one above
Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?
You create a matrix also known as a 2 dimensional array the same way you'd instantiate a normal array except the first array space remains blank and you'd insert the number for the amount of rows. Due to the fact that you want 5 columns and 4 rows, you'd only input the 4 into the second array.
Compare your answer with the correct one above
Write a program that iterates through this data structure and prints the data (choose the best answer):
List<List<String>> listOflistOfStrings = new ArrayList<ArrayList<String>>();
The correct answer uses a ForEach loop. A ForEach loop is recommended for iterating through Lists because Lists contain iterators. ForEach loops use the iterator to iterate through the List. One of the answers used a regular For loop, while the answer was correct, it was not the best choice.
Compare your answer with the correct one above
Which of the following blocks of code converts an array of characters into a string?
The easiest way to consider this is by commenting on the correct answer. You must begin by defining the character array:
char\[\] vals = {'A','t', ' ', '6',' ','a','m','!'};
Next, you must initialize the string value s to be an empty string. This is critical. Otherwise, you can't build your string!
String s = "";
Next, you have the loop. This goes through the characters and concatenates the values to the variable s. The operation to concatenate the characters is the "+=". This will give you the string value of the array of characters.
Compare your answer with the correct one above
String greet = "Hello ";
String sub;
int len = greet.length();
sub = greet.substring(0, (len/2));
System.out.println (sub);
What is printed?
The length of greet is 6 characters including the space at the end.
greet.substring(0, (len/2)) is equal to greet.substring(0, 3)
The substring of greet from the zeroth position to second position, not to the third position.
Compare your answer with the correct one above
In Swift (iOS), define an unwrapped boolean.
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
How do we set a method to return a Boolean 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 boolean. So we set the return type using -> Bool.
Compare your answer with the correct one above
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?
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
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?
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
string str = "Hello"
char newvar = str\[4\];
What is the value of newvar?
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
What is the output of the following code?
System.out.println( (1+2) + (3-1) + new Integer(5));
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
What is the output of the following code?
System.out.println((2*2) + " foo " + (3-1) + new Integer(5) + "3-2");
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
Consider the following code:
What is the output of the method call mystery("Green eggs and ham")?
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
What is the best way to print out a String variable into a sentence in Swift (iOS)?
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
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
Compare your answer with the correct one above