Java find output programs (Operators) | set 2

Find the output of Java programs | Operators | Set 2: 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) {
    final float A = 4.23F;
    float B = 3.23F;
    float Z = 2;

    Z *= ++B + A % B;

    System.out.println(Z);
  }
}

Output:

8.46

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 constant A with initial value 4.23F and two local variables B and Z initialized with 3.23F and 2 respectively.

Let's evaluate the expression,

Z *= ++B+A%B;

Now, evaluate pre increment operator then value of B will be 4.23F,

Z *= B+A%B;
Z *= 4.23+4.23%4.23;
Z *= 4.23+0;
Z = Z *4.23;
Z = 2 *4.23;
Z = 8.46;

Then the final value of Z will print on the console screen.

Question 2:

public class Main {
  public static void main(String[] args) {
    float A = 3.14F;
    double B = 3.14;

    int Z = 0;

    Z = (A == B);

    System.out.println(Z);
  }
}

Output:

Main.java:8: error: incompatible types: boolean cannot be converted to int
    Z = (A == B);
           ^
1 error

Explanation:

The above program will generate syntax error because the relational operator returns Boolean values in java. Here, we used relation operator "==" to check equality.

Z = (A == B);

In the above expression, Z is an integer variable, and here we assigned a Boolean value to Z, then it will generate an error.

Question 3:

public class Main {
  public static void main(String[] args) {
    float A = 3.14F;
    double B = 3.14;

    (A == B) ? System.out.println("Hello") : System.out.println("Hiiii");
  }
}

Output:

Main.java:6: error: not a statement
    (A == B) ? System.out.println("Hello") : System.out.println("Hiiii");
             ^
1 error

Explanation:

The above program will generate syntax error, because conditional operator required LVALUE for assignment.

Question 4:

public class Main {
  public static void main(String[] args) {
    float A = 3.14F;
    double B = 3.14;
    int ret = 0;

    ret = (A == B) ? 1 : 0;

    System.out.println(ret);
  }
}

Output:

0

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 A, B, and ret that are initialized with "3.14F", "3.14", and 0 respectively. Here, data type of A is float and data type of B is double then (A==B) condition will false and 0 assigned to variable ret. So finally '0' will print on the console screen.

Question 5:

public class Main {
  public static void main(String[] args) {
    boolean ret = false;
    int a = 0;
    double pi = 3.14;

    ret = (a = 20) && (3.14F == pi);

    System.out.println(ret);
  }
}

Output:

Main.java:7: error: bad operand types for binary operator '&&'
    ret = (a = 20) && (3.14F == pi);
                   ^
  first type:  int
  second type: boolean
1 error

Explanation:

The above program will generate syntax error because in the below expression we used assignment operator "=" instead of "equal to" operator "==", that why 20 is assigned to the variable a, and we know that we cannot apply "&&" operator between 'int' and 'boolean' operands,

ret = (a=20)&&(3.14F==pi);




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.