Q:

Java find output programs (Operators) | set 3

belongs to collection: Java find output programs

0

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.

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);
  }
}

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);
  }
}

Question 3:

public class Main {
  public static void main(String[] args) {
    boolean ret = true;

    System.out.println(ret &= true);
  }
}

Question 4:

public class Main {
  public static void main(String[] args) {
    System.out.println(typeof(boolean));
    System.out.println(typeof(short));
  }
}

Question 5:

public class Main {
  public static void main(String[] args) {
    int X = (1, 2, 3, 4, 5);

    System.out.println(X);
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

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.

Answer Question 2:

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.

Answer Question 3:

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.

Answer Question 4:

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.

Answer Question 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (if else) | set 1... >>
<< Java find output programs (Operators) | set 2...