+ 1
Who can help me with this java program please. What is the value of the variable q and r?
public class Program { public static void main(String[] args) { int p = 1; int q = 2; int r = 3; if(r > p + q) { r = 2 * p } else if(r < p + q) { r = 2 * q } System.out.printIn("r"); else if{ q = 2 * r System.out.println("q"); } } }
10 Answers
+ 24
wait... it will show many errors (most probably)
here is the corrected code âș & value of q & r will be 6 & 3 respectively
public class Program {
public static void main(String[] args) {
int p = 1;
int q = 2;
int r = 3;
if(r > p + q) {
//this condition is false bcz 3 is not > 1+2
r = 2 * p
;
}
else if(r < p + q) {
//this condition is also false
r = 2 * q
;
}
else {
System.out.println("r = "+r);
q = 2 * r
; // q is 6 & r = 3
System.out.println("q ="+q);
}
}
}
+ 2
It will show compile time error due to break of else if chain
+ 1
don't u think it will give the error of dangling else
+ 1
When you want to print the value of any variable, you must not write it in "". Replace "r" by r in print statement & same for q.
+ 1
@SuspiciousUser Thank you for the correction. I really appreciate. Cheers
+ 1
Correct! I meant the same :-) @SuspiciousUser
+ 1
@reshma cheers thank you.
0
Yes it is given an error, what will be the correct code for it please? I know I have made a mistake somewhere.
0
Write print statement in loop
0
@reshma please can you show me what you mean because I have tried it and it didn't work.