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 1 of 4.
Harshit Rochiramani said:
7 years ago
When the second call is made i.e. if ( b[1] && b[(++count - 2)] ) => if ( FALSE && b[(++count - 2)]).
Another part is never executed because of compiler optimisation in this case JVM hence it outputs 3!
if you write if (b[(++count - 2)] && b[1]).
It will output 4.
Another part is never executed because of compiler optimisation in this case JVM hence it outputs 3!
if you write if (b[(++count - 2)] && b[1]).
It will output 4.
(2)
Sanjai Krishnan said:
7 years ago
In the main method, we pass values for the index for 0 and 2.
Hence X[0]=true ,[2]= true. Default X[1]= false... Hence by changing the X[0], X[2] to true the count will become as count=2.
Next when the test method is called from the main method. First, if the condition is like (T&&F|T) which is false and hence count value is not incremented and remains as Count=2.
Second if condition is like (T && b[(++ count-2)])... Ie b[3-2] = b[1 ].
Since it is ++Count ... count = 3.... Where b[1] is false.
It looks like (T&&F ) which is False and hence count+=7 is not executed.
Thus the count=3 is to be displayed.
Hence X[0]=true ,[2]= true. Default X[1]= false... Hence by changing the X[0], X[2] to true the count will become as count=2.
Next when the test method is called from the main method. First, if the condition is like (T&&F|T) which is false and hence count value is not incremented and remains as Count=2.
Second if condition is like (T && b[(++ count-2)])... Ie b[3-2] = b[1 ].
Since it is ++Count ... count = 3.... Where b[1] is false.
It looks like (T&&F ) which is False and hence count+=7 is not executed.
Thus the count=3 is to be displayed.
(1)
Sanjai Krishnan said:
7 years ago
In main method , we pass values for index for 0 and 2.
Hence X[0]=true ,[2]= true. Default X[1]= false... Hence by changing the X[0], X[2] to true the count will become as count=2.
Next when test method is called from main method.
First ,if condition is like (T&&F|T) which is false and hence count value is not incremented and remains as Count=2.
Second,if condition is like (T && b[(++ count-2)])... Ie b[3-2] = b[1 ].
Since it is ++Count ... count = 3.... Where b[1] is false.
It looks like (T&&F ) which is False and hence count+=7.
Thus the count=3 is to be displayed.
Hence X[0]=true ,[2]= true. Default X[1]= false... Hence by changing the X[0], X[2] to true the count will become as count=2.
Next when test method is called from main method.
First ,if condition is like (T&&F|T) which is false and hence count value is not incremented and remains as Count=2.
Second,if condition is like (T && b[(++ count-2)])... Ie b[3-2] = b[1 ].
Since it is ++Count ... count = 3.... Where b[1] is false.
It looks like (T&&F ) which is False and hence count+=7.
Thus the count=3 is to be displayed.
Anant said:
7 years ago
I am not getting it. Can anyone help me?
Preethi said:
7 years ago
Thank you for the clear explanation @Jndsln.
Parth Soni said:
8 years ago
Can anyone explain how b and x refer to the same array because x's scope is in set method only and it will not update the array b right ?
Sainath said:
8 years ago
You are absolutely right Thanks, @Jndlsnl.
Bittu said:
8 years ago
Yes, @Jndlsnl.
You are right. Thanks a lot for writing down if values.
You are right. Thanks a lot for writing down if values.
Swagatika said:
9 years ago
Please anyone explain the compleat program.
Ravi said:
9 years ago
Thanks @Huy.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers