QUIZ: Topic 3 - Strings in Java Total Questions: 10 Instructions: Choose the most correct option (A, B, C or D) for each question. Check your answers using the Answer Key at the end. 1. How many objects are created by the statement String s = new String("Welcome"); ? A) One object only B) Two objects and one reference variable C) Three objects D) No object, only a reference is created 2. What is the output of the following code? String str1 = "Java Programming"; System.out.println(str1.charAt(2)); A) 'J' B) 'a' C) 'v' D) 'P' 3. Consider the following code: String example = "Hello! "; example = example.concat(" World"); Which statement is true after this code runs? A) The original string "Hello! " was modified in place to "Hello! World" B) A new string "Hello! World" was created and assigned to example, while "Hello! " remains unchanged C) The program will not compile because strings cannot be reassigned D) The example variable still holds "Hello! " 4. Suppose str1 is null and str2 is "Java". What are the results of str1.concat(str2) and str1 + str2 respectively? A) Both throw a NullPointerException B) Both give "nullJava" C) str1.concat(str2) throws NullPointerException, while str1 + str2 gives "nullJava" D) str1.concat(str2) gives "nullJava", while str1 + str2 throws NullPointerException 5. Which method of the String class is used to test two strings for equality of their content? A) concat() B) equals() C) indexOf() D) contains() 6. Which statement correctly distinguishes StringBuilder from StringBuffer? A) StringBuilder is synchronized, while StringBuffer is not B) StringBuilder is non-synchronized (and hence faster in single-threaded use), while StringBuffer is synchronized C) StringBuilder is immutable, while StringBuffer is mutable D) Both StringBuilder and StringBuffer are immutable like String 7. What is the output of the following code? String str1 = " Learn Java Programming "; System.out.println(str1.trim()); A) " Learn Java Programming " B) "Learn Java Programming" C) "LearnJavaProgramming" D) "Learn Java Programming " 8. What is the output of the following code? StringBuilder sb = new StringBuilder("Hello"); sb.replace(1, 3, "Java"); System.out.println(sb); A) Javalo B) HelloJava C) HeJavalo D) HJava 9. Which statement about the capacity of a StringBuilder is correct? A) The default capacity of new StringBuilder() is 10, and it grows by 10 each time B) The default capacity of new StringBuilder() is 16, and when it is exceeded the capacity becomes (oldcapacity * 2) + 2 C) The default capacity of new StringBuilder() is 16, and it always doubles exactly D) StringBuilder has no capacity limit and never needs to grow 10. What is the output of the following code? String str1 = "Learn Java"; System.out.println(str1.indexOf("ava")); A) 5 B) 6 C) 7 D) 9 ANSWER KEY 1. B) The literal "Welcome" goes in the string constant pool and a new String object is created in heap memory, so the statement creates two objects plus one reference variable. 2. C) Indexing starts at 0, so index 0 is 'J', 1 is 'a' and 2 is 'v', hence charAt(2) prints 'v'. 3. B) Strings are immutable, so concat() creates a new string object and the original "Hello! " is never changed. 4. C) concat() on a null reference throws NullPointerException, whereas the + operator converts null to the text "null" and produces "nullJava". 5. B) equals() compares the character content of two strings and returns true when they are equal. 6. B) StringBuilder is mutable and non-synchronized (available since JDK 1.5), while StringBuffer is the synchronized version. 7. B) trim() removes only the leading and trailing whitespace, so the result is "Learn Java Programming". 8. A) replace(1, 3, "Java") replaces characters at index 1 and 2 ("el") with "Java", giving H + Java + lo = "Javalo". 9. B) A default StringBuilder starts with capacity 16, and when the capacity is exceeded it grows to (oldcapacity * 2) + 2, i.e. 34 from 16. 10. C) The substring "ava" first occurs at index 7 of "Learn Java" (L=0, e=1, a=2, r=3, n=4, space=5, J=6, a=7).