Card 0 of 616
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Compare your answer with the correct one above
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Compare your answer with the correct one above
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Compare your answer with the correct one above
Why is the "default" keyword used in a switch statement?
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
Compare your answer with the correct one above
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
Compare your answer with the correct one above
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
Compare your answer with the correct one above
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Compare your answer with the correct one above
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
Remember that if a variable is declared as an integer, it can't have any decimals.
int c=(x*y) + z;
int c=(2*2.1)+3.0
From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.
int c=4.2+3
int c= 4+3
int c=7
Any leftover fractions are cut off, so the answer is shortened to just 7.
Compare your answer with the correct one above
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Compare your answer with the correct one above
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Compare your answer with the correct one above
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Compare your answer with the correct one above
Why is the "default" keyword used in a switch statement?
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
Compare your answer with the correct one above
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
Compare your answer with the correct one above
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
Compare your answer with the correct one above
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Compare your answer with the correct one above
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
Remember that if a variable is declared as an integer, it can't have any decimals.
int c=(x*y) + z;
int c=(2*2.1)+3.0
From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.
int c=4.2+3
int c= 4+3
int c=7
Any leftover fractions are cut off, so the answer is shortened to just 7.
Compare your answer with the correct one above
Which of the following blocks of code will output an evenly-spaced 2D array (i.e. an evenly-spaced matrix)? For example, a simple evenly-spaced matrix is:
1 2 3
4 5 6
7 8 9
There are several critical points to look at in the code given as possible answers above.
First, you need to make sure that the main output is not a new line. Thus, the following is incorrect:
System.out.println(matrix\[i\]\[j\]);
This would create a new line for every single value!
Next, you need to make sure that a new line is output after every row of the matrix. This is the final output statement. Thus, it cannot be:
System.out.print(" ");
This would only output a character with no new line.
Finally, for everything except for the first element of each row, you will need to output sufficient padding before the given element element (so as to make the matrix to be evenly-spaced). We are not going to be too particular here about the problem of very big numbers—the values are given to us as is. Note that it is insufficient to use a mere space for this. You should use a tab character (defined as '\t'). A space will not guarantee enough spacing (no pun intended)!
The correct answer has the following logic:
if(j != 0) {
System.out.print("\t\t");
}
This means "when the column is not the first column" (i.e. j != 0), "then output tabs to space sufficiently."
Compare your answer with the correct one above
Consider the following code:
for(int i = 1; i <= 10; i++) {
for(int j = 0; j < (20 - i * 2) / 2; j++) {
System.out.print(" ");
}
for(int j = 0; j < i * 2; j++) {
System.out.print("*");
}
for(int j = 0; j < (20 - i * 2) / 2; j++) {
System.out.print(" ");
}
System.out.println();
}
Describe the output of the code above.
This code is best explained by analyzing it directly. Comments will be provided in bold below:
// The loop runs from 1 to 10
for(int i = 1; i <= 10; i++) {
// First, you run through 0 to some number
// That number is computed by taking the current value of i, doubling it,
// subtracting that from 20, then dividing by 2.
// So, consider the following values for i:
// 0: 20 - 0 = 20; Divided by 2: 10
// 1: 20 - 2 = 18; Divided by 2: 9
// 2: 20 - 2 = 16; Divided by 2: 8
// etc...
// Thus, you will output single spaces that number of times (10, 9, 8...)
for(int j = 0; j < (20 - i * 2) / 2; j++) {
System.out.print(" ");
}
// Next, you will output an asterix i * 2 number of times. Thus, your
// smallest value is 2 and your largest 20.
for(int j = 0; j < i * 2; j++) {
System.out.print("*");
}
// This is just a repeat of the same loop from above
for(int j = 0; j < (20 - i * 2) / 2; j++) {
System.out.print(" ");
}
// This bumps us down to the next line
System.out.println();
}
Thus, you have output that basically has even padding on the left and right, with an increasing number of asterix characters (2, 4, 6, etc). This is an upward-facing arrow, having a point of width 2 and a base of width 20.
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("") splits the String at every character. The "for" loop concatenates the elements of the String array together, separated by a comma.
Compare your answer with the correct one above
public static void main(String[] args) {
int[][] x = {{4,5,6,7},{91,15,14,13}};
int[][] y = {{-13,4,41,14},{14,5,13,3}};
int[][] z = doWork(x,y);
for(int i = 0; i < z.length; i++) {
for(int j = 0; j < z[0].length;j++) {
System.out.print(z[i][j] + " ");
}
System.out.println();
}
}
``
public static int[][] doWork(int[][] a, int[][] b) {
int[][] ret = new int[a.length][a[0].length];
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
ret[i][j] = a[i][j] + b[i][j];
}
}
return ret;
}
What is the console output for the code above?
The doWork
method implements a relatively standard type of 2D array iteration, one that goes through each element. The outer loop goes through the first dimension of the array. Then, the inner loop provides index for the second dimension. From this, you get the total 2D index [i][j]
. The line ret[i][j] = a[i][j] + b[i][j];
actually performs the operation at the given index. Here, it performs an addition, combining the values at [i][j]
found in the two arrays. This gives you the sum at each index of ret
. The main method outputs each of these values, following the same standard algorithm for 2D array traversal.
Compare your answer with the correct one above