9th final pratice
🌟 JAVA QUICK REVISION – FULL EXAM NOTE 🌟
(Read Carefully – High Scoring Concepts)
🔹 1. Encapsulation
Encapsulation means wrapping data (variables) and methods (functions) together into a single unit called a class.
It is achieved by:Declaring variables as
privateProviding access through
publicgetter and setter methods
Example:
class Student { private int marks; public void setMarks(int m) { marks = m; } public int getMarks() { return marks; } }🔹 2. Comparison Operator →
==Used to compare two values.
Returns
trueorfalse.
Example:
if (a == b)🔹 3. Loop That Runs At Least Once →
do-whileCondition is checked after execution.
Executes minimum one time even if condition is false.
Example:
do { System.out.println("Hello"); } while(condition);🔹 4. Constant Variable Keyword →
finalOnce assigned, value cannot be changed.
final int x = 10;🔹 5.
Math.sqrt(16)→ 4Math.sqrt()returns square root.
System.out.println(Math.sqrt(16)); // 4.0🔹 6. Logical AND →
&&Returns true only if both conditions are true.
Example:
if (a > 5 && b < 10)🔹 7. Best Loop When Iterations Are Known →
forUsed when number of repetitions is fixed.
Example:
for(int i = 1; i <= 5; i++)🔹 8. Compile-Time Error → Syntax Error
Occurs when grammar rules of Java are violated.
Found during compilation.
Example:
System.out.println("Hello") // Missing ;🔹 9. Maximum of Two Numbers →
Math.max()Math.max(10, 20); // Returns 20🔹 10. Statement Ends With →
;Every Java statement must end with a semicolon.
int a = 5;🔹 11. Decision Making Keyword →
ifUsed to check conditions.
if (a > b) { System.out.println("A is greater"); }🔹 12. Default Value of int (Instance Variable) → 0
Applies only to instance variables, not local variables.
🔹 13. Remainder Operator →
%Returns remainder after division.
10 % 3 // 1🔹 14. Error During Execution → Runtime Error
Happens while program is running.
Example: Division by zero.
🔹 15. Power Function →
Math.pow()Math.pow(2, 3); // 8.0🔹 16. Pre-Check Loops →
forandwhileCondition checked before execution.
May execute zero times.
🔹 17. Increment Operator →
++Increases value by 1.
a++; // or ++a;🔹 18. True/False Data Type →
booleanStores only:
truefalse
Example:
boolean flag = true;🔹 19. Stop a Loop →
breakImmediately terminates loop.
Example:
break;🔹 20. Assignment Operator →
=Used to assign value to variable.
int x = 10;🎯 FINAL REVISION TIPS
✔ Know difference between
=and==
✔ Understand loop types clearly
✔ Remember Math functions
✔ Practice operator precedence
✔ Focus on errors (Syntax vs Runtime vs Logical)
✔ Revise increment/decrement carefully🔥 If you revise this note properly, you are fully prepared to score maximum marks in your Java exam!
✍ Important Theory & Programming Notes
🔹 Mathematical Statement in Java
Sum of a raised to b and cube root of c:
Math.pow(a, b) + Math.cbrt(c);
🔹 While vs Do-While Loop
| while | do-while |
|---|---|
| Condition checked first | Condition checked after execution |
| May execute 0 times | Executes at least once |
🔹 Final & If Concepts
finalvariable → ❌ Cannot be changed after initialization.ifstatement → ✅ Can exist withoutelse.
🔹 Logical Output
int x = 5;
System.out.println(x > 2 && x < 4);
✔ Output → false
(Because 5 is not less than 4)
🔹 Type Casting Types
double m = 'b';→ Implicit casting (Widening)char ch = (char)68;→ Explicit casting (Narrowing)
🔹 Post-Decrement Concept
int lives = 9;
System.out.println(lives--);
System.out.println(lives);
✔ Output:
9
8
(Post-decrement prints first, then decreases)
🔹 Syntax Error vs Logical Error
| Syntax Error | Logical Error |
|---|---|
| Violates grammar rules | Program runs but gives wrong output |
| Found at compile time | Found after checking output |
🔹 Creating Scanner Object
Scanner sc = new Scanner(System.in);
🔹 Creating Object of Class BANK
BANK obj = new BANK();
🔹 Expression Evaluation Trick
Expression:
3 * 9 % 5 * 2 / 2 * 7 % 5
Step-by-step (Left to Right for *, /, %):
3 * 9 = 27
27 % 5 = 2
2 * 2 = 4
4 / 2 = 2
2 * 7 = 14
14 % 5 = 4
✔ Final Answer → 4
🎯 Final Exam Tips
Remember operator precedence:
*, /, %(Left to Right)Know difference between
=and==Understand loop conditions clearly
Practice Math functions
Focus on type casting and increment/decrement
🌟 JAVA PROGRAMMING SECTION – SOLVED & RESHUFFLED (With 5 Extra Questions) 🌟
(Perfect Practice Set for Full Marks – 15 Marks Each)
🔹 Question 1
Write a program to display an ATM Menu with the following options:
Check Balance
Withdraw Money
Deposit Money
Use if–else statements to perform the selected operation.
import java.util.Scanner;
public class ATMMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double balance = 10000;
System.out.println("1. Check Balance");
System.out.println("2. Withdraw Money");
System.out.println("3. Deposit Money");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
if (choice == 1) {
System.out.println("Balance = " + balance);
}
else if (choice == 2) {
System.out.print("Enter amount to withdraw: ");
double amount = sc.nextDouble();
if (amount <= balance) {
balance -= amount;
System.out.println("Remaining Balance = " + balance);
} else {
System.out.println("Insufficient Balance");
}
}
else if (choice == 3) {
System.out.print("Enter amount to deposit: ");
double amount = sc.nextDouble();
balance += amount;
System.out.println("Updated Balance = " + balance);
}
else {
System.out.println("Invalid Choice");
}
}
}
🔹 Question 2
Write a program to generate and display the first 10 terms of the Fibonacci series.
public class Fibonacci {
public static void main(String[] args) {
int a = 0, b = 1, c;
System.out.print(a + " " + b + " ");
for (int i = 3; i <= 10; i++) {
c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}
}
🔹 Question 3
Write a program to find the sum of digits of an integer.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of the digits = " + sum);
}
}
🔹 Question 4
Write a program to calculate the GCD using the continued division method.
import java.util.Scanner;
public class GCDProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int remainder;
while (b != 0) {
remainder = a % b;
a = b;
b = remainder;
}
System.out.println("GCD = " + a);
}
}
🔹 Question 5
Write a program to calculate volume of solids.
import java.util.Scanner;
public class VolumeSolids {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Cube
System.out.print("Enter side of cube: ");
double side = sc.nextDouble();
System.out.println("Volume of Cube = " + (side * side * side));
// Cylinder
System.out.print("Enter radius of cylinder: ");
double r = sc.nextDouble();
System.out.print("Enter height of cylinder: ");
double h = sc.nextDouble();
System.out.println("Volume of Cylinder = " + (Math.PI * r * r * h));
// Sphere
System.out.print("Enter radius of sphere: ");
double rs = sc.nextDouble();
System.out.println("Volume of Sphere = " + ((4.0/3) * Math.PI * rs * rs * rs));
}
}
🔹 Question
Write a program to check whether a number is Palindrome.
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
int original = num;
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
if (original == reverse)
System.out.println("Palindrome Number");
else
System.out.println("Not a Palindrome");
}
}
🔹 Question 7
Write a program to check whether a number is Prime.
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
boolean prime = true;
if (num <= 1)
prime = false;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
if (prime)
System.out.println("Prime Number");
else
System.out.println("Not Prime");
}
}
🔹 Question 8
Write a program to find factorial of a number.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
}
}
🔹 Question 9
Write a program to reverse a number.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
int reverse = 0;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
System.out.println("Reversed Number = " + reverse);
}
}
Here are extra Menu-Driven Programs with answers (Very Important for Exams – 15 Marks type questions).
🌟 1️⃣ Menu Driven Program – Simple Calculator
👉 Program to perform:
Addition
Subtraction
Multiplication
Division
import java.util.Scanner;
public class CalculatorMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter second number: ");
double b = sc.nextDouble();
if (choice == 1) {
System.out.println("Result = " + (a + b));
}
else if (choice == 2) {
System.out.println("Result = " + (a - b));
}
else if (choice == 3) {
System.out.println("Result = " + (a * b));
}
else if (choice == 4) {
if (b != 0)
System.out.println("Result = " + (a / b));
else
System.out.println("Division by zero not allowed");
}
else {
System.out.println("Invalid Choice");
}
}
}
Addition
Subtraction
Multiplication
Division
import java.util.Scanner;
public class CalculatorMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter second number: ");
double b = sc.nextDouble();
if (choice == 1) {
System.out.println("Result = " + (a + b));
}
else if (choice == 2) {
System.out.println("Result = " + (a - b));
}
else if (choice == 3) {
System.out.println("Result = " + (a * b));
}
else if (choice == 4) {
if (b != 0)
System.out.println("Result = " + (a / b));
else
System.out.println("Division by zero not allowed");
}
else {
System.out.println("Invalid Choice");
}
}
}
🌟 2️⃣ Menu Driven Program – Number Operations
👉 Options:
Check Even or Odd
Check Prime
Find Factorial
import java.util.Scanner;
public class NumberMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Check Even/Odd");
System.out.println("2. Check Prime");
System.out.println("3. Find Factorial");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (choice == 1) {
if (num % 2 == 0)
System.out.println("Even Number");
else
System.out.println("Odd Number");
}
else if (choice == 2) {
boolean prime = true;
if (num <= 1)
prime = false;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
if (prime)
System.out.println("Prime Number");
else
System.out.println("Not Prime");
}
else if (choice == 3) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
}
else {
System.out.println("Invalid Choice");
}
}
}
Check Even or Odd
Check Prime
Find Factorial
import java.util.Scanner;
public class NumberMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Check Even/Odd");
System.out.println("2. Check Prime");
System.out.println("3. Find Factorial");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (choice == 1) {
if (num % 2 == 0)
System.out.println("Even Number");
else
System.out.println("Odd Number");
}
else if (choice == 2) {
boolean prime = true;
if (num <= 1)
prime = false;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
if (prime)
System.out.println("Prime Number");
else
System.out.println("Not Prime");
}
else if (choice == 3) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
}
else {
System.out.println("Invalid Choice");
}
}
}
🌟 3️⃣ Menu Driven Program – Area Calculator
👉 Options:
Area of Circle
Area of Rectangle
Area of Triangle
import java.util.Scanner;
public class AreaMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Area of Circle");
System.out.println("2. Area of Rectangle");
System.out.println("3. Area of Triangle");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
if (choice == 1) {
System.out.print("Enter radius: ");
double r = sc.nextDouble();
System.out.println("Area = " + (Math.PI * r * r));
}
else if (choice == 2) {
System.out.print("Enter length: ");
double l = sc.nextDouble();
System.out.print("Enter breadth: ");
double b = sc.nextDouble();
System.out.println("Area = " + (l * b));
}
else if (choice == 3) {
System.out.print("Enter base: ");
double base = sc.nextDouble();
System.out.print("Enter height: ");
double h = sc.nextDouble();
System.out.println("Area = " + (0.5 * base * h));
}
else {
System.out.println("Invalid Choice");
}
}
}
Area of Circle
Area of Rectangle
Area of Triangle
import java.util.Scanner;
public class AreaMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("1. Area of Circle");
System.out.println("2. Area of Rectangle");
System.out.println("3. Area of Triangle");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
if (choice == 1) {
System.out.print("Enter radius: ");
double r = sc.nextDouble();
System.out.println("Area = " + (Math.PI * r * r));
}
else if (choice == 2) {
System.out.print("Enter length: ");
double l = sc.nextDouble();
System.out.print("Enter breadth: ");
double b = sc.nextDouble();
System.out.println("Area = " + (l * b));
}
else if (choice == 3) {
System.out.print("Enter base: ");
double base = sc.nextDouble();
System.out.print("Enter height: ");
double h = sc.nextDouble();
System.out.println("Area = " + (0.5 * base * h));
}
else {
System.out.println("Invalid Choice");
}
}
}
🔥 EXAM TIP (Very Important)
✔ Always display menu first
✔ Take input using Scanner
✔ Use if–else or switch
✔ Handle invalid choice
✔ Handle special cases (like division by zero)
If you want, I can also give:
⭐ Menu Driven using
switch⭐ Menu Driven using
do-while(repeat menu)⭐ ICSE Board style 15-mark answer format
⭐ Advanced menu (Banking system full version)
Comments
Post a Comment