Practice commonly asked
Data Types interview questions with clear answers and explanations.
30
Interview Questions
Questions & Answers
Data Types Interview Questions
30 Questions
Q 1
What is type inference in Java numeric expressions?
Hard
Answer
Java determines expression types using compile-time rules such as numeric promotion, literal types, and contextual typing.
Explanation
Understanding these rules is important for overloaded methods, arithmetic expressions, assignments, and generic APIs.
Reference:
Java Language Specification
Q 2
Can BigInteger and BigDecimal overflow like int and long?
Medium
Answer
They are arbitrary-precision numeric classes and do not overflow at fixed primitive widths, although operations can consume substantial memory and CPU as values grow.
Explanation
Applications should still consider resource usage when working with extremely large values.
Reference:
Java BigInteger and BigDecimal APIs
Q 3
What is the difference between BigInteger and BigDecimal?
Medium
Answer
BigInteger provides arbitrary-precision integer arithmetic, while BigDecimal provides arbitrary-precision decimal arithmetic with scale and rounding support.
Explanation
BigInteger is appropriate for integers beyond long's range, while BigDecimal is useful when exact decimal calculations are required.
Reference:
Java BigInteger and BigDecimal APIs
Q 4
Why can floating-point calculations produce unexpected decimal results?
Medium
Answer
Many decimal fractions cannot be represented exactly in binary floating-point format, so calculations can contain small representation errors.
Explanation
For exact decimal arithmetic, use BigDecimal with appropriate construction and rounding rules.
Reference:
Java BigDecimal API
Q 5
What is the difference between float/double precision and range?
Medium
Answer
Precision describes how many significant digits can be represented accurately, while range describes the magnitude of values that can be represented.
Explanation
A floating-point type can represent a very large range while still having limited precision for many decimal values.
Reference:
IEEE 754 / Java Language Specification
Q 6
What is binary numeric promotion?
Hard
Answer
Binary numeric promotion converts operands of certain arithmetic and comparison operations to a common numeric type before the operation is performed.
Explanation
byte, short, and char are generally promoted to int, while wider operands can cause promotion to long, float, or double.
Reference:
Java Language Specification
Q 7
Can two different primitive types be compared using ==?
Medium
Answer
Compatible numeric primitive types can be compared after binary numeric promotion.
Explanation
For example, an int and long can be compared after the int value is promoted to long.
Reference:
Java Language Specification
Q 8
What is the difference between primitive equality and reference equality?
Medium
Answer
For primitive operands, == compares their values. For reference operands, == compares whether the references identify the same object or array.
Explanation
Logical equality of objects should normally be implemented and checked with equals().
Reference:
Java Language Specification
Q 9
What happens when parsing an invalid integer String?
Easy
Answer
Methods such as Integer.parseInt() throw NumberFormatException when the supplied String is not a valid integer representation.
Explanation
Applications should validate or handle invalid input when parsing external data.
Reference:
Java Integer API
Q 10
Can String be converted directly to a primitive type?
Easy
Answer
String values can be parsed into primitive types using methods such as Integer.parseInt(), Long.parseLong(), and Boolean.parseBoolean().
Explanation
Parsing converts textual representations into typed values and may throw NumberFormatException for invalid numeric input.
Reference:
Java Wrapper Classes API
Q 11
Can boolean be converted to int using casting?
Easy
Answer
No. Java does not define a primitive cast between boolean and numeric types.
Explanation
If an application needs a numeric representation, it must explicitly map true and false to chosen numeric values.
Reference:
Java Language Specification
Q 12
When should you use long instead of int?
Easy
Answer
Use long when the required numeric range may exceed int's limits or when the domain naturally represents 64-bit values.
Explanation
Examples include large counters, database identifiers in some systems, file sizes, and epoch-based numeric values.
Reference:
Java Language Specification
Q 13
What is the difference between int and Integer?
Easy
Answer
int is a primitive 32-bit signed integer, while Integer is an immutable wrapper object representing an int value.
Explanation
Integer can be null and can be used in generic collections, while int cannot be null and cannot be used directly as a generic type argument.
Reference:
Java Integer API
Q 14
What is the difference between char and Character?
Easy
Answer
char is a primitive type, while Character is its wrapper class.
Explanation
Character provides object behavior and utility methods for Unicode and character classification while char provides the underlying UTF-16 code unit value.
Reference:
Java Character API
Q 15
How can an int be treated as an unsigned value in Java?
Medium
Answer
Java provides methods such as Integer.toUnsignedLong(), compareUnsigned(), divideUnsigned(), and remainderUnsigned() to interpret int bit patterns as unsigned values.
Explanation
The underlying int remains a 32-bit value, but these APIs provide unsigned arithmetic and comparison semantics.
Reference:
Java Integer API
Q 16
What is signed and unsigned behavior in Java primitive types?
Medium
Answer
Java's byte, short, int, and long types are signed, while char is an unsigned 16-bit type.
Explanation
Java also provides unsigned arithmetic utility methods for some integral types without changing the underlying signed primitive representation.
Reference:
Java Language Specification
Q 17
Can underscores be placed anywhere in a numeric literal?
Medium
Answer
No. Java restricts underscore placement; for example, underscores cannot appear directly adjacent to the decimal point, type suffix, or at the beginning or end of a literal.
Explanation
These restrictions prevent ambiguous or confusing literal syntax.
Reference:
Java Language Specification
Q 18
What are underscores in numeric literals used for?
Easy
Answer
Underscores can improve readability of numeric literals by separating groups of digits, such as 1_000_000.
Explanation
They do not change the numeric value and are removed as part of literal processing.
Reference:
Java Language Specification
Q 19
What is a hexadecimal, binary, and octal integer literal?
Easy
Answer
Java supports decimal literals and non-decimal integer literals using prefixes 0x or 0X for hexadecimal, 0b or 0B for binary, and a leading 0 for octal.
Explanation
These forms are useful when working with bit patterns, masks, and low-level representations.
Reference:
Java Language Specification
Q 20
What is the difference between 1, 1L, 1.0, and 1.0F?
Easy
Answer
1 is an int literal, 1L is a long literal, 1.0 is a double literal, and 1.0F is a float literal.
Explanation
Literal types influence overload resolution, assignment compatibility, and arithmetic promotion.
Reference:
Java Language Specification
Q 21
Why does 10.5 have type double by default?
Easy
Answer
A floating-point literal without an F or f suffix is of type double.
Explanation
Use the F suffix when a float literal is specifically required, such as 10.5F.
Reference:
Java Language Specification
Q 22
What is a numeric literal suffix in Java?
Easy
Answer
Numeric literal suffixes such as L, F, and D can explicitly specify long, float, and double literal types.
Explanation
For example, 10L is a long literal and 10.5F is a float literal.
Reference:
Java Language Specification
Q 23
What is the difference between primitive literals and wrapper objects?
Medium
Answer
A primitive literal such as 10 represents a primitive value, while a wrapper object such as Integer.valueOf(10) represents that value as an object.
Explanation
The compiler can perform boxing when a primitive is used where an object is required.
Reference:
Java Language Specification
Q 24
What is the difference between Integer.parseInt() and Integer.valueOf()?
Medium
Answer
parseInt() returns a primitive int, while valueOf() returns an Integer object.
Explanation
valueOf() may reuse cached Integer instances, while parseInt() is intended to produce the primitive numeric value.
Reference:
Java Integer API
Q 25
What happens when a null wrapper is unboxed?
Medium
Answer
Unboxing a null wrapper object throws NullPointerException.
Explanation
This can occur implicitly in expressions such as assigning an Integer reference to an int variable or performing arithmetic with a null Integer.
Reference:
Java Language Specification
Q 26
What is the difference between null and zero?
Easy
Answer
null represents the absence of an object reference, while zero is a numeric primitive value.
Explanation
They have different meanings and cannot be used interchangeably in Java.
Reference:
Java Language Specification
Q 27
Can primitive data types be null?
Easy
Answer
No. Primitive types cannot contain null.
Explanation
If nullable numeric or boolean values are required, the corresponding wrapper classes such as Integer or Boolean can be used.
Reference:
Java Language Specification
Q 28
What is null in Java?
Easy
Answer
null is a special reference value that indicates a reference does not currently refer to an object or array.
Explanation
Primitive variables cannot contain null, but reference variables can unless restricted by application design or type annotations.
Reference:
Java Language Specification
Q 29
Why should == not be used to compare Integer values?
Medium
Answer
== compares references when both operands are Integer objects, so it may compare object identity rather than numeric value.
Explanation
Integer caching can make some comparisons appear to work while others fail, so equals() should normally be used for value comparison.
Reference:
Java Integer API
Q 30
What is Integer caching in Java?
Medium
Answer
Java implementations commonly cache Integer instances for a range of small integer values, and the language specification guarantees identity for certain constant boxing cases.
Explanation
Because wrapper identity is not value equality, application code should generally use equals() rather than == for comparing Integer values.
Reference:
Java Language Specification
About This Topic
Prepare for
Data Types interviews with important concepts
and commonly asked questions.