Java Fundamentals - Viva Cheat Sheet
Variables
Q: What is a variable?
A: A variable is a named memory location that stores a value.
Q: Types of variables?
A: Local, Instance, Static.
Data Types
Q: How many primitive data types in Java?
A: 8 → byte, short, int, long, float, double, char, boolean.
Q: Size of int?
A: 4 bytes.
Operators
Q: Difference between == and .equals()?
A: == compares references, .equals() compares values.
Q: Pre-increment vs post-increment?
A: ++i increments before use, i++ increments after use.
Conditional Statements
Q: Difference between if-else and switch?
A: if-else handles ranges/conditions, switch works with fixed values.
Q: Can we use strings in switch?
A: Yes, from Java 7 onwards.
Loops
Q: Difference between while and do-while?
A: while checks before execution, do-while executes at least once.
Q: What is enhanced for loop?
A: A loop for iterating collections/arrays without index.
User Defined Functions
Q: What is method overloading?
A: Same method name, different parameter list.
Q: Can methods return objects?
A: Yes.
Arrays
Q: Default value of int array elements?
A: 0.
Q: Can array size be changed after creation?
A: No, arrays are fixed size.
Strings
Q: Why are strings immutable?
A: For security, caching, and thread-safety.
Q: Difference between String and StringBuilder?
A: String is immutable, StringBuilder is mutable.
Wrapper Classes
Q: What is autoboxing?
A: Automatic conversion of primitive → wrapper.
Q: Example of wrapper class?
A: Integer, Double, Boolean, etc.
Getter & Setter
Q: What is a getter?
A: Method to access private variable.
Q: Why use them?
A: To achieve encapsulation.
Threads
Q: Two ways to create a thread?
A: Extend Thread class, implement Runnable interface.
Q: Difference between start() and run()?
A: start() creates a new thread, run() runs in the same thread.
Errors
Q: Error vs Exception?
A: Error = serious, not recoverable; Exception = can be handled.
Q: Example of checked exception?
A: IOException.