Java find output programs (Data Types) | set 3

Find the output of Java programs | Data Types | Set 3: Enhance the knowledge of Java Data Types 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 num1 = 10;
    int num2 = 20;

    long num3 = 0;

    num3 = num1 + num2 * 10 + Char.SIZE;

    System.out.println(num3);
  }
}

Output:

Main.java:8: error: cannot find symbol
    num3 = num1 + num2 * 10 + Char.SIZE;
                              ^
  symbol:   variable Char
  location: class Main
1 error

Explanation:

The above program will generate syntax error because Char is not any built-in class or type in Java. Here, we need to use Character instead of Char.

Question 2:

public class Main {
  public static void main(String[] args) {
    decimal A = 2.3;
    int B = 3;
    decimal C = 0.0;

    C = A * B - 4;

    System.out.println(C);
  }
}

Output:

Main.java:3: error: cannot find symbol
    decimal A = 2.3;
    ^
  symbol:   class decimal
  location: class Main
Main.java:5: error: cannot find symbol
    decimal C = 0.0;
    ^
  symbol:   class decimal
  location: class Main
2 errors

Explanation:

The above program will generate syntax error because decimal is not any built-in type in Java.

Question 3:

public class Main {
  public static void main(String[] args) {
    char A = 'A';
    int B = 3;
    int C = 0;

    C = (byte)(A) * B - 4;

    System.out.println(C);
  }
}

Output:

191

Explanation:

In the above program, we created a class MainClass that contains a main() method, which is the entry point for the program. In the main() method we created three local variables A, B, and C initialized with 'A', 3, and 0 respectively.

Now evaluate the expression.

C = (byte)(A) *B-4;
C = (byte)(’A’)*3-4;
C = 65*3-4;
C = 195-4;
C = 191;

Here we typecast value of 'A' into byte, the ASCII value of 'A' is 65, and then finally print the value of variable C on the console screen.

Question 4:

public class Main {
  public static void main(String[] args) {
    char A = 'A';
    int C = 0;

    String val = "123";

    C = (byte)(A) * Integer.parseInt(val) - 4;

    System.out.println(C);
  }
}

Output:

7991

Explanation:

In the above program, we created a class Main that contains a main() method, which is the entry point for the program. In the main() method we created three local variables A, C, and val initialized with 'A', 0, and "123" respectively.

Now evaluate the expression,

C = (byte)(A) *Integer.parseInt(val)-4;
C = 65 * 123-4;
C = 7995-4;
C = 7991;

Here, we typecast value of 'A' into byte, the ASCII value of 'A' is 65 and convert string "123" into integer number 123 using parseInt() method of Integer class and then finally print the value of variable C on the console screen.

Question 5:

public class Main {
  public static void main(String[] args) {
    char A = 'A';
    double C = 0;

    String val = "123.34";

    C = (byte)(A) * Double.parseDouble(val) - 4;

    System.out.println(C);
  }
}

Output:

8013.1

Explanation:

In the above program, we created a class Main that contains a main() method, which is the entry point for the program. In the main() method we created three local variables A, C, and val initialized with 'A', 0, and "123.34" respectively.

Now evaluate the expression,

C = (byte)(A) *Double.parseDouble(val)-4;
C = 65 * 123.34-4;
C = 8017-4;
C = 8013.1;

Here, we typecast value of 'A' into byte, the ASCII value of 'A' is 65 and convert string "123.34" into double number 123.34 using parseDouble() method of Double class and then finally print the value of variable C on the console screen.






Comments and Discussions!

Load comments ↻






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