0% found this document useful (0 votes)
9 views6 pages

CS3391 CIA-II - Key-Set - A

The document provides a comprehensive overview of key concepts in Object-Oriented Programming, specifically focusing on exception handling, multithreading, and Java I/O basics. It includes explanations of various keywords, classes, and methods related to these topics, along with examples and comparisons. Additionally, it discusses the importance of synchronization, generic programming, and the distinction between built-in and user-defined exceptions.

Uploaded by

pradeeplavrp09
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)
9 views6 pages

CS3391 CIA-II - Key-Set - A

The document provides a comprehensive overview of key concepts in Object-Oriented Programming, specifically focusing on exception handling, multithreading, and Java I/O basics. It includes explanations of various keywords, classes, and methods related to these topics, along with examples and comparisons. Additionally, it discusses the importance of synchronization, generic programming, and the distinction between built-in and user-defined exceptions.

Uploaded by

pradeeplavrp09
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
You are on page 1/ 6

CS3391 – OBJECT ORIENTED PROGRAMMING

CIA - II
Study ALL Questions.

SHORT QUESTIONS
1. What is Finally keyword in Exception handling?
Ans. The finally block is used to execute important code, such as
: closing resources, regardless of whether an exception is thrown
or not. It is always executed after the try and catch blocks.

Syntax
try
{
// try block code
}
catch ( ExceptionType e)
{
// Handle exception
}
finally
{
//code that has to be executed finally
}

2. Write down the syntax for multiple catch clauses.


try
{
//code that may throw a exception
}
catch(ExceptionType1 e1)
Ans. {
: //Handle ExceptionType1
}
catch(ExceptionType2 e2)
{
//Handle ExceptionType2
}
3. Classify the chained exception.
Ans. Chained exceptions allow an exception to be
: associated with another, typically indicating the
cause.

Constructors Of Throwable class


1.Throwable(Throwable cause):-Where cause is the
exception that causes the current exception.
2.Throwable(String msg, Throwable cause) :- Where
msg is the exception message and cause is the
exception that causes the current exception.

Methods Of Throwable class


1.getCause() method :- This method returns actual
cause of an exception.
2.initCause(Throwable cause) method :-This
method sets the cause for the calling exception.

4. Discuss about multithreading.

Multithreading is an environment in which multiple


threads are created and they can execute
simultaneously.The multiple threads can be
created either by extending the thread class or by
Ans.
implementing the runnable interface.
:
Multiple threads can be created by,
1. Extending a Thread class.
2. Implementing the runnable interface.

5. Compare throw and throws keyword in java.


Throw Throws
For explicity throwing
For declaring the
the exception , the
exception, the keyword
Throw keyword is
Throws is used.
used.
Ans. Throw is followed by Throws is followed by
: instance. exception class.
Throw is used within Throws is used with
the method. methos signature.
It is possible to declare
We cannot throw
multiple exceptions
multiole exceptions.
using Throws.
6. Write down the use of try and catch block in
exception handling. With example.
try block
If an exception occurs within the try block , the
program does not terminate immediately.
If no exception occurs, the code runs normally, and
the catch block is skipped.
catch block:
The catch block follows the try block and is used to
handle specific types of exceptions that may be
thrown by the code in the try block.
It takes an argument , which is the type of
Ans. exception that you want to handle (eg.
: ArithmeticException , IOException),and the name of
the exception object.
Example :
try
{
int division = 10 / 0;
}
catch (ArithmeticException e)
{
System.out.println("Cannot divide by zero.");
}
7. Thread is light weight process - Comment.
Threads do not require separate address for
execution . It runs in the address space of the
Ans. process to which it belongs to.
: In Thread , the Parent Thread share their Memory
to their Children Thread. But, In Process even
Children Process need separate memory space.
8. Distinguish between multi-threading and multi-
tasking?
Multitasking Multithreading
Multiple programs gets Multiple parts of a
executed in single program gets
multitasking executed in
environment. multithread
environment
In multitasking, the In multitasking, the
processes share processes share
separate memory. separate memory.
Ans. The multitasking The multithreading
: component involves component does not
multiprocessing. involve
multiprocessing.

Multitasking is slow Multithreading is


compared to faster.
multithreading.
Termination of a Termination of thread
process takes more takes less time.
time.
9. List the byte stream classes.

FileInputStream
FileOutputStream
FilterInputStream
Ans. FilterOutputStream
: ByteArrayInputStream
ByteArrayOutputStream
PipedInputStream
PipedOutputStream

10. Distinguish between byte stream and character


stream.
Ans. Byte Stream Character Stream
: It is used for inputting It is used for inputting
and outputting the and outputting the
bytes. characters.
It never supports It supports unicode
unicode characters. characters.
There are two super There are two super
classes used in byte classes used in byte
stream and those stream and those are,
are,InputStream and Reader and Writer.
OutputStream.
Byte is a 8-bit number Character is a 16-bit
type that represent number type that
values from -127 to represent Unicode.
127.
11. Discuss why generic programming is needed.
It saves the programmers burden of creating
separate
methods for handling data belonging to different
Ans. data
: types.
Compact code can be created.
Enhancing code reusability.

12. Illustrate a simple generic class with an example.


class Box<T>
{
private T value;
public void set(T value)
{
this.value = value;
Ans.
}
:
public T get()
{
return value;
}
}

13. Mention the important methods of Buffered Reader


and Writer class.
BufferedReader: read(), readLine(),close().
Ans.
: BufferedWriter: write(), newLine(), flush(), close().

14. Compare byte stream and character stream.


Ans. Byte Stream Character Stream
: It is used for inputting It is used for inputting
and outputting the and outputting the
bytes. characters.
It never supports It supports unicode
unicode characters. characters.
There are two super There are two super
classes used in byte classes used in byte
stream and those stream and those are,
are,InputStream and Reader and Writer.
OutputStream.
Byte is a 8-bit number Character is a 16-bit
type that represent number type that
values from -127 to represent Unicode.
127.
15. Define Java I/O basics.
Java I/O (Input/Output) is a part of Java
programming that allows handling input and output
operations, such as reading from and writing to
files, console, or other input sources. Java provides
Ans.
various classes in the java.io package, like
:
FileReader, FileWriter, BufferedReader,
BufferedWriter, InputStream, and OutputStream, to
facilitate these operations.

16. Illustrate a simple generic class with an example.


What is the difference between error and
exception in java.
class Box<T>
{
private T value;
public void set(T value)
{
this.value = value;
}
public T get()
{
return value;
}
Ans. }
: Here, T is a type parameter, allowing GenericBox
to handle any data type.

Difference between Error and Exception:

Error Exception
Represents serious Represents conditions
issues, such as that that applications can
applications generally catch and handle.
cannot recover from.
OutOfMemoryError IOException
ArithmeticException.
17. Why synchronization is required in thread?
Synchronization is required to manage access to
shared resources when multiple threads are
involved, preventing conflicts and ensuring data
Ans.
consistency. Without synchronization, multiple
:
threads might modify shared data simultaneously,
leading to incorrect results.

18. Give the methods used for inter thread


communication.
Ans. wait(): Causes the current thread to wait until
: another thread invokes notify() or notifyAll() on the
same object.
notify(): Wakes up a single thread that is waiting
BIG QUESTIONS
1. i) What is meant by exceptions? Why it is
needed? Describe the exception hierarchy.
An exception is an unexpected event that occurs
during the execution of a program, disrupting the
normal flow of instructions. Exceptions are used
on the object's monitor.
to handle errors gracefully in Java, allowing the
notifyAll(): Wakes up all threads waiting on the
program to run smoothly without abrupt
object's monitor.
termination.
19. Illustrate what is daemon
Why exceptions thread and which
are needed?
method is used to create the daemon thread.
Daemon
SeparationThread: A daemon
of error handlingthread is a background
and normal logic :
thread that provides services to user
Exception handling allows the programmer threads. to
When all user
separate threads
error finish code
handling execution,
from thetheJVMmain
terminates daemon threads
logic , making automatically.
code more readable and
Daemon threads
maintainable. are typically used for tasks like
Ans. garbage collection.
: Method
Resources to management
Create a Daemon Thread
The setDaemon(true)
Improves code robustnessmethod can be used to mark
a thread as a daemon.
Thread thread
Exception = new Thread();
Hierarchy
thread.setDaemon(true); // Marks the thread as
daemon
20. Illustrate what is thread group.

Thread Group: In Java, a thread group is a group of


threads that share a common parent. It helps in
managing multiple threads as a single unit,
allowing operations like suspending or resuming all
Ans. threads in the group. Thread groups are created
: using the ThreadGroup class.

ThreadGroup group = new


ThreadGroup("MyGroup");
Thread t1 = new Thread(group, "Thread1");
Thread t2 = new Thread(group, "Thread2");

Two main types of exceptions


1.Built-in Exceptions
2.User-Defined Exceptions
Built-in Exceptions:
Built-in exceptions are available in Java libraries.
These exceptions are suitable for handling
common error situations. Here are some
important built-in exceptions in Java:
1. ArithmeticException: Thrown when an
exceptional condition has occurred in an
arithmetic operation.
2. ArrayIndexOutOfBoundsException: Thrown to
indicate that an array has been accessed with an
illegal index. The index is either negative or
greater than or equal to the size of the array.
3. ClassNotFoundException: Thrown when trying
Ans. : to access a class whose definition is not found.
4. FileNotFoundException: Thrown when a file is
not accessible or does not open.
Example
// Java program to demonstrate
ArithmeticException
class ArithmeticException_Demo {
public static void main(String args[]) {

You might also like