Which are printed out?public static boolean gtr(int num) { System.out.print("gtr"); return num > 3;}public static boolean lss(int num) { System.out.print("lss"); return num < 5;}public static void main(String[] args) { if (gtr(5) && lss(3)) { System.out.print("True"); } else { System.out.print("False"); } }
Question
Which are printed out?public static boolean gtr(int num) { System.out.print("gtr"); return num > 3;}public static boolean lss(int num) { System.out.print("lss"); return num < 5;}public static void main(String[] args) { if (gtr(5) && lss(3)) { System.out.print("True"); } else { System.out.print("False"); } }
Solution
The output will be "gtrFalse".
Here's the step by step explanation:
-
The main method is called first. Inside the main method, there is an if statement with two conditions: gtr(5) and lss(3).
-
The gtr(5) method is called first. Since 5 is greater than 3, the method prints "gtr" and returns true.
-
Because the && operator is short-circuit, the second condition (lss(3)) is only checked if the first condition is true. In this case, the first condition is true, so the lss(3) method is called next.
-
The lss(3) method checks if 3 is less than 5. It is, so the method prints "lss" and returns true.
-
Now, both conditions of the if statement have been checked. Because both are true, the if statement's body is executed, and "True" is printed.
-
However, the lss(3) method returns false because 3 is not less than 5. So, the else part of the if statement is executed and "False" is printed.
So, the final output is "gtrFalse".
Similar Questions
What will be the output of the following code ?public static void print(int n){ if(n < 0){ return; } if(n == 0){ System.out.println(n); return; } print(n--); System.out.print(n+" ");}public static void main(String[] args) { int num = 3; print(num);
What is the output of the below program?public class Main{ public static void main(String[] args) { int a = 25; if(a > 5) System.out.print("Hi "); if(a < 20) System.out.print("Hello "); else System.out.print("Know Program "); }}
What will be the output of the following Java program?class mainclass { public static void main(String args[]) { boolean ct1 = true; boolean ct2 = false; if (ct1) System.out.println(ct1); else System.out.println(ct2); } }Options10truefalse
What is the output of the following code?public class Test { public static void main(String args[]) { int x = 10; System.out.print(x + " "); } static { int x = 20; System.out.print(x + " "); }}Options: Pick one correct answer from below10 2020 1010 1020 20
What will be the output of the following code?public class Solution { public static void main(String args[]) { int i = 5; switch(i) { case 1: System.out.print("Case 1" + " "); break; case 2: System.out.print("Case 2" + " "); break; case 3: System.out.print("Case 3" + " "); break; default: System.out.print("Value of i is greater than 3" + " "); } } }Options: Pick one correct answer from belowCase 1Case 2Case 3Value of i is greater than 3PrevSubmit MCQNext
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.