Java find output programs (Data Types) | set 2

Find the output of Java programs | Data Types | Set 2: 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) {
    float A = 2.3;
    int B = 3;
    float C = 0.0;

    C = A * B - 4;

    System.out.printf("C : %f", C);
  }
}

Output:

Main.java:3: error: incompatible types: possible lossy conversion from double to float
    float A = 2.3;
              ^
Main.java:5: error: incompatible types: possible lossy conversion from double to float
    float C = 0.0;
              ^
2 errors

Explanation:

The above program will generate syntax errors because here we assigned double values to local variables A and C.  By default a floating-point number is double type, here we need to use character 'F' in suffix to represent number of float type.

The correct way is given below:

float A=2.3F;
float C=0.0F;

Question 2:

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

    C = A * B - 4;

    System.out.println(C);
  }
}

Output:

Main.java:7: error: incompatible types: possible lossy conversion from float to int
    C = A * B - 4;
              ^
1 error

Explanation:

The above program will generate a syntax error because of below statement,

C = A*B-4;

In the above statement after evaluating the expression result will be float type but we are assigning result to the integer variable C.

The correct expression will be:

C = (int)A*B-4;

Question 3:

public class Main {
  public static void main(String[] args) {
    byte A = 10;
    byte B = 30;
    byte C = 0;

    C = A * B;

    System.out.println(C);
  }
}

Output:

Main.java:7: error: incompatible types: possible lossy conversion from int to byte
    C = A * B;
          ^
1 error

Explanation:

The above program will generate syntax error. In Java variable of byte type occupies 1 byte in memory. The maximum value of byte type variable is 255.

C = A*B;

In the above expression multiplication of 10 and 30 will be 300 that cannot be assigned in variable C because C is byte type.

Question 4:

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

    num = System.out.printf("Hello World");

    System.out.println(num);
  }
}

Output:

Main.java:5: error: incompatible types: PrintStream cannot be converted to int
    num = System.out.printf("Hello World");
                           ^
1 error

Explanation:

The above program will generate error because return type of printf() method is PrintStream that cannot be converted into integer value.

Question 5:

public class Main {
  public static void main(String[] args) {
    long int num1 = 10;
    short int num2 = 20;

    long num3 = 0;

    num3 = num1 + num2 * 10 + 20;

    System.out.println(num3);
  }
}

Output:

Main.java:3: error: not a statement
    long int num1 = 10;
    ^
Main.java:3: error: ';' expected
    long int num1 = 10;
        ^
Main.java:4: error: not a statement
    short int num2 = 20;
    ^
Main.java:4: error: ';' expected
    short int num2 = 20;
         ^
4 errors

Explanation:

The above program will generate syntax errors because long int and short int cannot not be used in Java. If we want to  declare a variable of short type then we need to use short data type instead of short int.





Comments and Discussions!

Load comments ↻





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