ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
public class Main {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer c = 200;
Integer d = 200;
System.out.println((a == b) + " " + (c == d));
}
}- A. true true
- B. false false
- C. true false
- D. false true
Correct answer: C. true false
Autoboxing uses Integer.valueOf, which caches boxed values in the range -128 to 127. 127 is within that range, so a and b refer to the same cached object (a == b is true). 200 is outside the cache range, so c and d are distinct boxed objects (c == d is false).
More OOP with C++ and Java questions
- What is printed? java class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) {…
- What is printed? java class Base { void show() { System.out.print("Base"); } } class Sub extends Base { void show() {…
- What is printed? cpp struct Animal { virtual std::string sound() const = 0; void makeSound() const { cout << sound(); } virtual ~Animal()…
- What is printed? java class Base { Base() { System.out.print("Base-ctor "); } static { System.out.print("Base-static "); } } class Sub…
- What is printed? cpp struct Base { virtual void f() { cout << "B"; } }; struct Mid : Base { }; struct Leaf : Mid { void f() override {…
- What is printed? java public class Main { static class Base { public String toString() { return "Base"; } public boolean equals(Object o)…