Java Variables, Data Types & Type Casting Quiz

Java
0 Passed
0% acceptance

40 focused questions Java primitive and reference types, variable declaration rules, widening/narrowing conversions, the final keyword, and the most common type-casting pitfalls.

40 Questions
~80 minutes
1

Question 1

How many primitive data types does Java have?

A
8
B
6
C
9
D
7
2

Question 2

What is the default value of an instance int variable?

A
0
B
null
C
Compilation error if not initialized
D
undefined
3

Question 3

What is the output of this code?

java
byte b = 127;
b = (byte)(b + 1);
System.out.println(b);
A
-128
B
128
C
Compilation error
D
127
4

Question 4

Which of these is a reference type?

A
String
B
int
C
char
D
boolean
5

Question 5

What happens when this code is compiled?

java
float f = 3.14;
A
Compilation error — double literal cannot be assigned to float without suffix or cast
B
Compiles successfully
C
Runtime exception
D
f becomes exactly 3.14
6

Question 6

What does the final keyword do when applied to a primitive variable?

A
Makes the variable immutable — its value cannot be changed after initialization
B
Makes it thread-safe
C
Gives it a default value
D
Only works on local variables
7

Question 7

What is the output?

java
long l = 100L;
int i = (int) l;
System.out.println(i);
A
100
B
Compilation error
C
0
D
100L
8

Question 8

Which statement about var is true?

A
It can only be used for local variables, not fields or parameters
B
It makes the variable dynamically typed
C
It can be used without an initializer
D
It was added in Java 5
9

Question 9

What is the result of this autoboxing/unboxing example?

java
Integer a = 1000;
Integer b = 1000;
System.out.println(a == b);
A
false
B
true
C
Compilation error
D
Runtime exception
10

Question 10

What does final do when applied to a reference variable?

A
The reference cannot point to a different object, but the object itself can be mutated
B
The object becomes completely immutable
C
Only works with primitives
D
Prevents the object from being garbage collected
11

Question 11

What is the output?

java
char c = 'A';
int i = c + 1;
System.out.println((char)i);
A
B
B
66
C
A1
D
Compilation error
12

Question 12

Which conversion requires an explicit cast?

A
int → short
B
byte → int
C
short → long
D
float → double
13

Question 13

What happens here?

java
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);
A
false
B
true
C
Compilation error
D
0.0
14

Question 14

What is the default value of a boolean instance variable?

A
false
B
true
C
null
D
Compilation error
15

Question 15

What is the result of this compound assignment?

java
byte b = 10;
b += 1000;
System.out.println(b);
A
Compilation error without cast
B
1074
C
-106 (overflow)
D
10
16

Question 16

Which of these declarations is invalid?

A
var x;
B
var x = 10;
C
var list = new ArrayList<>();
D
var n = null;
17

Question 17

What is printed?

java
final StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
A
Hello World
B
Hello
C
Compilation error
D
Runtime exception
18

Question 18

What is the size of a long variable?

A
64 bits
B
32 bits
C
Platform dependent
D
16 bits
19

Question 19

What is the output of this unboxing null scenario?

java
Integer i = null;
int j = i;
System.out.println(j);
A
Throws NullPointerException at runtime
B
Prints 0
C
Compilation error
D
Prints null
20

Question 20

Which cast will compile successfully?

java
Number n = Integer.valueOf(42);
Double d = (Double) n;
A
None — throws ClassCastException at runtime
B
Compiles and runs fine
C
Compilation error
D
Only works with primitives
21

Question 21

Can a local variable be declared final?

A
Yes, and it's commonly used with lambdas and effectively final rules
B
No, final is only for fields
C
Only in try-with-resources
D
Only for static variables
22

Question 22

What is the range of byte?

A
-128 to 127
B
0 to 255
C
-127 to 128
D
-256 to 255
23

Question 23

What is printed?

java
int x = 10;
double d = x;
System.out.println(d);
A
10.0
B
10
C
Compilation error
D
10.00
24

Question 24

Which literal represents a long value?

A
100L
B
100
C
100l (lowercase L)
D
100.0
25

Question 25

What is the output of this classic precision loss example?

java
int i = 123456789;
float f = i;
int back = (int) f;
System.out.println(i - back);
A
Some non-zero value (e.g., 1 or -1)
B
0
C
Compilation error
D
123456789
26

Question 26

Can a final variable be a blank final (uninitialized at declaration)?

A
Yes, if it's an instance or static field and initialized in constructor or static block
B
No, final must always have a value at declaration
C
Only for local variables
D
Only in interfaces
27

Question 27

What is printed?

java
boolean b = false;
if (b = true) {
    System.out.println("True");
} else {
    System.out.println("False");
}
A
True
B
False
C
Compilation error
D
No output
28

Question 28

Which of these is NOT a widening conversion?

A
double → float
B
int → long
C
byte → short
D
char → int
29

Question 29

What is the output?

java
final int[] arr = {1, 2, 3};
arr[0] = 99;
System.out.println(arr[0]);
A
99
B
1
C
Compilation error
D
ArrayIndexOutOfBoundsException
30

Question 30

Why does this fail to compile?

java
var x = 10;
x = "hello";
A
var infers int — second assignment is incompatible
B
var is not allowed in methods
C
String must be in quotes
D
var can only be used once
31

Question 31

What is the literal type of 0b1010?

A
int (binary literal)
B
byte
C
long
D
Binary not supported
32

Question 32

What is printed?

java
System.out.println(0.1 + 0.2 == 0.3);
A
false
B
true
C
Compilation error
D
0.3
33

Question 33

Which wrapper class has a cache larger than -128 to 127 by default?

A
None — only Integer caches that range by default
B
Long
C
Boolean
D
Character (0-127)
34

Question 34

What is the output?

java
char c = 65;
System.out.println(c);
A
A
B
65
C
Compilation error
D
null
35

Question 35

Why does this compile without error?

java
short s = 100;
s += 500;
System.out.println(s);
A
Compound assignment operators perform automatic narrowing cast
B
short can hold 600
C
int is promoted to short
D
Compilation error
36

Question 36

What is the default value of a reference type instance variable?

A
null
B
0
C
Compilation error
D
empty object
37

Question 37

What is printed?

java
final var list = java.util.List.of(1, 2, 3);
// list.add(4);
System.out.println(list);
A
[1, 2, 3]
B
Compilation error on var
C
Empty list
D
Runtime exception
38

Question 38

Which of these assignments requires a cast?

A
long l = 1234567890123;
B
int i = 100;
C
float f = 3.14f;
D
double d = 3.14;
39

Question 39

What is the result?

java
Object obj = "Hello";
String s = (String) obj;
System.out.println(s.toUpperCase());
A
HELLO
B
ClassCastException
C
Compilation error
D
null
40

Question 40

Why is understanding primitive vs reference types crucial in Java?

A
It affects memory usage, nullability, performance, and how equality/comparison works
B
Only reference types can be used in collections
C
Primitives cannot be passed to methods
D
Reference types are slower always

QUIZZES IN Java