Output from part a) System.out.printin1++2+3+4) System.out.println (1(2+3+4)) Sy
ID: 3706902 • Letter: O
Question
Output from part a) System.out.printin1++2+3+4) System.out.println (1(2+3+4)) System.out.println ((1+2)"+3+4) int a- 10, b 20, 30 System.out.printin (a+++b++) System.out.println (a+++a++) System.out-println (a+--b+c) Output from part b) String first"Paul" StringBuilder last new StringBuilder("Cao"); Output from part c) first.concat(" Cao")//space before Cao last.append (" Paul")//space before Paul System.out.println (first) System.out.printin (last) int vl-2, v2-3 if (vl v2 5 I v2++>3) system.out.println("1. " vl+""+v2)Output from part d) System.out.printin("2. "+ vl +"" v2) System.out-printin("3. " +vl*""v2) else if (v2-3&&+4v1--2) else(Explanation / Answer
a) Output:
1:234
1:9
3:34
In the first print statement, 1 is printed and then seperately 2, 3, and 4 are printed after ':'
In second statement, 1 is printed and then addition of 2,3,and 4 is done first and then the sum 9 is printed.
In third statement, first 1+2 is done and then 3 is printed and then seperately 3 and 4 is printed.
b) OUTPUT:
30
23
62
1st statement, a++ + b++ means post incrments of a and b are added. since, its a post increment, the update will be done in next statement not the current ne so 10+20 =30 is printed.
2nd statement, a++ + a++. Due to post incrementin previos statement a's value is now 11. Again it is post incremented here two times and added. So, after the first a++, the vaalue will remail 11 and after second a++ it becomes 12. So, 11+12 =23 is printeed.
3rd statement, is like a + --b + --c. Value of a due to previos post increment becomes 1 now. And value of b due to post increment in 1st statement becomes 21 now but it is preincremented as --b so it becoomes 20 again here. The value of c is also preincremented to 29. So, 13+20+29 =62 is prited.
c) OUTPUT:
Paul
Cao Paul
String datatype in itself is immutable. That means it cannot be changed once initialised. So, first.concat(" Cao") will not have any effect So, 1st print statement gives "Paul" only.
String Builder on the other hand can be altered once initialised. So, appending " Paul" joins the string at the end of the initialised string which is "Cao". It prints "Paul Cao"
d) OUTPUT:
3. 2 4
The if statement checks if v1+v2 > 5 which is false OR v2++ > 3 which is also false because its a post increment so v3 remains 3 in this statement.
The else if statement checks if v2 == 3 which is false as due to post increment in previous statement v3 is now 4 Rest of the condition is nort checked one condition is false in AND statement so whole statement is false now.
The else statement is what's left and is printed. With v1 = 2 and v2 =4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.