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.
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Answer Question 1:
Output:
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.
Answer Question 2:
Output:
Explanation:
The above program will generate syntax error because decimal is not any built-in type in Java.
Answer Question 3:
Output:
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.
Answer Question 4:
Output:
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.
Answer Question 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer