Q:

Java find output programs (Data Types) | set 2

belongs to collection: Java find output programs

0

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.

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

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

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

Question 4:

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

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

    System.out.println(num);
  }
}

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

All Answers

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

Answer Question 1:

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;

Answer Question  2:

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;

Answer Question 3:

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.

Answer Question 4:

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.

Answer Question 5:

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.

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