1. What output will be produced by the following code?
int extra = -37; if (extra < 0) System.out.println("small"); else if (extra == 0) System.out.println("medium"); else System.out.println("large")
a. large |
||
b. medium |
||
c. small |
||
d. nothing |
answer is c) small,because extra is -37 first it checks condition in "if" and checks extra<0 if it's true and print small.
extra -37<0 so it prints small and do not go to else if and else.so program ends
program is:
class y{
public static void main(String args[]){
int extra = -37;
if (extra < 0)
System.out.println("small");
else if (extra == 0)
System.out.println("medium");
else
System.out.println("large"); // here if
semicolon missing it gives error.
}
}
Output:
small
Get Answers For Free
Most questions answered within 1 hours.