Introduction to Java – Top Interview Questions, Answers & Examples
What is Java? History, Features & Editions (JDK / JRE / JVM)
Q1. What is Java?
Java is a high-level, object-oriented, platform-independent programming language designed to build secure and portable applications.
Q2. Who developed Java and when?
Java was developed by James Gosling at Sun Microsystems in 1995.
Q3. Why is Java called platform independent?
Because Java programs are converted into bytecode, which runs on any OS where a JVM is installed.
Example:
Compile on Windows → Run on Linux
class Test {
public static void main(String[] args) {
System.out.println("Platform Independent");
}
}
Q4. What are the main features of Java?
- Platform Independent
- Object-Oriented
- Robust & Secure
- Simple & Portable
- Multithreaded
- High Performance (JIT compiler)
- Distributed
Q5. What is JDK?
JDK (Java Development Kit) is used to develop Java applications.
It includes:
- JRE
- Compiler (javac)
- Development tools
Example:
To compile a program:
javac Hello.java
Q6. What is JRE?
JRE (Java Runtime Environment) provides libraries and JVM required to run Java programs.
Example:
To run compiled bytecode:
java Hello
Q7. What is JVM?
JVM executes the bytecode and provides platform independence.
Q8. Is JVM platform dependent?
Yes, JVM is platform dependent. Java bytecode is platform independent.
Real-World Use Cases & Platform Independence & Bytecode
Q9. What are real-world applications of Java?
- Banking applications
- Android apps
- Web applications
- Enterprise software
- Big Data (Hadoop)
- Cloud-based solutions
- Embedded systems
Example:
- Android apps → built using Java/Kotlin
- Hadoop → written in Java
Q10. What is platform independence?
Write the code once → compile → run anywhere that has a JVM.
Q11. What is bytecode?
Bytecode is the intermediate code (.class file) generated after compilation.
It is executed by the JVM.
Example:
Test.java → javac → Test.class (bytecode)
Q12. Why does Java use bytecode?
Bytecode makes Java portable, fast, and secure.
How Java Works: Compilation → Execution Process
Q13. Explain the Java program compilation and execution process.
- Write code →
.javafile - Compile →
javac→.classbytecode - JVM loads bytecode using Class Loader
- Bytecode Verifier checks safety
- Execution Engine runs the code
- JIT compiler optimizes performance
Example Summary:
Hello.java → javac → Hello.class → JVM → Output
Q14. What is the role of Class Loader?
It loads class files into JVM at runtime.
Q15. What is Bytecode Verifier?
It checks bytecode for:
- Illegal code
- Violations
- Security threats
Q16. What is JIT compiler?
JIT (Just-In-Time) compiler improves performance by converting bytecode into machine code at runtime.
Setting Up Java Environment (JDK Installation, IDE Setup – Eclipse / IntelliJ)
Q17. How do you install JDK?
- Download JDK from Oracle/OpenJDK
- Install it
- Set PATH and JAVA_HOME variables
Q18. Which IDEs are commonly used for Java?
- IntelliJ IDEA
- Eclipse
- NetBeans
Q19. Why use an IDE?
Because it provides:
- Syntax highlighting
- Auto-completion
- Debugger
- Build tools
- Error reporting
Example:
When typing:
System.
IDE shows suggestions: out, err, in etc.
Q20. How do you check Java is installed?
Commands:
java -version
javac -version
Your First Java Program
Q21. Write and explain a simple Java program.
class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Explanation:
class→ blueprintmain()→ entry pointSystem.out.println→ prints output
Output:
Hello Java
Q22. Why is the main method static?
So JVM can call it without creating an object.
Q23. Why should the class name and file name be same?
If a class is public, the filename must match the class name, or compilation fails.
Q24. What is the output of the above program?
Hello Java
Java Development Workflow – Write, Compile & Run Your First Program
Q25. What are the steps for writing, compiling and running a Java program?
- Write code →
.java - Compile →
javac Hello.java
3. Run →
java Hello
Q26. What happens during compilation?
Compiler checks:
- Syntax
- Type safety
- Converts code into bytecode
Example of compile-time error:
System.out.println("Hello" // missing parenthesis
Q27. What happens during runtime?
JVM executes bytecode using:
- ClassLoader
- Verifier
- Execution Engine
Example of runtime error:
int x = 10 / 0; // ArithmeticException
Q28. What tools are used in Java development?
- JDK
- Maven / Gradle
- IntelliJ, Eclipse
Example - Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Understanding the Java Ecosystem
Q29. What is the Java ecosystem?
Includes:
- JVM
- JDK
- Java API
- Frameworks
- Libraries
- Tools (Maven, Gradle)
Q30. What are popular Java frameworks?
- Spring / Spring Boot
- Hibernate / JPA
- Jakarta EE
- Micronaut / Quarkus
Q31. What are Java APIs?
Built-in library packages like:
java.langjava.utiljava.iojava.time
Example using java.util:
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("Java");
Q32. What are JVM languages?
Languages that run on JVM:
- Kotlin
- Scala
- Groovy
- Clojure
Q33. What is the role of Maven/Gradle?
Build automation tools used for:
- Managing dependencies
- Compiling code
- Packaging applications
Example - Gradle build snippet:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
}
