Chapter 3 : Core Java APIs Flashcards
What will be the output of the following source?
String name1 = "Fluffy"; String name2 = new String("Fluffy"); String name3 = "Fluffy"; boolean x = name1 == name2; boolean y = name1 == name3; System.out.println(x + " " + y);
a) true true
b) true false
c) false false
d) false true
d) false true
boolean x = name1 == name2; is false because the new String() creates a new object
boolean y = name1 == name3; is true because the literal “Fluffy” has one single immutable object in the String pool and therefore both references x and y point to it.
What will be the output of the following source?
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);
a) 1234
b) 64
c) 334
d) 10
b) 64
When doing String concatenation using the + operator, the statement is processed in pairs left-to-right
What will be the output of the following source?
String s = “1”;
s += “2”;
s += 3;
System.out.println(s);
a) 15
b) 123
c) 1223
d) 11223
b) 123
What method will give the expected result? Choose all that apply.
String string = “animals”;
System.out.println(string.____()); // Expected 7
a) size
b) charAt
c) indexOf
d) length
d) length
What method will give the expected result? Choose all that apply.
String string = “animals”;
System.out.println(string.____); // Expected s
a) indexOf(6)
b) indexOf(7)
c) charAt(6)
d) charAt(7)
c) charAt(6)
a) and b) are wrong because indexOf() returns an int
d) will throw an exception since it is out of bounds
What method will give the expected result? Choose all that apply.
String string = “animals”;
System.out.println(string.____); // Expected 4
a) indexOf(“al”)
b) indexOf(“al”, 4)
c) contains(“al”)
d) contains(“al”, 4)
a) indexOf(“al”) & b) indexOf(“al”, 4)
b) indexOf(“al”, 4) searches for the string “al” starting at position 4. Since “al” occurs starting at 4, it returns immediately.
c) and d) are wrong because contains() returns a boolean
What method will give the expected result? Choose all that apply.
String string = “animals”;
System.out.println(string.____); // Expected mals
a) substring(3)
b) substring(string.indexOf(‘m’)));
c) substring(3,6);
d) substring(3,7);
a) , b), d)
c) substring(3,6); would result in “mal”
What will be the output of the following source?
String string = “animals”;
System.out.println(string.substring(3,2));
a) i
b) mals
c) Does not compile
d) Exception thrown
d) Exception thrown
What method will give the expected result? Choose all that apply.
String string = “animals”;
System.out.println(string.____(“animals”)); // Expected true
a) equals
b) compare
c) equalsIgnoreCase
d) compareTo
a) equals & c) equalsIgnoreCase
b) compare, is not a valid method
d) compareTo is a method that does lexicographical comparison and it returns an integer. 0 means the strings are equals.
What will be the output of the following source?
String string = “animals”;
System.out.println(string.equals(null));
a) false
b) true
c) Exception is thrown
c) Does not compile
a) false
equals does not throw an exception when parameter is null
What will be the output of the following source? String string = “animals”; System.out.println(string.compareTo(null)); a) false b) -1 c) Exception is thrown c) Does not compile
c) Exception is thrown compareTo throws an exception when parameter is null
What method will give the expected result? Choose all that apply. String string = “animals”; System.out.println(string.____)); // Expected AnimAls a) toUpperCase(“a”) b) toUpperCase(‘a’) c) replace(“a”, “A”) d) replace(‘a’, ‘A’)
c) and d) a) and b) are wrong since those methods act on the whole string and do not take a String or char parameter
What will be the output of the following source? (Note that \t is a invisible tab) String string = “\tA\tBC\t”; System.out.print(string.trim()); System.out.println(“C”); a) \tA\tBC b) A\tBC c) ABC c) Does not compile
b) A\tBC
What will be the output of the following source? 4 StringBuilder a = new StringBuilder(“abc”); 5 StringBuilder b = a.append(“de”); 6 b = b.append(“f”).append(“g”); 7 System.out.println(“a=” + a); 8 System.out.println(“b=” + b); a) a=abc , b=abcdefg b) a=abc , b=fg c) a=abc , b=g d) a=abcdefg, b=abcdefg
d) a=abcdefg, b=abcdefg Both a and b are pointing to the same object. append() returns a reference to the object itself.
How many strings are created? StringBuilder alpha = “”; for (char current=’a’; current <= ‘z’; current++) { alpha.append(current); } System.out.println(alpha); a) 1 b) 27 c) Does not compile d) Exception thrown
c) StringBuilder cannot be assigned a String literal. The following would work: StringBuilder alpha = new StringBuilder();
What will be the output of the following source? StringBuilder alpha = new StringBuilder(10); alpha.append(12345); System.out.println(alpha.length()); a) 5 b) 10 c) Does not compile d) Exception thrown
a) The constructor takes in 10 which means the object can hold up to 10 characters. However capacity != length
What will be the output of the following source? StringBuilder alpha = new StringBuilder().append(1).append(‘c’); alpha.append(“-“).append(true); System.out.println(alpha); a) 1c b) 1c-true c) Does not compile d) Exception thrown
b) 1c-true
What is the difference between StringBuilder and StringBuffer?
They do the same thing except StringBuffer is thread safe.
What will be the output of the following source? public class Junk { String name; public static void main(String[] args) { Junk a = new Junk(); Junk b = new Junk(); Junk c = a; System.out.print((a == c) + “ “); System.out.print((a == b) + “ “); System.out.print(a.equals(b)); } } a) false false false b) true false true c) true false false d) true true true
c) true false false a.equals(b) is false because, when equals() is not implemented, then the default behavior is to compare references.
What statements will compile? Choose all that apply. 1 int[] a; 2 int [] b; 3 int c[]; 4 int d []; 5 int[] e, f; 6 int g[], h; 7 int i[]; a) 1, 3 b) 1, 3, 5, 6 c) 1, 2, 5 d) 1, 2, 5, 7 e) All of them compile
e) All of them compile
How many arrays are declared? Choose all that apply. int[] a, b; int c[], d; a) 1 b) 2 c) 3 d) 4
c) 3 a, b, c are arrays d is just an int