Java Variables, Data Types & Type Casting Quiz
40 focused questions Java primitive and reference types, variable declaration rules, widening/narrowing conversions, the final keyword, and the most common type-casting pitfalls.
Question 1
How many primitive data types does Java have?
Question 2
What is the default value of an instance int variable?
Question 3
What is the output of this code?
byte b = 127;
b = (byte)(b + 1);
System.out.println(b);Question 4
Which of these is a reference type?
Question 5
What happens when this code is compiled?
float f = 3.14;Question 6
What does the final keyword do when applied to a primitive variable?
Question 7
What is the output?
long l = 100L;
int i = (int) l;
System.out.println(i);Question 8
Which statement about var is true?
Question 9
What is the result of this autoboxing/unboxing example?
Integer a = 1000;
Integer b = 1000;
System.out.println(a == b);Question 10
What does final do when applied to a reference variable?
Question 11
What is the output?
char c = 'A';
int i = c + 1;
System.out.println((char)i);Question 12
Which conversion requires an explicit cast?
Question 13
What happens here?
double d = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
System.out.println(d == 1.0);Question 14
What is the default value of a boolean instance variable?
Question 15
What is the result of this compound assignment?
byte b = 10;
b += 1000;
System.out.println(b);Question 16
Which of these declarations is invalid?
Question 17
What is printed?
final StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);Question 18
What is the size of a long variable?
Question 19
What is the output of this unboxing null scenario?
Integer i = null;
int j = i;
System.out.println(j);Question 20
Which cast will compile successfully?
Number n = Integer.valueOf(42);
Double d = (Double) n;Question 21
Can a local variable be declared final?
Question 22
What is the range of byte?
Question 23
What is printed?
int x = 10;
double d = x;
System.out.println(d);Question 24
Which literal represents a long value?
Question 25
What is the output of this classic precision loss example?
int i = 123456789;
float f = i;
int back = (int) f;
System.out.println(i - back);Question 26
Can a final variable be a blank final (uninitialized at declaration)?
Question 27
What is printed?
boolean b = false;
if (b = true) {
System.out.println("True");
} else {
System.out.println("False");
}Question 28
Which of these is NOT a widening conversion?
Question 29
What is the output?
final int[] arr = {1, 2, 3};
arr[0] = 99;
System.out.println(arr[0]);Question 30
Why does this fail to compile?
var x = 10;
x = "hello";Question 31
What is the literal type of 0b1010?
Question 32
What is printed?
System.out.println(0.1 + 0.2 == 0.3);Question 33
Which wrapper class has a cache larger than -128 to 127 by default?
Question 34
What is the output?
char c = 65;
System.out.println(c);Question 35
Why does this compile without error?
short s = 100;
s += 500;
System.out.println(s);Question 36
What is the default value of a reference type instance variable?
Question 37
What is printed?
final var list = java.util.List.of(1, 2, 3);
// list.add(4);
System.out.println(list);Question 38
Which of these assignments requires a cast?
Question 39
What is the result?
Object obj = "Hello";
String s = (String) obj;
System.out.println(s.toUpperCase());Question 40
Why is understanding primitive vs reference types crucial in Java?
