[Go to site: main page, start]

0% found this document useful (0 votes)
8 views18 pages

Java Exam Revision Notes with Examples

The document provides concise and student-friendly notes on Java, covering essential concepts like Java basics, operators, control statements, OOP principles, and methods, along with clear examples and expected outputs for each topic. It includes a table of contents, quick exam tips, and practice multiple-choice questions for revision. The notes are designed for quick exam preparation, emphasizing clarity and practical understanding.

Uploaded by

khushidhir54
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)
8 views18 pages

Java Exam Revision Notes with Examples

The document provides concise and student-friendly notes on Java, covering essential concepts like Java basics, operators, control statements, OOP principles, and methods, along with clear examples and expected outputs for each topic. It includes a table of contents, quick exam tips, and practice multiple-choice questions for revision. The notes are designed for quick exam preparation, emphasizing clarity and practical understanding.

Uploaded by

khushidhir54
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 — Student-Friendly Notes (Clear examples, usage &

outputs)
Prepared for quick exam revision. Each concept has: 1) short explanation, 2) a compact example (with
proper indentation), 3) expected output.

Length: concise, clear and exam-focused. Read each example, run mentally or in an IDE, and check
the expected output.
Table of Contents
1. Introduction to Java (Hello World)
2. Java Basics (Identifiers, Variables, Data types, Casting)
3. Operators (examples + precedence)
4. Packages ([Link], [Link])
5. Math class functions (signature, return type, example, output)
6. Control Statements (if, switch, loops)
7. Methods (declarations, return types for int,double,String,char,boolean,array)
8. OOPs (Class/Object, Constructors, Encapsulation, Inheritance, Polymorphism, Abstraction, Enum)
9. Bubble Sort (code + output)
10. Quick exam tips (must-remember)
11. Practice MCQs (with answers)
1. Introduction to Java
What is Java (short):
Java is a high-level, object-oriented, platform-independent programming language. Java programs
compile to bytecode which runs on the JVM.

JVM / JRE / JDK:


- JVM: Java Virtual Machine. Runs .class (bytecode). - JRE: JVM + standard libraries required to run
Java programs. - JDK: Development kit (javac compiler + JRE + tools).

Hello World — example (exact indentation). Run this first to check environment.
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

Expected Output:
Hello, World!
2. Java Basics
Identifiers and naming rules (short):
- Must start with a letter, $, or _ (but avoid $ and _ in normal code). - No spaces, case-sensitive, cannot
use Java keywords.

Variables — declaration and usage (examples with output):


public class VariablesExample {
public static void main(String[] args) {
int a = 10;
double pi = 3.14;
char letter = 'A';
boolean flag = true;
String name = "Khushi";
[Link](a);
[Link](pi);
[Link](letter);
[Link](flag);
[Link](name);
}
}

Expected Output:
10
3.14
A
true
Khushi

Primitive data types (short examples):


// int (32-bit)
int x = 42; // Example usage: counting
float vs double (example):
float f = 2.5f; // 'f' suffix required
double d = 2.5; // default for decimals is double

String and common methods (example + output):


public class StringExample {
public static void main(String[] args) {
String s = "Hello";
[Link]([Link]()); // returns int
[Link]([Link](1)); // returns char (index 1)
[Link]([Link]()); // returns String
}
}

Expected Output:
5
e
HELLO

Arrays — 1D and 2D (example + output):


public class ArrayExample {
public static void main(String[] args) {
int[] a = {2, 4, 6};
[Link]([Link]); // prints 3
for (int i = 0; i < [Link]; i++) {
[Link](a[i]);
}
int[][] mat = { {1,2}, {3,4} };
[Link]([Link]); // rows = 2
[Link](mat[0][1]); // prints 2
}
}

Expected Output:
3
2
4
6
2
2

Type casting — implicit (widening) & explicit (narrowing):


double d = 5; // implicit: int -> double (5.0)
int i = (int) 3.9; // explicit: double -> int (3)

Expected Output (if printed):


5.0
3
3. Operators
Arithmetic operators (example + output):
public class Arith {
public static void main(String[] args) {
int a = 7, b = 3;
[Link](a + b); // 10
[Link](a - b); // 4
[Link](a * b); // 21
[Link](a / b); // 2 (integer division)
[Link](a % b); // 1 (remainder)
}
}

Expected Output:
10
4
21
2
1

Relational & Logical (example):


public class RelLog {
public static void main(String[] args) {
int x = 5, y = 10;
[Link](x > y); // false
[Link](x <= y); // true
[Link](x == 5 && y==10); // true
[Link](x == 5 || y==5); // true
}
}

Expected Output:
false
true
true
true

Ternary operator (example):


int n = 8;
String res = (n % 2 == 0) ? "Even" : "Odd";
[Link](res); // Even
4. Java Packages
[Link] (auto-imported): String, Math, Object, wrapper classes (Integer, Double, ...).
[Link] (common classes): Scanner (input), Arrays, ArrayList, HashMap.

Example: Using Scanner and ArrayList (example with expected output):


import [Link];
public class PkgExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("apple");
[Link]("banana");
[Link]([Link](0)); // apple
[Link]([Link]()); // 2
}
}

Expected Output:
apple
2
5. [Link] — common functions
- [Link](x) : Returns absolute value (int/double)
- [Link](a,b) : Returns larger of a and b (same type)
- [Link](a,b) : Returns smaller of a and b
- [Link](x) : Returns double (square root)
- [Link](a,b) : Returns double (a raised to b)
- [Link](x) : Returns double (smallest integer >= x)
- [Link](x) : Returns double (largest integer <= x)
- [Link](x) : Returns long (if double) or int (if float)
- [Link]() : Returns double between 0.0 (inclusive) and 1.0 (exclusive)

Examples (code + output):


public class MathEx {
public static void main(String[] args) {
[Link]([Link](-5)); // 5
[Link]([Link](3,7)); // 7
[Link]((int)[Link](16)); // 4
[Link]([Link](2,3)); // 8.0
[Link]([Link](3.2)); // 4.0
[Link]([Link](3.8)); // 3.0
[Link]([Link](3.6)); // 4
[Link]([Link]()); // e.g., 0.3745 (changes)
}
}

Expected Output (example):


5
7
4
8.0
4.0
3.0
4
0.3745 (varies)
6. Control Statements
if / if-else (example + output):
public class IfExample {
public static void main(String[] args) {
int marks = 72;
if (marks >= 90) {
[Link]("A+");
} else if (marks >= 75) {
[Link]("A");
} else if (marks >= 60) {
[Link]("B");
} else {
[Link]("Fail");
}
}
}

Expected Output:
A

switch (example + output):


public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch(day) {
case 1: [Link]("Mon"); break;
case 2: [Link]("Tue"); break;
case 3: [Link]("Wed"); break;
default: [Link]("Other");
}
}
}

Expected Output:
Wed

for loop (example + output):


for (int i = 0; i < 3; i++) {
[Link](i);
}

Expected Output:
0
1
2

while loop (example + output):


int i = 0;
while (i < 3) {
[Link](i);
i++;
}

Expected Output:
0
1
2
do-while (example + output):
int i = 0;
do {
[Link](i);
i++;
} while (i < 2);

Expected Output:
0
1

enhanced for (foreach) (example + output):


int[] a = {5,6,7};
for (int v : a) {
[Link](v);
}

Expected Output:
5
6
7

break / continue (example + output):


for (int i = 0; i < 5; i++) {
if (i == 3) break;
if (i == 1) continue;
[Link](i);
}

Expected Output:
0
2
7. Methods
Definition: A method is a block of code that performs a task. Syntax: returnType name(parameters) {
body }

Examples - methods with different return types (example + output):


public class MethodsExample {
// returns int
public static int add(int a, int b) {
return a + b;
}
// returns double
public static double areaCircle(double r) {
return [Link] * r * r;
}
// returns String
public static String greet(String name) {
return "Hello, " + name;
}
// returns boolean
public static boolean isEven(int n) {
return n % 2 == 0;
}
public static void main(String[] args) {
[Link](add(2,3)); // 5
[Link]((int)areaCircle(1)); // 3 (approx)
[Link](greet("Khushi")); // Hello, Khushi
[Link](isEven(7)); // false
}
}

Expected Output (approx):


5
3
Hello, Khushi
false

Actual vs Formal arguments:


- Actual: values passed when calling a method (e.g., add(2,3)). - Formal: parameter names in method
declaration (e.g., int a, int b).

Pure vs Impure functions (short):


- Pure function: no side effects, same output for same input. Example: add(a,b). - Impure function:
changes external state or depends on it. Example: method that modifies a global/static list.
8. Object-Oriented Programming (OOP) Concepts
Class & Object (example + output):
class Student {
String name;
int roll;
Student(String name, int roll) {
[Link] = name;
[Link] = roll;
}
void show() {
[Link](name + " - " + roll);
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s = new Student("Asha", 7);
[Link](); // Asha - 7
}
}

Expected Output:
Asha - 7

Constructors (default, parameterized, copy) with examples:


class Point {
int x, y;
// default constructor
Point() { x = 0; y = 0; }
// parameterized constructor
Point(int x, int y) { this.x = x; this.y = y; }
// copy constructor (manual)
Point(Point p) { this.x = p.x; this.y = p.y; }
}
Usage (example):
Point p1 = new Point(); // p1 = (0,0)
Point p2 = new Point(3,4); // p2 = (3,4)
Point p3 = new Point(p2); // p3 = (3,4) copy

Encapsulation (example + output):


class BankAccount {
private double balance;
public void deposit(double amt) { if (amt>0) balance += amt; }
public void withdraw(double amt) { if (amt>0 && amt <= balance) balance -= amt; }
public double getBalance() { return balance; }
}
public class BankDemo {
public static void main(String[] args) {
BankAccount b = new BankAccount();
[Link](1000);
[Link](250);
[Link]((int)[Link]()); // 750
}
}

Expected Output:
750

Inheritance & Polymorphism (example + output):


class Animal {
void sound() { [Link]("Some sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Bark"); } // overriding
}
public class InheritDemo {
public static void main(String[] args) {
Animal a = new Dog(); // polymorphism: reference type Animal, object Dog
[Link](); // Bark (runtime overriding)
}
}

Expected Output:
Bark

Abstraction (interface example):


interface Shape {
double area();
}
class Circle implements Shape {
double r;
Circle(double r){ this.r = r; }
public double area() { return [Link] * r * r; }
}
public class AbstrDemo {
public static void main(String[] args) {
Shape s = new Circle(1);
[Link]((int)[Link]()); // 3 (approx)
}
}
Enum — short example:
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
public class EnumDemo {
public static void main(String[] args) {
Day d = [Link];
[Link](d); // WED
}
}

Expected Output:
WED
9. Bubble Sort (example + output)
public class BubbleSortDemo {
public static void main(String[] args) {
int[] arr = {5, 3, 1, 4, 2};
[Link]("Before:");
for (int v : arr) [Link](v + " ");
// bubble sort
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
int t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
}
}
}
[Link]("\nAfter:");
for (int v : arr) [Link](v + " ");
}
}

Expected Output:
Before:
5 3 1 4 2
After:
1 2 3 4 5
10. Quick exam tips — must remember
1. String comparison: use equals() not == for content.
2. Default values: int -> 0, double -> 0.0, boolean -> false, object refs -> null.
3. main signature: public static void main(String[] args)
4. Java is pass-by-value (objects: reference value is passed).
5. Array length is property ([Link]), String length() is method.
6. Integer division truncates decimals (7/2 = 3).
7. Use braces {} to avoid 'dangling else' mistakes.
11. Practice MCQs (answers below)
1. Which of these is not a Java feature?
(a) Object-Oriented (b) Use of pointers (c) Robust (d) Secure
2. Default value of boolean is:
(a) true (b) false (c) 0 (d) null
3. Which package contains Scanner?
(a) [Link] (b) [Link] (c) [Link] (d) [Link]
4. Which keyword is used for inheritance?
(a) implement (b) extends (c) inherit (d) super
5. What does [Link](3.6) return?
(a) 3 (b) 4 (c) 3.6 (d) Error
6. String length of "Hi"?
(a) 1 (b) 2 (c) 0 (d) 3
7. Output of: int x = 5/2; [Link](x);
(a) 2.5 (b) 2 (c) 3 (d) Error
8. Which compares references?
(a) equals() (b) compareTo() (c) == (d) contains()
9. Which is correct main method signature?
(a) public void main(String[] args) (b) public static void main(String args) (c) public static void
main(String[] args) (d) static public void main()
10. What is output of: [Link]([Link](2,7));
(a) 2 (b) 7 (c) 0 (d) Error
Answers (MCQs):
1.(b) 2.(b) 3.(b) 4.(b) 5.(b) 6.(b) 7.(b) 8.(c) 9.(c) 10.(b)

You might also like