150+ Java Interview Questions for Experienced Developers - Part 2 (Core Java Fundamentals)
150+ Java Interview Questions That Separate $100K from $200K Engineers
Coding Interview coming up? Join ByteByteGo now for a structured preparation. They are also offering a rare 50% discount now on their lifetime plan
Hello guys, this is the second part of my mega series of Java interview questions, 150+ Java Interview Questions for Experienced Developers. In the first part, I have shared frequently asked Java interview questions on Multithreading and Concurrency and in this part, I will cover Java fundamentals, including data types, casting and conversions.
If you are preparing for Java interview or Java certification then knowing these core concepts will help you a lot.
I have also written a book for Java interviews, Grokking the Java Interview, and Grokking the Spring Boot Interview, where I have shared tips, tricks and frequently asked Java questions from different topics. If you are preparing for Java interviews, then definitely check them out. You can also use code friends20 to get 20% discount now.
📣Coursera 50% OFF (Sponsored)
Level up your AI skills with Coursera Plus at 50% off, get a full year for just $199 (normally $399). The offer ends Jan 29, making this a great time to start learning AI, Machine Learning, and other in-demand tech skills from top universities and companies.
2. Date types and Basic Java Interview Questions
Now, let’s take a look at Java Interview questions which are based upon core Java and basic Java concepts like data types int, float, long, double and conditional and logical operator including the ternary operator
17) What is the right data type to represent a price in Java?BigDecimal if memory is not a concern and performance is not critical, otherwise double with predefined precision. You can also use long to store price in cents like, this works better in most of the cases but you need to sacrifice readability a bit.
In HFT space, double and long is preferred but they comes with risk and require careful coding.
What is the risk with float or double?
Floating-point types use binary representation, which cannot precisely store decimal values like 0.1, 0.2, 19.99, etc.
This leads to rounding errors:
System.out.println(0.1 + 0.2); // 0.30000000000000004
For money, even tiny precision errors are unacceptable.
Why BigDecimal is correct?
BigDecimal stores numbers with exact decimal precision and gives you control over rounding.
BigDecimal price = new BigDecimal(”19.99”);
BigDecimal tax = new BigDecimal(”2.50”);
BigDecimal total = price.add(tax);
System.out.println(total); // 22.49
✔ No precision loss
✔ Designed for financial calculations
✔ Supports controlled rounding modes
⚠️ Important Best Practice
Always create BigDecimal using String, not double:
new BigDecimal("19.99") // ✅ Correct
new BigDecimal(19.99) // ❌ Wrong (inherits double error)In short, the correct data type in most cases to represent price in Java is BigDecimal because it provides exact decimal precision and avoids the rounding errors that occur with floating-point types like double or float.
18) How do you convert bytes to String?
you can convert bytes to the string using string constructor which accepts byte[], just make sure that right character encoding otherwise platform’s default character encoding will be used which may or may not be same.
19) How do you convert bytes to long in Java? (answer)
This questions if for you to answer :-) They to answer this one in comments.
20) Can we cast an int value into byte variable? what will happen if the value of int is larger than byte?
Yes, we can cast but int is 32 bit long in java while byte is 8 bit long in java so when you cast an int to byte higher 24 bits are lost and a byte can only hold a value from -128 to 128.
21) There are two classes B extends A and C extends B, Can we cast B into C e.g. C = © B;
Short answer - B reference can only be cast to C if the actual object is an instance of C. If the object was created as new B(), casting to C will compile but throw a ClassCastException at runtime.
Now, let’s examine this question in more detail
This is a classic Java inheritance + casting interview topic 👇
You have:
class A {}
class B extends A {}
class C extends B {}
❓ Can we cast B into C?
Short answer:
👉 Not safely, unless the object is actually a C.
Why?
In Java, casting doesn’t change the object, it only changes the reference type.
So this:
B b = new B();
C c = (C) b; // ❌ Compiles, but fails at runtime
Will throw:
ClassCastException: B cannot be cast to C
Because the actual object in memory is a B, not a C.
When does it work?
It only works if the B reference is actually pointing to a C object.
B b = new C(); // Upcasting (always safe)
C c = (C) b; // Downcasting (safe here)
✔ This works because the real object is C.
Rule to Remember (Great for Interviews)
You can downcast (parent → child) only if the object was originally created as the child type.
Safe pattern:
if (b instanceof C) {
C c = (C) b;
}
22) Which class contains clone method? Cloneable or Object?
The java.lang.Cloneable is marker interface and doesn’t contain any method clone method is defined in the object class. It is also knowing that clone() is a native method means it’s implemented in C or C++ or any other native language.
23) Is ++ operator is thread-safe in Java? (answer)
No it’s not a thread safe operator because its involve multiple instructions like reading a value, incriminating it and storing it back into memory which can be overlapped between multiple threads.
24) Difference between a = a + b and a += b ? (answer)
The += operator implicitly cast the result of addition into the type of variable used to hold the result. When you add two integral variable e.g. variable of type byte, short, or int then they are first promoted to int and them addition happens.
If result of addition is more than maximum value of a then a + b will give compile time error but a += b will be ok as shown below
byte a = 127;
byte b = 127;
b = a + b; // error : cannot convert from int to byte
b += a; // ok25) Can I store a double value in a long variable without casting?
No, you cannot store a double value into a long variable without casting because the range of double is more that long and you we need to type cast. It’s not difficult to answer this question but many developer get it wrong due to confusion on which one is bigger between double and long in Java.
26) What will this return 3*0.1 == 0.3? true or false?
This is one of the really tricky questions. Out of 100, only 5 developers answered this question and only of them have explained the concept correctly. The short answer is false because some floating point numbers can not be represented exactly.
Now, let’s deep dive a bit:
Why isn’t 3 * 0.1 == 0.3 true?
Because of floating-point precision errors.
In Java (and most languages), decimal numbers like 0.1 cannot be represented exactly in binary. They are stored as the closest possible approximation.
So when you do:
System.out.println(3 * 0.1);
You actually get something like:
0.30000000000000004
Now the comparison becomes:
0.30000000000000004 == 0.3 // false
What’s happening internally?
Computers store floating-point numbers using IEEE 754 binary format.
Some decimals (like 0.5, 0.25) are exact in binary.
But 0.1 and 0.3 are repeating fractions in binary, so they get rounded.
Tiny rounding errors → equality check fails.
Correct Way to Compare Doubles
Never compare floating-point numbers directly using ==.
Use a tolerance (epsilon):
double a = 3 * 0.1;
double b = 0.3;
if (Math.abs(a - b) < 1e-9) {
System.out.println(”Equal”);
}
In short, 3 * 0.1 == 0.3 returns false due to floating-point precision errors. Decimal values like 0.1 cannot be represented exactly in binary, causing small rounding differences that make direct equality comparisons unreliable.
27) Which one will take more memory, an int or Integer?
An Integer object will take more memory an Integer is the an object and it store meta data overhead about the object and int is primitive type so its takes less space.
Now, let’s see how much more space Integer takes than an int
✅ int (Primitive Type)
Stores the actual 32-bit value directly
Takes 4 bytes of memory
No extra overhead
Integer (Wrapper Class)
Integer is an object, and objects come with extra memory overhead:
Object header (JVM metadata)
The actual
intvalue inside the objectPossible padding for memory alignment
So an Integer typically takes:
16 bytes or more (depends on JVM, but always much larger than 4 bytes)
Integer x = 10;
ere you’re storing:
A reference (4 or 8 bytes depending on JVM)
Plus a separate Integer object in heap memory
In short, you can answer like - an Integer consumes significantly more memory than an int because int is a 4-byte primitive, while Integer is an object with additional JVM object overhead along with the wrapped int value.
28) Why is String Immutable in Java?
One of my favorite Java interview question. The String is Immutable in java because java designer thought that string will be heavily used and making it immutable allow some optimization easy sharing same String object between multiple clients.
This is a great question for Java programmers with less experience as it gives them food for thought, to think about how things works in Java, what Java designers might have thought when they created String class etc.
29) Can we use String in the switch case?
Yes from Java 7 onward we can use String in switch case but it is just syntactic sugar. Internally string hash code is used for the switch.
30) What is constructor chaining in Java?
When you call one constructor from other than it’s known as constructor chaining in Java. This happens when you have multiple, overloaded constructor in the class.
It helps avoid code duplication and ensures objects are properly initialized step by step.
There are Two Types of Constructor Chaining
1️⃣ Within the Same Class (Using this())
A constructor can call another constructor in the same class using this(). Here is an example
class Student {
String name;
int age;
Student() {
this(”Unknown”, 0); // Calls parameterized constructor
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Here:
The no-arg constructor calls the parameterized constructor
This avoids repeating initialization logic
📌 Rule: this() must be the first statement in the constructor
2️⃣ Between Parent and Child Class (Using super())
When a child class object is created, Java automatically calls the parent class constructor.
You can explicitly call it using super().
class Person {
Person(String name) {
System.out.println(”Person constructor: “ + name);
}
}
class Employee extends Person {
Employee(String name, int id) {
super(name); // Calls parent constructor
System.out.println(”Employee ID: “ + id);
}
}
📌 If you don’t write super(), Java inserts a default no-arg super() automatically (if available).
🔄 Order of Constructor Calls
When creating a child object:
Parent class constructor runs first
Then child class constructor runs
Example:
Employee e = new Employee(”John”, 101);
Output:
Person constructor: John
Employee ID: 101
🚫 Important Rules (Interview Favorites)
this()andsuper()must be the first statement in a constructorYou cannot use both
this()andsuper()in the same constructorConstructor chaining helps in code reuse and proper initialization
In short, Constructor chaining is the process of calling one constructor from another using this() within the same class or super() to call a parent class constructor, ensuring proper and reusable object initialization.
That’s all about the core Java interview questions part 2 guys. In the first part, we have seen multithreading and concurrency and in this part we have focused on core Java fundamentals like casting and operators.
I will share more question on third part where we will see the Garbage Collection and JVM internals interview questions, so stay tuned for next part. You can also share this post to your friends who are preparing for Java interviews, its free.
In case you don’t know, I have also written a book for Java interviews, Grokking the Java Interview, and Grokking the Spring Boot Interview, where I have shared tips, tricks and frequently asked Java questions from different topics.
You can read the book to better prepare for your Java interviews. You can also use the code — friends20 to get a 20% discount because you are already my reader.
Other Java and Spring Boot Interview Questions you may like







