ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
// in main: String a = "Hello".toLowerCase(); String b = "hello"; System.out.println((a == b) + " " + a.equals(b));
- A. true true
- B. false true
- C. true false
- D. false false
Correct answer: B. false true
toLowerCase() always returns a newly constructed String object (never the pool entry, even if the content happens to match a literal), so a == b is false, but the character content is identical, so equals() is true.
More OOP with C++ and Java questions
- Which overridden equals() below is actually BROKEN with respect to the symmetry requirement of the equals contract? java class A { int x…
- What is printed? java class Version { int major, minor; Version(int ma, int mi) { major = ma; minor = mi; } @Override public boolean…
- What is printed? java public class Main { static int f() { int x = 10; try { return x; } finally { x = 20; } } public static void…
- What is printed? java public class Main { public static void main(String[] a) { try { System.out.print("A"); throw new RuntimeException()…
- Does this compile? java class MyException extends Exception {} class Demo { void risky() throws MyException { throw new MyException(); }…
- Does this compile? java class Demo { void risky() { throw new IllegalArgumentException(); } void call() { risky(); } }