Card 0 of 16
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?
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
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?
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
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?
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
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?
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
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?
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
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?
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
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?
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
What does the expression (7 != 14) evaluate to?
The "!" symbol means not. Therefore, 7 does not equal 14 is true.
Compare your answer with the correct one above
boolean x;
int y;
x = false;
y = 10;
x = !(y < 10 || y > 10);
System.out.println ( x );
What is printed?
The expression !(y < 10 || y > 10) evaluates to (y >= 10 && y <= 10).
Since y = 10, the expression evaluates to true.
Compare your answer with the correct one above
What does this statement return?
int a = 2
int b = 3
a == b
a and b are both primitive types of integer. a is equal to 2 and b is equal to 3. 2 does not equal 3, so the answer is false.
Compare your answer with the correct one above
Which is correct?
String a = "2"
String b = "2"
Is a == b correct? Or is a.equals(b) correct?
Since both objects are not primitive types, double equals, ==, would return whether the pointers of the type objects are equivalent. The string object requires the string equals method which is String.eq("string")
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
Which of the following Boolean expression evalutes to True?
I. False && False && False && True && False
II. (False && False) || (True || False) || (False && (True || False))
III. (True && False) && (True || False || (True && True))
IV. !(True && True && False)
V. False || False || False || True || False
Answer: II, IV, V
The best way to approach this problem is to divide each conditional into chunks. Then, if there is at least one True in a long list of OR statements, the statement must be true according to Boolean Logic. Likewise, if there is at least one False in a long list of AND's, the statment must be False. For example
False && False && False && True evalulates to False
and
True || False || False ... evaluetes to True
Statement I evalutes to False because False and any number of And's must evaluate to false because False && X evalutes to False, regardless of the value of X.
Statement II evalutes to True because there is one True statement in a list of OR's that make the entire statement True.
Statement III evalutes to False for the same reason Statement I does.
Statement IV is true because although the conditionals in the parentheses are False, the ! (not) operator negates the False to True.
Statement V is true.
Compare your answer with the correct one above
What does the following code print?
int i = 3;
int j = 4;
int k = 5;
int l = i * k - j;
int m = i*2 + j + k;
if ( ( l > m ) || ( l * i ) = (m+m+i) )
System.out.println("The statement proves true");
else
System.out.println("The statement proves false");
In the second condition, it is not a statement of equivalence but a statement of assignment. This would cause it to have a runtime error. If it was to be a == instead of =, the statement would be true.
Compare your answer with the correct one above