Java Programming Questions with
Answers
Variables and Data Types
Declare and initialize all primitive data types.
int a = 10;
byte b = 20;
short s = 100;
long l = 10000L;
float f = 10.5f;
double d = 20.99;
char c = 'A';
boolean bool = true;
Swap two numbers using a temporary variable.
int a = 5, b = 10;
int temp = a;
a = b;
b = temp;
System.out.println("a: " + a + ", b: " + b);
Control Statements
Find the largest of three numbers using if-else.
int a = 10, b = 20, c = 15;
if (a >= b && a >= c)
System.out.println("Largest: " + a);
else if (b >= a && b >= c)
System.out.println("Largest: " + b);
else
System.out.println("Largest: " + c);
Display whether a character is a vowel or consonant using switch.
char ch = 'a';
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
System.out.println("Vowel"); break;
default:
System.out.println("Consonant");
}
Print multiplication table of a number.
int n = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
Print even numbers from 1 to 100 using while loop.
int i = 1;
while (i <= 100) {
if (i % 2 == 0)
System.out.println(i);
i++;
}
Classes and Objects
Create a class Employee and print details.
class Employee {
int id;
String name;
double salary;
void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
Employee e = new Employee();
e.id = 1;
e.name = "John";
e.salary = 50000;
e.display();
Demonstrate constructors with parameters.
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
void show() {
System.out.println(name + " " + age);
}
}
Student s = new Student("Alice", 20);
s.show();
Method returning square of a number.
int square(int x) {
return x * x;
}
System.out.println(square(5));
Object reference assignment effect.
class MyClass {
int value;
}
MyClass a = new MyClass();
MyClass b = a;
b.value = 100;
System.out.println(a.value); // 100
Garbage Collection and finalize()
Make object eligible for GC.
class MyClass {
int value;
}
MyClass obj = new MyClass();
obj = null; // eligible for GC
Override finalize() method.
class Demo {
protected void finalize() {
System.out.println("Object is garbage collected");
}
}
Demo d = new Demo();
d = null;
System.gc();
Inheritance
Superclass Animal and subclass Dog.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Dog d = new Dog();
d.sound();
d.bark();
Using super to access superclass.
class A {
int x = 10;
}
class B extends A {
void show() {
System.out.println(super.x);
}
}
Multilevel inheritance.
class A {
void showA() { System.out.println("A"); }
}
class B extends A {
void showB() { System.out.println("B"); }
}
class C extends B {
void showC() { System.out.println("C"); }
}
Method overriding.
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
void show() { System.out.println("Child"); }
}
Parent p = new Child();
p.show();
Abstract class example.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Circle"); }
}
Final method example.
class A {
final void show() { System.out.println("Final method"); }
}
class B extends A {
// Cannot override show()
}
Final class example.
final class A {
void show() { System.out.println("Final class"); }
}
// class B extends A {} // Error