Java · Question 30 · Hard
What will be the output of the following Java program?
public class Main {
public static void main(String[] args) {
System.out.println(-17 % 5 + " " + (-17 / 5));
}
}
- -2 -3
- 2 -3
- 3 -4
- -2 -4
Answer and explanation
Answer: A. -2 -3
Explanation: Java integer division truncates toward zero, so -17 / 5 is -3. The remainder has the sign of the dividend: -17 - (-3 × 5) = -2. Python would print 3 -4, but Java does not.
Previous: What will be the output of the following Java program?
Practise java
Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.
More java questions
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
More technical & programming practice
Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge