Java Practice Questions - Solutions
/* 1. Define class and object */
// A class is a blueprint for objects. It defines variables and methods that an object can use.
// An object is an instance of a class that holds actual values for the class's attributes.
class Example {
int num; // Instance variable
}
/* 2. Method Overloading */
// Method Overloading allows multiple methods with the same name but different parameters.
// It improves code readability and reusability.
class OverloadDemo {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(double b) {
System.out.println("Double: " + b);
}
}
/* 3. Recursion */
// Recursion is a process where a function calls itself to solve a problem in a smaller instance.
// Example: Factorial calculation using recursion.
class RecursionExample {
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
}
/* 4. Static Variables and Methods */
// Static variables are shared across all instances of a class.
// Static methods belong to the class rather than instances and can be called without creating an object.
class StaticDemo {
static int count = 0;
static void increment() {
count++;
}
}
/* 5. Access Specifiers */
// Access specifiers define the scope and visibility of class members.
// Types:
// - private: Accessible within the class only.
// - protected: Accessible within the package and subclasses.
// - public: Accessible from anywhere.
class AccessSpecifiers {
private int a = 10;
protected int b = 20;
public int c = 30;
}
/* 6. Overloading max method */
// Overloading allows multiple definitions of a method for different data types.
class MaxOverload {
int max(int a, int b) {
return (a > b) ? a : b;
}
double max(double a, double b) {
return (a > b) ? a : b;
}
}
/* 7. Overloading swap method */
// Swap method overloaded for integer and double values.
class SwapOverload {
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
System.out.println("Swapped Integers: " + a + ", " + b);
}
void swap(double a, double b) {
double temp = a;
a = b;
b = temp;
System.out.println("Swapped Doubles: " + a + ", " + b);
}
}
/* 8. Overloading area method */
// Overloading area method to calculate area of different shapes.
class AreaOverload {
double area(double radius) {
return Math.PI * radius * radius;
}
int area(int side) {
return side * side;
}
int area(int length, int breadth) {
return length * breadth;
}
double area(double base, double height) {
return 0.5 * base * height;
}
}
/* 9. Factorial using recursion */
// Factorial of a number is calculated as n! = n * (n-1) * ... * 1
class FactorialRecursion {
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
}
/* 10. Fibonacci using recursion */
// Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
// Formula: F(n) = F(n-1) + F(n-2)
class FibonacciRecursion {
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
/* 11. Sum of n numbers using recursion */
// Sum of first n natural numbers: sum(n) = n + sum(n-1)
class SumRecursion {
int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
}