Java Topics - Easy, Moderate, Tough
Level Questions with Answers
Easy Level
What is the default value of int and boolean in Java?
int → 0
boolean → false
Declare a variable to store a student’s grade as a character.
char grade = 'A';
Write a for loop that prints numbers from 1 to 5.
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
What is the difference between if and switch?
if: evaluates boolean expressions
switch: evaluates a single expression (like int, char, String)
Create a simple class Book with two fields: title and author.
class Book {
String title;
String author;
}
What is the purpose of a constructor in Java?
To initialize objects when they are created.
What is inheritance?
Inheritance allows a class to inherit properties and methods from another class.
What does the extends keyword do?
Used to inherit a class:
class Dog extends Animal { }
Moderate Level
What are the rules for naming identifiers in Java?
Must start with a letter, _, or $
Cannot be a keyword
Case-sensitive
Differentiate between integer, character, and string literals with examples.
Integer: 10
Character: 'A'
String: "Hello"
Write a program to check if a number is even or odd using if-else.
int n = 5;
if (n % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
Write a program using switch to display day names (1=Monday, ..., 7=Sunday).
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
// ...
default: System.out.println("Invalid");
}
Create a class Calculator with methods: add, subtract, and multiply.
class Calculator {
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
}
Demonstrate the use of constructor overloading.
class Student {
String name;
int age;
Student() { }
Student(String n) { name = n; }
Student(String n, int a) { name = n; age = a; }
}
Show how super is used to call a superclass constructor.
class A {
A() { System.out.println("A’s constructor"); }
}
class B extends A {
B() {
super();
System.out.println("B’s constructor");
}
}
Create a multilevel inheritance hierarchy using classes A, B, and C.
class A { }
class B extends A { }
class C extends B { }
Tough Level
Create an abstract class Shape and implement it in Circle and Rectangle.
abstract class Shape {
abstract void area();
}
class Circle extends Shape {
void area() {
System.out.println("Area of circle: πr^2");
}
}
What happens if you try to override a final method?
class A {
final void display() { System.out.println("Hello"); }
}
class B extends A {
// void display() {} // Error
}
Explain object reference assignment and how changes affect objects.
MyClass a = new MyClass();
MyClass b = a;
b.value = 100;
System.out.println(a.value); // Output: 100
What is the use of finalize() and why is it deprecated?
Used for cleanup before object is garbage collected. Deprecated due to unpredictable
behavior.
Demonstrate runtime polymorphism using method overriding.
class A {
void show() { System.out.println("From A"); }
}
class B extends A {
void show() { System.out.println("From B"); }
}
A obj = new B();
obj.show();
Write a program that makes an object eligible for garbage collection.
MyClass obj = new MyClass();
obj = null; // Now eligible for GC
Explain how memory leaks can occur even with garbage collection.
If objects are still referenced (e.g., in static lists) but no longer used, they won't be collected.