1. What is the core difference between primitive and non-primitive data types in memory?
Answer:
-
Primitive Types (
int,double,char,boolean, etc.): Predefined by Java and store their raw primitive values directly in stack memory. They have fixed memory sizes and cannot call methods. -
Non-Primitive Types (
String, Arrays, Objects): Created by developers (exceptString) and store memory references (addresses) on the stack that point to the actual data located in the heap memory. They can invoke built-in methods and have flexible memory allocation.
Â
2. Why does 5 / 2 output 2 in Java instead of 2.5, and how can you fix it?
Answer: In Java, dividing an integer by another integer performs integer division, which discards the decimal remainder entirely. To get 2.5, at least one operand must be explicit floating-point data:
-
5.0 / 2 -
(double) 5 / 2
Â
3. Explain the difference between x++ (post-increment) and ++x (pre-increment) using code behavior.
Answer:
-
Post-increment (
x++): Uses the current value ofxin the evaluating expression first, and then incrementsxby 1. -
Pre-increment (
++x): Incrementsxby 1 first, and then uses the updated value in the expression.
Â
int a = 5, b = 5;
int result1 = a++; // result1 gets 5, then 'a' becomes 6
int result2 = ++b; // 'b' becomes 6, then result2 gets 6
4. What is the difference between explicit (narrowing) and implicit (widening) type casting?
Answer:
-
Implicit (Widening) Casting: Happens automatically when converting a smaller data type into a larger data type (e.g.,
inttodouble). No data precision is lost. -
Explicit (Narrowing) Casting: Must be written manually using parentheses when converting a larger type to a smaller type (e.g.,
(int) 9.99). It truncates decimal data and can lead to precision loss.
Â
5. Why does calling scanner.nextLine() immediately after scanner.nextInt() often skip user input?
Answer:Â nextInt() reads only the numeric value typed by the user, leaving the newline character (\n) generated when pressing “Enter” behind in the input buffer. When nextLine() runs immediately afterward, it sees that leftover newline, treats it as an empty line submission, and moves on without waiting for user input.
Fix: Add an extra scanner.nextLine(); call right after nextInt() to clean out the buffer.
Â
6. How do logical AND (&&) and bitwise AND (&) differ when evaluating boolean conditions?
Answer:
-
Short-circuit Logical AND (
&&): Evaluates the left expression first. If the left side isfalse, Java skips evaluating the right side completely because the overall condition can never be true. -
Bitwise AND (
&): Evaluates both sides of the condition every single time, regardless of whether the left side evaluated tofalse.
Â
Â
7. What is the default value assigned to primitive numerical instance variables in Java if they aren’t initialized?
Answer:
-
int,byte,short,long:0(or0L) -
float,double:0.0(or0.0f) -
boolean:false -
char:\u0000(null character)
Â
8. What is the key difference between single-line, multi-line, and Javadoc comments?
Answer:
-
Single-line (
//): Ignores text to the end of the line. -
Multi-line (
/* ... */): Ignores blocks of text spanning multiple lines. -
Javadoc (
/** ... */): Special documentation comments extracted by thejavadoctool to build structured HTML API documentation pages.
Â
9. Why should floating-point types like float or double be avoided when handling financial currency calculations?
Answer:Â float and double represent numbers using binary floating-point representation (IEEE 754 format). They cannot precisely represent certain base-10 decimal fractions (like $0.1$), causing tiny rounding errors over repeated calculations. Financial software uses BigDecimal or stores values as integer cents instead.
Â
10. What is the difference between == and .equals() when comparing String variables?
Answer:
-
==Operator: Checks whether two reference variables point to the exact same memory location (reference equality). -
.equals()Method: Compares the actual sequence of characters stored inside the string objects (content equality).
Â
Â