info@meritcampus.com    +91-85006-22255
...
`

Operate with numbers
What will be the output of the following program?
public class Operation {

public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
for (int i = 0; i < 5; i++) {
if ((++num1 > 2) && (++num2 > 2)) {
num1++;
}
}
System.out.println(num1 + " and " + num2);
System.out.println(num2 | 12);
}
}

 A. 5 and 2 14 B. 5 and 315 C. 6 and 315 D. 6 and 412 E. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

ans is d

Posted by Navya Reddy    2013-05-01 12:47:04

since the loop is repeated for 4 times and...and addition of num1++ takes two times...soo num1 is 6 and nums takes place for 4times

Posted by Navya Reddy    2013-05-01 12:48:21

answer is C because after executing the loop the values of num1 and num2 are 6 and 3 and operation of bitwise OR operator gives 15

Posted by Rohith Gadalay    2013-05-01 12:52:11

Ans is c.. loop vl excute 3 times so num2=3, num1 is increasing twice so num1=6.. num2 | 12= num2+12=15

Posted by Om Prakash Om    2013-05-01 12:52:29

The answer is option C,,intially the values of Num1 and Num2 is 0, in the for loop it repeats till i<5,so as if condition is given ++num1 if >2 n ++num2if >2 then num1++ ,1st num1 will be 0, thus do ++num1 in IF condition,it is not >2,does the same til num1>2,now num1=3 and num2 =0,,nw it satisfies,,does ++num1 n ++num2 til i<5...now d values r num1=6,num2=3...thus pritns 6 and 3 in 1st s.o.p then...in 2nd s.o.p it takes 0011 | 1100 it gives 15...hence C is d answer :)

Posted by Shaik Nyamath    2013-05-01 12:59:49

Posted by Aparna Kanjarla    2013-05-01 13:00:55

answer is c..num2 does not get incremented until ++num1>2 is true...so the ending values vl bcum  5 n 3 n finally as both the conditions are true num1 is incremented in if condition..finally 6 n 3 are the values..3 | 12 using bit or 0011 | 1100 gives 1111 i.e, 15

Posted by Pramod Jain    2013-05-01 13:40:54

Congratulations Rohith Gadalay. You are today's daily dose winner. Please send us your mobile details and we will process the recharge tomorrow after considering the new likes.

Posted by Merit Campus    2013-05-01 15:36:09

thank u mc received rc..:) :)

Posted by Rohith Gadalay    2013-05-03 13:29:26