4. Determine the oupput for each of the given sets of code when x is 9 and y is
ID: 3812410 • Letter: 4
Question
4. Determine the oupput for each of the given sets of code when x is 9 and y is 11 and when x is 11 and y is 9. Note that the compiler ignores the indentation in a Java program.Also, the Java compiler always associates an else with the immediately preceding ifunless told to do otherwise by the placement of braces On first glance, the programmer may not be sure which ifa particular else matches this situation is referred to as the "dangling-else problem." indentation from the following code has been eliminated to make the problem more challenging) [Hint: Apply the indentation conventions you've learned from the chapter Part A. if( x 10) if y> 10) System.out.println( else System.out.println( System.out println( $ssss"Explanation / Answer
PartA:
When x = 9, y = 11;
if (x < 10)
if (y > 10)
System.out.println("*****");
else
System.out.println("#####");
System.out.println("$$$$$");
If we didn’t provide the braces({})the immediate single will be taken into consideration after the if or else keywords.
Here as we have to check for x=9 ,
If(x<10) is true (as 9 < 10 is true).As this condition satisfies ,then the immediate condition will be executed.Here the immediate next line is if(y>10) so We have to check this condition.As y>10 is true (since 11>10 is true).Then the immediate statement will get executed.So the statement System.out.println("*****"); will be executed.So else part of this if block will be discarded(System.out.println("#####"); will not be executed).Next Control will come out of the if(x<10) block.So the Statement outside that block will get executed which is System.out.println("$$$$$");
So the final outout of this snippet of code is :
*****
$$$$$
__________________
When x = 11, y = 9;
if (x < 10)
if (y > 10)
System.out.println("*****");
else
System.out.println("#####");
System.out.println("$$$$$");
First we have to check the if condition (x<10) is true or not.As 11<10 which is false.Then the immediate block of code i.e if(y>10) will not be executed.So the statement outside the if(x<10) block is System.out.println("$$$$$");.So it will be executed irrespective of x<10 is true or false.As this statement is outside of the block.
___________________________
Part B:
int x = 9, y = 11;
if (x < 10)
{
if (y > 10)
System.out.println("*********");
}
else
{
System.out.println("######");
System.out.println("$$$$$$$");
}
}
Here first it will check whether x<10 or not.As this is true.Then the statements inside the curly braces will get executed.
So it will check y>10 or not.As this istre.The immediate statement will be executed.(i.e System.out.println("*********");)
After that else block of if(x<10) will not get executed.So the final output is:
******
____________________
int x = 11, y = 9;
if (x < 10)
{
if (y > 10)
System.out.println("*********");
}
else
{
System.out.println("######");
System.out.println("$$$$$$$");
}
Now as the if(x<10) condition fails .Then the code inside the curly braces willl not get executed.So control will jump to the else block.So the code inside the else block will get executed.
System.out.println("######");
System.out.println("$$$$$$$");
These two statements are these with in the curly braces of the else block.So these statements will be executed.
So the final output is :
#####
$$$$$
________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.