Home »
Java »
Java find output programs
Java find output programs (Data Types) | set 1
Find the output of Java programs | Data Types | Set 1: 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 MainClass {
public static void main(String[] args) {
int Len = 0;
int A = 100;
Len = sizeof(int);
System.out.println("Len : " + Len);
Len = sizeof(A);
System.out.println("Len : " + Len);
}
}
Output
MainClass.java:6: error: '.class' expected
Len = sizeof(int);
^
1 error
Explanation
The above program will generate syntax error because sizeof() operator is not exist in Java.
Question 2
public class MainClass {
public static void main(String[] args) {
System.out.println(Long.SIZE);
System.out.println(Double.SIZE);
System.out.println(Integer.SIZE);
}
}
Output
64
64
32
Explanation
In the above program, we created a class MainClass that contains a main() method, which is the entry point for the program. Here, we used SIZE constant of Long, Double, and Integer class. The SIZE constant contains the size of specified class in bits. The size of Long is 64 it means 8 bytes.
Here, we used the println() method to print the values on the console screen.
Question 3
public class MainClass {
public static void main(String[] args) {
Long A = 234;
Double PI = 3.14;
System.out.println(A.SIZE);
System.out.println(PI.SIZE);
}
}
Output
MainClass.java:3: error: incompatible types: int cannot be converted to Long
Long A = 234;
^
1 error
Explanation
The above program will generate syntax error because we cannot use SIZE constant with variables.
Question 4
public class Main {
public static void main(String[] args) {
unsigned short A = 234;
int B = 254;
int C = 0;
C = A * 10 + B - A;
System.out.println(C);
}
}
Output
Main.java:3: error: not a statement
unsigned short A = 234;
^
Main.java:3: error: ';' expected
unsigned short A = 234;
^
2 errors
Explanation
The above program will generate syntax error because unsigned short is not built-in data type in java.
Question 5
public class Main {
public static void main(String[] args) {
short A = 234;
int B = 254;
int C = 0;
C = A * 10 + B - A;
System.out.printf("C : %d", C);
}
}
Output
C : 2360
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, B, and C initialized with 234, 254, and 0 respectively.
Now evaluate the expression:
C = A*10+B-A;
C = 234*10+254-234;
C = 2340+254-234;
C = 2360
Here, we used printf() method to print the value of C in formatted manner.