Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 14)
14.
What will be the output of the program?
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i)
{
x[i] = true;
++count;
}
public static void main(String [] args)
{
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
Answer: Option
Explanation:
The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test.
Discussion:
31 comments Page 3 of 4.
Jigar said:
1 decade ago
Two set() method incremented count,
Now when test() called,
1st condition is true,
Hence, count is incremented! (post incremented).
Now when test() called,
1st condition is true,
Hence, count is incremented! (post incremented).
Nitish said:
1 decade ago
Ashi:-because by default the value for boolean will be false.
Gourav Soni and Varsha:-its working like:-
if ( b[1] && b[(++count - 2)] )
count += 7;
if(false && ----- )
here b[1] is false.So.. if the value for left operand is false then it won't check for right operand and will go to next line.that's y count won't be incremented further.
i hope it will be helpful for you.
Gourav Soni and Varsha:-its working like:-
if ( b[1] && b[(++count - 2)] )
count += 7;
if(false && ----- )
here b[1] is false.So.. if the value for left operand is false then it won't check for right operand and will go to next line.that's y count won't be incremented further.
i hope it will be helpful for you.
Ashi said:
1 decade ago
b[1]=false..how??
Varsha said:
1 decade ago
@gourav : evn i had the same doubt but after reading shipras explanation i am convinced and my doubt is resolved.....
Gourav Soni said:
1 decade ago
@[jndlsnl@gmail.com] :
in second if condition we have used ++count...which should increase count by 1 ...so count shud be 4 ..ryt??
in second if condition we have used ++count...which should increase count by 1 ...so count shud be 4 ..ryt??
Shipra said:
1 decade ago
ba.set(ba.b, 0); will set b[0]=true and ba.set(ba.b, 2); will set b[2]=true.. n b[a]=0 so we take it as false.
now in test method first if statement will return true because (true & false) || true = true;
now in second if statement first statement b[1] is false so next statement will not be executed ..because there is AND operator does not check second condition if first is false..so count value will not be incremented n will remain 3.
now in test method first if statement will return true because (true & false) || true = true;
now in second if statement first statement b[1] is false so next statement will not be executed ..because there is AND operator does not check second condition if first is false..so count value will not be incremented n will remain 3.
Thanvir513@yahoo.com said:
1 decade ago
Why b[0]=true, b[1]=false and b[2]=true ? please explain clearly.
Jndlsnl@gmail.com said:
1 decade ago
Bcz in if ( b[0] && b[1] | b[2] ) ---> if(true && false | true) ----> if(true) so count++ =3.
but if ( b[1] && b[(++count - 2)] ) ----> if(false && b[(3-2)])---> if(false && false) ----> if(false) ----> so it will not execute.
Correct me if I am wrong.
but if ( b[1] && b[(++count - 2)] ) ----> if(false && b[(3-2)])---> if(false && false) ----> if(false) ----> so it will not execute.
Correct me if I am wrong.
Gopi said:
1 decade ago
But why it is incremented only in first if in test method ?
Samir said:
1 decade ago
When test method call at that time count is increment at one more time.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers