0% found this document useful (0 votes)
3 views4 pages

Java String and Exception Handling Guide

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

Java String and Exception Handling Guide

Java Tutorial
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java String, Exception Handling, and Exceptions Tutorial 4 (40 minutes)

Name:
ID:
Date:

Section A: Java String (9 Marks)

1. What is the output of the following code? Explain why. (3 Marks)

String str1 = "Hello";

String str2 = "Hello";

[Link](str1 == str2);

2. Given the string String message = "Java Programming";, answer the following:
a) What is the result of [Link]()? (1 Mark)

b) What does [Link](5, 14) return? (2 Marks)

3 Why are strings in Java immutable? Explain one benefit of this property in terms of
performance or security. (3 Marks)

Page 1 of 4
Java String, Exception Handling, and Exceptions Tutorial 4 (40 minutes)

Section B: Java Exception Handling (11 Marks)

4. Define checked and unchecked exceptions. Provide one example of each and explain when
each type should be used. (4 Marks)

5. What will the following code print? (3 Marks)

try {

int x = 5 / 0;

} catch (ArithmeticException e) {

[Link]("Error");

6. Write a try-catch block that handles both FileNotFoundException and IOException and
prints a custom error message for each. (2 Marks)

Page 2 of 4
Java String, Exception Handling, and Exceptions Tutorial 4 (40 minutes)

7. How do you throw an exception using the throw keyword? Write an example where you
throw an IllegalArgumentException. (2 Marks)

Section C: Try-Catch-Finally and Propagation (10 Marks)

8. What is the purpose of the finally block in exception handling? Does it always execute?
Provide an example. (4 Marks)

9. What is the role of the throws keyword in a method declaration? Write an example method
that throws an IOException and explain how it is handled in the calling method. (3 Marks)

Page 3 of 4
Java String, Exception Handling, and Exceptions Tutorial 4 (40 minutes)

10. Consider the following code:

public static void main(String[] args) {

try {

[Link]("In Try Block");

int result = 10 / 0; // ArithmeticException

} catch (ArithmeticException e) {

[Link]("In Catch Block");

} finally {

[Link]("In Finally Block");

}}

What will be the output when the program runs? Explain why the finally block is executed. (3 Marks)

Page 4 of 4

You might also like