ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
System.out.print(Math.abs(Integer.MIN_VALUE) + " "); System.out.print(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);
- A. -2147483648 true
- B. 2147483647 true
- C. 2147483647 false
- D. -2147483648 false
Correct answer: A. -2147483648 true
+2147483648 does not fit in an int, so negating MIN_VALUE overflows back to MIN_VALUE and Math.abs returns a negative number. int arithmetic wraps silently, so MAX_VALUE + 1 equals MIN_VALUE.
More OOP with C++ and Java questions
- What is printed? java System.out.print((-7 / 2 == -3) + " " + (-7 % 2 == 1) + " " + (Math.floorMod(-7, 2) == 1) + " " + (7 % -2 == 1));
- Which line fails to compile? java byte b = 10; b += 5; // line 1 b++; // line 2 b = b + 1; // line 3 b = 10 + 20; // line 4
- What is printed? java int i = 0, j = 0; boolean p = (i++ > 0) & (i++ > 0); boolean q = (j++ > 0) && (j++ > 0); System.out.print(i + " " +…
- What is printed? java class P { private void f() { System.out.print("P"); } void call() { f(); } } class C extends P { void f() {…
- What is printed? java class P { static String s() { return "S"; } } // in main: P p = null; System.out.print(p.s());
- Which line fails to compile? java interface I { static String s() { return "I"; } default String d() { return "D"; } } class C implements…