Sr.
No Practical Questions
Write a Java program to print 'Hello, World!' to the console.
1
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Develop a Java program to add two numbers and display the result.
2
import [Link];
public class AddTwoNumbers {
public static void main(String[] args) {
int num1, num2, sum;
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
num1 = [Link]();
[Link]("Enter second number: ");
num2 = [Link]();
sum = num1 + num2;
[Link]("Sum = " + sum);
}
}
Create a Java program to find the factorial of a number.
3
import [Link];
public class Factorial {
public static void main(String[] args) {
int n;
long factorial = 1;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
n = [Link]();
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
}
[Link]("Factorial of " + n + " = " + factorial);
}
}
Write a Java program to check if a number is prime or not.
4
import [Link];
public class PrimeNumber {
public static void main(String[] args) {
int n;
boolean isPrime = true;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
n = [Link]();
if (n <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](n + " is a Prime number");
} else {
[Link](n + " is not a Prime number");
}
}
}
Develop a Java program to reverse a string.
5
import [Link];
public class ReverseString {
public static void main(String[] args) {
String str, reverse = "";
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
str = [Link]();
for (int i = [Link]() - 1; i >= 0; i--) {
reverse = reverse + [Link](i);
}
[Link]("Reversed string: " + reverse);
}
}
Enter a string: Hello
Reversed string: olleH
Create a Java program to sort an array of integers in ascending order.
6
import [Link];
public class SortArray {
public static void main(String[] args) {
int n, temp;
int arr[];
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
n = [Link]();
arr = new int[n];
[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
// Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
[Link]("Array in ascending order:");
for (int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}
}
}
Output
Enter number of elements: 4
Enter elements:
6
8
3
2
Array in ascending order:
2368
Write a Java program to find the largest and smallest elements in an
7 array.
import [Link];
public class MinMaxArray {
public static void main(String[] args) {
int n, min, max;
int arr[];
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
n = [Link]();
arr = new int[n];
[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
min = max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
[Link]("Largest element = " + max);
[Link]("Smallest element = " + min);
}
}
Enter number of elements: 5
Enter elements:
83519
Largest element = 9
Smallest element = 1
Develop a Java program to check if a given string is a palindrome.
8
import [Link];
public class PalindromeString {
public static void main(String[] args) {
String str, reverse = "";
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
str = [Link]();
for (int i = [Link]() - 1; i >= 0; i--) {
reverse = reverse + [Link](i);
}
if ([Link](reverse)) {
[Link]("The string is a Palindrome");
} else {
[Link]("The string is not a Palindrome");
}
}
}
Enter a string: madam
The string is a Palindrome
Create a Java program to print the Fibonacci series up to n terms.
9
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
int n, a = 0, b = 1, c;
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
n = [Link]();
[Link]("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
[Link](a + " ");
c = a + b;
a = b;
b = c;
}
}
}
Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8
Write a Java program to count the number of vowels and consonants in a
10 string.
import [Link];
public class VowelConsonantCount {
public static void main(String[] args) {
String str;
int vowels = 0, consonants = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
str = [Link]().toLowerCase();
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
}
[Link]("Number of vowels = " + vowels);
[Link]("Number of consonants = " + consonants);
}
}
Enter a string: Hello World
Number of vowels = 3
Number of consonants = 7
Develop a Java program to find the sum of digits of a given number.
11
import [Link];
public class SumOfDigits {
public static void main(String[] args) {
int num, sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
num = [Link]();
int temp = num;
while (temp != 0) {
sum += temp % 10; // extract last digit and add to sum
temp /= 10; // remove last digit
}
[Link]("Sum of digits of " + num + " = " + sum);
}
}
Enter a number: 1234
Sum of digits of 1234 = 10
Create a Java program to find the GCD and LCM of two numbers.
12
import [Link];
public class GCDandLCM {
public static void main(String[] args) {
int num1, num2, gcd = 1, lcm;
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
num1 = [Link]();
[Link]("Enter second number: ");
num2 = [Link]();
// Finding GCD using Euclidean Algorithm
int a = num1, b = num2;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
gcd = a;
// Finding LCM
lcm = (num1 * num2) / gcd;
[Link]("GCD of " + num1 + " and " + num2 + " = " + gcd);
[Link]("LCM of " + num1 + " and " + num2 + " = " + lcm);
}
}
Enter first number: 12
Enter second number: 18
GCD of 12 and 18 = 6
LCM of 12 and 18 = 36
Write a Java program to remove duplicates from an array.
13
import [Link];
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 1, 5};
int n = [Link];
int[] temp = new int[n];
int k = 0;
for (int i = 0; i < n; i++) {
boolean isDuplicate = false;
for (int j = 0; j < k; j++) {
if (arr[i] == temp[j]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
temp[k] = arr[i];
k++;
}
}
[Link]("Array after removing duplicates:");
for (int i = 0; i < k; i++) {
[Link](temp[i] + " ");
}
}
}
Array after removing duplicates:
12345
Develop a Java program to implement a simple calculator using switch
14 case.
Define a Java class named BankAccount with private attributes
accountNumber, balance, and customerName. Implement appropriate
accessor and mutator methods to manipulate these attributes.
Additionally, implement a method withdraw(double amount) to
15
withdraw funds from the account, ensuring that withdrawal operations
do not exceed the available balance. Create objects of the BankAccount
class and demonstrate the use of these methods
Write a Java program to find the second largest element in an array.
16
class SecondLargest {
public static void main(String[] args) {
int[] arr = {12, 35, 1, 10, 34, 1};
int n = [Link];
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
if (secondLargest == Integer.MIN_VALUE) {
[Link]("Second largest element does not exist.");
} else {
[Link]("Second largest element is: " + secondLargest);
}
}
}
Second largest element is: 34
Develop a Java program to implement binary search.
17
import [Link];
class BinarySearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60};
int n = [Link];
Scanner sc = new Scanner([Link]);
[Link]("Enter element to search: ");
int key = [Link]();
int low = 0, high = n - 1;
boolean found = false;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
[Link]("Element found at index: " + mid);
found = true;
break;
} else if (key < arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
if (!found) {
[Link]("Element not found.");
}
[Link]();
}
}
Enter element to search: 40
Element found at index: 3
Create a Java program to convert a binary number to a decimal number.
18
import [Link];
class BinaryToDecimal {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a binary number: ");
int binary = [Link]();
int decimal = 0;
int base = 1; // 2^0
while (binary > 0) {
int lastDigit = binary % 10;
decimal = decimal + lastDigit * base;
base = base * 2;
binary = binary / 10;
}
[Link]("Decimal equivalent: " + decimal);
[Link]();
}
}
Enter a binary number: 1011
Decimal equivalent: 11
Write a Java program to find the sum of the elements in an array.
19
class SumOfArray {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < [Link]; i++) {
sum += arr[i];
}
[Link]("Sum of array elements = " + sum);
}
}
Sum of array elements = 150
Develop a Java program to check if a given number is an Armstrong
20 number.
import [Link];
class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
int original = number;
int temp = number;
int digits = 0;
int sum = 0;
// Count number of digits
while (temp != 0) {
digits++;
temp /= 10;
}
temp = number;
// Calculate Armstrong sum
while (temp != 0) {
int digit = temp % 10;
sum += [Link](digit, digits);
temp /= 10;
}
if (sum == original) {
[Link](original + " is an Armstrong number.");
} else {
[Link](original + " is not an Armstrong number.");
}
}
} Enter a number: 153
153 is an Armstrong number.
Define a class Person with private attributes name, age, and address.
Implement appropriate accessor and mutator methods to manipulate
these attributes. Additionally, implement a method displayDetails() to
21
display all the details of the person. Demonstrate encapsulation by
accessing/modifying these attributes through methods only.
Write a program to accept a number from the user through command
22 line and display whether the given number is palindrome or not.
class PalindromeNumber {
public static void main(String[] args) {
// Check if command line argument is provided
if ([Link] == 0) {
[Link]("Please provide a number as a command line
argument.");
return;
}
int number = [Link](args[0]);
int original = number;
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
if (original == reverse) {
[Link](original + " is a Palindrome number.");
} else {
[Link](original + " is not a Palindrome number.");
}
}
121 is a Palindrome number.
Develop a Java program to find the length of a string without
23 using built-in functions.
import [Link];
class StringLength {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int count = 0;
// Counting characters manually
for (char c : [Link]()) {
count++;
}
[Link]("Length of the string = " + count);
}
}
Enter a string: Hello World
Length of the string = 11
Create a Java program to swap two numbers without using a temporary
24 variable.
import [Link];
class SwapWithoutTemp {
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]("Before swapping:");
[Link]("a = " + a + ", b = " + b);
// Swapping without temporary variable
a = a + b;
b = a - b;
a = a - b;
[Link]("After swapping:");
[Link]("a = " + a + ", b = " + b);
}
}
Enter first number: 10
Enter second number: 20
Before swapping:
a = 10, b = 20
After swapping:
a = 20, b = 10
Write a program to create a class Publisher with attributes publisher
name and publisher id. Derive a subclass Book with attributes bookname,
25 bookid and author name. All these data should be entered by the user.
Create two methods getdata() and showdata() to display the details of
book and publisher
import [Link];
// Base class
class Publisher {
int publisherId;
String publisherName;
// Method to get publisher data
void getdata(Scanner sc) {
[Link]("Enter Publisher ID: ");
publisherId = [Link]();
[Link](); // consume newline
[Link]("Enter Publisher Name: ");
publisherName = [Link]();
}
// Method to display publisher data
void showdata() {
[Link]("Publisher ID : " + publisherId);
[Link]("Publisher Name : " + publisherName);
}
}
// Derived class
class Book extends Publisher {
int bookId;
String bookName;
String authorName;
// Method to get book data
void getdata(Scanner sc) {
// Get publisher details first
[Link](sc);
[Link]("Enter Book ID: ");
bookId = [Link]();
[Link](); // consume newline
[Link]("Enter Book Name: ");
bookName = [Link]();
[Link]("Enter Author Name: ");
authorName = [Link]();
}
// Method to display book data
void showdata() {
[Link]("\n--- Book & Publisher Details ---");
[Link]();
[Link]("Book ID : " + bookId);
[Link]("Book Name : " + bookName);
[Link]("Author Name : " + authorName);
}
}
// Main class
public class PublisherBookDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Book b = new Book();
[Link](sc);
[Link]();
}
}
Enter Publisher ID: 101
Enter Publisher Name: Pearson
Enter Book ID: 501
Enter Book Name: Java Programming
Enter Author Name: James Gosling
--- Book & Publisher Details ---
Publisher ID : 101
Publisher Name : Pearson
Book ID : 501
Book Name : Java Programming
Author Name : James Gosling
Develop a Java program to check if a given year is a leap year.
26
import [Link];
class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
// Leap year logic
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
[Link](year + " is a Leap Year.");
} else {
[Link](year + " is not a Leap Year.");
}
[Link]();
}
}
Enter a year: 2024
2024 is a Leap Year.
Declare a variable called x with integer as the data type in base class
and subclass. Make a method named as show() which displays the value
27
of x in the superclass and subclass.
// Base class
class SuperClass {
int x = 10; // variable in superclass
void show() {
[Link]("Value of x in SuperClass: " + x);
}
}
// Subclass
class SubClass extends SuperClass {
int x = 20; // variable in subclass (hides superclass variable)
void show() {
[Link]("Value of x in SuperClass: " + super.x);
[Link]("Value of x in SubClass : " + x);
}
}
// Main class
public class VariableHidingDemo {
public static void main(String[] args) {
SubClass obj = new SubClass();
[Link]();
}
}
Value of x in SuperClass: 10
Value of x in SubClass : 20
Write a Java program to implement the bubble sort algorithm.
28
import [Link];
class BubbleSort {
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
int n = [Link];
// Bubble Sort logic
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Display sorted array
[Link]("Sorted array using Bubble Sort:");
for (int num : arr) {
[Link](num + " ");
}
}
}
Sorted array using Bubble Sort:
11 12 22 25 34 64 90
🔑 Key Points (For Exams)
Repeatedly compares adjacent elements
Time Complexity:
Best case: O(n) (already sorted)
Worst/Average case: O(n²)
Stable sorting algorithm
Optimized Bubble Sort (Optional)
java
Copy code
class BubbleSortOptimized {
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
int n = [Link];
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) break;
}
[Link]("Sorted array:");
for (int num : arr) {
[Link](num + " ");
}
}
}
Sorted array using Bubble Sort:
11 12 22 25 34 64 90
Develop a Java program to implement the selection sort algorithm.
29
class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
int n = [Link];
// Selection Sort logic
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap minimum element with first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
// Display sorted array
[Link]("Sorted array using Selection Sort:");
for (int num : arr) {
[Link](num + " ");
}
}
}
Sorted array using Selection Sort:
11 12 22 25 64
Create a Java program to implement the insertion sort algorithm.
30
class InsertionSort {
public static void main(String[] args) {
int[] arr = {9, 5, 1, 4, 3};
int n = [Link];
// Insertion Sort logic
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements greater than key one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
// Display sorted array
[Link]("Sorted array using Insertion Sort:");
for (int num : arr) {
[Link](num + " ");
}
}
}
Develop a Java program to calculate the power of a number using
31 recursion.
import [Link];
class PowerUsingRecursion {
// Recursive method to calculate power
static int power(int base, int exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter base number: ");
int base = [Link]();
[Link]("Enter exponent: ");
int exponent = [Link]();
int result = power(base, exponent);
[Link](base + " ^ " + exponent + " = " + result);
}
}
Enter base number: 2
Enter exponent: 5
2 ^ 5 = 32