Practice commonly asked
Data Types interview questions with clear answers and explanations.
30
Interview Questions
Questions & Answers
Data Types Interview Questions
30 Questions
Q 31
Why are wrapper classes immutable?
Medium
Answer
Wrapper objects such as Integer, Long, and Boolean are immutable, meaning their represented value cannot change after construction.
Explanation
Immutability makes wrapper objects safe to share and simplifies their behavior in collections and concurrent code.
Reference:
Java Wrapper Classes API
Q 32
What are wrapper classes in Java?
Easy
Answer
Wrapper classes represent primitive values as objects, such as Integer for int, Long for long, and Boolean for boolean.
Explanation
They are required in APIs that work with objects, including generic collections, and also provide useful conversion and utility methods.
Reference:
Java Wrapper Classes API
Q 33
What is unboxing?
Easy
Answer
Unboxing is the automatic conversion of a wrapper object to its corresponding primitive type.
Explanation
If the wrapper reference is null, unboxing can throw NullPointerException.
Reference:
Java Language Specification
Q 34
What is autoboxing?
Easy
Answer
Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper object, such as int to Integer.
Explanation
It allows primitive values to be used in contexts requiring reference types, such as generic collections.
Reference:
Java Language Specification
Q 35
What is the difference between implicit and explicit casting?
Easy
Answer
Implicit casting occurs automatically for permitted conversions such as int to long, while explicit casting uses a cast expression such as (int) longValue.
Explanation
Explicit casts are required when the conversion may lose information or when reference types need a checked narrowing conversion.
Reference:
Java Language Specification
Q 36
Can a byte variable store the result of byte + byte without casting?
Medium
Answer
No, because the arithmetic expression is evaluated as int. An explicit cast to byte is normally required.
Explanation
The cast should be used carefully because the resulting int may be outside the byte range.
Reference:
Java Language Specification
Q 37
Why does byte + byte produce an int in Java?
Medium
Answer
Binary numeric promotion converts byte operands to int before performing the arithmetic operation.
Explanation
Therefore, assigning the result directly to a byte requires an explicit cast if the result is known to fit.
Reference:
Java Language Specification
Q 38
What is type promotion in arithmetic expressions?
Medium
Answer
Java promotes smaller integral types such as byte, short, and char to int during most arithmetic operations.
Explanation
This means an expression involving two byte values generally has type int rather than byte.
Reference:
Java Language Specification
Q 39
Can char store an integer value in Java?
Medium
Answer
char can store an integer value within its 16-bit unsigned range, and Java permits conversions between char and integral types with the appropriate conversion rules.
Explanation
The numeric value represents a UTF-16 code unit when interpreted as a character.
Reference:
Java Language Specification
Q 40
What is the difference between char and String?
Easy
Answer
char represents one UTF-16 code unit, while String represents an immutable sequence of characters.
Explanation
A String can contain many characters and can also represent Unicode code points that require multiple UTF-16 code units.
Reference:
Java Character and String APIs
Q 41
Why is BigDecimal preferred over double for financial calculations?
Medium
Answer
BigDecimal provides decimal arithmetic with explicit precision and rounding control, while binary floating-point types such as double can represent many decimal fractions only approximately.
Explanation
Financial calculations often require predictable decimal results and controlled rounding, making BigDecimal more appropriate.
Reference:
Java BigDecimal API
Q 42
What is the difference between float and double?
Easy
Answer
float uses 32-bit floating-point representation, while double uses 64-bit representation and provides greater precision.
Explanation
double is normally preferred for general floating-point calculations unless memory or domain constraints favor float.
Reference:
Java Language Specification
Q 43
What is NaN in Java?
Medium
Answer
NaN means Not-a-Number and represents an undefined or unrepresentable floating-point result.
Explanation
NaN has special comparison behavior: it is not equal to itself using ==, so Double.isNaN() should be used to test for NaN.
Reference:
Java Double API
Q 44
What happens when a floating-point value is divided by zero?
Medium
Answer
Floating-point division by positive or negative zero can produce positive or negative infinity, while zero divided by zero produces NaN.
Explanation
These results follow IEEE 754 floating-point semantics rather than throwing ArithmeticException.
Reference:
Java Language Specification
Q 45
What happens when an integer is divided by zero?
Easy
Answer
Integer division by zero throws ArithmeticException.
Explanation
This differs from floating-point division, where division by zero can produce infinity or NaN according to IEEE 754 rules.
Reference:
Java Arithmetic API
Q 46
What is integer division in Java?
Easy
Answer
When both operands are integral types, division performs integer division and discards the fractional part toward zero.
Explanation
For example, 5 / 2 produces 2, while 5.0 / 2 produces 2.5.
Reference:
Java Language Specification
Q 47
What happens when floating-point arithmetic overflows?
Medium
Answer
Floating-point overflow can produce positive or negative infinity rather than wrapping like signed integer arithmetic.
Explanation
IEEE 754 semantics also define special values such as NaN and signed zero.
Reference:
Java Language Specification
Q 48
What happens when an integer value overflows its primitive type?
Medium
Answer
For signed integer arithmetic, overflow wraps around according to the fixed-width two's-complement representation rather than automatically throwing an exception.
Explanation
For example, incrementing Integer.MAX_VALUE produces Integer.MIN_VALUE in ordinary int arithmetic.
Reference:
Java Language Specification
Q 49
Why does assigning a long to int require casting?
Easy
Answer
A long has a wider range than int, so converting it to int may lose information.
Explanation
Java requires an explicit cast to make this potentially lossy conversion visible in the code.
Reference:
Java Language Specification
Q 50
Why can int be assigned to long without casting?
Easy
Answer
An int can be assigned to a long through an automatic widening conversion because every int value can be represented by long.
Explanation
The reverse conversion is narrowing and requires an explicit cast because a long may contain values outside the int range.
Reference:
Java Language Specification
Q 51
What is narrowing primitive conversion?
Medium
Answer
Narrowing conversion converts a value to a type with a smaller range or precision, such as long to int.
Explanation
Narrowing conversions generally require an explicit cast and may lose information or change the resulting value.
Reference:
Java Language Specification
Q 52
What is widening primitive conversion?
Medium
Answer
Widening conversion converts a value to a type that can represent its range without normally losing magnitude, such as int to long.
Explanation
Widening conversions are generally performed automatically by Java when the conversion is permitted.
Reference:
Java Language Specification
Q 53
What is type casting in Java?
Easy
Answer
Type casting is the conversion of a value from one type to another compatible type, either explicitly or implicitly.
Explanation
Casting is common when converting numeric types or working with reference types in an inheritance hierarchy.
Reference:
Java Language Specification
Q 54
Do local variables have default values in Java?
Easy
Answer
No. Local variables do not receive automatic default values and must be definitely assigned before they are read.
Explanation
This compile-time rule prevents many accidental uses of uninitialized local variables.
Reference:
Java Language Specification
Q 55
What is the default value of each primitive instance variable in Java?
Easy
Answer
Numeric primitives default to zero, char defaults to the null character, and boolean defaults to false.
Explanation
These defaults apply to fields and array elements. Local variables must be explicitly initialized before use.
Reference:
Java Language Specification
Q 56
Why is boolean size not specified as exactly 1 bit in Java?
Medium
Answer
The Java language specification defines the boolean type by its logical values rather than requiring a specific storage size for variables or fields.
Explanation
The JVM specification also does not require a particular memory representation for boolean values, allowing implementations flexibility.
Reference:
Java Virtual Machine Specification
Q 57
What values can boolean hold in Java?
Easy
Answer
A boolean can have the value true or false.
Explanation
Unlike some languages, Java does not allow integers such as 0 or 1 to be used directly as boolean values.
Reference:
Java Language Specification
Q 58
What is the char data type in Java?
Easy
Answer
char is a 16-bit unsigned type representing a UTF-16 code unit.
Explanation
A char represents one UTF-16 code unit, not necessarily one complete Unicode character because some Unicode code points require surrogate pairs.
Reference:
Java Language Specification
Q 59
What is the size of double in Java?
Easy
Answer
double is a 64-bit IEEE 754 floating-point type.
Explanation
double is the default floating-point type in Java and provides greater precision than float.
Reference:
Java Language Specification
Q 60
What is the size of float in Java?
Easy
Answer
float is a 32-bit IEEE 754 floating-point type.
Explanation
float provides less precision than double and should generally be used only when its memory characteristics or domain requirements justify it.
Reference:
Java Language Specification
About This Topic
Prepare for
Data Types interviews with important concepts
and commonly asked questions.