ProgrammingHardGATE CS · TCS Digital · Advanced
What is printed?
What is printed?
String a = "ab"; String b = "a"; String c = b + "b"; final String d = "a"; String e = d + "b"; System.out.print((a == c) + " " + (a == e) + " " + (a == c.intern()));
- A. false true true
- B. false false true
- C. true true true
- D. false true false
Correct answer: A. false true true
b is not a constant variable, so b + "b" is built at run time as a new object (a == c is false). d is a final constant, so d + "b" is folded at compile time to the pooled "ab"; intern() returns the pooled instance.
More OOP with C++ and Java questions
- What is printed? java interface A { default String m() { return "A"; } } interface B extends A { default String m() { return "B"; } }…
- What is printed? java class P { String n = "P"; static String s() { return "Ps"; } String t() { return "Pt"; } } class C extends P {…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct M { const char n; M(const char s) : n(s) { } ~M() { cout <<…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct P { P(const char s) { cout << s; } }; struct Q { P y{"y"}…
- What is printed? java String s1 = "hi"; String s2 = new String("hi"); System.out.print((s1 == s2) + " " + s1.equals(s2));
- What is printed? (C++, assume <iostream> and using namespace std) cpp struct C { static int n; C() { n++; } ~C() { n--; } }; int C::n = 0…