Java find output programs (Operators) | set 1

Find the output of Java programs | Operators | Set 1: 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) {
    int a = 10;
    int b = 20;
    int c = 0;

    c = ++a + ++b * 2 + a++;

    System.out.println(a + "," + b + "," + c);
  }
}

Output:

12,21,64

Explanation:

In the above program, we created three integer variables a, b, and c initialized with 10, 20, and 0 respectively. Here, we used pre and post-increment operators with variable 'a' and 'b'. The pre-increment operator will evaluate before the expression and post-increment operator will evaluate after the expression.

The value of variable 'a' will be 11 and the value of 'b' will be 21 because of pre-increment operator. Let's evaluate the expression,

c = ++a + ++b * 2+a++;

Now remove all pre and post-increment operators from the expression,

c = a +b * 2+a;
c = 11+21*2+11;
c = 11+42+11’
c = 64;

Then post increment operator will evaluate for variable a then the final value of a will be 22.

Question 2:

public class Main {
  public static void main(String[] args) {
    double Z = 0.0;
    float X = 3.5F;

    Z = (X * 10 - 23) * Math.pow(2, 0);

    System.out.println(Z);
  }
}

Output:

12.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 two local variables Z and X initialized with 0.0 and 3.5F respectively.

Now evaluate the expression:

Z = (X*10-23)*Math.pow(2,0);
Z = (3.5*10-23)*1;
Z = (35.0-23)*1;
Z = (12.0)*1;
Z = 12.0 

Question 3:

public class Main {
  public static void main(String[] args) {
    int Z = 0;

    Z += (Float.SIZE / 8) + (Math.pow(2, 3) % 3);

    System.out.println(Z);
  }
}

Output:

6

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 local variables Z initialized with 0.

Now evaluate the expression:

Z += (Float.SIZE/8)+ (Math.pow(2,3)%3);
Z += (32/8)+(8%3);
Z += (4)+(2);
Z += 6;
Z = Z + 6;
Z = 0 + 6;
Z = 6;

Question 4:

public class Main {
  public static void main(String[] args) {
    int Z = 10;

    Z += (Char.SIZE / 8) + (Math.pow(2, 3) % 3);

    System.out.println(Z);
  }
}

Output:

Main.java:5: error: cannot find symbol
    Z += (Char.SIZE / 8) + (Math.pow(2, 3) % 3);
          ^
  symbol:   variable Char
  location: class Main
1 error

Explanation:

The above program will generate syntax error because Char is not a built-in class in Java.

Question 5:

public class Main {
  public static void main(String[] args) {
    const float A = 4.23F;
    float B = 3.23F;
    float Z = 2;

    Z *= ++B + A % B;

    System.out.println(Z);
  }
}

Output:

Main.java:3: error: illegal start of expression
    const float A = 4.23F;
    ^
1 error

Explanation:

The above program will generate syntax error because of const, to define a constant in java we need to use final keyword instead of const.






Comments and Discussions!

Load comments ↻






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