ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
public class Main {
public static void main(String[] args) {
String a = "hello";
String c = new String("hello").intern();
System.out.println(a == c);
}
}- A. false
- B. true
- C. Compile-time error: intern() is not a method of String
- D. Throws an exception at runtime
Correct answer: B. true
intern() looks up (or adds) the string's content in the string pool and returns the canonical pooled reference. Since "hello" is already pooled from the literal a, c.intern() returns that exact same pooled reference as a, making a == c true.
More OOP with C++ and Java questions
- What is printed? java import java.util.; public class Main { public static void main(String[] args) { List<Integer> nums =…
- What is printed? java public class Main { static void risky(int x) throws Exception { if (x < 0) throw new Exception("neg")…
- What is printed? java public class Main { static String f(int x) { try { if (x == 0) throw new ArithmeticException("div0"); return "ok" +…
- What is printed? java class LowBalanceException extends Exception { LowBalanceException(String m) { super(m); } } class Account { double…
- What is printed? java public class Main { public static void main(String[] args) throws Exception { try { method1(); } catch…
- What is printed? java public class Main { static class Resource implements AutoCloseable { public void close() { System.out.print("closed…