Java Variables & Data Types
Test your understanding of Java primitive types, variables, type casting, and memory basics.
Questions in this quiz
20 questions on Java Variables & Data Types. Work through them here, then start the quiz to answer them and see your score.
1.What is the default value of an int instance variable in Java?
- A0
- Bundefined
- C-1
- Dnull
2.Which of the following is NOT a primitive data type in Java?
- Aint
- Bchar
- CString
- Dboolean
3.How many bytes does a long data type use in Java?
- A2
- B8
- C16
- D4
4.What will int x = 5 / 2 give you in Java?
- A3
- B2
- CCompilation error
- D2.5
5.Which keyword makes a variable's value impossible to change after it is set?
- Aconst
- Bfinal
- Cfixed
- Dstatic
6.What is the size in bits of a char in Java?
- A16
- B64
- C8
- D32
7.What is autoboxing in Java?
- AAutomatically converting a primitive to its wrapper class object
- BAutomatically resizing arrays
- CAutomatically sorting values
- DAutomatically importing packages
8.What is the range of the byte data type in Java?
- A-128 to 127
- B-256 to 255
- C0 to 127
- D0 to 255
9.What does this code print?
int a = 5; int b = a++; System.out.println(b);- ACompilation error
- B4
- C5
- D6
10.What happens when you add an int and a double in Java?
- AThe result is an int
- BThe result is a long
- CCompilation error
- DThe result is a double
11.Which of these correctly declares a double variable with value 5?
- Adouble x = 5;
- BNeither works
- CBoth A and B work
- Ddouble x = 5.0;
12.What does this code print?
System.out.println(0.1 + 0.2);- A0.30
- B0.3
- CCompilation error
- D0.30000000000000004
13.What is the var keyword introduced in Java 10?
- AA keyword for constants
- BShort for "variable"
- CA new primitive data type
- DLocal variable type inference
14.What is the default value of a boolean instance variable?
- Atrue
- Bnull
- C0
- Dfalse
15.What does this code print?
int x = 10; x += 5; x *= 2; System.out.println(x);- A20
- B30
- C15
- D25
16.Which literal suffix tells Java a number is a long and not an int?
- A100f
- B100L
- C100
- D100.0
17.What does type casting mean in Java?
- ASending a value to a printer
- BImporting a library
- CDeclaring a new type
- DConverting one data type into another
18.What is the output of this code?
char c = 'A'; System.out.println(c + 1);- AB
- BA1
- C66
- DCompilation error
19.What is the difference between float and double in Java?
- Afloat is for whole numbers, double is for decimals
- Bfloat uses 4 bytes, double uses 8 bytes
- CThere is no difference
- Dfloat is more precise than double
20.What is the output?
String s = "Hello"; s = s + " World"; System.out.println(s.length());- A5
- B11
- C12
- D10