ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
public class Main {
public static void main(String[] args) {
String a = "Hello";
String b = "hello";
System.out.print(a.equals(b) + " " + a.equalsIgnoreCase(b) + " " + (a == b));
}
}- A. false true true
- B. true false false
- C. true true false
- D. false true false
Correct answer: D. false true false
equals() is case-sensitive, so "Hello".equals("hello") is false. equalsIgnoreCase() ignores case, so it's true. The literals differ in case, so even the string pool stores them as distinct entries, making a == b false too.
More OOP with C++ and Java questions
- What is printed? java class Base { Base() { System.out.print("Base "); } protected Base(int x) { System.out.print("Base(int) "); } } class…
- What is printed? java class Vehicle { void start() { System.out.print("Vehicle-start "); } } class ElectricCar extends Vehicle { @Override…
- What is printed? cpp struct Base { Base() { cout << "Base "; } }; struct Mixin1 { Mixin1() { cout << "Mixin1 "; } }; struct Mixin2 {…
- What is printed? java class Base { public String describe() { return "Base"; } } interface Describable { String describe(); } class Sub…
- What is printed? java class Resource { String name; Resource(String n) { name = n; System.out.print("open:" + n + " "); } void use() {…
- What is printed? cpp class Base { public: Base() { cout << "Base() "; } Base(int x) : Base() { cout << "Base(int) "; } }; int main() {…