Top 60 Java Interview Questions and Answers for Freshers and Experienced Developers

Illustration for Top 60 Java Interview Questions and Answers for Freshers and Experienced Developers
By Last updated:

Ace your next Java interview with this comprehensive list of 60 essential questions, including beginner to intermediate-level topics commonly asked in tech interviews.

Java Basics and Compilation

1. Can a Java interpreter be used for the execution of source code?

Yes, Java source code can be interpreted using the Java interpreter (java command), which executes bytecode produced by the Java compiler.

2. On successful compilation, is a .class file created?

Yes, compiling a .java file with javac generates a .class file containing bytecode.

3. Can Java source code be written in Notepad?

Yes, Java source code is plain text and can be written in any text editor including Notepad.

4. Is every Java program enclosed in a class definition?

Yes, all Java code must reside inside a class definition, including the main method.

5. What is a compilation unit in Java?

A compilation unit refers to a single .java file. It's the smallest unit of source code that can be compiled independently.

6. Is an empty .java file a valid source file?

Yes, it's valid but doesn’t perform any function until it contains at least a class definition.

7. Can a .java file contain more than one class?

Yes, it can have multiple classes, but only one public class per file is allowed, and the file name must match the public class name.

JVM and Execution Environment

8. Is the JVM a compiler or an interpreter?

JVM is an interpreter that runs compiled Java bytecode. Modern JVMs also use JIT (Just-In-Time) compilation.

9. Difference between JVM Spec, Implementation, and Runtime?

  • Spec: Blueprint for JVM behavior, defined by Oracle.
  • Implementation: Actual software that follows the spec (e.g., HotSpot).
  • Runtime: Running instance of a JVM implementation.

10. What is BDK in Java?

Bean Development Kit (BDK) is a tool to build and test JavaBeans without writing any application code.

11. Purpose of bin and lib in JDK?

  • bin: Executables like javac, java, javadoc.
  • lib: Core Java packages, JVM resources, etc.

12. What are the two main parts of Java program execution?

  • Compiler: Compiles code to bytecode.
  • Interpreter: Executes bytecode using JVM.

13. Difference between PATH and CLASSPATH?

  • PATH: OS uses it to find Java executables.
  • CLASSPATH: JVM uses it to locate .class files and libraries.

14. Which environment variables are required to run Java?

Set JAVA_HOME, PATH, and CLASSPATH appropriately to compile and run Java programs.

JavaBeans, POJO, Data Types

15. What are JavaBeans?

Reusable components with private fields, public getters/setters, a no-arg constructor, and implement Serializable.

16. Rules for defining a JavaBean class?

  • Must have public default constructor
  • Getter/setter methods for all fields
  • Implement Serializable

17. Difference between JavaBean and POJO?

  • POJO: Plain Old Java Object; no strict rules.
  • JavaBean: Follows JavaBeans conventions (getter/setter, serializable).

18. Unicode, ASCII, UTF-16, UTF-8 bit length?

  • ASCII: 7-bit
  • Unicode: 16-bit
  • UTF-8: 8/16/18-bit (variable)
  • UTF-16: 16-bit minimum

19. Minimum requirement of any Java application?

At least one class and a public static void main(String[] args) method.

20. What is the return type of main() method?

Void. It doesn't return anything to the OS.

Main Method Specifics

21. What is the argument type of main()?

String[] args - used for command-line arguments.

22. Can a program have multiple main() methods?

No, only one main() per class. But multiple classes can have their own main().

23. Can main() be overloaded?

Yes, Java allows method overloading, including main().

24. Why is main() public static?

  • public: JVM can access it.
  • static: No object needed.
  • void: Doesn’t return a value.

25. What is the use of String[] args?

Receives command-line arguments during runtime.

26. Can main() be declared final?

Yes, it can be final, public, and static.

27. How to prove args is empty and not null?

Check args.length == 0. If args were null, this would throw an exception.

28. Is the first argument in args the program name?

No. Java’s args[] does not include the program name (unlike C/C++).

29. Is args null or empty if no input is provided?

It’s an empty array (length == 0), not null.

30. Can multiple classes have main()?

Yes, but only the specified class in the java command is executed.

JVM Memory and Miscellaneous

31. What does -Xms and -Xmx do?

They set initial and max heap memory for JVM. Example: java -Xms64m -Xmx256m App.

32. Output of System.out.println("// Is this a comment?");?

Output: // Is this a comment? – it's a string, not a comment.

33. How to assign null to int?

You can't. But you can assign null to Integer (wrapper).

34. Why does char use 2 bytes in Java?

To support Unicode characters ( is the default null character).

35. Difference between declaring and defining a variable?

  • Declaring: int x;
  • Defining: int x = 10;

36. Argument vs Parameter?

  • Parameter: In method declaration.
  • Argument: Actual value passed.

37. What is the default value of instance variables?

They are initialized: int = 0, boolean = false, Object = null, etc.

38. When is static variable memory destroyed?

When the JVM shuts down.

39. Which operations throw ArithmeticException?

/ and % on zero.

Operators and Flow Control

40. Difference between & and &&, | and ||?

  • &, |: Bitwise and always evaluate both sides.
  • &&, ||: Short-circuit; only evaluate right-hand if needed.

41. Difference between >> and >>>?

  • >>: Signed right shift (preserves sign bit).
  • >>>: Unsigned right shift (fills with 0).

42. Can shift operators be used with floats?

No. Only integer types are allowed.

43. What happens to shifted-off bits?

They are discarded.

44. What fills in shifted bits?

  • Left shift (<<): 0s.
  • Signed right shift (>>): depends on sign bit.
  • Unsigned right shift (>>>): 0s.

45. What are Java operators?

Special symbols that perform operations: +, -, ==, etc.

46. Types of operators in Java?

  • Arithmetic
  • Logical
  • Relational
  • Assignment
  • Bitwise
  • Conditional

47. Can if be used without else?

Yes.

48. Can else exist without if?

No.

49. What are valid values for switch cases?

Only constant expressions that evaluate to byte, short, char, int, enum, or String.

50. Can a for loop run forever?

Yes, like this: for(;;) {}

Control Statements and Variables

51. Difference between break and continue?

  • break: Exits loop.
  • continue: Skips current iteration.

52. Difference between do-while and while?

  • do-while: Runs at least once.
  • while: Might not run if condition is false.

53. What is a variable?

A memory location holding a value.

54. Types of variables in Java?

  • Local
  • Instance
  • Static (Class)

55. What is a literal?

A constant value assigned directly: "Hello", 42, 'A', true, etc.

56. What is an array?

An indexed container for elements of the same type.

57. How to declare an array?

int[] arr;
String[] names;

58. Valid String array declarations?

  • String[] s;
  • String s[];
  • String []s;

59. What is a static variable?

A variable shared across all instances of a class.

60. What is a final variable?

A variable whose value cannot change after initialization.