Strings & Arrays
Difficulty: easy
Overview
String is immutable — all modification methods return new String objects. The String pool (heap since Java 7) reuses string literals. intern() returns a canonical String from the pool. StringBuilder is mutable and not thread-safe; StringBuffer is thread-safe. Arrays have a fixed size; default int[] values = 0. Multi-dimensional arrays are arrays of arrays. Arrays utility class provides sort(), binarySearch(), and fill().
Practice Linked Questions
Q1. String immutability in Java means:
Select one answer before revealing.
Q2. What is the key difference between StringBuilder and StringBuffer in Java?
Select one answer before revealing.
Q3. Since Java 7, where is the String constant pool located?
Select one answer before revealing.
Q4. What does the String.intern() method do?
Select one answer before revealing.
Q5. In Java, array indices start at:
Select one answer before revealing.
Q6. What is the default value of elements in a newly created int[] array in Java?
Select one answer before revealing.
Q7. Which of the following correctly declares a 2D integer array in Java?
Select one answer before revealing.
Q8. Which String methods return a new String object? (More than one answer may be correct)
Select one answer before revealing.
Q9. What does str.charAt(0) return when str = "Java"?
Select one answer before revealing.
Q10. String.compareTo() in Java:
Select one answer before revealing.
Q11. What does String.format() return in Java?
Select one answer before revealing.
Q12. What is the difference between .length for arrays and .length() for Strings?
Select one answer before revealing.
Q13. What is the output of the following code? String s1 = "Java"; String s2 = "Java"; String s3 = new String("Java"); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s1.equals(s3));
Select one answer before revealing.
Q14. What is the output of the following code? int[] arr = new int[5]; System.out.println(arr[4]); System.out.println(arr[5]);
Select one answer before revealing.
Q15. What is the output of the following code? StringBuilder sb = new StringBuilder("Hello World"); sb.delete(5, 11); System.out.println(sb); System.out.println(sb.length());
Select one answer before revealing.
Q16. What is the output of the following code? String s = "Hello"; s.toUpperCase(); System.out.println(s);
Select one answer before revealing.
Q17. What is the output of the following code? int[] numbers = {3, 1, 4, 1, 5}; int sum = 0; for (int n : numbers) { sum += n; } System.out.println(sum);
Select one answer before revealing.
Q18. What is the output of the following code? int n = 42; double d = 3.14159; String s = String.format("n=%d, d=%.2f", n, d); System.out.println(s);
Select one answer before revealing.
Q19. What is the output of the following code? String s = "1.2.3.4"; String[] parts = s.split("."); System.out.println(parts.length);
Select one answer before revealing.
Q20. What is the output of the following code? String s1 = "hello"; String s2 = "HELLO"; System.out.println(s1.equals(s2)); System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.compareTo(s2) > 0);
Select one answer before revealing.
Q21. What is the output of the following code? String s = "Java Programming"; System.out.println(s.indexOf("Programming")); System.out.println(s.contains("Java")); System.out.println(s.startsWith("Pro"));
Select one answer before revealing.