0% found this document useful (0 votes)
14 views16 pages

Solution Page 2

The document provides an overview of Java concepts including the Java Runtime Environment, Object-Oriented Programming features, and various Java programming tasks such as class creation, method overloading, and exception handling. It includes code examples for each concept, demonstrating practical applications and differences between various programming constructs. Additionally, it discusses built-in packages, polymorphism, and components used in Java Swing.

Uploaded by

xevermama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Solution Page 2

The document provides an overview of Java concepts including the Java Runtime Environment, Object-Oriented Programming features, and various Java programming tasks such as class creation, method overloading, and exception handling. It includes code examples for each concept, demonstrating practical applications and differences between various programming constructs. Additionally, it discusses built-in packages, polymorphism, and components used in Java Swing.

Uploaded by

xevermama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1.(a) What do you mean by Java Runtime Environment?

(1 mark)

Answer:
Java Runtime Environment (JRE) is a part of the Java Development Kit (JDK) that provides
the libraries, Java Virtual Machine (JVM), and other components needed to run applications
written in Java.

(b) What are the fundamental features of OOP, and how do these features help in
designing and developing software systems? (2 marks)

Answer:
Fundamental features of OOP:

1. Encapsulation – Bundles data and methods; improves security and code


organization.

2. Abstraction – Hides complex implementation details; provides clear interfaces.

3. Inheritance – Enables code reuse by allowing a class to inherit from another.

4. Polymorphism – Allows objects to take multiple forms (method


overloading/overriding).

These features promote modular, scalable, and maintainable code.

(c) Find the output of the following programs: (3 marks)

(i) TrianglePattern Output:

class TrianglePattern {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

System.out.print("*");

System.out.println();

}
}

Output:

markdown

**

***

****

*****

(ii) OperatorTest Output:

public class OperatorTest {

public static void main(String[] args) {

int a = 10, b = 5;

int x = ++a - b--; // a=11, x=11-5=6, b becomes 4

int y = b++ + a--; // y=4+11=15, b=5, a=10

System.out.println(x + y); // 6 + 15 = 21

Output: 21

(d) Write a Java program that will find output based on sample input/output. (4 marks)

Sample Input: 35072 → Output: 17

Logic: Add all digits until a single digit is left (repeated digit sum). But here, 3+5+0+7+2 =
17, and 4098115 → 4+0+9+8+1+1+5 = 28, so it's just simple digit sum.

Program:

import java.util.Scanner;
public class DigitSum {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number: ");

int num = sc.nextInt();

int sum = 0;

while (num > 0) {

sum += num % 10;

num /= 10;

System.out.println("Output: " + sum);

2.(a) What do you mean by static variable? (1 mark)

Answer:
A static variable in Java is shared among all instances of a class. It is allocated memory
only once during class loading.

(b) Write major differences between method and constructor. (3 marks)

Feature Constructor Method

Purpose Initializes objects Performs operations on objects

Name Same as class name Any valid name

Return type No return type (not even void) Must have return type

Call time Called automatically during object creation Called explicitly using object
(c) Create class Student, perform tasks: (3 marks)

Instructions:

• Create object std1

• Initialize: "SFMU-CSE", Semester 2.19

• Copy to std2

Code:

class Student {

String department;

double semester;

int age;

// Constructor

Student(String dept, double sem, int age) {

this.department = dept;

this.semester = sem;

this.age = age;

// Copy constructor

Student(Student s) {

this.department = s.department;

this.semester = s.semester;

this.age = s.age;

}
void display() {

System.out.println("Department: " + department);

System.out.println("Semester: " + semester);

System.out.println("Age: " + age);

public static void main(String[] args) {

Student std1 = new Student("SFMU-CSE", 2.19, 20);

Student std2 = new Student(std1); // Copying

std2.display();

(d): Account and Loan Classes

Task:
Create a class Account and another class Loan that uses Account.

Solution:

class Account {

public String account_name;

public int id_account_id;

public float amount;

class Loan {

public static void main(String[] args) {

Account acc = new Account();

acc.account_name = "John Doe";


acc.id_account_id = 12345;

acc.amount = 10000.50f;

System.out.println("Account Name: " + acc.account_name);

System.out.println("Account ID: " + acc.id_account_id);

System.out.println("Amount: " + acc.amount);

Question 3 (a): String Manipulation

Given:
String str = "Hello CSE-13 Batch, SFMU";

Solution:

public class StringTask {

public static void main(String[] args) {

String str = "Hello CSE-13 Batch, SFMU";

// i) Find 8th character (index 7)

System.out.println("8th Character: " + str.charAt(7));

// ii) Convert string to lowercase

System.out.println("Lowercase String: " + str.toLowerCase());

// iii) Find total length

System.out.println("Length of String: " + str.length());

}
Question 3 (b): Use of this keyword

Solution:

class Student {

String name;

int id;

Student(String name, int id) {

this.name = name;

this.id = id;

void display() {

System.out.println("Name: " + this.name);

System.out.println("ID: " + this.id);

public static void main(String[] args) {

Student s = new Student("Alice", 101);

s.display();

Question 3 (c): Method Overloading in Mathematics Class

Solution:

class Mathematics {
void add(int a, int b) {

System.out.println("Addition is: " + (a + b));

void add(int a, int b, int c) {

System.out.println("Addition is: " + (a + b + c));

public static void main(String[] args) {

Mathematics obj = new Mathematics();

obj.add(5, 10); // 2 args

obj.add(2, 4, 6); // 3 args

Question 4 (a): What is Interface? Purpose in Java

Answer:
An interface in Java is a reference type, similar to a class, that can contain only constants,
method signatures, default methods, static methods, and nested types. It is used to
achieve abstraction and multiple inheritance in Java.

Purposes:

• Achieve abstraction

• Provide contract for what a class can do

• Support multiple inheritance

• Promote loose coupling

Question 4 (b): Relationship between Interface and Class


Answer:

• A class implements an interface to provide the implementation of abstract


methods.

• A class can implement multiple interfaces.

Example:

interface Animal {

void sound();

class Dog implements Animal {

public void sound() {

System.out.println("Bark");

Question 4 (c): Explain final keyword in code snippet

final class FinalExam {

int question = 6;

public final void showQuestion() {

System.out.println("Number of Question is= " + (++question));

class OOPExam extends FinalExam {

public final void showQuestion() {


System.out.println("Number of Question in OOP is= " +
question);

Explanation:
This code will throw a compile-time error because:

• The FinalExam class is declared final, which means it cannot be extended.

• The showQuestion() method is also final, so it cannot be overridden in the


subclass.

Corrected Version (without final):


To compile and run it, you must remove final from the class and method.

Effect of final:

• final class: cannot be inherited.

• final method: cannot be overridden.

• final variable: cannot be reassigned.

6 (a) Convert the given modules into Java classes

Modules:

• setName(), getName(), getRadius() → in Circle

• getCenterX(), getCenterY() → in Point

• getArea() → in ColoredCircle (which extends Circle)

Java Classes:

class Point {

private double x, y;

public Point(double x, double y) {

this.x = x;

this.y = y;
}

public double getCenterX() {

return x;

public double getCenterY() {

return y;

class Circle {

private String name;

private double radius;

public Circle(String name, double radius) {

this.name = name;

this.radius = radius;

public String getName() {

return name;

public double getRadius() {

return radius;

}
public void setName(String name) {

this.name = name;

class ColoredCircle extends Circle {

private Point center;

public ColoredCircle(String name, double radius, Point center) {

super(name, radius);

this.center = center;

public double getCenterX() {

return center.getCenterX();

public double getCenterY() {

return center.getCenterY();

public double getArea() {

return Math.PI * getRadius() * getRadius();

}
6 (a) What do you mean by built-in package? Mention some common built-in packages
used in Java. (3 marks)

Answer:
A built-in package is a collection of predefined classes and interfaces provided by Java to
perform common programming tasks.

Examples:

• java.util – collections, date/time

• java.io – input/output

• java.lang – core classes (automatically imported)

• java.math – mathematical operations

• java.net – networking

6 (b) Code analysis: Is getAmount() accessible from Child class? (3 marks)

class Parents {

private amount = 5;

void getAmount() {

System.out.println("Getting Amount from Parents");

class Child {

public static void main(String args[]) {

Parents obj = new Parents();

obj.getAmount(); // Is this accessible?

}
Answer:
Yes, getAmount() is accessible from Child because it is default (package-private) and the
Child class is in the same package. If it were private, it wouldn’t be accessible.

6 (c) Differences between Run-Time and Compile-Time Polymorphism (with examples)


(4 marks)

Feature Compile-Time Polymorphism Run-Time Polymorphism

Also Known As Method Overloading Method Overriding

Binding Time Compile Time Run Time

Method
Done at compile time Done at runtime
Resolution

Inheritance
No Yes
Required

add(int a, int b) and add(double a, Subclass overrides a superclass


Example
double b) method

Example:

// Compile-Time

class Calculator {

int add(int a, int b) { return a + b; }

double add(double a, double b) { return a + b; }

// Run-Time

class Animal {

void sound() { System.out.println("Animal sound"); }

class Dog extends Animal {


void sound() { System.out.println("Bark"); }

7 (a) Reasons for Exception Handling in Java (2 marks)

Answer:

• To maintain normal program flow after an error.

• To detect and fix runtime errors.

• To provide user-friendly error messages.

• To separate error-handling logic from regular logic.

7 (b) Write a Java program to handle ArithmeticException using try-catch (4 marks)

public class ExceptionExample {

public static void main(String[] args) {

int a = 56;

int b = 0;

try {

int result = a / b;

System.out.println("Result: " + result);

} catch (ArithmeticException e) {

System.out.println("Cannot divide by zero!");

7 (c) Write a program using finally block (4 marks)

public class FinallyExample {


public static void main(String[] args) {

try {

int a = 5, b = 0;

int result = a / b;

} catch (ArithmeticException e) {

System.out.println("Error: Division by zero");

} finally {

System.out.println("This block always executes.");

7 (d) Mention some common components used in Java Swing container (2 marks)

Answer:

• JButton – Button

• JLabel – Text label

• JTextField – Single-line text input

• JTextArea – Multi-line text input

• JComboBox – Dropdown list

• JCheckBox – Checkbox

• JRadioButton – Radio button

• JPanel – Container panel

You might also like