Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Multiple choice E for Encapsulation, A for Abstraction, I for Inheritance, and P for Polymorphism.

1. A Vehicle class has subclasses like Car. 11. A bank account class contains balance
Bicycle. and Motorcycle. These subclasses information and external code interacts
will still have the common features of the through well-defined methods.
Vehicle (e.g., wheels, engine) while the - E
subclass adds their specific details. 12. Allows a new class to inherit properties and
- I behaviors from an existing class.
2. Restricts direct access to internal details, - I
promoting data protection. 13. Promotes the “is-a” relationship of objects.
- E - I
3. Allows flexibility by providing a single interface 14. Ensures that data remains private and can
for multiple related classes. only be accessed through controlled
- A interfaces.
4. Facilitates the reuse of code and extensibility. - E
- I 15. Access modifiers (public, private, package-
5. Promotes creating generic functions. private, protected) play a role in this principle.
- A - E
6. Also known to perform late-binding. 16. Allows us to focus on what an object does
- P rather than how it does it.
7. Hiding the implementation details of an object - A
or system and exposing only essential features 17. Focuses on creating a hierarchy of classes.
that the user only needs to know. - I
- A 18. A Shape class has subclasses like Circle,
8. Provides a high-level view of the system. Rectangle, and Triangle and despite having
- A different implementations, the Shape class
9. Allows dynamic method dispatch and runtime can be used to call their common methods
flexibility. like calculatePerimeter().
- P - P
10. The computer only has keys and a mouse for 19. Bundles data and methods into a single unit.
you to interact with it, wherein you don't have - E
to worry about how it works internally. 20. Enables objects of different types to be
- A treated uniformly.
- P

Identification.

1. Thrown when an object is typecasted into 3. The parent class of


a class which ibs not an instance of the ArraylndexOutOfBoundsException and
object. StringlndexOutOfBoundsException.
- ClassCastException - IndexOutOfBoundsException
2. Thrown when dividing an integer by zero. 4. Thrown when an assertion fails.
- ArithmeticException - AssertionError
5. Thrown by the Scanner class when an 12. What is the method used to make the
input cannot be parsed to the chosen thread go to either waiting states?
data type. - wait()
-InputMismatchException
13. What is the method used to run the thread
6. Thrown when a file to be opened is not
in that separate thread?
found.
- FileNotFoundException -start()
7. The two methods found in Throwable
which helps provide details for 14. The thread state wherein the thread is to
debugging. (2 pts) pause and will only resume after the other
- getMessage() and printStackTrace(). thread completes its action.
8. The three types of assertions are - WAITING
precondition, postcondition, and
- invariant 15. Write the code for the thread thrd to wait
9. The complete form (not acronym) of the for the thread foo for a maximum of 2
Java' engine that manages the threads run seconds before resuming itself. [2 pts]
by the application. synchronized (foo) {
- Java Virtual Machine (JVM)
10. Write the code to create a thread using try {
the TestRunnable class. Name the thread
foo.wait(2000);
thrd. [2 pts]
- TestRunnable myRunnable = new } catch (InterruptedException e) {
TestRunnable();
e.printStackTrace();
Thread thrd = new Thread(myRunnable);
11. What is the method that is to be }
implemented by every thread class?
-run() }

True or False

1. When you catch multiple exception -True


classes in a single catch block, you use the 5. You can throw errors.
II operator to divide them. -True
-False 6. An exception thrower could be both an
2. You can catch errors. exception catcher and an exception
-True propagator.
3. We must use the throws keyword for -True
methods that may throw any kinds of 7. The try block, when interrupted with
exceptions, otherwise the program will not handling exceptions, will resume after the
compile. completion of the catch block.
-False -False
4. You can throw exceptions inside a catch 8. When there are no exceptions caught in
block, and it will be caught again by the the try block, all the catch blocks will be
same catch block provided it is an skipped.
instance of the same exception class used -True
to catch it.
9. There could be a try block and a finally 12. If there is only one thread of an
block with no catch block. application, then only one logical
-True processor will execute it at a time.
10. You can catch RuntimeException first, -True
then catch IllegalArgumentException next 13. If the main function that created the
in another catch block. threads were to end, the created threads
-True will also no longer run.
11. When you have a try-catch statement, -True
your program will never crash since there 14. The first thread run will always finish first
will always be a catch statement that will before the other threads.
catch the exception. -False
-False 15. A runnable thread could go to the blocked
state multiple times in its lifespan.
-True

Essay.

1. When do we use exceptions and when do we use assertions?

Exceptions are typically used to handle unexpected runtime errors or exceptional


conditions, allowing a program to gracefully recover or terminate. Assertions, on the other
hand, are used to check assumptions during development and testing, providing a means to
detect logical errors and validate program invariants, but they are often disabled in production
for performance reasons.

2. During the previous activity, why do you think the ArrayFullException was a checked exception
and the InvalidPositionException an unchecked exception.

The decision to make ArrayFullException a checked exception might be because a full array
is a foreseeable and recoverable condition that the calling code should handle explicitly. On the
other hand, InvalidPositionException may be an unchecked exception because it represents an
unexpected and potentially unrecoverable error, and forcing every method to declare it might be
impractical or burdensome.

3. A method, when performing a task that may throw an exception, becomes an exception
thrower. An exception thrower has two types: a propagator and a catcher. An exception catcher
is conceptually good since calls to the method will surely not result in an exception thrown.
However when should we make one a propagator and not a catcher?

A method should be a propagator, allowing exceptions to propagate upwards, when the


method itself cannot adequately handle or recover from the exceptional condition, and it is
more appropriate for higher-level code to address and manage the exception. Catchers are
suitable when the method has the capability to handle the exception locally and continue with
its operation, ensuring a more graceful error recovery within the method.
4. What is the purpose of a finally block and how is it different from simply placing the line of code
after the entire try-catch block?

The purpose of a finally block is to contain code that must be executed regardless of
whether an exception is thrown or not, providing a reliable way to perform cleanup operations,
such as releasing resources. Unlike placing code after the entire try-catch block, a finally block
ensures execution even if an exception occurs, making it suitable for tasks that must be
performed in both normal and exceptional cases, enhancing code reliability and
maintainability.

5. What are the two techniques of creating threads? Describe how one is better than the other.

The two techniques of creating threads in Java are extending the Thread class and
implementing the Runnable interface. Implementing the Runnable interface is often considered
better because it allows better separation of concerns, as a class can extend another class
while implementing multiple interfaces, promoting a more flexible and modular design
compared to the single inheritance constraint of extending the Thread class.

You might also like