three Flashcards
What is output by the following code? (Choose all that apply) 1: public class Fish { 2: public static void main(String[] args) { 3: int numFish = 4; 4: String fishType = "tuna"; 5: String anotherFish = numFish + 1; 6: System.out.println(anotherFish + " " + fishType); 7: System.out.println(numFish + " " + 1); 8: } } A. 4 1 B. 41 C. 5 D. 5 tuna E. 5tuna F. 51tuna G. The code does not compile.
G. Line 5 does not compile. This question is checking to see if you are paying attention
to the types. numFish is an int and 1 is an int. Therefore, we use numeric addition and
get 5. The problem is that we can’t store an int in a String variable. Supposing line 5
said String anotherFish = numFish + 1 + “”;. In that case, the answer would be
options A and D. The variable defined on line 5 would be the string “5”, and both out-put statements would use concatenation.
Which of the following are output by this code? (Choose all that apply)
3: String s = “Hello”;
4: String t = new String(s);
5: if (“Hello”.equals(s)) System.out.println(“one”);
6: if (t == s) System.out.println(“two”);
7: if (t.equals(s)) System.out.println(“three”);
8: if (“Hello” == s) System.out.println(“four”);
9: if (“Hello” == t) System.out.println(“five”);
A. one
B. two
C. three
D. four
E. five
F. The code does not compile.
A, C, D. The code compiles fine. Line 3 points to the String in the string pool. Line 4
calls the String constructor explicitly and is therefore a different object than s. Lines 5
and 7 check for object equality, which is true, and so print one and three. Line 6 uses
object reference equality, which is not true since we have different objects. Line 7 also
compares references but is true since both references point to the object from the string
pool. Finally, line 8 compares one object from the string pool with one that was explic-itly constructed and returns false.
Which are true statements? (Choose all that apply)
A. An immutable object can be modified.
B. An immutable object cannot be modified.
C. An immutable object can be garbage collected.
D. An immutable object cannot be garbage collected.
E. String is immutable.
F. StringBuffer is immutable.
G. StringBuilder is immutable.
B, C, E. Immutable means the state of an object cannot change once it is created.
Immutable objects can be garbage collected just like mutable objects. String is immu-table. StringBuilder can be mutated with methods like append(). Although
StringBuffer isn’t on the exam, you should know about it anyway in case older ques-tions haven’t been removed.
What is the result of the following code?
7: StringBuilder sb = new StringBuilder();
8: sb.append(“aaa”).insert(1, “bb”).insert(4, “ccc”);
9: System.out.println(sb);
A. abbaaccc
B. abbaccca
C. bbaaaccc
D. bbaaccca
E. An exception is thrown.
F. The code does not compile.
B. This example uses method chaining. After the call to append(), sb contains “aaa”.
That result is passed to the first insert() call, which inserts at index 1. At this point
sb contains abbbaa. That result is passed to the final insert(), which inserts at index
4, resulting in abbaccca.
What is the result of the following code? 2: String s1 = "java"; 3: StringBuilder s2 = new StringBuilder("java"); 4: if (s1 == s2) 5: System.out.print("1"); 6: if (s1.equals(s2)) 7: System.out.print("2"); A. 1 B. 2 C. 12 D. No output is printed. E. An exception is thrown. F. The code does not compile.
F. The question is trying to distract you into paying attention to logical equality versus
object reference equality. It is hoping you will miss the fact that line 4 does not com-pile. Java does not allow you to compare String and StringBuilder using ==.
What is the result of the following code? public class Lion { public void roar(String roar1, StringBuilder roar2) { roar1.concat("!!!"); roar2.append("!!!"); } public static void main(String[] args) { String roar1 = "roar"; StringBuilder roar2 = new StringBuilder("roar"); new Lion().roar(roar1, roar2); System.out.println(roar1 + " " + roar2); } } A. roar roar B. roar roar!!! C. roar!!! roar D. roar!!! roar!!! E. An exception is thrown. F. The code does not compile.
B. A String is immutable. Calling concat() returns a new String but does not change
the original. A StringBuilder is mutable. Calling append() adds characters to the
existing character sequence along with returning a reference to the same object.
Which are the results of the following code? (Choose all that apply) String letters = "abcdef"; System.out.println(letters.length()); System.out.println(letters.charAt(3)); System.out.println(letters.charAt(6)); A. 5 B. 6 C. c D. d E. An exception is thrown. F. The code does not compile.
B, D, E. length() is simply a count of the number of characters in a String. In this
case, there are six characters. charAt() returns the character at that index. Remember
that indexes are zero based, which means that index 3 corresponds to d and index 6
corresponds to 1 past the end of the array. A StringIndexOutOfBoundsException is
thrown for the last line.
Which are the results of the following code? (Choose all that apply)
String numbers = “012345678”;
System.out.println(numbers.substring(1, 3));
System.out.println(numbers.substring(7, 7));
System.out.println(numbers.substring(7));
A. 12
B. 123
C. 7
D. 78
E. A blank line.
F. An exception is thrown.
G. The code does not compile.
A, D, E. substring() has two forms. The first takes the index to start with and the
index to stop immediately before. The second takes just the index to start with and
goes to the end of the String. Remember that indexes are zero based. The first call
starts at index 1 and ends with index 2 since it needs to stop before index 3. The sec-ond call starts at index 7 and ends in the same place, resulting in an empty String.
This prints out a blank line. The final call starts at index 7 and goes to the end of the
String.
What is the result of the following code? 3: String s = "purr"; 4: s.toUpperCase(); 5: s.trim(); 6: s.substring(1, 3); 7: s += " two"; 8: System.out.println(s.length()); A. 2 B. 4 C. 8 D. 10 E. An exception is thrown. F. The code does not compile.
C. This question is trying to see if you know that String objects are immutable. Line
4 returns “PURR” but the result is ignored and not stored in s. Line 5 returns “purr”
since there is no whitespace present but the result is again ignored. Line 6 returns “ur”
because it starts with index 1 and ends before index 3 using zero-based indexes. The
result is ignored again. Finally, on line 6 something happens. We concatenate four new
characters to s and now have a String of length 8.
What is the result of the following code? (Choose all that apply) 13: String a = ""; 14: a += 2; 15: a += 'c'; 16: a += false; 17: if ( a == "2cfalse") System.out.println("=="); 18: if ( a.equals("2cfalse")) System.out.println("equals"); A. Compile error on line 14. B. Compile error on line 15. C. Compile error on line 16. D. Compile error on another line. E. == F. equals G. An exception is thrown.
F. a += 2 expands to a = a + 2. A String concatenated with any other type gives
a String. Lines 14, 15, and 16 all append to a, giving a result of “2cfalse”. The if
statement on line 18 returns true because the values of the two String objects are the
same using object equality. The if statement on line 17 returns false because the two
String objects are not the same in memory. One comes directly from the string pool
and the other comes from building using String operations.
What is the result of the following code?
4: int total = 0;
5: StringBuilder letters = new StringBuilder(“abcdefg”);
6: total += letters.substring(1, 2).length();
7: total += letters.substring(6, 6).length();
8: total += letters.substring(6, 5).length();
9: System.out.println(total);
A. 1
B. 2
C. 3
D. 7
E. An exception is thrown.
F. The code does not compile.
E. Line 6 adds 1 to total because substring() includes the starting index but not
the ending index. Line 7 adds 0 to total. Line 8 is a problem: Java does not allow the
indexes to be specified in reverse order and the code throws a StringIndexOutOf-BoundsException.
What is the result of the following code? (Choose all that apply) StringBuilder numbers = new StringBuilder("0123456789"); numbers.delete(2, 8); numbers.append("-").insert(2, "+"); System.out.println(numbers); A. 01+89– B. 012+9– C. 012+–9 D. 0123456789 E. An exception is thrown. F. The code does not compile.
A. First, we delete the characters at index 2 until the character one before index 8. At
this point, 0189 is in numbers. The following line uses method chaining. It appends a
dash to the end of the characters sequence, resulting in 0189–, and then inserts a plus
sign at index 2, resulting in 01+89–.
What is the result of the following code? StringBuilder b = "rumble"; b.append(4).deleteCharAt(3).delete(3, b.length() - 1); System.out.println(b); A. rum B. rum4 C. rumb4 D. rumble4 E. An exception is thrown. F. The code does not compile.
F. This is a trick question. The first line does not compile because you cannot assign a String to a StringBuilder. If that line were StringBuilder b = new StringBuilder("rumble"), the code would compile and print rum4. Watch out for this sort of trick on the exam. You could easily spend a minute working out the character positions for no reason at all.
Which of the following can replace line 4 to print “avaJ”? (Choose all that apply)
3: StringBuilder puzzle = new StringBuilder(“Java”);
4: // INSERT CODE HERE
5: System.out.println(puzzle);
A. puzzle.reverse();
B. puzzle.append(“vaJ$”).substring(0, 4);
C. puzzle.append(“vaJ$”).delete(0, 3).deleteCharAt(puzzle.length() - 1);
D. puzzle.append(“vaJ$”).delete(0, 3).deleteCharAt(puzzle.length());
E. None of the above.
A, C. The reverse() method is the easiest way of reversing the characters in a String-Builder; therefore, option A is correct. Option B is a nice distraction—it does in fact
return “avaJ”. However, substring() returns a String, which is not stored anywhere. BULLSHIT!!!
Option C uses method chaining. First it creates the value “JavavaJ$”. Then it removes
the first three characters, resulting in “avaJ$”. Finally, it removes the last character,
resulting in “avaJ”. Option D throws an exception because you cannot delete the char-acter after the last index. Remember that deleteCharAt() uses indexes that are zero
based and length() counts starting with 1.
Which of these array declarations is not legal? (Choose all that apply)
A. int[][] scores = new int[5][];
B. Object[][][] cubbies = new Object[3][0][5];
C. String beans[] = new beans[6];
D. java.util.Date[] dates[] = new java.util.Date[2][];
E. int[][] types = new int[];
F. int[][] java = new int[][];
C, E, F. Option C uses the variable name as if it were a type, which is clearly illegal.
Options E and F don’t specify any size. Although it is legal to leave out the size for later
dimensions of a multidimensional array, the first one is required. Option A declares a
legal 2D array. Option B declares a legal 3D array. Option D declares a legal 2D array.
Remember that it is normal to see on the exam types you might not have learned. You
aren’t expected to know anything about them.