AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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 165
basics x 169
Posted On : 2017-05-06 19:14:52.0
profile Saksham Kumar - anyforum.in Saksham Kumar
72933915323
up-rate
3
down-rate

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
profile Rishi Kumar - anyforum.in Rishi Kumar
523186220593
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in