0% found this document useful (0 votes)
19 views12 pages

Java Programming Concepts Explained

Uploaded by

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

Java Programming Concepts Explained

Uploaded by

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

Q1.

Print your name, age, and city

Description: Learn variables & printing.

public class Main {

public static void main(String[] args) {

String name = "Arif Dhali";

int age = 24;

String city = "Kolkata";

[Link]("Name: " + name);

[Link]("Age: " + age);

[Link]("City: " + city);

Q2. Simple Calculator (Add, Subtract, Multiply, Divide)

Description: Practice operators & user input.

import [Link];

public class Calculator {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

[Link]("Addition: " + (a + b));


[Link]("Subtraction: " + (a - b));

[Link]("Multiplication: " + (a * b));

[Link]("Division: " + (a / b));

2. Control Flow (if-else, switch, loops)

✅ Concepts: Conditions & looping

Q1. Check if a number is even or odd

import [Link];

public class EvenOdd {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int n = [Link]();

if (n % 2 == 0)

[Link]("Even");

else

[Link]("Odd");

Q2. Print multiplication table of 5

public class Table {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

[Link]("5 x " + i + " = " + (5 * i));

}
}

Q3. Find sum of digits of a number

public class SumDigits {

public static void main(String[] args) {

int num = 1234, sum = 0;

while (num > 0) {

sum += num % 10;

num /= 10;

[Link]("Sum = " + sum);

3. Functions & Methods

✅ Concepts: Function definition, return, recursion

Q1. Factorial of a number

public class Factorial {

static int factorial(int n) {

if (n == 0 || n == 1) return 1;

return n * factorial(n - 1);

public static void main(String[] args) {

[Link](factorial(5)); // 120

}
Q2. Find maximum of 3 numbers using a method

public class MaxOfThree {

static int max(int a, int b, int c) {

return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

public static void main(String[] args) {

[Link]("Max: " + max(10, 25, 5));

4. Arrays

✅ Concepts: Traversal, min/max, reverse

Q1. Find largest element in array

public class MaxArray {

public static void main(String[] args) {

int[] arr = {4, 9, 2, 15, 7};

int max = arr[0];

for (int num : arr) {

if (num > max) max = num;

[Link]("Max = " + max);

Q2. Reverse an array

import [Link];
public class ReverseArray {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

int i = 0, j = [Link] - 1;

while (i < j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

i++; j--;

[Link]([Link](arr));

5. Strings

✅ Concepts: Palindrome, vowel count, reverse

Q1. Check if string is palindrome

public class Palindrome {

static boolean isPalindrome(String s) {

int i = 0, j = [Link]() - 1;

while (i < j) {

if ([Link](i) != [Link](j)) return false;

i++; j--;

return true;

public static void main(String[] args) {


[Link](isPalindrome("madam")); // true

[Link](isPalindrome("hello")); // false

Q2. Count vowels in string

public class CountVowels {

public static void main(String[] args) {

String str = "programming";

int count = 0;

for (char ch : [Link]()) {

if ("aeiouAEIOU".indexOf(ch) != -1) count++;

[Link]("Vowels: " + count);

6. OOP (Classes & Objects)

Q1. Car class with drive method

class Car {

String brand;

int speed;

Car(String brand, int speed) {

[Link] = brand;

[Link] = speed;

}
void drive() {

[Link](brand + " is driving at " + speed + "


km/h");

public class Main {

public static void main(String[] args) {

Car c = new Car("BMW", 120);

[Link]();

Q2. BankAccount with deposit & withdraw

class BankAccount {

private double balance;

public BankAccount(double initial) {

balance = initial;

public void deposit(double amount) {

balance += amount;

public void withdraw(double amount) {

if (amount <= balance) balance -= amount;

else [Link]("Insufficient balance!");

}
public double getBalance() {

return balance;

public class Main {

public static void main(String[] args) {

BankAccount acc = new BankAccount(500);

[Link](200);

[Link](100);

[Link]("Balance: " + [Link]());

7. Exception Handling

Q1. Divide by zero handling

public class ExceptionExample {

public static void main(String[] args) {

try {

int result = 10 / 0;

} catch (ArithmeticException e) {

[Link]("Error: " + [Link]());

8. Collections (List, Set, Map)

Q1. Store and print names


import [Link].*;

public class Names {

public static void main(String[] args) {

ArrayList<String> names = new ArrayList<>();

[Link]("Arif");

[Link]("Soumya");

[Link]("Rahul");

for (String n : names) {

[Link](n);

Q2. Word frequency counter

import [Link].*;

public class WordCount {

public static void main(String[] args) {

String text = "java is fun and java is powerful";

String[] words = [Link](" ");

HashMap<String, Integer> map = new HashMap<>();

for (String w : words) {

[Link](w, [Link](w, 0) + 1);

[Link](map);

}
}

9. File Handling

Q1. Write to file

import [Link];

public class WriteFile {

public static void main(String[] args) {

try {

FileWriter writer = new FileWriter("[Link]");

[Link]("Hello Java!");

[Link]();

[Link]("File written.");

} catch (Exception e) {

[Link]();

Q2. Read file

import [Link];

import [Link];

public class ReadFile {

public static void main(String[] args) {

try {

File file = new File("[Link]");

Scanner sc = new Scanner(file);

while ([Link]()) {
[Link]([Link]());

[Link]();

} catch (Exception e) {

[Link]();

10. Advanced Java (Lambdas, Streams)

Q1. Sort list using lambda

import [Link].*;

public class LambdaSort {

public static void main(String[] args) {

List<Integer> nums = [Link](5, 1, 9, 3);

[Link]((a, b) -> a - b);

[Link](nums);

Q2. Filter even numbers using stream

import [Link].*;

import [Link].*;

public class EvenFilter {

public static void main(String[] args) {

List<Integer> nums = [Link](1, 2, 3, 4, 5, 6);


List<Integer> evens = [Link]().filter(n -> n % 2 ==
0).toList();

[Link](evens);

11. Multithreading

Q1. Odd-Even threads

class EvenThread extends Thread {

public void run() {

for (int i = 0; i <= 10; i += 2)

[Link]("Even: " + i);

class OddThread extends Thread {

public void run() {

for (int i = 1; i <= 10; i += 2)

[Link]("Odd: " + i);

public class Main {

public static void main(String[] args) {

new EvenThread().start();

new OddThread().start();

You might also like