0% found this document useful (0 votes)
3 views

Java_IA3_Answers

The document provides an internal assessment on Object Oriented Programming with Java, covering exception handling, thread creation using the runnable interface, and package creation. It includes sample programs demonstrating try-catch blocks, nested try statements, thread execution, and package usage. Additionally, it outlines the steps for creating and using packages in Java.

Uploaded by

ak47gunm
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)
3 views

Java_IA3_Answers

The document provides an internal assessment on Object Oriented Programming with Java, covering exception handling, thread creation using the runnable interface, and package creation. It includes sample programs demonstrating try-catch blocks, nested try statements, thread execution, and package usage. Additionally, it outlines the steps for creating and using packages in Java.

Uploaded by

ak47gunm
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/ 5

Internal Assessment - III

Object Oriented Programming with JAVA

Q1(a): What are the uses of try and catch blocks in exception handling?

The try block contains code that might throw an exception. The catch block is used to handle the

exception

and prevent the program from crashing. Exception handling improves code reliability and debugging.

Sample Program:

public class ExceptionHandlingExample {

public static void main(String[] args) {

try {

int result = 10 / 0; // Division by zero

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

} catch (ArithmeticException e) {

System.out.println("Exception occurred: " + e.getMessage());

} finally {

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

Output:

Exception occurred: / by zero

This block always executes.

Q1(b): Write a Java program to demonstrate nested try statements in exception handling.

public class NestedTryExample {

public static void main(String[] args) {

Page 1
Internal Assessment - III

try {

int[] numbers = {1, 2, 3};

System.out.println("Outer try: Accessing element: " + numbers[5]);

try {

int result = 10 / 0; // Nested try block

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

} catch (ArithmeticException e) {

System.out.println("Inner catch: " + e.getMessage());

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Outer catch: " + e.getMessage());

Output:

Outer catch: Index 5 out of bounds for length 3

Q2(a): Illustrate the process to create a thread using the runnable interface.

class MyRunnable implements Runnable {

public void run() {

System.out.println("Thread is running...");

public class ThreadExample {

public static void main(String[] args) {

MyRunnable runnable = new MyRunnable(); // Create a Runnable object

Page 2
Internal Assessment - III

Thread thread = new Thread(runnable); // Create a Thread object

thread.start(); // Start the thread

Output:

Thread is running...

Q2(b): What is the important role of the main thread in Java?

public class MainThreadExample {

public static void main(String[] args) {

System.out.println("Main thread is running...");

Thread thread = new Thread(() -> {

System.out.println("Child thread is running...");

});

thread.start();

System.out.println("Main thread ends.");

Output:

Main thread is running...

Main thread ends.

Child thread is running...

Q3(a): Write a Java program to demonstrate the working procedure of Packages and Member Acces

// Package 1 (mypackage)

package mypackage;

Page 3
Internal Assessment - III

public class MyClass {

public void display() {

System.out.println("This is a public method in MyClass.");

// Main Program

import mypackage.MyClass;

public class PackageExample {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.display();

Output:

This is a public method in MyClass.

Q3(b): What is the procedure to create a package in Java? How does the Java run-time system know

Steps:

1. Use the 'package' keyword at the top of the file.

2. Save the file in a directory matching the package name.

3. Compile using javac.

4. Import and use the package in another program.

Example:

Page 4
Internal Assessment - III

package mypackage;

public class MyClass {

public void showMessage() {

System.out.println("Hello from the package!");

// Access:

import mypackage.MyClass;

public class Main {

public static void main(String[] args) {

MyClass obj = new MyClass();

obj.showMessage();

Output:

Hello from the package!

Page 5

You might also like