Primitive data types are the most basic data types built into the Java language. They store simple values like numbers, characters, and true/false values directly in memory.
Click to reveal answer
beginner
Name the 8 primitive data types in Java.
The 8 primitive data types are: byte, short, int, long, float, double, char, and boolean.
Click to reveal answer
beginner
What is the difference between int and long in Java?
int stores 32-bit integer values, while long stores 64-bit integer values. Use long when you need to store bigger whole numbers.
Click to reveal answer
beginner
Which primitive data type would you use to store a single character?
Use the char data type to store a single character, like a letter or symbol. It uses 16 bits and stores Unicode characters.
Click to reveal answer
beginner
What values can a boolean hold in Java?
A boolean can hold only two values: true or false. It is used to represent simple yes/no or on/off states.
Click to reveal answer
Which of these is NOT a primitive data type in Java?
AString
Bint
Cboolean
Dchar
✗ Incorrect
String is not a primitive type; it is a class in Java. The others are primitive data types.
What is the size of a float in Java?
A64 bits
B8 bits
C16 bits
D32 bits
✗ Incorrect
A float uses 32 bits to store decimal numbers.
Which primitive type would you use to store the value true?
Aint
Bchar
Cboolean
Ddouble
✗ Incorrect
boolean stores true or false values.
What is the default value of an int variable in Java?
Anull
B0
Cfalse
D1
✗ Incorrect
The default value of an int is 0 when declared as a class member.
Which primitive type can store the largest range of whole numbers?
Along
Bshort
Cint
Dbyte
✗ Incorrect
long stores 64-bit integers, which is the largest range among integer primitives.
Explain what primitive data types are in Java and list all of them.
Think about simple values like numbers and true/false.
You got /2 concepts.
Describe when you would use int versus long in Java.
Consider the size in bits and the range of values.
You got /2 concepts.
Practice
(1/5)
1. Which of the following is NOT a primitive data type in Java?
easy
A. char
B. boolean
C. int
D. String
Solution
Step 1: Recall Java primitive types
Java primitive types include int, boolean, char, byte, short, long, float, and double.
Step 2: Identify non-primitive type
String is a class in Java, not a primitive type.
Final Answer:
String -> Option D
Quick Check:
Primitive types exclude classes like String [OK]
Hint: Remember primitives are simple types, not objects like String [OK]
Common Mistakes:
Confusing String as primitive
Thinking boolean is not primitive
Mixing wrapper classes with primitives
2. Which of the following is the correct way to declare a variable of type byte in Java?
easy
A. byte b = 127;
B. byte b = 128;
C. byte b = 'a';
D. byte b = 128L;
Solution
Step 1: Understand byte range
byte stores values from -128 to 127 inclusive.
Step 2: Check each option
byte b = 128; uses 128 which is out of range; byte b = 127; uses 127 which is valid; byte b = 'a'; assigns a char which is not directly compatible; byte b = 128L; uses long literal 128L which is invalid for byte.
Final Answer:
byte b = 127; -> Option A
Quick Check:
byte max value = 127 [OK]
Hint: byte max is 127, so use values within -128 to 127 [OK]
Common Mistakes:
Assigning values outside byte range
Using char or long literals directly
Not knowing byte limits
3. What is the output of the following Java code?
int x = 5;
float y = 2.0f;
System.out.println(x + y);
medium
A. 7.0
B. 7.0f
C. 7
D. 7.0d
Solution
Step 1: Understand type promotion in expression
int x is promoted to float when added to float y.
Step 2: Calculate the sum and output type
5 + 2.0f = 7.0 (float), but println prints float as decimal number without suffix.
Final Answer:
7.0 -> Option A
Quick Check:
int + float = float output 7.0 [OK]
Hint: int + float results in float decimal output [OK]
Common Mistakes:
Expecting integer output
Confusing float suffix in output
Ignoring type promotion rules
4. Identify the error in the following Java code snippet:
char c = 'AB';
medium
A. char cannot be assigned a value directly
B. Missing semicolon at end
C. char can only hold a single character
D. char must be initialized with a number
Solution
Step 1: Check char assignment rules
char stores a single 16-bit Unicode character, so it must be assigned a single character in single quotes.
Step 2: Analyze the given code
'AB' is two characters, which is invalid for char type.
Final Answer:
char can only hold a single character -> Option C
Quick Check:
char = single character only [OK]
Hint: char holds exactly one character in single quotes [OK]
Common Mistakes:
Assigning multiple characters to char
Confusing char with String
Forgetting single quotes for char
5. You want to store a large whole number that is bigger than int can hold. Which primitive type should you use in Java?
hard
A. short
B. long
C. float
D. double
Solution
Step 1: Recall int and long ranges
int holds up to about 2 billion (2^31-1), long holds much larger values (2^63-1).
Step 2: Choose type for large whole number
long is the correct primitive type for large integers beyond int range; float and double are for decimals, short is smaller than int.
Final Answer:
long -> Option B
Quick Check:
Use long for large integers beyond int [OK]
Hint: Use long for big whole numbers beyond int range [OK]
Common Mistakes:
Using float/double for whole numbers
Choosing short which is smaller
Not knowing long size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.