Multi variable for loop output
What will be the output of the following program?
class MultiVariableFor
{
public static void main(String s[])
{
int a, b;
for(a = 1, b = 4; a < b; a++, b--)
{
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}
A.
|
Compilation error since both a++ and b-- should not be present in the iteration section (i.e after the second semicolon)
|
B.
|
a = 1 b = 4 a = 2 b = 3 a = 3 b = 2
|
C.
|
a = 1 b = 4 a = 2 b = 3
|
D.
|
Compilation error since both a = 1 and b = 4 should not be present in the initialization section (i.e before the first semicolon)
|
Topic:
for Loop In Java
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
c ..here we are using for loop for iteratiom.... initially a=1 and b=4 as a<b condition satisfies.... so it will print a and b as 1 and 4 then a is incremented to 2 and b decremntd to 3 acc to loop....... again a<b condition satisfies amd it will print 2 and 3 ...now a incrmntd to 3 and b decremented. to 3 ....here a=b condition not met and it will exit frm loop....
Posted by Asma Mujtaba Khan 2015-01-07 03:56:46
i'll go with option "c" ... the reason is like .. we are using post increment and post decrement operators in the for(a++ , b--).. So in 1st run a<b and a=1,b=4 after 1st iteration a=2,b=3 .. So 2nd iteration also a<b , and after 2nd iteration a=3,b=2 ... So while Checking for 3rd iteration a<b condition fails... and loop exits
Posted by ?????????? ????? 2015-01-07 06:56:49
ANS IS C
Here For Loop is used with two counters 'a' and 'b'.
--Counter 'a' is initialized to '1' and 'b' to '4'
1st iteration-a=1
b=4
a++ -->a=2
b-- -->b=3
a<b --> 2<3(true)
Therefore Loop will execute it's next iteration.
2nd iteration-a=2
b=3
a++ -->a=3
b-- -->b=2
a<b --> 3<2 (false)
Therefore Loop will not execute it's next iteration.Loop will terminate here.
Posted by Mânïshå Mùlchåndânï 2015-01-07 06:58:29
C is the correct the loop will work until a>=b
Posted by Deion Davis 2015-01-07 10:14:31
c. is the correct ans...
Posted by Shubham Bansal 2015-01-07 12:32:06
Answer is C.. a>b fails in the third iteration because of changing in the values of a and b after 1st and 2nd iteration. so loop exists.
Posted by Sai Ram 2015-01-07 17:25:19
ans c.in this program initially we declared a,b int variables,thn in for both variables initialized with 1,4.here "," is used to separate 2 variables,but in condition statement we need to give 1 condition with combination of both variables,thn a post incremnted ,b post decrementd.here a=1,b=4,condition 1<4 true so 1,4 get printed ,thn a incremented to 2,b decrementd to 3,nw 2<3 true so a=2,b=3 get printed,thn increment a value is 3,b value decremented is 2,3<2 condition failed so control goes out of loop
Posted by Maheshwari Natarajan 2015-01-08 11:35:07
This dose is now closed and the winners are ?????????? ?????, for 'First Correct Comment', ?????????? ?????, Mânïshå Mùlchåndânï, Maheshwari Natarajan, for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.
Posted by Merit Campus 2015-01-09 02:28:47
hey my comment is also correct....... then y u didnt consider it..... for frst crrct cmmnt
Posted by Asma Mujtaba Khan 2015-01-09 03:55:59
I logged in ... but how to claim ??
Posted by ?????????? ????? 2015-01-09 05:40:57