Home »
Java »
Java find output programs
Java find output programs (Operators) | set 3
Find the output of Java programs | Operators | Set 3: Enhance the knowledge of Java Operators concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on January 29, 2021
Question 1:
public class Main {
public static void main(String[] args) {
boolean ret = false;
int a = 20;
double pi = 3.14;
ret = (a == 20) && (3.14F == pi);
System.out.println(ret);
}
}
Output:
false
Explanation:
In the above program, we created a class Main that contains the main() method. The main() method is the entry point of the program. Here, we created three variables ret, a, and pi, that are initialized with "false", "20", and "3.14" respectively.
ret = (a==20)&&(3.14F==pi);
In the above expression, first condition (a==20) will be true, but the second condition (3.14F==pi) will false, because pi is the double type and 3.14F is float type. Both are not equivalent to each other in Java.
Then the final value of ret that is false will be printed on the console screen.
Question 2:
public class Main {
public static void main(String[] args) {
boolean ret = false;
int a = 20;
int b = 0;
String str = "www.includehelp.com";
ret = (a == 20) || ((b >= str.length()) ? true: false);
System.out.println(ret);
}
}
Output:
true
Explanation:
In the above program, we created a class Main that contains the main() method. The main() method is the entry point of the program. Here, we created four variables ret, a, b, and str that are initialized with false, 20, 0, and www.includehelp.com respectively.
ret = (a==20)||((b>=str.length())?true:false);
ret = True||((b>=str.length())?true:false);
Let's evaluate the expression,
In case of "||" operator, if first condition is True the second will not check. So finally True is assigned to variable ret and will be printed on the console screen.
Question 3:
public class Main {
public static void main(String[] args) {
boolean ret = true;
System.out.println(ret &= true);
}
}
Output:
true
Explanation:
In the above program, we created a class Main that contains the main() method. The main() method is the entry point of the program. Here, we created a variable ret which is initialized with true.
System.out.println(ret&=true);
Let's evaluate the expression used in println() method,
ret &=true;
ret =ret &true;
ret =true &true;
ret = true;
Then finally true will be printed on the console screen.
Question 4:
public class Main {
public static void main(String[] args) {
System.out.println(typeof(boolean));
System.out.println(typeof(short));
}
}
Output:
Main.java:3: error: '.class' expected
System.out.println(typeof(boolean));
^
Main.java:4: error: '.class' expected
System.out.println(typeof(short));
^
2 errors
Explanation:
The above program will generate a syntax error because the typeof() method or operator does not exist in Java.
Question 5:
public class Main {
public static void main(String[] args) {
int X = (1, 2, 3, 4, 5);
System.out.println(X);
}
}
Output:
Main.java:3: error: ')' expected
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: expected
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: illegal start of expression
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: ';' expected
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: illegal start of expression
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: ';' expected
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: illegal start of expression
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: ';' expected
int X = (1, 2, 3, 4, 5);
^
Main.java:3: error: illegal start of expression
int X = (1, 2, 3, 4, 5);
^
9 errors
Explanation:
The above program will generate errors because we cannot use the comma ',' operator for value assignment like this. We use this kind of assignment in C and C++ languages.