Java Full Revision-1
Java Full Revision-1
MUST PREPARE IMPORTANT ★ = Key point to remember Exam Tip = Direct PYQ hint
Bytecode Intermediate .class file produced by javac compiler — NOT machine code. JVM reads this.
JVM Java Virtual Machine — reads bytecode and runs it on any operating system.
WORA Write Once Run Anywhere — same .class file runs on Windows, Linux, Mac without
recompiling.
Exam Tip: Part A: significance of bytecode = platform independence. Part C: explain JVM + list all 7 features.
Encapsulation Wrapping Abstraction Hide Inheritance Child class gets Polymorphism One name,
data + methods in a class. implementation details. fields and methods of parent many forms. Overloading =
Use private fields + Show only what is class using extends. compile-time. Overriding =
getters/setters. necessary to the user. runtime.
1.3 Operators
• Bitwise: & (AND) | (OR) ^ (XOR) ~ (NOT) << (left shift) >> (right shift)
• Logical: && (short-circuit AND) || (short-circuit OR) ! (NOT)
• Relational: == != < > <= >= — always return true or false
★ Difference: & evaluates BOTH sides always. && stops at first false (short-circuit).
Exam Tip: 2024-25 Part B exact output question: p=9, q=5. Memorise 9|5=13 and 9&5=1.
Explicit Large to Small Must write (type) Yes — double x=9.9; int y=(int)x; // y=9
(Narrowing) decimal cut
Exam Tip: 2024-25 Part B: difference between implicit and explicit casting — write both with example and output.
Note: Constructor = same name as class, NO return type, called automatically when object is created.
class Student {
String name; int age;
Student() { name = "Unknown"; age = 0; } // 1. default
Student(String n, int a) { name=n; age=a; } // 2. parameterized
Student(Student s) { name=[Link]; age=[Link]; } // 3. copy
void show() { [Link](name + " | " + age); }
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Rahul", 20);
Student s3 = new Student(s2); // copy of s2
[Link](); [Link](); [Link]();
}
}
OUTPUT:
Unknown | 0
Rahul | 20
Rahul | 20
Exam Tip: 2023-24 Part B: write all 3 constructor types in one class — easy full 4 marks.
Exam Tip: 2024-25 Part B: Can we overload main()? Always answer YES with a program.
• Use 1: Distinguish field from parameter with same name — [Link] = name
• Use 2: Call another constructor in same class — this() (must be first statement)
• Use 3: Pass current object as argument — someMethod(this)
Exam Tip: Part A: always mention ALL 3 uses for full 2 marks.
2.5 Recursion
OUTPUT:
factorial(5) = 120
factorial(6) = 720
Exam Tip: Aug 2023 Part B: find factorial using recursion. Trace the steps in your answer.
3.1 Interface
Note: Interface = a pure contract. It says WHAT to do, not HOW to do it.
interface Shape {
double getArea(); // abstract by default
double getPerimeter();
}
class Circle implements Shape {
double r;
Circle(double r) { this.r = r; }
public double getArea() { return [Link] * r * r; }
public double getPerimeter() { return 2 * [Link] * r; }
}
class Rectangle implements Shape {
double l, b;
Rectangle(double l, double b) { this.l=l; this.b=b; }
public double getArea() { return l * b; }
public double getPerimeter() { return 2*(l+b); }
}
Shape s = new Circle(7); // interface reference variable
[Link]("Circle Area: %.2f%n", [Link]());
OUTPUT:
Exam Tip: Appeared ALL 3 papers. Prepare Shape with Circle + Rectangle — most repeated program.
try {
int result = 10 / 0; // ArithmeticException here
} catch(ArithmeticException e) {
[Link]("Error: " + [Link]());
} finally {
[Link]("Finally always runs"); // even if no error
}
// throw manually:
if(age < 0) throw new ArithmeticException("Age cannot be negative");
OUTPUT:
Error: / by zero
Exam Tip: 2023-24 Part B: division-by-zero program. Always write finally block. 2-mark: difference checked vs unchecked.
• Overriding = child class gives its own version of parent method (same name + same parameters)
• Rules: cannot override final, static, or private methods
• Dynamic dispatch = parent reference holds child object → child method called at RUNTIME
★ This is runtime polymorphism. Method resolved based on ACTUAL object type, not reference type.
Exam Tip: Part A: distinguish abstract class vs concrete class — write 3 differences.
3.5 Packages
Exam Tip: Aug 2023 Part C: define package + benefits + example. Access levels table is common 2-marker.
Note: Thread = a lightweight sub-process. Multiple threads run simultaneously in same program.
• Way 1: Extend Thread class — override run() method — call start() to launch
• Way 2: Implement Runnable interface — implement run() — pass to Thread — call start()
• Prefer Runnable when your class already extends another class (Java = single class inheritance)
★ ALWAYS call start() — NOT run(). start() creates a new thread. run() alone executes in same thread.
Exam Tip: isAlive() = true if thread started and not dead. join() = current thread waits for that thread to finish.
Exam Tip: Part A: List thread states = New, Runnable, Running, Blocked, Dead — very common 2-marker.
• Problem: 2+ threads access shared data simultaneously → race condition → wrong/corrupt data
• Solution: synchronized keyword — only ONE thread can run the method at a time
• Lock/Monitor: each object has a lock; synchronized method automatically acquires it
• Synchronized method: synchronized void myMethod() { ... }
• Synchronized block: synchronized(obj) { ... } — gives finer control
• wait(): release lock and wait | notify(): wake one waiting thread | notifyAll(): wake all
★ Without synchronization: two threads can corrupt shared data. Bank account withdrawal is the classic
example.
class BankAccount {
int balance = 1000;
synchronized void withdraw(int amount) {
// only ONE thread runs this at a time
if(balance >= amount) {
balance -= amount;
[Link]("Withdrawn. Balance: " + balance);
} else {
[Link]("Insufficient balance");
}
}
}
Exam Tip: Appeared ALL 3 papers. 10-mark answer needs: WHY needed + synchronized keyword + program + lock concept.
Exam Tip: Part A: 'What is daemon thread?' | 'How assign priority?' | 'Role of deadlock?' — all common 2-markers.
Note: Applet = Java program that runs inside a web browser. It has NO main() method.
Exam Tip: Appeared ALL 3 papers. For Part C: draw this lifecycle diagram + write the applet program = easy 10 marks.
Exam Tip: Appeared ALL 3 papers. Know both server-side AND client-side code.
3 Network Protocol Connects through middleware server — good for web apps Fast
• 6 steps: Load driver → Create Connection → Create Statement → Execute Query → Process ResultSet → Close
Exam Tip: Part A: 'Need of JDBC type 3 and type 4' = 2 marks. Type 3 = web apps via middleware. Type 4 = direct DB.
Data handled Raw binary (8-bit bytes) Text (16-bit Unicode characters)
Used for Images, audio, any binary file Text files only
class ExceptionDemo {
public static void main(String[] args) {
// Example 1: division by zero
try {
int result = 10 / 0; // ERROR here — ArithmeticException
[Link](result); // this line is SKIPPED
} catch(ArithmeticException e) {
[Link]("Caught: " + [Link]());
} finally {
[Link]("Finally block — always runs");
}
// Example 2: manually throw exception
try {
int age = -5;
if(age < 0)
throw new ArithmeticException("Age cannot be negative");
} catch(ArithmeticException e) {
[Link]("Thrown: " + [Link]());
}
}
}
OUTPUT:
Caught: / by zero
Exam tip: Division-by-zero is the most asked program. Always write finally block — examiners check for it.
Unit 3 | Part B 4m | Part C 10m
Program 2 — Interface (Shape with Circle & Rectangle) | appeared ALL 3 papers
• Interface = pure contract. All methods are abstract by default.
• Class implements interface using implements keyword.
• Interface reference variable: Shape s = new Circle(7); enables runtime polymorphism.
interface Shape {
double getArea();
double getPerimeter();
}
class Circle implements Shape {
double r;
Circle(double r) { this.r = r; }
public double getArea() { return [Link] * r * r; }
public double getPerimeter() { return 2 * [Link] * r; }
}
class Rectangle implements Shape {
double l, b;
Rectangle(double l, double b) { this.l=l; this.b=b; }
public double getArea() { return l * b; }
public double getPerimeter() { return 2 * (l + b); }
}
class InterfaceDemo {
public static void main(String[] args) {
Shape s1 = new Circle(7);
Shape s2 = new Rectangle(4, 5);
[Link]("Circle Area: %.2f%n", [Link]());
[Link]("Rectangle Area: %.2f%n", [Link]());
}
}
OUTPUT:
Exam tip: Appeared ALL 3 papers! Most repeated program in Unit 3. Prepare both Circle and Rectangle classes.
Unit 4 | Part C 10m | appeared
Program 3 — Thread Synchronization ALL 3 papers
• Without synchronized: two threads corrupt shared data (race condition).
• synchronized keyword allows only ONE thread to run the method at a time.
• Each object has a lock — synchronized method acquires it automatically.
class BankAccount {
int balance = 1000;
// synchronized = only 1 thread at a time
synchronized void withdraw(int amount) {
if(balance >= amount) {
[Link]([Link]().getName()
+ " withdrawing: " + amount);
balance -= amount;
[Link]("Remaining: " + balance);
} else {
[Link]("Insufficient balance");
}
}
}
class SyncDemo {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
// both threads share same account
Thread t1 = new Thread(() -> [Link](600), "T1");
Thread t2 = new Thread(() -> [Link](600), "T2");
[Link]();
[Link]();
}
}
OUTPUT:
T1 withdrawing: 600
Remaining: 400
Exam tip: Appeared ALL 3 papers. 10-mark: explain WHY needed + synchronized keyword + program + lock/monitor concept.
OUTPUT:
Good morning
Do Yoga
Good morning
Keep healthy
Exam tip: 2024-25 Part C Q4 EXACT question. Always wrap [Link]() in try-catch — forgetting it = compile error.
Unit 5 | Part C 10m | 2024-25
Program 5 — Applet with Three Colored Rectangles exact PYQ — appeared ALL 3
papers
• Applet extends Applet class. No main() method needed.
• paint(Graphics g) is called automatically to draw on screen.
• fillRect(x, y, width, height) draws a filled rectangle at position x,y.
import [Link].*;
import [Link].*;
/*
* HTML to run this applet:
* <applet code='[Link]' width=300 height=320></applet>
*/
public class ColorApplet extends Applet {
public void init() {
setBackground([Link]); // white background
}
public void paint(Graphics g) {
// Rectangle 1 — RED (top)
[Link]([Link]);
[Link](50, 20, 200, 70);
// Rectangle 2 — BLUE (middle)
[Link]([Link]);
[Link](50, 110, 200, 70);
// Rectangle 3 — BLACK (bottom)
[Link]([Link]);
[Link](50, 200, 200, 70);
}
}
OUTPUT:
Exam tip: 2024-25 Part C Q5 exact. After writing code, also write applet lifecycle (init->start->paint->stop->destroy) = full 10
marks.
Unit 5 | Part B 4m | Part C 10m
Program 6 — TCP/IP Server and Client Socket | appeared ALL 3 papers
• ServerSocket listens on a port. accept() blocks until a client connects.
• Socket connects to server using IP address and port number.
• Run SERVER first in one terminal, then CLIENT in another terminal.
OUTPUT:
Exam tip: Appeared ALL 3 papers. Know both programs. Steps: ServerSocket->accept->streams->read/write->close.