Write This — Exam Sheet
This is the only file to revise the night before.
The other files teach you the concepts. This one is what you actually put on the answer sheet.
Every answer here fits ~15 minutes of handwriting — about 25 lines. That is your real budget: 60 marks in 180 minutes = 3 minutes per mark, so a 5-mark sub-part gets 15 minutes. Nothing here asks for more than that.
Do not memorise the long answers. Memorise these.
The 4-step recipe for every 5-mark answer
Write these four things, in this order. That's it.
| Step | What | Space | Marks |
|---|---|---|---|
| 1 | Define in one sentence | 2 lines | 1 |
| 2 | Explain the how — 2 or 3 short points, or a 3-row table | 5 lines | 1½ |
| 3 | Code — a small program that compiles | 12–14 lines | 2 |
| 4 | Output — what it prints | 2 lines | ½ |
1 + 1½ + 2 + ½ = 5. Skip step 3 and the best you can score is 2½.
Timing rules for the paper
| If the question says | You write | Minutes |
|---|---|---|
| Define / List | 2–4 lines, no code | 3 |
| Explain … with example | steps 1–4 above | 15 |
| Differentiate | A table, 4 rows — no code | 8 |
| Write a program | Code + output only, one line of intro | 15 |
Never write both sides of an OR. That is the single most common way to fail this paper. Pick one, finish it, move on.
The 21 answers
1 · Class vs Object
Define: A class is a blueprint and takes no memory. An object is an instance of a class and lives in the heap.
| Class | Object |
|---|---|
| No memory | Memory on heap |
| Written once | Many can exist |
class Dog { } |
Dog d = new Dog(); |
class Dog {
String name;
void bark() {
System.out.println(name + " barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.name = "Tommy";
d1.bark();
}
}
Tommy barks
2 · Constructor and Constructor Overloading
Define: A constructor initialises an object. It has the class name, no return type, and runs once when new is used.
Overloading: several constructors in one class with different parameter lists.
Types (name all five — often asked): default · no-argument · parameterized · overloaded · copy.
class Student {
int id;
String name;
Student() {
this(0, "Unknown"); // calls the 2-arg constructor
}
Student(int id, String name) {
this.id = id;
this.name = name;
}
void show() {
System.out.println(id + " " + name);
}
}
public class Main {
public static void main(String[] args) {
new Student().show();
new Student(101, "Aarav").show();
}
}
0 Unknown
101 Aarav
this(...)must be the first line of the constructor. Say that.
3 · Method Overloading vs Method Overriding
Define: Overloading = same name, different parameters, same class, decided at compile time. Overriding = child redefines the parent's method with the same parameters, decided at run time.
| Overloading | Overriding |
|---|---|
| Same class | Parent + child |
| Parameters differ | Parameters identical |
| Compile time | Run time |
| No inheritance needed | Needs inheritance |
class Calc {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
class Animal {
void sound() { System.out.println("animal"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("dog barks"); }
}
public class Main {
public static void main(String[] args) {
Calc c = new Calc();
System.out.println(c.add(2, 3));
System.out.println(c.add(2, 3, 4));
Animal a = new Dog();
a.sound(); // runs Dog's version — decided at run time
}
}
5
9
dog barks
4 · Inheritance and Its Types
Define: Inheritance lets a child class acquire the fields and methods of a parent, using extends. It represents an IS-A relationship and gives code reusability.
Types: single (A→B) · multilevel (A→B→C) · hierarchical (A→B, A→C) · multiple (interfaces only) · hybrid (interfaces only).
Why no multiple inheritance with classes? The diamond problem — if C extends A and B and both define show(), Java cannot decide which one to use. Interfaces have no method bodies, so there is nothing to be ambiguous about.
class Vehicle {
void start() { System.out.println("start"); }
}
class Bike extends Vehicle { // single inheritance
void ride() { System.out.println("ride"); }
}
public class Main {
public static void main(String[] args) {
Bike b = new Bike();
b.start(); // inherited from Vehicle
b.ride(); // Bike's own
}
}
start
ride
5 · Interface
Define: An interface is a contract. It says what a class must do, not how. A class uses implements and must write every method.
Rules (3 points): cannot be instantiated · methods are public abstract by default · a class can implement many interfaces — this is how Java does multiple inheritance.
interface Shape {
double area();
}
interface Bounded {
double perimeter();
}
class Square implements Shape, Bounded {
double side;
Square(double side) { this.side = side; }
public double area() { return side * side; }
public double perimeter() { return 4 * side; }
}
public class Main {
public static void main(String[] args) {
Square s = new Square(5);
System.out.println(s.area());
System.out.println(s.perimeter());
}
}
25.0
20.0
6 · Abstract Class vs Interface
Define: An abstract class is partly built — it can have completed methods and unfinished ones. An interface is only a promise.
| Abstract class | Interface |
|---|---|
| Can have constructors | No constructors |
| Normal + abstract methods | All abstract |
| Extends one | Implements many |
| Use when code is shared | Use when only the contract is shared |
abstract class Shape {
abstract void draw(); // must be finished by the child
void show() { System.out.println("shape"); } // already finished
}
class Square extends Shape {
void draw() { System.out.println("square"); }
}
public class Main {
public static void main(String[] args) {
Shape s = new Square();
s.draw();
s.show();
}
}
square
shape
7 · try-catch-finally
Define: try holds risky code, catch handles the failure, finally runs no matter what and is used for cleanup.
Three facts worth a mark: finally runs even if try has a return · it does not run if System.exit() is called · specific catches must come before general ones.
public class Main {
public static void main(String[] args) {
try {
int a = 10, b = 0;
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.println("cannot divide by zero");
} finally {
System.out.println("cleanup");
}
System.out.println("continues");
}
}
cannot divide by zero
cleanup
continues
8 · Checked vs Unchecked Exceptions
Define: A checked exception is verified by the compiler — you must catch it or declare throws. An unchecked exception is only discovered at run time.
| Checked | Unchecked |
|---|---|
| Compiler forces handling | Optional |
| Compile time | Run time |
IOException, SQLException |
ArithmeticException, NullPointerException |
Parent: Exception |
Parent: RuntimeException |
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
new FileReader("data.txt").close(); // checked - must be handled
} catch (IOException e) {
System.out.println("file problem");
}
int[] a = {1, 2, 3};
// a[5] = 9; unchecked - would fail only at run time
System.out.println("compiled fine");
}
}
file problem
compiled fine
9 · The Four Pillars of OOP
Define: The four principles every OOP language is built on.
| Pillar | One line | Example |
|---|---|---|
| Encapsulation | Hide data, expose methods | private int balance + getter |
| Inheritance | Child reuses parent | class Dog extends Animal |
| Polymorphism | One name, many forms | draw() differs per shape |
| Abstraction | Show what, hide how | abstract void sound() |
class Account {
private double bal; // encapsulation
void deposit(double a) { bal += a; }
double get() { return bal; }
}
class Animal { void eat() { System.out.println("eats"); } }
class Dog extends Animal { } // inheritance
abstract class Shape { abstract void draw(); }
class Circle extends Shape {
void draw() { System.out.println("circle"); } // polymorphism
}
public class Main {
public static void main(String[] args) {
Account a = new Account();
a.deposit(500);
System.out.println(a.get());
new Dog().eat();
new Circle().draw();
}
}
500.0
eats
circle
10 · JDK, JRE and JVM
Define: Three nested layers. JDK ⊃ JRE ⊃ JVM.
| Name | Job | Remember |
|---|---|---|
| JVM | Runs the .class file |
The player |
| JRE | JVM + libraries | Player + films |
| JDK | JRE + compiler javac |
Player + films + camera |
Bytecode: javac turns .java into .class — a platform-independent halfway language. The JVM runs it anywhere. "JVM is platform-dependent, but Java is not."
Hello.java --javac--> Hello.class --JVM--> output
Diagram to draw — four boxes:
Class Loader → Memory Areas → Execution Engine → JNI/Native
11 · Primitive Data Types
Define: Java has 8 basic types. Everything else is an object.
| Type | Size | Holds | Default |
|---|---|---|---|
byte |
1 | −128 to 127 | 0 |
short |
2 | ±32,767 | 0 |
int |
4 | ±2.1 billion | 0 |
long |
8 | very large | 0L |
float |
4 | decimals | 0.0f |
double |
8 | precise decimals | 0.0 |
char |
2 | one character | '\u0000' |
boolean |
1 bit | true / false | false |
Casting: widening is automatic (small→big). Narrowing needs a cast and may lose data — (int) 9.99 gives 9, it truncates.
12 · Operators
Define: An operator is a symbol that performs an operation on operands.
| Family | Symbols |
|---|---|
| Arithmetic | + - * / % |
| Relational | == != > < >= <= |
| Logical | && \|\| ! |
| Bitwise | & \| ^ ~ |
| Assignment | = += -= *= |
| Unary | ++ -- |
| Ternary | ? : |
public class Main {
public static void main(String[] args) {
System.out.println(10 / 3); // integer division
System.out.println(10 % 3); // remainder
System.out.println(5 & 3); // bitwise
System.out.println(10 + 20 + "J" + 10 + 20);
int a = 5;
System.out.println(a++); // prints then adds
System.out.println(++a); // adds then prints
}
}
3
1
1
30J1020
5
7
10 + 20 + "J" + 10 + 20→30J1020. Numbers add first; once a String appears, everything after is joined as text.
13 · Arrays
Define: An array is a fixed-size collection of elements of the same type, indexed from 0. a.length is a field, not a method.
2D arrays are arrays of arrays — that's why m.length gives rows and m[0].length gives columns.
public class Main {
public static void main(String[] args) {
int[] a = {12, 45, 7, 89};
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) max = a[i];
}
System.out.println("max = " + max);
int[][] m = {{1,2}, {3,4}};
System.out.println("rows=" + m.length + " cols=" + m[0].length);
}
}
max = 89
rows=2 cols=2
14 · String vs StringBuffer vs StringBuilder
Define: String cannot be changed — every edit makes a new object. StringBuffer and StringBuilder can be changed in place.
| String | StringBuffer | StringBuilder | |
|---|---|---|---|
| Changeable | No | Yes | Yes |
| Thread-safe | Yes | Yes | No |
| Speed | Slowest | Slow | Fastest |
| Use | fixed text | many threads | one thread |
public class Main {
public static void main(String[] args) {
String s = "Java";
s.concat(" Rocks");
System.out.println(s); // unchanged!
StringBuilder sb = new StringBuilder("Java");
sb.append(" Rocks");
System.out.println(sb); // changed
System.out.println("Java" == "Java"); // true
System.out.println("Java" == new String("Java")); // false
System.out.println("Java".equals(new String("Java"))); // true
}
}
Java
Java Rocks
true
false
true
15 · Packages
Define: A package is a namespace that groups related classes and prevents name clashes.
Five benefits: avoids name conflicts · groups related classes · controls access · reusable · easier maintenance.
Steps: ① write package mypack; as the first line ② javac -d . MyClass.java ③ import mypack.MyClass; ④ java -cp . Main
// file: mypack/MyClass.java
package mypack;
public class MyClass {
public void show() { System.out.println("hello"); }
}
// file: Main.java
import mypack.MyClass;
public class Main {
public static void main(String[] args) {
new MyClass().show();
}
}
hello
PATH finds programs (
javac.exe). CLASSPATH finds classes (.class,.jar).
16 · static vs final
Define: static belongs to the class and is shared by every object. final means it cannot be changed.
| static | final |
|---|---|
| One copy for all objects | Cannot be reassigned |
static int count; |
final int MAX = 100; |
| Method callable without an object | Method cannot be overridden |
| — | Class cannot be extended |
class Counter {
static int total = 0;
int own = 0;
Counter() { total++; own++; }
}
public class Main {
public static void main(String[] args) {
new Counter();
new Counter();
System.out.println(Counter.total); // shared
System.out.println(new Counter().own); // per object
}
}
2
1
Don't confuse
final(modifier),finally(block),finalize()(method).
17 · this vs super
Define: this refers to the current object. super refers to the parent.
this |
super |
|---|---|
| This object | Parent object |
this() — own constructor |
super() — parent constructor |
this.x — own field |
super.x — parent field |
| — | super.m() — parent method |
class Person {
String name;
Person(String name) { this.name = name; }
void show() { System.out.println("Person " + name); }
}
class Employee extends Person {
int id;
Employee(String name, int id) {
super(name); // must be the first line
this.id = id;
}
@Override
void show() {
super.show();
System.out.println("Employee " + id);
}
}
public class Main {
public static void main(String[] args) {
new Employee("Riya", 501).show();
}
}
Person Riya
Employee 501
18 · Collections — List, Set, Map
Define: A collection is a container that grows and shrinks automatically, unlike an array.
| List | Set | Map | |
|---|---|---|---|
| Duplicates | Yes | No | Keys unique |
| Order | Yes | No | Depends |
| Example | ArrayList |
HashSet |
HashMap |
Map is NOT a Collection — it stores key-value pairs on its own branch. Say this.
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java"); list.add("Java");
System.out.println(list.size()); // 2
HashSet<String> set = new HashSet<>(list);
System.out.println(set.size()); // 1
HashMap<Integer, String> map = new HashMap<>();
map.put(101, "Aarav");
map.put(101, "Replaced");
System.out.println(map);
}
}
2
1
{101=Replaced}
19 · Multithreading
Define: Multithreading lets one program run several tasks at the same time.
Two ways: ① class MyThread extends Thread ② class MyTask implements Runnable ← preferred, because extends uses up your one inheritance slot.
Thread states — Java's Thread.State has SIX: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.
For the life-cycle diagram, draw five:
NEW --start()--> RUNNABLE --> RUNNING --run() ends--> TERMINATED
^ |
+-- sleep/wait/blocked --+
start() vs run(): start() creates a new thread. Calling run() directly is just a normal method call — no concurrency.
class Counter {
private int count = 0;
public synchronized void inc() { count++; } // one thread at a time
public int get() { return count; }
}
class Worker implements Runnable {
Counter c;
Worker(Counter c) { this.c = c; }
public void run() {
for (int i = 0; i < 100000; i++) c.inc();
}
}
public class Main {
public static void main(String[] args) throws Exception {
Counter c = new Counter();
Thread t1 = new Thread(new Worker(c));
Thread t2 = new Thread(new Worker(c));
t1.start(); t2.start();
t1.join(); t2.join();
System.out.println(c.get());
}
}
200000
Delete
synchronizedand the number comes out lower every run. That is a race condition — mention it.
20 · Custom Exception
Define: A custom exception is your own error type, made by extending Exception.
Three steps: ① extends Exception ② super(message) ③ throw it, and declare throws on the method.
class InvalidAgeException extends Exception {
InvalidAgeException(String m) {
super(m);
}
}
public class Main {
static void check(int age) throws InvalidAgeException {
if (age < 18)
throw new InvalidAgeException("Age " + age + " below 18");
System.out.println("ok " + age);
}
public static void main(String[] args) {
try { check(15); }
catch (InvalidAgeException e) {
System.out.println(e.getMessage());
}
try { check(20); }
catch (InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
}
Age 15 below 18
ok 20
21 · String Methods
Define: String has ready-made methods for searching, cutting and changing text. It is immutable — methods return a new string.
| Method | Does | Example → result |
|---|---|---|
length() |
count | "Hello".length() → 5 |
charAt(i) |
letter at i | "Hello".charAt(1) → e |
substring(a,b) |
cut | "Hello".substring(1,4) → ell |
toUpperCase() |
capitals | "ab".toUpperCase() → AB |
trim() |
removes end spaces | " hi ".trim() → hi |
indexOf(x) |
position | "Hello".indexOf('l') → 2 |
replace(a,b) |
swap | "cat".replace("c","b") → bat |
split(",") |
break up | "a,b".split(",") → [a, b] |
equals(x) |
compare text | "a".equals("a") → true |
public class Main {
public static void main(String[] args) {
String s = " Hello Java ";
System.out.println(s.trim().toUpperCase());
System.out.println(s.trim().substring(0, 5));
String[] p = "a,b,c".split(",");
System.out.println(p.length);
}
}
HELLO JAVA
Hello
3
length()has brackets (method).array.lengthhas none (field). Java tests this.
The 30-second decision tree
Stuck in the exam? Match the question to a shape.
| If the question starts with… | Write | Min |
|---|---|---|
| "Define" | 2 sentences. Stop. | 3 |
| "List any four" | 4 bullets | 4 |
| "Differentiate" | Table, 4 rows. Stop. | 8 |
| "Explain … with example" | Define → 3 points → code → output | 15 |
| "Write a program" | Code → output | 15 |
| "Describe with diagram" | Diagram first, then 3 labels | 12 |
If you blank completely
You still get marks for structure. Do this:
- Write the definition you half-remember. (1 mark)
- Draw a box diagram with the right words in boxes. (1 mark)
- Write the class skeleton —
public class X { public static void main(String[] args) { } }(1 mark) - Write one
System.out.printlnwith a plausible result. (½ mark)
Rough and structured beats blank every time. A blank page is the only guaranteed zero.
The 8 things to check before you hand in
- ☐ Every "explain" answer has a code example
- ☐ Every "differentiate" has 4 rows, not 2
- ☐ Every program has its output written under it
- ☐ You answered one side of each OR, not both
- ☐
import java.util.*;written for anyArrayList/HashMap/Scanner - ☐ Diagrams drawn for JVM architecture and thread life cycle
- ☐ Question numbers match the paper
- ☐ 10 minutes left to re-read
Companion to IMPORTANT-QUESTIONS-AND-ANSWERS.md (the teaching version). All code
targets Java 25 LTS and also compiles on Java 21 and Java 17.