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);
}
}
Answer Question 1:
Output:
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:
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:
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:
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:
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