Menu
CoddyTech

Java Basics Interview Questions

Campus placement basics: the JDK and JVM, data types, operators and their output puzzles.

13 questions
  1. What is the difference between JDK, JRE and JVM?

    The JVM runs bytecode, the JRE is the JVM plus the standard library, and the JDK is the JRE plus tools such as javac and jar. Since Java 11 Oracle ships no separate JRE: you install a JDK, or build a trimmed runtime with jlink.

  2. Why is Java platform independent? Is the JVM platform independent?

    javac compiles to bytecode, not native code, and any machine with a JVM runs it. The JVM itself is not platform independent: each OS and CPU has its own build, which turns the same bytecode into native instructions. More in what is Java.

  3. Why is the main method public static void main(String[] args)?

    It is the exact signature the JVM calls: public so it is reachable from outside, static because no object exists yet, void because exit codes go through System.exit, and String[] args for the command line arguments.

    Overloads are allowed, but the JVM never calls them. Instance main methods (void main()) were a preview in Java 21 to 24 and became final in Java 25 with JEP 512.

  4. What are the primitive data types in Java?

    Eight: byte (8 bits), short (16), int (32), long (64), float (32), double (64), char (16, one UTF-16 unit) and boolean. Everything else, including String and arrays, is a reference type.

    Java
    public class Main {
        public static void main(String[] args) {
            System.out.println("int max:  " + Integer.MAX_VALUE);
            System.out.println("long max: " + Long.MAX_VALUE);
            System.out.println("char 'A' as int: " + (int) 'A');
            System.out.println("byte range: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
        }
    }

    The sizes are fixed by the language spec, unlike in C. See the Java data types guide.

  5. What happens when you add 1 to Integer.MAX_VALUE?

    Java
    public class Main {
        public static void main(String[] args) {
            int x = Integer.MAX_VALUE;
            x++;
            System.out.println(x);
        }
    }

    It prints -2147483648. int arithmetic is 32-bit two's complement, so MAX_VALUE + 1 silently wraps to Integer.MIN_VALUE.

    Use Math.addExact to throw ArithmeticException on overflow, or switch to long or BigInteger.

    Practice it: Factorial

  6. What do 7 / 2, 7 % 2 and 7 / 2.0 return in Java?

    Java
    public class Main {
        public static void main(String[] args) {
            System.out.println(7 / 2 + " " + 7 % 2 + " " + 7 / 2.0);
        }
    }

    It prints 3 1 3.5. 7 / 2 is int division and truncates toward zero, 7 % 2 is the remainder, and 7 / 2.0 widens to double. For an average of ints, write (double) sum / count.

  7. What does 1 + 2 + "3" + 4 + 5 print in Java?

    Java
    public class Main {
        public static void main(String[] args) {
            System.out.println(1 + 2 + "3" + 4 + 5);
        }
    }

    It prints 3345. + runs left to right: 1 + 2 is addition, 3 + "3" is concatenation, and every + after a string concatenates. Write "sum: " + (4 + 5) to add first.

  8. What happens in a Java switch without break?

    Java
    public class Main {
        public static void main(String[] args) {
            int x = 2;
            switch (x) {
                case 1: System.out.print("one ");
                case 2: System.out.print("two ");
                case 3: System.out.print("three ");
                default: System.out.print("other");
            }
            System.out.println();
        }
    }

    It prints two three other. Colon-style cases fall through: after a match, execution runs into every following case until a break. The arrow form from Java 14 (case 2 -> ...) never falls through.

  9. What is type casting in Java? What is widening and narrowing?

    Casting converts a value to another type. Widening (int to long or double) is automatic and safe; narrowing (double to int) needs an explicit cast and can lose data.

    Java
    public class Main {
        public static void main(String[] args) {
            int i = 100;
            long l = i;            // widening, automatic
            double d = 3.99;
            int truncated = (int) d;   // narrowing drops the fraction
            byte b = (byte) 200;       // keeps the low 8 bits
            System.out.println(l + " " + truncated + " " + b);
        }
    }

    It prints 100 3 -56: the cast truncates 3.99 instead of rounding, and (byte) 200 keeps only the low 8 bits.

  10. What are wrapper classes and autoboxing in Java?

    Wrapper classes (Integer, Double, Character and the rest) let primitives be used as objects, for example in collections. Autoboxing is the compiler converting int to Integer with Integer.valueOf; unboxing is the reverse.

    Two traps: unboxing a null throws NullPointerException, and == on two Integers compares references.

  11. What is the difference between final, finally and finalize in Java?

    They are unrelated. final blocks reassigning a variable, overriding a method or extending a class. finally runs after try/catch whether or not an exception was thrown. finalize() was a garbage collector hook, deprecated in Java 9 and for removal in Java 18; use try-with-resources or Cleaner.

  12. What does the static keyword mean in Java?

    A static member belongs to the class, so all objects share one copy.

    Java
    class Counter {
        static int created = 0;   // shared by all objects
        int id;                   // one per object
    
        Counter() {
            created++;
            id = created;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Counter a = new Counter();
            Counter b = new Counter();
            System.out.println(a.id + " " + b.id + " " + Counter.created);
        }
    }

    It prints 1 2 2. Static methods cannot use this and are hidden, not overridden. More in static members.

  13. What is a constructor in Java? Can a constructor be private?

    A constructor initializes a new object: it has the class's name and no return type, and the compiler adds a no-argument one only if you write none.

    Yes, it can be private, which blocks new from other classes: that is how singletons, utility classes like Math and factories like List.of work. Until Java 25, when JEP 513 relaxed the rule, this(...) or super(...) had to be the first statement.

Coddy programming languages illustration

Learn to code with Coddy

Get started