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));
  1. A. false true true true
  2. B. true false true true
  3. C. true true true false
  4. 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

Exams that ask this topic