0% found this document useful (0 votes)
22 views8 pages

PL2 Test Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

PL2 Test Questions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Define parameterized abstract data type.

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.

1.1. Describe it, how it is different from the basic ADTs.


A parameterized abstract datatype (generic datatype) allows flexibility by
accepting type parameters, making it adaptable to various data types. In
contrast, basic ADTs have fixed operations designed for a specific data type,
lacking the generic adaptability of parameterized ADTs.

1.2. Show Examples in Java

2. Describe the question of continuation after exception handler was executed.


(Could be explained as a figure as well)
After an exception handler is executed, program control typically moves to the finally
block (if present), then continues with the subsequent code following the try-catch-
finally construct. If there is no finally block, the program proceeds directly to the next
statement after the try-catch block.
3. What is the difference between a class variable and an instance variable. Show
an example.
A class variable is shared among all instances of a class, while an instance variable
is unique to each instance.
Ex:
class Example {
static int classVariable; // Class variable
int instanceVariable; // Instance variable
}

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.

6.1. Why do methods need to be virtual?


Methods need to be virtual to enable dynamic method resolution in
polymorphic scenarios, allowing the correct method to be invoked at runtime
based on the actual object type.

6.2. Show a very short example for it in C++.


class Animal {
public:
virtual void makeSound() {
std::cout << "Animal makes a generic sound." << std::endl;
}
};

class Cat : public Animal {


public:
void makeSound() override {
std::cout << "Cat meows." << std::endl;
7. Why are destructors not as frequently needed in Java as they are in C++?
In Java, automatic garbage collection manages memory, reducing the need for explicit
destructors as compared to manual memory management in C++.

8. Compare function overloading vs overriding.


Function overloading involves defining multiple functions with the same name but
different parameters in the same class. Function overriding occurs when a derived
class provides a specific implementation for a method already defined in the base
class, maintaining the same signature.

8.1. Show examples on each of them.


Overloading:

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

10. Why coroutines are called quasi-concurrent?


Coroutines are called quasi-concurrent because they enable cooperative multitasking,
allowing multiple tasks to appear concurrent while running on a single-threaded event
loop.

11. Describe the general characteristic of subprograms! [entry point, calling


program unit, returning…]
Subprograms have an entry point where control begins, are invoked by a calling
program unit, and may return control and possibly a value to the calling unit. They
encapsulate reusable code, promoting modularity and maintainability in a program.

12. Explain Pass-by-Value-Result parameter passing mode!


Pass-by-value-result is an implementation model for inout-mode parameters
in which actual values are copied. It is in effect a combination of pass-by-value
and pass-by-result. The value of the actual parameter is used to initialize the
corresponding formal parameter, which then acts as a local variable. In fact,
pass-by-value-result formal parameters must have local storage associated with
the called subprogram. At subprogram termination, the value of the formal
parameter is transmitted back to the actual parameter.

13. What is information hiding?


Information hiding is the principle of restricting access to certain details of an object
and revealing only what is necessary for its external interactions.

14. Describe exception handling in Java! (include examples) [exception, exception


handler, raising an exception, continuation, finally]
Exception handling in Java involves the use of try, catch, and finally blocks.
Exceptions are raised with throw and caught with catch. The finally block is used for
cleanup code that should run regardless of an exception.
Ex:
15. Describe the semantics of a call to a “simple” subprogram!
Save the execution status of the caller
Pass the parameters
Pass the return address to the callee
Transfer control to the callee

16. Explain what is activation record.


An activation record, also known as a stack frame, stores the state of a function or
method during its execution, including local variables and return addresses.

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).

Overriding: Providing a specific implementation for a method in a subclass.

Abstract Method: Declared in an abstract class, without implementation.

Interface: Defines a contract for implementing classes.

Visibility: Public, Private, Protected, Package-Private.


Polymorphism: Objects of different types treated through a common 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.

In Java, to override a method:


Use @Override annotation.
The method signature must match that of the superclass.
It should have the same return type or its covariant subtype.
Access level in the subclass should not be more restrictive than in the superclass.

21. Show the Java way of abstract data types!


In Java, abstract data types (ADTs) are often implemented using classes and
interfaces.

You might also like