Java Programming - Flow Control - Discussion
Discussion Forum : Flow Control - Finding the output (Q.No. 7)
7.
What will be the output of the program?
public class If1
{
static boolean b;
public static void main(String [] args)
{
short hand = 42;
if ( hand < 50 && !b ) /* Line 7 */
hand++;
if ( hand > 50 ); /* Line 9 */
else if ( hand > 40 )
{
hand += 7;
hand++;
}
else
--hand;
System.out.println(hand);
}
}
Answer: Option
Explanation:
In Java, boolean instance variables are initialized to false, so the if test on line 7 is true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The else-if is true so hand has 7 added to it and is then incremented.
Discussion:
13 comments Page 2 of 2.
Sangeetha said:
1 decade ago
By default is boolean instance variable are initialized to false?
Sonu said:
1 decade ago
How line 7 can be true with one '&' operator? Can somebody explain this.
Ankur Varshney said:
1 decade ago
if ( hand > 50 );
This statement is not legal .so according to me this is error
This statement is not legal .so according to me this is error
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers