Interview Q&A · Java · 9 questions in this topic
Java Basics: Classes, Objects & Platform — Java Interview Q&A
The foundational questions every Java interview opens with — what a class and object are, why main() looks the way it does, and why Java is portable. Part of Core Java Basics · 294 questions in the Java bank.
Why is Java platform independent?
Answer
Java source is compiled to a machine-independent intermediate format called bytecode, not to native machine code. Any device with a Java Virtual Machine (JVM) can execute that same bytecode, so the compiled artifact does not depend on the operating system or CPU it was built on. The trade-off is that the JVM must translate bytecode to native instructions at runtime.
Key points
- 1javac turns .java source into .class files containing bytecode.
- 2The JVM (or its Just-In-Time compiler) turns bytecode into native instructions for the host machine.
- 3The same .class file runs on Windows, macOS, Linux and elsewhere — "write once, run anywhere".
- 4Portability lives in the JVM, not in the bytecode being native code.
What is a class?
Answer
A class is a blueprint or template that defines the state (fields) and behaviour (methods) shared by a set of objects. It declares what data an object of that type holds and what operations it supports. In Java every piece of code lives inside a class, and a public class must be saved in a file named exactly after it (for example Hello.java).
Example
public class Hello { private String name; // state public void greet() { // behaviour System.out.println("Hi " + name); } }
What is an object?
Answer
An object is a concrete instance of a class, created with the new keyword. It has its own copy of the instance fields (its state) and can invoke the methods defined by its class (its behaviour). Objects live on the heap; the variable you use to reach one is a reference, not the object itself.
Example
Hello h = new Hello(); // h is a reference; the Hello instance is the object h.greet();
What is a method in Java?
Answer
A method is a named block of executable code that performs one task. Its declaration states an optional access modifier, a return type (or void), the method name, a parameter list in parentheses, and the body in braces. Methods give objects their behaviour, and related methods that share a name but differ in parameters are called overloaded.
Example
public int add(int a, int b) { // return type, name, parameters, body return a + b; }
What is encapsulation?
Answer
Encapsulation is bundling an object’s data and the methods that operate on it into a single unit (the class) and hiding the internal state behind a controlled interface. In Java it is implemented with access modifiers — typically private fields exposed through public getters and setters — so callers cannot corrupt the data or depend on implementation details.
Key points
- 1Fields are usually private; access goes through methods that can validate values.
- 2It protects invariants: a setter can reject invalid input instead of letting a field be overwritten directly.
- 3It decouples callers from the internal representation, so the internals can change without breaking users.
Why is the main() method public, static and void?
Answer
public lets the JVM call it from outside the class; static lets the JVM call it without first creating an instance of the class; and void means it returns no value to the caller. Its exact signature is public static void main(String[] args). Since Java 21 you can also declare it without the String[] parameter, but the classic signature is what interviews expect.
Key points
- 1public — the launcher must be able to invoke it.
- 2static — no object has been constructed yet when the program starts.
- 3void — the JVM does not use a return value.
- 4String[] args carries command-line arguments.
Explain the main() method.
Answer
main() is the entry point the JVM looks for when it launches an application. It receives the command-line arguments as a String array and is the first method that runs; every child thread is spawned from this "main" thread. There must be at least one main method per application, and execution ends when main finishes and all non-daemon threads have finished.
Example
public static void main(String[] args) { System.out.println("args: " + java.util.Arrays.toString(args)); }
Can we have multiple classes in a single file?
Answer
Yes. A .java file can contain many top-level classes, but at most one of them may be public, and the file name must match that public class. If no class is public the file name is free, though by convention it still matches the main class. During compilation each top-level class produces its own .class file.
Key points
- 1Two public top-level classes in one file cause a compile error ("The public type must be defined in its own file").
- 2Nested (inner) classes are a separate concept and may also live in the same file.
- 3Splitting one class per file is the recommended style.
In Java, how do we copy objects?
Answer
There is no built-in copy operator. Assigning one reference to another (b = a) copies only the reference, so both variables point at the same object. A real copy must be created explicitly — either a shallow copy (Object.clone(), a copy constructor, or a record’s canonical constructor) that duplicates the top-level fields but shares referenced objects, or a deep copy that recursively duplicates everything the object refers to.
Example
// Shallow copy via copy constructor record Point(int x, int y) {} Point p2 = new Point(p1.x(), p1.y());
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission