Java assignment solutions
Thanks — I opened your assignment file and solved all questions (Assignment 2-
1.pdf).
Below are clear, runnable Java answers for every question (short answers for Section
A + full example programs for Sections B & C). Copy each code block into its own
.java file (or combine as noted) and run.
Section A — Short answers (5 × 2 = 10 marks)
Q1 — Why we use static classes in Java?
In Java a static nested class (a class declared static inside another class) is
associated with the outer class, not an instance. Use it when the nested class
does not need access to instance members of the outer class — it helps group
related classes, improves encapsulation, and avoids an implicit reference to the
outer instance (memory & clarity benefits).
Q2 — Difference between abstract class and interface in Java
Abstract class: can have fields, constructors, concrete (implemented) methods
and abstract methods; single inheritance (a class can extend one abstract class).
Interface: primarily method contracts; from Java 8+ can have default and
static methods and (Java 9+) private methods; fields are public static
final ; a class can implement multiple interfaces. Use abstract class for shared
implementation/state; interface for pure capability contracts and multiple
inheritance of type.
Q3 — Difference between SIB and IIB
SIB (Static Initialization Block): static { ... } — runs once when the class is
loaded; can initialize static variables.
IIB (Instance Initialization Block): { ... } — runs every time an instance is
created, before constructor; can initialize instance fields common to all
constructors.
Q4 — How to access hidden outer-class variable in inner class?
Use OuterClassName.this.variableName . Example: Outer.this.x accesses
outer class x when inner class has a field named x .
Q5 — Boxing vs Unboxing
1/9
Boxing (autoboxing): converting a primitive to its wrapper type (e.g., int →
Integer ).
Unboxing: converting a wrapper to its primitive (e.g., Integer → int ). Java
performs these automatically in many places, but beware of null wrapper
causing NullPointerException on unboxing.
Section B — Coding (7 * 3 = 21 marks)
Q1 — Monkey / Human / BasicAnimal
java
// BasicAnimal.java
interface BasicAnimal {
void eat();
void sleep();
}
// Monkey.java
class Monkey {
public void jump() {
System.out.println("Monkey jumps!");
}
public void bite() {
System.out.println("Monkey bites!");
}
}
// Human.java
class Human extends Monkey implements BasicAnimal {
@Override
public void eat() {
System.out.println("Human eats using utensils.");
}
@Override
public void sleep() {
System.out.println("Human sleeps at night.");
}
}
2/9
// TestHuman.java (with main)
public class TestHuman {
public static void main(String[] args) {
Human h = new Human();
h.jump(); // inherited from Monkey
h.bite(); // inherited from Monkey
h.eat(); // from BasicAnimal
h.sleep(); // from BasicAnimal
}
}
Expected output
cpp
Monkey jumps!
Monkey bites!
Human eats using utensils.
Human sleeps at night.
Q2 — Create package Calculation and use its functionality
Create file Calculation/Calculator.java :
java
package Calculation; // following assignment wording (case-sensitive)
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static double divide(double a, double b) {
if (b == 0) throw new ArithmeticException("Division by zero");
return a / b;
3/9
}
}
Then use it in another class (in default package or another package), UseCalc.java :
java
import Calculation.Calculator;
public class UseCalc {
public static void main(String[] args) {
System.out.println("10 + 5 = " + Calculator.add(10,5));
System.out.println("10 - 5 = " + Calculator.subtract(10,5));
System.out.println("10 * 5 = " + Calculator.multiply(10,5));
System.out.println("10 / 5 = " + Calculator.divide(10,5));
}
}
Notes: Put Calculator.java in a folder named Calculation and compile with
javac Calculation/Calculator.java UseCalc.java , then run java UseCalc .
Q3 — Initializer with static block
java
public class Initializer {
static int initialValue;
static {
initialValue = 1000; // static block initialization
System.out.println("Static block executed: initialValue set to " + initialValue);
}
public Initializer() {
System.out.println("Initializer constructor called.");
}
public static void main(String[] args) {
// Print before creating instance (class is already loaded by running main so the static block
runs first)
System.out.println("Before creating instance: initialValue = " + Initializer.initialValue);
4/9
Initializer obj = new Initializer();
System.out.println("After creating instance: initialValue = " + Initializer.initialValue);
}
}
Expected flow / output
yaml
Static block executed: initialValue set to 1000
Before creating instance: initialValue = 1000
Initializer constructor called.
After creating instance: initialValue = 1000
Section C — Coding (11 * 3 = 33 marks)
Q1 — Abstract Vehicle , Car , Motorcycle , and Driver
java
import java.util.ArrayList;
import java.util.List;
// Vehicle.java
abstract class Vehicle {
private String name;
public Vehicle(String name) { this.name = name; }
public String getName() { return name; }
public abstract void start();
public abstract void stop();
}
// Car.java
class Car extends Vehicle {
public Car(String name) { super(name); }
@Override
public void start() { System.out.println(getName() + " (Car) starting: ignition ON."); }
@Override
public void stop() { System.out.println(getName() + " (Car) stopping: ignition OFF."); }
}
5/9
// Motorcycle.java
class Motorcycle extends Vehicle {
public Motorcycle(String name) { super(name); }
@Override
public void start() { System.out.println(getName() + " (Motorcycle) starting: kick-
start/ignition."); }
@Override
public void stop() { System.out.println(getName() + " (Motorcycle) stopping: brakes
applied."); }
}
// Driver.java
class Driver {
private List<Vehicle> vehicles = new ArrayList<>();
public void addVehicle(Vehicle v) {
vehicles.add(v);
}
public void startAll() {
System.out.println("Starting all vehicles...");
for (Vehicle v : vehicles) v.start();
}
public void stopAll() {
System.out.println("Stopping all vehicles...");
for (Vehicle v : vehicles) v.stop();
}
}
// TestVehicles.java (main)
public class TestVehicles {
public static void main(String[] args) {
Car car1 = new Car("Honda City");
Motorcycle bike1 = new Motorcycle("Yamaha R15");
Driver driver = new Driver();
driver.addVehicle(car1);
driver.addVehicle(bike1);
driver.startAll();
System.out.println("--- doing some driving ---");
6/9
driver.stopAll();
}
}
Sample output
sql
Starting all vehicles...
Honda City (Car) starting: ignition ON.
Yamaha R15 (Motorcycle) starting: kick-start/ignition.
--- doing some driving ---
Stopping all vehicles...
Honda City (Car) stopping: ignition OFF.
Yamaha R15 (Motorcycle) stopping: brakes applied.
Q2 — this keyword with Student
java
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name; // using 'this' to distinguish instance variables
this.age = age;
}
public void displayDetails() {
System.out.println("Student name: " + this.name + ", age: " + this.age);
}
public static void main(String[] args) {
Student s = new Student("Amit", 20);
s.displayDetails();
}
}
Expected output
7/9
yaml
Student name: Amit, age: 20
Q3 — Method overriding and use of super
java
// Superclass: Animal
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound.");
}
}
// Subclass: Dog overrides and calls super
class Dog extends Animal {
@Override
public void makeSound() {
// call superclass method
super.makeSound();
// then extend/override behavior
System.out.println("Dog barks: Woof! Woof!");
}
}
// TestOverride.java (main)
public class TestOverride {
public static void main(String[] args) {
Animal a = new Animal();
a.makeSound();
System.out.println("---");
Dog d = new Dog();
d.makeSound(); // will call Dog's method which calls super.makeSound()
}
}
Expected output
8/9
yaml
Animal makes a sound.
---
Animal makes a sound.
Dog barks: Woof! Woof!
If you'd like, I can:
Combine related classes into single files ready for submission, or
Prepare a small ZIP of .java files for you to download, or
Add comments/line-by-line explanations for any specific question.
Which would you prefer?
9/9