[go: up one dir, main page]

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

OOP2 Prelims

The document discusses object-oriented programming principles like encapsulation, abstraction, inheritance, and polymorphism. It provides examples of each principle and asks multiple choice questions to test understanding. Key points are that encapsulation restricts access to internal details, inheritance allows subclasses to inherit properties, and polymorphism allows a single interface for multiple classes.

Uploaded by

keiru277
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 views4 pages

OOP2 Prelims

The document discusses object-oriented programming principles like encapsulation, abstraction, inheritance, and polymorphism. It provides examples of each principle and asks multiple choice questions to test understanding. Key points are that encapsulation restricts access to internal details, inheritance allows subclasses to inherit properties, and polymorphism allows a single interface for multiple classes.

Uploaded by

keiru277
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/ 4

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

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

Identification.
1. Thrown when an object is typecasted into a 5. Thrown by the Scanner class when an input
class which is not an instance of the object. cannot be parsed to the chosen data type.
- ClassCastException -InputMismatchException
2. Thrown when dividing an integer by zero. 6. Thrown when a file to be opened is not
- ArithmeticException found.
3. The parent class of - FileNotFoundException
ArraylndexOutOfBoundsException and 7. The two methods found in Throwable which
StringlndexOutOfBoundsException. helps provide details for debugging. (2 pts)
- IndexOutOfBoundsException - getMessage() and printStackTrace().
4. Thrown when an assertion fails. 8. The three types of assertions are
- AssertionError precondition, postcondition, and
- invariant -start()
9. The complete form (not acronym) of the
14. The thread state wherein the thread is to
Java' engine that manages the threads run by
pause and will only resume after the other thread
the application.
completes its action.
- Java Virtual Machine (JVM)
10. Write the code to create a thread using the - WAITING
TestRunnable class. Name the thread thrd. [2
pts] 15. Write the code for the thread thrd to wait for
- TestRunnable myRunnable = new the thread foo for a maximum of 2 seconds
TestRunnable(); before resuming itself. [2 pts]
Thread thrd = new Thread(myRunnable); synchronized (foo) {
11. What is the method that is to be
implemented by every thread class? try {
-run()
foo.wait(2000);
12. What is the method used to make the thread
go to either waiting states? } catch (InterruptedException e) {
- wait()
e.printStackTrace();
13. What is the method used to run the
thread in that separate thread? }
}
True or False
1. When you catch multiple exception classes 7. The try block, when interrupted with
in a single catch block, you use the II handling exceptions, will resume after the
operator to divide them. completion of the catch block.
-False -False
2. You can catch errors. 8. When there are no exceptions caught in the
-True try block, all the catch blocks will be
3. We must use the throws keyword for skipped.
methods that may throw any kinds of -True
exceptions, otherwise the program will not 9. There could be a try block and a finally
compile. block with no catch block.
-False -True
4. You can throw exceptions inside a catch 10. You can catch RuntimeException first, then
block, and it will be caught again by the catch IllegalArgumentException next in
same catch block provided it is an instance another catch block.
of the same exception class used to catch it. -True
-True 11. When you have a try-catch statement, your
5. You can throw errors. program will never crash since there will
-True always be a catch statement that will catch
6. An exception thrower could be both an the exception.
exception catcher and an exception -False
propagator. 12. If there is only one thread of an application,
-True then only one logical processor will execute
it at a time.
-True
13. If the main function that created the threads -False
were to end, the created threads will also no 15. A runnable thread could go to the blocked
longer run. state multiple times in its lifespan.
-True -True
14. The first thread run will always finish first
before the other threads.

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