Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adamward459/8aa572a403639c9e65d7c828cf1781bb to your computer and use it in GitHub Desktop.
Save adamward459/8aa572a403639c9e65d7c828cf1781bb to your computer and use it in GitHub Desktop.

1. What is Object-Oriented Programming (OOP)? Explain with an example or a real case study

OOP is a programming paradigm that revolves around creating "objects." An object encapsulates data (properties or attributes) and the code that operates on that data (methods or functions). Objects interact with each other through messages, promoting modularity and reusability.

Pizza Store Example:

Object: Pizza

Properties: size (small, medium, large), crust (thin, thick), toppings (list of strings)

Methods: calculatePrice(), addExtraTopping(toppingName), removeTopping(toppingName)

Object: Order

Properties: customerName, deliveryAddress, pizzas (list of Pizza objects)

Methods: calculateTotalCost(), addPizza(pizza), removePizza(pizza)

Object: Customer

Properties: name, phoneNumber, addressHistory (list of strings)

Methods: placeOrder(order), getPastOrders()

2. What is polymorphism? Explain with an example or a real case study

Polymorphism allows objects of different classes to respond to the same message in different ways. This enables flexible and dynamic behavior. There are two main types:

Overloading: Methods with the same name but different parameter lists can be defined within a class.

Overriding: Subclasses can inherit methods from a parent class and provide their own specialized implementations.

Pizza Store Example:

Base Class: Topping (abstract class)

Method: getPrice() (abstract - subclasses must implement)

Subclasses: PepperoniTopping, MushroomTopping, ExtraCheeseTopping

Override getPrice() to return their specific price

This way, when calculating the pizza price, the system calls getPrice() on each topping object, but gets the correct price based on the specific topping type.

3. What is the async programming? Explain with an example or a real case study

Asynchronous programming helps applications handle tasks that take time to complete (e.g., network requests, file I/O) without blocking the main thread. This allows the application to remain responsive while waiting for results. Common techniques include callbacks, promises, and async/await.

Pizza Store Example:

  • User submits order online.
  • System asynchronously sends the order to the kitchen for preparation.
  • While the order is being prepared, the user can browse the website or track their order status (without waiting).
  • Once the order is ready, the system updates the user asynchronously.
  • This lets users interact with the application smoothly even while waiting for the order.

4. SQL Query Analysis

Write a SQL query to get a list of bookings which for CY/CY booking associated with 2 different types of item

SELECT h.COMP_CD, h.BOOKING_KEY, h.VENDOR_CD, h.CONSIGNEE, h.CFS_CLOSING_DATE, h.CFS_CLOSING_TIME, h.FREIGHT_TYPE, h.PLANNED_VSL, h.PLANNED_VOYAGE, h.DEST, h.DELIVERY_LOC, h.BOOKING_STATUS, h.HOLD, h.DOC_SET_ID
FROM Booking_header h
JOIN Booking_detail d ON h.BOOKING_KEY = d.BOOKING_KEY
WHERE h.FREIGHT_TYPE = 'CY/CY'
GROUP BY h.COMP_CD, h.BOOKING_KEY
HAVING COUNT(DISTINCT d.ITEM) >= 2;

This query retrieves bookings from the Booking_header table where the FREIGHT_TYPE is 'CY/CY' and there are at least two different types of items associated with each booking in the Booking_detail table. The GROUP BY clause ensures that each booking is considered as a single entity, and the HAVING clause filters out bookings that do not meet the condition of having at least two distinct item types.

Performance Comparison (A vs. B)

Query A performs significantly better than Query B. Here's why:

Query B (SELECT * FROM Booking_header): This retrieves all columns from Booking_header, regardless of whether they're needed. Database engines typically have to read the entire row from storage, even if you don't use all the data, which is inefficient.

Query A (Specific Columns + Filtering): This specifies the columns required and applies a filter (FREIGHT_TYPE = 'CY/CY'). The database only fetches relevant data, reducing I/O overhead and processing time.

Explanation for Performance Difference

Even though both queries might seem to provide the same results. Query A is generally faster.

Query A focuses on relevant information, reducing data transfer and server processing.

Query B retrieves potentially unnecessary data, increasing network traffic and server load.

Query B might involve additional processing on the server side to filter data after fetching, whereas Query A filters at the database level, leading to less processing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment