Q:

Java find output programs (Operators) | set 2

belongs to collection: Java find output programs

0

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

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

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

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

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

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

All Answers

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

Answer Question 1:

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.

Answer Question 2:

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.

Answer Question 3:

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.

Answer Question 4:

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.

Answer Question 5:

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

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 (Operators) | set 3... >>
<< Java find output programs (Operators) | set 1...