Java Interview Questions for Freshers: Part 1
1. Why is Java a platform-independent language?
Java accomplishes platform independence by utilizing bytecode and the Java Virtual Machine (JVM). When you compile Java code, it’s converted into bytecode, a platform-neutral intermediate code. The JVM interprets this bytecode, allowing it to execute on any device or operating system that has a compatible JVM installed, making Java programs portable across different platforms.
2. Why does Java deviate from being a purely object-oriented language?
Java accomplishes platform independence by utilizing bytecode and the Java Virtual Machine (JVM). When you compile Java code, it’s converted into bytecode, a platform-neutral intermediate code. The JVM interprets this bytecode, allowing it to execute on any device or operating system that has a compatible JVM installed, making Java programs portable across different platforms.
3. Explain the Distinction Between Heap and Stack Memory in Java and How Java Implements Them.
Heap memory is used for dynamic memory allocation of objects and instances in Java. It’s where the objects, along with their instance variables, are stored. The stack is used for method execution and stores local variables and references to objects in heap memory. Java manages these memories automatically: heap memory is managed by the garbage collector, while the stack memory is managed by the JVM.