1. Show what this program prints. Be exact and complete. Can you explain the beh
ID: 3781138 • Letter: 1
Question
1. Show what this program prints. Be exact and complete. Can you explain the behavior of each print statement?
1
2
3
4
5
6
7
public class Test {
public static void main(String[] args) {
System.out.println("39 + 3");
System.out.println(39 + 3);
System.out.println("39" + 3);
}
}
1
2
3
4
5
6
7
public class Test {
public static void main(String[] args) {
System.out.println("39 + 3");
System.out.println(39 + 3);
System.out.println("39" + 3);
}
}
Explanation / Answer
Following is the output of given program:
Explaination:
First print Statement: System.out.println("39 + 3");
As the statement contains "39 + 3" in double inverted commas, program treats 39 + 3 as a string and hence the output is the string itself.
Second print Statement: System.out.println(39 + 3);
There is no double inverted comma, So expression 39 + 3 is evaluated first which is 42, the then 42 is printed.
Third print Statement: System.out.println("39" + 3);
Here , "39" is in double inverted commas, so 39 is treated as a string and printed. And 3 is appended to the output screen as a result of + 3.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.