Sample questions
Question 1Easy
What is the output?
public class Main {
public static void main(String[] args) {
String s = "Java";
s.concat(" 17");
System.out.println(s);
}
}
- 17
- Java 17
- Java
- Compilation error
Answer: C. Java
Explanation
Strings are immutable: concat() returns a new string, which is discarded here. s still refers to "Java".
Step-by-step solution
s.concat(' 17') creates a new string 'Java 17' but does not change s.System.out.println(s) prints the original value of s, which is 'Java'.- The
concat() method returns a new string but does not modify the existing one. - Since
s remains 'Java', the output is Java. - No compilation error occurs because all methods are valid.
- Final answer: Java.
Why the other options are wrong
- A. The output does not start with a space; s still holds Java, and concat's result is not printed.
- B. Java 17 would need s to be changed, but concat returns a new string and s is not updated.
- D. The code compiles, since concat is a valid String method and the call is legal.
Tip: Remember: String methods do not modify the original string; they return a new one.
Question 2Easy
What is the output?
public class Main {
public static void main(String[] args) {
String s = "banana";
System.out.println(s.replace('a', 'o') + " " + s);
}
}
- banana bonono
- bonono bonono
- bonana banana
- bonono banana
Answer: D. bonono banana
Explanation
replace() returns a new string with every 'a' replaced; the original s is unchanged because strings are immutable.
Step-by-step solution
s.replace('a', 'o') replaces all 'a's in 'banana' with 'o', resulting in 'bonono'.- The original string
s remains 'banana' because strings are immutable. - Adding the modified string and original string gives 'bonono banana'.
- Final output is 'bonono banana'.
Why the other options are wrong
- A. replace returns a new string and the original s is not changed, so the first part is bonono, not banana.
- B. The original s is still banana, since strings are immutable, so the second part is not bonono.
- C. replace changes a to o, so the first part is bonono, not bonana, and the second part is banana.
Tip: Remember: replace() does not modify the original string.
Question 3Easy
Where are string literals such as "abc" stored in modern Java (Java 7 and later)?
- In a CPU register cache managed by the JIT
- In PermGen, separate from the heap
- In the string pool, which is part of the heap
- In the method stack frame of the method that uses them
Answer: C. In the string pool, which is part of the heap
Explanation
Since Java 7 the string pool lives on the main heap (it was moved out of PermGen), so pooled strings can be garbage collected like other objects.
Step-by-step solution
- Since Java 7, string literals such as "abc" are stored in the string pool.
- The string pool is part of the heap; it was moved out of PermGen in Java 7.
- It is not a CPU register cache and it is not in the method stack frame.
- So the answer is the string pool in the heap, option C.
Why the other options are wrong
- A. Literals are not stored in a JIT-managed CPU register cache.
- B. PermGen was the old location; from Java 7 the pool is on the heap.
- D. The stack frame holds references to strings, not the literal strings themselves.
Tip: In modern Java, string literals and interned strings live in the heap string pool.
Question 4Easy
What is the output?
public class Main {
public static void main(String[] args) {
System.out.println(1 + 2 + "3" + 4 + 5);
}
}
- 12345
- 15
- 339
- 3345
Answer: D. 3345
Explanation
Evaluation is left to right: 1 + 2 = 3 (numeric), then "3" + "3" = "33", and after a String is involved each + is concatenation, giving "3345".
Step-by-step solution
- Java reads left to right. 1 + 2 are both int, so the sum is 3.
- 3 + "3" now has a String, so + joins text and gives "33".
- "33" + 4 gives "334", and "334" + 5 gives "3345".
- The output is 3345, option D.
Why the other options are wrong
- A. 1 + 2 is added as numbers first, so the output cannot start with 12345.
- B. The numbers are not all added once a String appears, so the result is not 15.
- C. Once the String appears, the 4 and 5 are joined as text, so the result is 3345, not 339.
Tip: In a chain of +, numbers add until the first String. After that, every + joins text.
Question 5Medium
What is the output?
public class Main {
public static void main(String[] args) {
String s = " Hi There ";
System.out.println("[" + s.strip() + "]" + s.length());
}
}
- [HiThere]12
- [ Hi There ]12
- [Hi There]12
- [Hi There]8
Answer: C. [Hi There]12
Explanation
strip() returns a new string without leading and trailing whitespace; s itself is unchanged, so its length is still 12.
Step-by-step solution
- S is " Hi There ", with two spaces on each side.
- strip() returns a new String with the outer whitespace removed, giving "Hi There".
- S itself is unchanged, so s.length() is still 12 (2 + 8 + 2).
- The output is [Hi There]12, option C.
Why the other options are wrong
- A. strip() removes only the outer spaces, so the space between Hi and There stays.
- B. strip() removes the outer spaces, so the brackets do not contain any spaces.
- D. The length comes from s, which still has 12 characters, not the stripped 8.
Tip: strip() returns a new String and never changes the original. length() counts every character, spaces included.
Question 6Medium
What is the output?
public class Main {
public static void main(String[] args) {
char[] arr = {'j', 'a', 'v', 'a'};
String s = new String(arr);
String t = String.valueOf(arr, 1, 2);
System.out.println(s + " " + t + " " + "java".equals(s));
}
}
- java ava true
- java av false
- java av true
- java va true
Answer: C. java av true
Explanation
new String(arr) copies all characters. valueOf(arr, 1, 2) takes 2 chars starting at index 1 ("av"). equals compares content, so it is true.
Step-by-step solution
New String(arr) creates 'java' from the array.String.valueOf(arr, 1, 2) takes characters at index 1 and 2: 'av'.'java'.equals(s) compares content, so it is true.S + ' ' + t + ' ' + ... concatenates to 'java av true'.
Why the other options are wrong
- A. valueOf with offset 1 and count 2 gives av, not ava, so the second value is wrong.
- B. 'java'.equals(s) is true, since s holds the same content, so the last value is true.
- D. valueOf starts at index 1 and takes two chars, so the second value is av, not va.
Tip: Remember: String.equals() compares content, not memory.
Question 7Medium
Why is String immutable in Java? Choose the best reason.
- Because String is declared abstract
- So pooled strings can be shared safely and their hash codes cached
- Because the char array inside it is stored in read-only memory by the OS
- Because Java has no way to modify an array after creation
Answer: B. So pooled strings can be shared safely and their hash codes cached
Explanation
Immutability lets many references share one pooled literal, makes strings safe as HashMap keys (hash cached) and thread-safe without locks.
Step-by-step solution
- Immutable strings allow multiple references to share the same object in memory.
- Sharing is safe because a String can never change, and that also lets its hash code be cached.
- String pooling ensures efficient memory usage and avoids duplicate objects.
- Hash caching makes strings ideal as keys in HashMaps without lock issues.
- The best reason combines safety, efficiency, and thread-safety through immutability.
Why the other options are wrong
- A. String is declared final, not abstract, and abstract classes cannot be instantiated, which is not the reason.
- C. The OS does not keep the char array in read-only memory; immutability is a design rule enforced by the Java class.
- D. Java arrays can be modified after creation, so this is false; String is immutable by its design.
Tip: Remember: String immutability enables string pooling and hash caching for efficient use.
Question 8Medium
What is the output?
public class Main {
public static void main(String[] args) {
String s = null;
System.out.println(s.length());
}
}
- NullPointerException
- 0
- Compilation error
- null
Answer: A. NullPointerException
Explanation
Calling an instance method on a null reference throws NullPointerException at runtime.
Step-by-step solution
S is assigned null, which means it refers to no object.length() is an instance method, requires an object reference.- Calling
length() on null throws NullPointerException at runtime. - The code compiles because there's no syntax error.
- Runtime exception occurs when the program runs.
NullPointerException is the correct output.
Why the other options are wrong
- B. 0 would need a real string object, but s is null and calling length() on null cannot return 0.
- C. The code compiles, because s.length() is valid syntax for a String variable.
- D. null would be printed only by printing s directly, but here length() is called on null and throws.
Tip: Always check for null before calling instance methods.
Question 9Hard
What is the output?
public class Main {
public static void main(String[] args) {
String a = "hi";
String b = "h";
String c = b + "i";
String d = c.intern();
System.out.println((a == c) + " " + (a == d) + " " + a.equals(c));
}
}
- false true false
- false false true
- false true true
- true true true
Answer: C. false true true
Explanation
c is built at runtime, so it is a new object. intern() returns the pooled "hi", which is the literal a. equals compares content, so it is true.
Step-by-step solution
- A is the literal "hi", which is a pooled object.
- c = b + "i" is built at runtime, so it is a new object and a == c is false.
- d = c.intern() returns the pooled "hi", the same object as a, so a == d is true.
- a.equals(c) compares contents and is true. The output is false true true, option C.
Why the other options are wrong
- A. a.equals(c) compares contents, so the last value is true, not false.
- B. intern() returns the pooled instance, so the second value is true, not false.
- D. a == c is false because c is a new runtime object, not the pooled one.
Tip: Runtime concatenation makes a new object. intern() maps it back to the pooled copy.
Question 10Hard
What is the output?
public class Main {
public static void main(String[] args) {
String s1 = "ab";
final String x = "a";
String y = "a";
System.out.println((s1 == x + "b") + " " + (s1 == y + "b"));
}
}
- false false
- true true
- false true
- true false
Answer: D. true false
Explanation
x is a final variable initialised with a constant, so x + "b" is a compile-time constant and is pooled. y is not final, so y + "b" is built at runtime as a new object.
Step-by-step solution
- X is final and set to the constant "a", so x + "b" is a compile-time constant "ab" and is pooled.
- S1 is the literal "ab", which is the same pooled object, so s1 == x + "b" is true.
- Y is not final, so y + "b" is built at runtime as a new String, so s1 == y + "b" is false.
- The output is true false, option D.
Why the other options are wrong
- A. The first comparison is true, because the constant x + "b" is pooled and matches s1.
- B. The second comparison is false, because y + "b" is built at runtime as a new object.
- C. The two results are reversed: the constant concatenation is the one equal to s1.
Tip: A final variable with a constant value is folded at compile time. A plain variable is concatenated at runtime into a new object.
Practise all 24 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →