Java Data Types Coding Questions and Answers

Welcome to the Java Data Types Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of Java Data Types. Each question has a correct and brief explanation.

1. What is the output of the following Java code snippet?

char ch = 'A';
int num = ch;
System.out.println(num);
a) 65
b) A
c) Compilation error
d) Runtime error

Answer:

a) 65

Explanation:

In Java, char values are stored as Unicode characters. The Unicode value of 'A' is 65, so when ch is implicitly cast to an int, it displays 65.

2. What does this Java code snippet output?

double value = 9.78;
int number = (int) value;
System.out.println(number);
a) 9.78
b) 9
c) 10
d) Compilation error

Answer:

b) 9

Explanation:

Casting a double to an int truncates the decimal part. Therefore, 9.78 becomes 9.

3. Identify the output of the following code:

byte b1 = 127;
byte b2 = -128;
System.out.println(b1 + " " + b2);
a) 127 -128
b) 128 -129
c) Compilation error
d) Runtime error

Answer:

a) 127 -128

Explanation:

The byte data type in Java has a range from -128 to 127. Thus, b1 is 127 and b2 is -128.

4. What will be printed by this Java code?

float f = 100.001f;
double d = f;
System.out.println(d);
a) 100.001
b) 100.0
c) 100.001000046
d) Compilation error

Answer:

c) 100.001000046

Explanation:

When a float is assigned to a double, precision may be added. float has less precision than double.

5. What does this code snippet output?

long l = 100L;
float f = l;
System.out.println(f);
a) 100
b) 100.0
c) 100L
d) Compilation error

Answer:

b) 100.0

Explanation:

The long value is implicitly cast to a float. float is a floating-point type, so the output is 100.0.

6. What is the result of executing this code?

boolean flag = false;
if (!flag) {
    System.out.println("False");
} else {
    System.out.println("True");
}
a) False
b) True
c) Compilation error
d) Runtime error

Answer:

a) False

Explanation:

The ! operator negates the boolean value. Since flag is false, !flag is true, so "False" is printed.

7. What will the following Java code snippet output?

short s = 10;
s = (short) (s + s);
System.out.println(s);
a) 10
b) 20
c) Compilation error
d) Runtime error

Answer:

b) 20

Explanation:

The expression s + s results in an int type. It must be cast back to short to be reassigned to s. The value of s becomes 20.

8. What does the following code snippet print?

String str = "12345";
int num = Integer.parseInt(str);
System.out.println(num);
a) 12345
b) "12345"
c) Compilation error
d) Runtime error

Answer:

a) 12345

Explanation:

Integer.parseInt converts the String "12345" to the int 12345.

9. Determine the output of this Java code:

int a = '2' - '0';
System.out.println(a);
a) 2
b) 50
c) 32
d) Compilation error

Answer:

a) 2

Explanation:

In Java, char values are stored as Unicode values. The Unicode value of '2' is 50 and '0' is 48. Thus, 50 - 48 equals 2.

10. What is the result of the following code snippet?

double d1 = 0.1;
double d2 = 0.2;
double sum = d1 + d2;
System.out.println(sum);
a) 0.3
b) 0.30000000000000004
c) 0.30000000000000003
d) 0.29999999999999999

Answer:

b) 0.30000000000000004

Explanation:

Floating-point arithmetic in Java can lead to precision errors. Here, adding 0.1 and 0.2 results in a minor precision error, producing 0.30000000000000004 instead of the exact 0.3.

Comments