Great—here are Java code solutions for all 30 beginner-level questions.
Each is a minimal
standalone program; you can copy, compile, and run them. (For brevity some use hardcoded
example values—feel free to replace with Scanner input if you want interactivity.)
Java Basics
1. Hello World
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
2. Print name, age, city
public class PersonalInfo {
public static void main(String[] args) {
String name = "Alice";
int age = 23;
String city = "Mumbai";
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("City: " + city);
}
}
3. Add two numbers
public class AddNumbers {
public static void main(String[] args) {
int a = 10;
int b = 15;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
4. Area of rectangle
public class RectangleArea {
public static void main(String[] args) {
double length = 8.5;
double width = 4.2;
double area = length * width;
System.out.println("Area of rectangle: " + area);
}
}
5. Celsius to Fahrenheit
public class CelsiusToFahrenheit {
public static void main(String[] args) {
double celsius = 30.0;
double fahrenheit = (celsius * 9 / 5) + 32;
System.out.println(celsius + "°C = " + fahrenheit + "°F");
}
}
Variables & Data Types
1. Swap two numbers using temporary variable
public class SwapWithTemp {
public static void main(String[] args) {
int x = 5;
int y = 10;
System.out.println("Before swap: x=" + x + ", y=" + y);
int temp = x;
x = y;
y = temp;
System.out.println("After swap: x=" + x + ", y=" + y);
}
}
2. Square and cube of a number
public class SquareCube {
public static void main(String[] args) {
int n = 4;
int square = n * n;
int cube = n * n * n;
System.out.println("Number: " + n);
System.out.println("Square: " + square);
System.out.println("Cube: " + cube);
}
}
3. Even or odd
public class EvenOdd {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}
4. Simple interest
public class SimpleInterest {
public static void main(String[] args) {
double principal = 1000.0;
double rate = 5.5; // percent per annum
double time = 2.0; // years
double interest = (principal * rate * time) / 100;
System.out.println("Simple Interest: " + interest);
}
}
5. Perimeter of a circle
public class CirclePerimeter {
public static void main(String[] args) {
double radius = 3.5;
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter (circumference) of circle: " +
perimeter);
}
}
Control Statements
1. Largest of two numbers
public class LargestOfTwo {
public static void main(String[] args) {
int a = 12;
int b = 25;
int max = (a > b) ? a : b;
System.out.println("Largest of two: " + max);
}
}
2. Largest of three numbers
public class LargestOfThree {
public static void main(String[] args) {
int a = 8, b = 19, c = 14;
int max = a;
if (b > max) max = b;
if (c > max) max = c;
System.out.println("Largest of three: " + max);
}
}
3. Positive, negative, or zero
public class NumberSign {
public static void main(String[] args) {
int num = -3;
if (num > 0) {
System.out.println(num + " is positive.");
} else if (num < 0) {
System.out.println(num + " is negative.");
} else {
System.out.println(num + " is zero.");
}
}
}
4. Leap year check
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0);
if (isLeap) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
5. Display numbers 1 to 10 using loop
public class OneToTen {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Loops
1. Multiplication table of a given number
public class MultiplicationTable {
public static void main(String[] args) {
int n = 7;
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}
2. Sum of first 10 natural numbers
public class SumFirstTen {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum of first 10 natural numbers: " + sum);
}
}
3. Reverse a number
public class ReverseNumber {
public static void main(String[] args) {
int num = 12345;
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed number: " + reversed);
}
}
4. Fibonacci series up to n terms
public class FibonacciSeries {
public static void main(String[] args) {
int n = 8;
int a = 0, b = 1;
System.out.print("Fibonacci series: ");
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}
System.out.println();
}
}
5. Factorial of a number
public class Factorial {
public static void main(String[] args) {
int n = 5;
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial of " + n + " is: " + fact);
}
}
Arrays
1. Store 5 numbers and print
public class PrintArray {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
System.out.println("Array elements:");
for (int num : arr) {
System.out.println(num);
}
}
}
2. Largest element in array
public class LargestInArray {
public static void main(String[] args) {
int[] arr = {5, 11, 3, 27, 14};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
System.out.println("Largest element: " + max);
}
}
3. Smallest element in array
public class SmallestInArray {
public static void main(String[] args) {
int[] arr = {5, 11, 3, 27, 14};
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) min = arr[i];
}
System.out.println("Smallest element: " + min);
}
}
4. Sum of all elements
public class SumArray {
public static void main(String[] args) {
int[] arr = {2, 4, 6, 8, 10};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum of array elements: " + sum);
}
}
5. Search for an element
public class SearchInArray {
public static void main(String[] args) {
int[] arr = {7, 3, 9, 12, 5};
int target = 12;
boolean found = false;
for (int num : arr) {
if (num == target) {
found = true;
break;
}
}
if (found) {
System.out.println(target + " is present in the array.");
} else {
System.out.println(target + " is not present in the array.");
}
}
}
Strings
1. Length of a string
public class StringLength {
public static void main(String[] args) {
String s = "hello";
System.out.println("Length: " + s.length());
}
}
2. Convert to uppercase
public class ToUpperCase {
public static void main(String[] args) {
String s = "java programming";
System.out.println("Uppercase: " + s.toUpperCase());
}
}
3. Check palindrome
public class PalindromeCheck {
public static void main(String[] args) {
String s = "madam";
String reversed = new StringBuilder(s).reverse().toString();
if (s.equals(reversed)) {
System.out.println(s + " is a palindrome.");
} else {
System.out.println(s + " is not a palindrome.");
}
}
}
4. Count vowels
public class VowelCount {
public static void main(String[] args) {
String s = "education";
int count = 0;
String vowels = "aeiouAEIOU";
for (char c : s.toCharArray()) {
if (vowels.indexOf(c) != -1) count++;
}
System.out.println("Number of vowels: " + count);
}
}
5. Reverse a string
public class ReverseString {
public static void main(String[] args) {
String s = "Java";
String reversed = new StringBuilder(s).reverse().toString();
System.out.println("Reversed: " + reversed);
}
}
If you’d like, I can bundle these into a single PDF (with optional space for answers), or give you
a version that reads input from the user instead of hardcoded values. Which next?