Java · Question 2 · Easy
What will be the output of the following Java program?
public class Main {
public static void main(String[] args) {
int a = 10;
double d = a / 4;
System.out.println(d);
}
}
- 2.5
- 2
- 2.0
- Compilation error
Answer and explanation
Answer: C. 2.0
Explanation: a / 4 is int / int, so integer division gives 2 first. That 2 is then widened to double and printed as 2.0.
Previous: What will be the output of the following Java program? · Next: Which Java keyword, when applied to a class, prevents any other class from…
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?
- Which Java keyword, when applied to a class, prevents any other class from…
- What will be the output of the following Java program?
- What is the default value of an int instance variable (field) that is declared…
- 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?
- 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