ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
System.out.print((-7 / 2 == -3) + " " + (-7 % 2 == 1) + " " + (Math.floorMod(-7, 2) == 1) + " " + (7 % -2 == 1));
- A. false true true true
- B. true false true true
- C. true true true false
- D. true false false true
Correct answer: B. true false true true
Integer division truncates toward zero (-3), and % takes the sign of the dividend: -7 % 2 = -1 and 7 % -2 = 1. floorMod takes the sign of the divisor, giving 1.
More OOP with C++ and Java questions
- 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…
- What is printed? java int[] a = {1, 2}; int[] b = {1, 2}; System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " +…