Autocoder

Core Java Interview Questions for Freshers

Last updated 2026-11-19

Fresher interviews stay close to fundamentals: why Java is platform independent, what the default values are, how constructors and static work, and what a short snippet prints. Interviewers want clear, correct statements with a tiny example, not advanced theory.

This page collects the basics in the order they are usually asked, with code for the output questions. When you are comfortable here, continue to Java OOPs questions, Java strings and the wider Java interview questions.

AudienceFinal-year students and 0 to 1 year experience
Java version17 and later
RoundWritten test and first technical interview

What to revise in what order

How to answer a fresher question

State the definition in one sentence, give a one-line example, then mention one limitation or gotcha. This pattern shows depth without rambling and gives the interviewer an easy follow-up to pick.

Core Java Interview Questions for Freshers: questions with worked answers

  1. 1

    What are the main features of Java?

    easy

    Java is object oriented, platform independent because source compiles to bytecode that any JVM runs, strongly typed, and has automatic garbage collection, so you do not free memory manually. It has built-in multithreading support, a large standard library and a security model with bytecode verification. It has no pointer arithmetic, which removes a class of memory errors.

  2. 2

    Why is Java called platform independent but the JVM is platform dependent?

    easy

    javac compiles source into bytecode, which is the same on every operating system. Each platform provides its own JVM that translates bytecode into machine instructions for that platform. So you compile once and run anywhere there is a JVM, though the JVM binary itself differs for Windows, Linux and macOS.

  3. 3

    List the primitive types with sizes and default values.

    easy

    byte 8 bits, short 16, int 32, long 64, float 32, double 64, char 16 and boolean (its size is JVM dependent). Defaults for fields are 0 for numeric types, 0.0 for float and double, the null character for char, false for boolean and null for references. Local variables have no default and must be assigned before use or the code does not compile.

    java
    class Demo {
        int x;                 // 0
        boolean flag;          // false
        String s;              // null
        void m() {
            int y;
            // System.out.println(y);   // compile error: might not have been initialized
        }
    }
  4. 4

    What does the main method signature mean?

    easy

    public so the JVM can call it from outside the class, static so it needs no object, void because it returns nothing to the JVM, and String[] args carries command-line arguments. Recent JDKs relax this with instance main methods and no required String[] parameter; the feature was in preview in Java 21 and finalised in Java 25, but traditional code still uses the full signature.

  5. 5

    What is a constructor? Is there a default constructor?

    easy

    A constructor has the class name, no return type, and runs when you create an object to initialise it. If you write no constructor, the compiler supplies a no-argument default one. As soon as you write any constructor, the default is not generated, so new Student() fails unless you add it yourself. Constructors can be overloaded and chained with this(...).

    java
    class Student {
        String name;
        Student(String name) { this.name = name; }
    }
    // new Student();   // compile error: no default constructor now
  6. 6

    What does the static keyword do?

    easy

    A static field belongs to the class and is shared by all instances. A static method can be called without an object and can use only static members directly. A static block runs once when the class is loaded, and a static nested class does not hold a reference to an outer instance. Static members cannot use this or super.

  7. 7

    Predict the output: i++ and ++i together.

    medium

    i++ yields the current value and then increments; ++i increments first and yields the new value. Evaluation is left to right. With i = 5, the expression i++ + ++i is 5 + 7, and i ends at 7.

    java
    int i = 5;
    int r = i++ + ++i;     // 5 + 7
    System.out.println(r); // 12
    System.out.println(i); // 7
  8. 8

    Predict the output of integer division and remainder with negatives.

    medium

    Integer division truncates towards zero and the remainder takes the sign of the dividend. So 7 / 2 is 3, -7 / 2 is -3, -7 % 2 is -1 and 7 % -2 is 1. For a non-negative modulus use Math.floorMod(-7, 2), which gives 1.

    java
    System.out.println(7 / 2);                 // 3
    System.out.println(-7 / 2);                // -3
    System.out.println(-7 % 2);                // -1
    System.out.println(Math.floorMod(-7, 2));  // 1
    System.out.println(7 / 2.0);               // 3.5
  9. 9

    Predict the output: switch fall-through.

    medium

    Without break, execution continues into the following cases regardless of their labels. With selector 2 it prints 2 and then 3 and stops at the break. The new arrow form (Java 14) does not fall through and can be used as an expression.

    java
    switch (2) {
        case 1: System.out.print(1);
        case 2: System.out.print(2);
        case 3: System.out.print(3); break;
        default: System.out.print("d");
    }
    // 23
    String r = switch (2) { case 1 -> "one"; case 2 -> "two"; default -> "many"; };
  10. 10

    Array versus ArrayList.

    easy

    An array has a fixed length set at creation, can hold primitives and has no methods beyond length. ArrayList resizes automatically, holds only objects (primitives are boxed) and offers methods such as add, remove and contains. Use an array for fixed-size numeric data and ArrayList for general collections. Arrays have default values and a length field; ArrayList uses size().

    java
    int[] a = new int[3];        // [0, 0, 0]
    System.out.println(a.length);  // 3
    List<Integer> list = new ArrayList<>();
    list.add(10);
    System.out.println(list.size()); // 1
  11. 11

    What are wrapper classes, autoboxing and unboxing?

    easy

    Each primitive has an object wrapper (Integer, Double, Boolean) needed for collections and generics. Autoboxing converts a primitive to its wrapper automatically and unboxing does the reverse. Unboxing a null throws NullPointerException, and == on two boxed values compares references, not numbers; see Java interview questions for the Integer cache puzzle.

  12. 12

    Difference between == and equals() for Strings?

    easy

    == tests whether two references point to the same object, while equals compares the characters. Two literals with the same text share one pooled object, so they are == true, but new String("a") creates a separate object. Always compare string content with equals.

  13. 13

    What are method overloading and overriding? Give one rule for each.

    medium

    Overloading means the same method name with different parameter lists in one class, resolved at compile time, and the return type alone cannot differentiate them. Overriding means a subclass supplies its own version of an inherited method with the same signature, resolved at runtime, and the access cannot be reduced.

    java
    class Calc {
        int add(int a, int b) { return a + b; }
        double add(double a, double b) { return a + b; }   // overload
    }
    class Parent { void show() { System.out.println("P"); } }
    class Child extends Parent { @Override void show() { System.out.println("C"); } }
    Parent p = new Child();
    p.show();   // C
  14. 14

    What does the final keyword do on a variable, method and class?

    easy

    A final variable can be assigned once; for a reference it means the reference cannot change but the object can still be mutated. A final method cannot be overridden and a final class cannot be extended, as with String. A blank final field must be assigned in every constructor.

  15. 15

    What is the difference between break, continue and a labelled break?

    medium

    break exits the innermost loop or switch, continue skips to the next iteration, and a labelled break exits a named outer loop, which is useful for nested searches. Java has no goto.

    java
    outer:
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (j == 2) continue;
            if (i == 1) break outer;
            System.out.print(i + "" + j + " ");
        }
    }
    // 00 01
  16. 16

    What is garbage collection in simple terms?

    easy

    The JVM automatically frees memory occupied by objects that the program can no longer reach. You cannot force it, only suggest it with System.gc(), and you do not call delete. It removes most memory-management bugs but does not stop a leak caused by keeping unneeded references. More depth is on the garbage collection page.

  17. 17

    Write a program to read integers and print their sum.

    medium

    Use Scanner for small inputs or BufferedReader for large ones. Close or wrap the resource in try-with-resources, and use hasNextInt to stop at the end of input. Mention that Scanner is convenient but slow on big inputs.

    java
    import java.util.Scanner;
    public class SumInput {
        public static void main(String[] args) {
            long sum = 0;
            try (Scanner sc = new Scanner(System.in)) {
                while (sc.hasNextInt()) sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }

Frequently asked questions

How should a fresher prepare for a Java interview?

Learn the basics on this page, write ten small programs without looking, and practise explaining OOP concepts with examples. See how to crack a Java interview for the round-wise plan.

Do I need Spring for a fresher role?

Not usually for a first technical round, but mass recruiters sometimes ask for it on backend tracks. Core Java and SQL come first.

Which Java version should I learn?

Java 17 or 21, both long-term support releases, with lambdas and streams from Java 8.

How many questions will be asked?

Typically 10 to 20 in a technical round, with follow-ups on whatever you answer, so depth matters more than breadth.

Related guides

More in Programming Language Interview Questions by Language

See all in this section