15 JAVA PROGRAMS
1. Hello World
Java
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Output: Hello, World!
2. Add Two Numbers
Java
import [Link];
public class AddNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
int a = [Link]();
int b = [Link]();
[Link]("Sum: " + (a + b));
}
}
Input: 5 10 | Output: Sum: 15
3. Even or Odd Number
Java
public class EvenOdd {
public static void main(String[] args) {
int num = 7;
if(num % 2 == 0) [Link](num + " is Even");
else [Link](num + " is Odd");
}
}
Output: 7 is Odd
4. Factorial of a Number
Java
public class Factorial {
public static void main(String[] args) {
int n = 5, fact = 1;
for(int i=1; i<=n; i++) fact *= i;
[Link]("Factorial of 5 is: " + fact);
}
}
Output: Factorial of 5 is: 120
5. Prime Number Check
Java
public class PrimeCheck {
public static void main(String[] args) {
int num = 13;
boolean isPrime = true;
for(int i=2; i <= num/2; i++) {
if(num % i == 0) { isPrime = false; break; }
}
[Link](num + " is prime: " + isPrime);
}
}
Output: 13 is prime: true
6. Reverse a String
Java
public class ReverseString {
public static void main(String[] args) {
String str = "Java", rev = "";
for(int i = [Link]()-1; i >= 0; i--) rev += [Link](i);
[Link]("Reversed: " + rev);
}
}
Output: Reversed: avaJ
7. Palindrome String
Java
public class Palindrome {
public static void main(String[] args) {
String s = "madam", r = new StringBuilder(s).reverse().toString();
[Link]([Link](r) ? "Palindrome" : "Not Palindrome");
}
}
Output: Palindrome
8. Largest of Three Numbers
Java
public class Largest {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
int max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
[Link]("Largest: " + max);
}
}
Output: Largest: 25
9. Fibonacci Series
Java
public class Fibonacci {
public static void main(String[] args) {
int n = 5, a = 0, b = 1;
for(int i=0; i<n; i++) {
[Link](a + " ");
int next = a + b; a = b; b = next;
}
}
}
Output: 0 1 1 2 3
10. Sort an Array
Java
import [Link];
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1};
[Link](arr);
[Link]([Link](arr));
}
}
Output: [1, 2, 5, 8]
11. Search Element in Array (Linear Search)
Java
public class Search {
public static void main(String[] args) {
int[] arr = {10, 20, 30}; int target = 20;
for(int i=0; i<[Link]; i++) {
if(arr[i] == target) [Link]("Found at index: " + i);
}
}
}
Output: Found at index: 1
12. Matrix Addition
Java
public class MatrixAdd {
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}}, b = {{1, 1}, {1, 1}};
for(int i=0; i<2; i++){
for(int j=0; j<2; j++) [Link]((a[i][j] + b[i][j]) + " ");
[Link]();
}
}
}
Output: 2 3 4 5
13. Inheritance
Java
class Animal { void eat() { [Link]("Eating..."); } }
class Dog extends Animal { void bark() { [Link]("Barking..."); } }
public class TestInheritance {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); [Link]();
}
}
Output: Eating... | Barking...
14. Method Overloading
Java
public class Overload {
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }
public static void main(String[] args) {
[Link](add(5, 5));
[Link](add(5, 5, 5));
}
}
Output: 10 | 15
15. Exception Handling
Java
public class ExceptionHandle {
public static void main(String[] args) {
try {
int data = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Error: Cannot divide by zero");
} finally {
[Link]("Execution finished.");
}
}
}
Output: Error: Cannot divide by zero | Execution finished.
15 QUERIES IN MYSQL
Prerequisites: Database & Table Setup
Run these first to create your environment:
SQL
CREATE DATABASE SchoolDB;
USE SchoolDB;
CREATE TABLE STUDENT (
RollNo INT PRIMARY KEY,
Name VARCHAR(30),
Gender CHAR(1),
Stream VARCHAR(15),
Marks INT,
City VARCHAR(20)
);
INSERT INTO STUDENT VALUES
(1, 'Aryan', 'M', 'Science', 95, 'Delhi'),
(2, 'Sanya', 'F', 'Commerce', 88, 'Mumbai'),
(3, 'Rohan', 'M', 'Humanities', 75, 'Delhi'),
(4, 'Isha', 'F', 'Science', 92, 'Bangalore'),
(5, 'Kabir', 'M', 'Commerce', 82, 'Mumbai');
15 Practical Queries & Outputs
1. Display the structure of the STUDENT table.
SQL
DESC STUDENT;
Output: A table showing Column names, Data Types, and Constraints (Null, Key,
etc.).
2. Display all records from the table.
SQL
SELECT * FROM STUDENT;
Output: All 5 rows with all columns (RollNo, Name, Gender, Stream, Marks,
City).
3. Display Name and Marks of students who scored more than 90.
SQL
SELECT Name, Marks FROM STUDENT WHERE Marks > 90;
Output: Aryan (95), Isha (92).
4. Display names of students whose stream is 'Science' or 'Commerce'.
SQL
SELECT Name FROM STUDENT WHERE Stream IN ('Science', 'Commerce');
Output: Aryan, Sanya, Isha, Kabir.
5. Find the average marks of all students.
SQL
SELECT AVG(Marks) AS Average_Marks FROM STUDENT;
Output: 86.4
6. Display names of students starting with the letter 'S'.
SQL
SELECT Name FROM STUDENT WHERE Name LIKE 'S%';
Output: Sanya.
7. Count the number of students from 'Mumbai'.
SQL
SELECT COUNT(*) FROM STUDENT WHERE City = 'Mumbai';
Output: 2
8. Display student details sorted by Marks in descending order.
SQL
SELECT * FROM STUDENT ORDER BY Marks DESC;
Output: List starting with Aryan (95) down to Rohan (75).
9. Display total marks scored by students in each Stream.
SQL
SELECT Stream, SUM(Marks) FROM STUDENT GROUP BY Stream;
Output: Science: 187, Commerce: 170, Humanities: 75.
10. Display names of students who do NOT live in 'Delhi'.
SQL
SELECT Name FROM STUDENT WHERE City <> 'Delhi';
Output: Sanya, Isha, Kabir.
11. Add a new column 'Grade' to the table.
SQL
ALTER TABLE STUDENT ADD (Grade CHAR(1));
Output: Query OK, 0 rows affected.
12. Update the Grade to 'A' for students with marks above 90.
SQL
UPDATE STUDENT SET Grade = 'A' WHERE Marks > 90;
Output: Query OK, 2 rows affected.
13. Display the highest and lowest marks in the class.
SQL
SELECT MAX(Marks), MIN(Marks) FROM STUDENT;
Output: 95, 75.
14. Find the number of Male (M) and Female (F) students.
SQL
SELECT Gender, COUNT(*) FROM STUDENT GROUP BY Gender;
Output: M: 3, F: 2.
15. Delete the record of the student with RollNo 3.
SQL
DELETE FROM STUDENT WHERE RollNo = 3;
Output: Query OK, 1 row affected.