0% found this document useful (0 votes)
7 views13 pages

Java Revision Guide

The document is a comprehensive Java revision guide covering essential topics such as variables, data types, control structures, object-oriented programming concepts, and collections. Each topic includes code examples and practice questions to reinforce learning. It serves as a valuable resource for both beginners and those looking to refresh their Java programming skills.

Uploaded by

harshalp2828
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Java Revision Guide

The document is a comprehensive Java revision guide covering essential topics such as variables, data types, control structures, object-oriented programming concepts, and collections. Each topic includes code examples and practice questions to reinforce learning. It serves as a valuable resource for both beginners and those looking to refresh their Java programming skills.

Uploaded by

harshalp2828
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Complete Revision Guide

All Topics | Full Code Examples | Practice Questions

# Topic # Topic

1 Variables & Data Types 14 Inheritance

2 Operators 15 Method Overloading

3 If-Else 16 Method Overriding

4 Switch Statement 17 Encapsulation

5 For Loop 18 Interface

6 While Loop 19 Abstract Class

7 Do-While Loop 20 Exception Handling

8 Functions / Methods 21 ArrayList

9 Arrays 22 HashMap

10 2D Arrays 23 Static Keyword

11 Strings 24 this & super

12 Class & Object 25 Multithreading

13 Constructor 26 File I/O


1. Variables & Data Types
public class Main {
public static void main(String[] args) {
int age = 20;
double price = 99.5;
String name = "Alice";
boolean isStudent = true;
[Link](name + " is " + age + " years old.");
[Link]("Price: " + price);
[Link]("Is Student: " + isStudent);
}
}

■ Practice Questions:
1. Declare variables for your name, age, height, and employment status. Print all.
2. What is the difference between int and double?
3. What happens if you store 3.5 in an int variable?

2. Operators
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
[Link]("Add: " + (a + b));
[Link]("Sub: " + (a - b));
[Link]("Mul: " + (a * b));
[Link]("Div: " + (a / b));
[Link]("Mod: " + (a % b));
[Link]("Greater: " + (a > b));
}
}

■ Practice Questions:
1. Find the remainder when 100 is divided by 7.
2. What is the result of 5 / 2 in Java? Why?
3. Check if a number is even or odd using the % operator.

3. If-Else
public class Main {
public static void main(String[] args) {
int marks = 75;
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 60) {
[Link]("Grade B");
} else {
[Link]("Fail");
}
}
}
■ Practice Questions:
1. Write a program to check if a number is positive, negative, or zero.
2. Check if a person is eligible to vote (age >= 18).
3. Find the largest of three numbers using if-else.

4. Switch Statement
public class Main {
public static void main(String[] args) {
int day = 2;
switch (day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
default: [Link]("Other day");
}
}
}

■ Practice Questions:
1. Write a switch program to print month name given its number (1-12).
2. Make a simple calculator using switch (+, -, *, /).
3. What happens if you forget to write break in a switch case?

5. For Loop
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("Number: " + i);
}
}
}

■ Practice Questions:
1. Print multiplication table of any number using for loop.
2. Print sum of numbers from 1 to 100.
3. Print all even numbers between 1 and 50.

6. While Loop
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("i = " + i);
i++;
}
}
}
■ Practice Questions:
1. Print digits of a number in reverse using while loop (e.g., 1234 -> 4 3 2 1).
2. Count how many digits are in a number.
3. Keep asking user for input until they enter 0 (use Scanner).

7. Do-While Loop
public class Main {
public static void main(String[] args) {
int i = 1;
do {
[Link]("i = " + i);
i++;
} while (i <= 5);
}
}

■ Practice Questions:
1. What is the difference between while and do-while?
2. Print numbers from 10 down to 1 using do-while.
3. Write a menu-driven program that runs until user selects Exit.

8. Functions (Methods)
public class Main {
static int add(int a, int b) {
return a + b;
}
static void greet(String name) {
[Link]("Hello, " + name + "!");
}
public static void main(String[] args) {
int result = add(5, 3);
[Link]("Sum: " + result);
greet("Alice");
}
}

■ Practice Questions:
1. Write a method to check if a number is prime.
2. Write a method that returns the factorial of a number.
3. Write a method to find the maximum of two numbers.

9. Arrays
public class Main {
public static void main(String[] args) {
int[] nums = {10, 20, 30, 40, 50};
for (int i = 0; i < [Link]; i++) {
[Link]("Element " + i + ": " + nums[i]);
}
}
}

■ Practice Questions:
1. Find the largest and smallest element in an array.
2. Reverse an array without using another array.
3. Find the sum and average of all elements in an array.

10. 2D Arrays
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}

■ Practice Questions:
1. Add two 3x3 matrices.
2. Find the sum of all elements in a 2D array.
3. Print only the diagonal elements of a matrix.

11. Strings
public class Main {
public static void main(String[] args) {
String s = "Hello Java";
[Link]("Length: " + [Link]());
[Link]("Uppercase: " + [Link]());
[Link]("Substring: " + [Link](6));
[Link]("Contains Java: " + [Link]("Java"));
[Link]("Replace: " + [Link]("Java", "World"));
[Link]("Trim: " + " hi ".trim());
}
}

■ Practice Questions:
1. Check if a string is a palindrome (e.g., madam).
2. Count the number of vowels in a string.
3. Reverse a string without using built-in reverse method.
12. Class & Object
public class Main {
static class Car {
String brand;
int speed;
void show() {
[Link](brand + " goes at " + speed + " km/h");
}
}
public static void main(String[] args) {
Car c1 = new Car();
[Link] = "Toyota";
[Link] = 120;
[Link]();
Car c2 = new Car();
[Link] = "BMW";
[Link] = 200;
[Link]();
}
}

■ Practice Questions:
1. Create a BankAccount class with balance, deposit(), and withdraw() methods.
2. Create a Student class with name, marks, and a method to print grade.
3. What is the difference between a class and an object?

13. Constructor
public class Main {
static class Student {
String name;
int age;
Student(String name, int age) {
[Link] = name;
[Link] = age;
}
void display() {
[Link]("Name: " + name + ", Age: " + age);
}
}
public static void main(String[] args) {
Student s1 = new Student("Bob", 20);
Student s2 = new Student("Alice", 22);
[Link]();
[Link]();
}
}

■ Practice Questions:
1. What is a default constructor? Write an example.
2. Create a Book class with title, author, price using constructor.
3. What is constructor overloading? Write an example.
14. Inheritance
public class Main {
static class Animal {
String name;
void eat() { [Link](name + " is eating."); }
}
static class Dog extends Animal {
void bark() { [Link](name + " is barking."); }
}
public static void main(String[] args) {
Dog d = new Dog();
[Link] = "Bruno";
[Link]();
[Link]();
}
}

■ Practice Questions:
1. Create a Shape class and extend it with Circle and Square classes.
2. What is multilevel inheritance? Give an example.
3. Can Java support multiple inheritance with classes? Why or why not?

15. Method Overloading


public class Main {
static int multiply(int a, int b) { return a * b; }
static double multiply(double a, double b) { return a * b; }
static int multiply(int a, int b, int c) { return a * b * c; }
public static void main(String[] args) {
[Link](multiply(2, 3));
[Link](multiply(2.5, 3.0));
[Link](multiply(2, 3, 4));
}
}

■ Practice Questions:
1. Write overloaded methods to calculate area of circle, rectangle, and triangle.
2. What is the difference between overloading and overriding?
3. Can two methods have the same name and parameters but different return types?

16. Method Overriding


public class Main {
static class Animal {
void sound() { [Link]("Some animal sound"); }
}
static class Dog extends Animal {
@Override
void sound() { [Link]("Dog barks"); }
}
static class Cat extends Animal {
@Override
void sound() { [Link]("Cat meows"); }
}
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
[Link]();
[Link]();
}
}

■ Practice Questions:
1. What is the use of @Override annotation?
2. Can we override a static method in Java?
3. Override a toString() method in a Person class to print name and age.

17. Encapsulation
public class Main {
static class Person {
private String name;
private int age;
public void setName(String name) { [Link] = name; }
public String getName() { return name; }
public void setAge(int age) { [Link] = age; }
public int getAge() { return age; }
}
public static void main(String[] args) {
Person p = new Person();
[Link]("Alice");
[Link](25);
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
}
}

■ Practice Questions:
1. Why do we use private variables with getters and setters?
2. Create an Employee class with encapsulated salary that cannot be negative.
3. What is the difference between public, private, and protected?

18. Interface
public class Main {
interface Shape {
double area();
double perimeter();
}
static class Rectangle implements Shape {
double length, width;
Rectangle(double l, double w) { [Link] = l; [Link] = w; }
public double area() { return length * width; }
public double perimeter() { return 2 * (length + width); }
}
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 3);
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
}
}

■ Practice Questions:
1. What is the difference between interface and abstract class?
2. Can a class implement multiple interfaces? Write an example.
3. Create a Printable interface and implement it in a Document class.

19. Abstract Class


public class Main {
abstract static class Vehicle {
String brand;
abstract void fuelType();
void start() { [Link](brand + " is starting..."); }
}
static class Bike extends Vehicle {
Bike(String brand) { [Link] = brand; }
public void fuelType() { [Link](brand + " uses Petrol."); }
}
static class ElectricCar extends Vehicle {
ElectricCar(String brand) { [Link] = brand; }
public void fuelType() { [Link](brand + " uses Electricity."); }
}
public static void main(String[] args) {
Vehicle v1 = new Bike("Honda");
[Link](); [Link]();
Vehicle v2 = new ElectricCar("Tesla");
[Link](); [Link]();
}
}

■ Practice Questions:
1. Can we create an object of an abstract class? Why?
2. What is the difference between abstract method and normal method?
3. Create an abstract class Animal with abstract method sound() and implement in 3 subclasses.

20. Exception Handling


public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 1;
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Array index error: " + [Link]());
} catch (Exception e) {
[Link]("General error: " + [Link]());
} finally {
[Link]("This always runs.");
}
}
}

■ Practice Questions:
1. What is the difference between checked and unchecked exceptions?
2. Create a custom exception called AgeNotValidException.
3. What does the finally block do? When does it not execute?

21. ArrayList
import [Link];

public class Main {


public static void main(String[] args) {
ArrayList fruits = new ArrayList();
[Link]("Apple");
[Link]("Banana");
[Link]("Mango");
[Link]("Banana");
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
}
[Link]("Size: " + [Link]());
}
}

■ Practice Questions:
1. What is the difference between Array and ArrayList?
2. Sort an ArrayList of integers in ascending order.
3. Remove duplicate elements from an ArrayList.

22. HashMap
import [Link];

public class Main {


public static void main(String[] args) {
HashMap scores = new HashMap();
[Link]("Alice", 90);
[Link]("Bob", 85);
[Link]("Charlie", 92);
for (Object key : [Link]()) {
[Link](key + " -> " + [Link](key));
}
}
}

■ Practice Questions:
1. What is the difference between HashMap and ArrayList?
2. Count the frequency of each character in a string using HashMap.
3. Check if a key exists in a HashMap before accessing it.

23. Static Keyword


public class Main {
static class Counter {
static int count = 0;
String name;
Counter(String name) {
[Link] = name;
count++;
}
static void showCount() {
[Link]("Total objects: " + count);
}
}
public static void main(String[] args) {
Counter c1 = new Counter("First");
Counter c2 = new Counter("Second");
Counter c3 = new Counter("Third");
[Link]();
}
}

■ Practice Questions:
1. What is the difference between static and non-static methods?
2. Can a static method access non-static variables? Why?
3. What is a static block? Write an example.

24. this & super Keywords


public class Main {
static class Animal {
String name = "Animal";
void display() { [Link]("I am an Animal"); }
}
static class Dog extends Animal {
String name = "Dog";
void display() {
[Link]("[Link]: " + [Link]);
[Link]("[Link]: " + [Link]);
[Link]();
}
}
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}

■ Practice Questions:
1. What is the use of this() and super() in constructors?
2. Can you use both this() and super() in the same constructor?
3. Write a program showing constructor chaining using super().

25. Multithreading
public class Main {
static class MyThread extends Thread {
String threadName;
MyThread(String name) { [Link] = name; }
public void run() {
for (int i = 1; i <= 3; i++) {
[Link](threadName + " - count: " + i);
}
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread("Thread-1");
MyThread t2 = new MyThread("Thread-2");
[Link]();
[Link]();
}
}

■ Practice Questions:
1. What is the difference between start() and run() in threads?
2. What is synchronization? Why is it needed?
3. Write a thread using Runnable interface instead of extending Thread.

26. File I/O


import [Link].*;

public class Main {


public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello from Java!\nSecond line.");
[Link]();
[Link]("File written successfully.");
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}

■ Practice Questions:
1. What is the difference between FileWriter and BufferedWriter?
2. Write a program to count the number of lines in a file.
3. Copy content from one file to another using Java.

Total: 26 Topics | 78 Practice Questions | Use any Java IDE or [Link] to run the code.

You might also like