AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
programming-basics: What will be the output of the following program?
public class Assess {
static int j;
static void methodA(int i){
boolean b;
do{
b=i<10 | methodB(4);
b=i<10 || methodB(8);
}while(!b);
}
static boolean methodB(int i){
j+=i;
return true;
}
public static void main(String[] args) {
methodA(0);
System.out.println(j);
}
}
programming x 169
basics x 171
Posted On :
2017-05-06 19:14:52.0
Saksham Kumar
734
339
39091
3
Answers
It´ll print 4.
Explanation:
-------------------------------------
b=i<10 | methodB(4): It has Bitwise OR operator hence it´ll check left and right both expression even if left is true. So j´s value will be 4 after this line.
b=i<10 || methodB(8): It has Logical OR operator. So if left expression is true it´ll not check right expression, Hence methodB(8) will be ignored as i<10 is true.
Since it´s a do-while, After executing do block it´ll check the condition which will be false so control will go back to System.out.println(j); j is declared as static, so output will be 4.
Posted On :
2017-05-06 19:24:32
Satisfied :
2 Yes
0 No
Rishi Kumar
523
1882
49150
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in