Core Java Unit 4:
Exception Handling, Strings, and Files
Easy Level Questions ( 5 Marks Each)
(Bloom’s Taxonomy:
Remembering, Understanding)
1.
Differentiate between
an Error and
an Exception
in Java. Provide one example of each.
2.
What are Checked
Exceptions in Java? Give two examples of
checked exceptions.
3.
Explain the purpose of
the try-catch
block in Java exception handling.
4.
What is the StringBuffer class in Java? How does it differ from the String class in terms of mutability?
5.
List two common methods
used for formatting string data in Java.
6.
What is a Stream in the context of Java File Handling? Name the two main
types of streams.
7.
How do you create a new
file in Java using basic file handling operations?
Moderate Level Questions ( 5 Marks Each)
(Bloom’s Taxonomy:
Understanding, Applying, Analyzing)
8.
Explain the hierarchy
of Exception
classes in Java. Differentiate between Checked and Unchecked exceptions with suitable examples for each.
9.
Write a Java program
that demonstrates how to catch and handle a NumberFormatException that occurs when trying to convert an invalid string to an
integer.
10.
Describe the advantages
of using a finally block in exception handling. Provide a code snippet to
illustrate its use, ensuring that a resource (e.g., a file) is closed
regardless of whether an exception occurs.
11.
Explain the process of
"Creating User-Defined Exceptions" in Java. Provide a simple example
of a custom exception class and demonstrate how to throw and catch it.
12.
Compare and contrast the
String
and StringBuffer classes in Java. Discuss scenarios where StringBuffer (or StringBuilder) would be preferred over String.
13.
Write a Java program to
concatenate several strings efficiently using StringBuffer. Also, demonstrate how to reverse a string using StringBuffer.
14.
Explain the format() method for string data in Java. Provide a Java code
example to format a floating-point number to two decimal places and an integer
with leading zeros.
15.
Differentiate between byte
streams and character
streams in Java File Handling. When
would you use one over the other?
16.
Write a Java program
that reads text content from a specified file named input.txt and prints it to the console. Include basic exception
handling for FileNotFoundException.
17.
Design a Java program
to append new text content to an existing file named log.txt. Ensure that existing content is not overwritten.
Difficult Level Questions ( 5 Marks Each)
(Bloom’s Taxonomy: Analyzing, Evaluating, Creating)
18.
Design and implement a
Java program that simulates a simple Account class. This class should have methods for deposit(double
amount) and withdraw(double
amount).
o Create a user-defined InsufficientFundsException that is thrown if a withdrawal attempt exceeds the current
balance.
o The withdraw method should declare this exception using throws.
o Demonstrate in your main method how to call the withdraw method within a try-catch block to handle the InsufficientFundsException gracefully, printing an appropriate message.
19.
Evaluate the efficiency
and best practices when performing extensive string manipulations in Java.
Compare the performance implications of using String concatenation repeatedly in a loop versus using StringBuilder or StringBuffer. Provide a conceptual analysis and suggest scenarios where
each would be most appropriate.
20.
Design a comprehensive
Java program for file handling that:
o Creates a new text file named data.txt.
o Writes at least five lines of text, including a mix of
numbers and words, into data.txt.
o Reads the content from data.txt line by line.
o For each line read, attempts to convert any numeric part of
the line into an integer. If a NumberFormatException occurs, it should log the error to the console but
continue processing.
o Finally, counts the total number of lines and the total
number of successfully parsed integers from the file, printing these statistics
to the console.
o Ensure all file operations are enclosed in try-catch-finally blocks to handle potential IOException and ensure resources are properly closed.