Find the output of Java programs | if else | Set 3: Enhance the knowledge of Java if else concepts by solving and finding the output of some Java programs.
Question 1:
public class Main {
public static void main(String[] args) {
int X = 20;
int Y = 10;
int Z = 30;
if (X > Y && X == 10) System.out.println("ABC");
else if (Z > Y && Z = 20) System.out.println("PQR");
else if (Z == 30 && Z == 10) System.out.println("XYZ");
}
}
Question 2:
public class Main {
public static void main(String[] args) {
int A = 20;
float B = 20.0F;
if (sizeof(A) == sizeof(B))
System.out.println("Hello");
else
System.out.println("Hiii");
}
}
Question 3:
public class Main {
public static void main(String[] args) {
double X = 20.23;
float Y = 20.23F;
if (X > Y) System.out.println("India");
else System.out.println("Australia");
}
}
Question 4:
public class Main {
public static void main(String[] args) {
String STR1 = "INDIA";
String STR2 = "india";
if (STR1 > STR2) System.out.println("WWW.INCLUDEHELP.COM");
else System.out.println("www.includehelp.com");
}
}
Question 5:
public class Main {
public static void main(String[] args) {
String ABC = "INDIA";
String XYZ = "india";
if (ABC == XYZ) System.out.println("WWW.INCLUDEHELP.COM");
else System.out.println("www.includehelp.com");
}
}
Answer Question 1:
Output:
Explanation:
The above program will generate a syntax error.
else if(Z>Y && Z=20)
In the above condition, we used the assignment operator "=" instead of equal to "==" operator. Then it will return an integer value that cannot be used as an operand with the logical end "&&" operator.
Answer Question 2:
Output:
Explanation:
The above program will generate syntax errors because the sizeof() operator does not exist in java.
Answer Question 3:
Output:
Explanation:
The above program will print "India" on the console screen. Because, X is the double type and Y is float type. In Java, a double number is always treated greater in case of equality. That's why the if condition will true and print "Hello" on the console screen.
Answer Question 4:
Output:
Explanation:
The above program will generate syntax error because we cannot use greater than the operator with string operands.
Answer Question 5:
Output:
Explanation:
The above program will print "www.includehelp.com" on the console screen. Because, ABC and XYZ do not contain the same values. That's why condition will false and else part will execute then it will print "www.includehelp.com" on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer