PL2 Test Questions
PL2 Test Questions
A parameterized abstract datatype is a data type that allows the specification of one
or more parameters, enabling the creation of a family of related types by substituting
actual values for these parameters.
4. Define state, data member and member function in OOP terminology. Show an
example.
State: The condition or attributes of an object at a particular point in time.
Data Member: A variable that holds the state of an object.
Member Function: A function that operates on the object's state.
Ex:
class Car {
String color; // Data member
void startEngine() { /* Member function */ }
}
5. Explain three reasons accessors to private types are better than making the
types public.
➢ Read-only access can be provided by having a getter method but no
corresponding setter method.
➢ Constraints can be included in setters. For example, if the data value should
be restricted to a particular range, the setter can enforce that.
➢ The actual implementation of the data member can be changed without
affecting the clients if getters and setters are the only access.
6. Define polymorphism
Polymorphism is the ability of a single function or method to operate on different types
of data or objects.
Overriding:
9. What advantage do monitors have over semaphores?
The advantage of monitors over semaphores is that they are a better way to provide
competition synchronization.
Monitors provide a higher-level abstraction with built-in mutual exclusion and condition
variables, simplifying synchronization compared to low-level semaphore-based
synchronization
17. What is java package and what is its purpose? (Show an example).
A Java package is a grouping mechanism to organize related classes and interfaces.
Its purpose is to prevent naming conflicts, improve code modularity, and facilitate
code organization.
package com.example;
public class MyClass { /* Class implementation */ }
18. Explain all the object-orientation concepts using Java examples for all
elements! (e.g: key characteristics, class, inheritance types, overriding, abstract
method, interface, visibility, polymorphism, nesting (briefly)]
Class: Blueprint for objects.
Inheritance Types: Single (extends), Multiple (interface).
Nesting (Brief): Inner classes declared within outer classes for encapsulation.
19. What are the advantages and disadvantages of static local variables?
Advantages:
Memory Efficiency: Static local variables persist between function calls, retaining
their values without reallocation.
Disadvantages:
Not Thread-Safe: Static local variables may lead to non-thread-safe code due to
shared state among multiple invocations.
Initialization Overhead: Initialization can introduce overhead, especially in resource-
intensive operations.
20. Define overriding and explain overriding methods in Java. (Show examples)
Overriding: Overriding is providing a specific implementation for a method that is
already defined in the superclass.