Programming Paradigms Flashcards

1
Q

Which programming paradigm emphasizes the use of pure functions and immutable data ?

A

FP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which programming paradigm emphasizes the use of objects

A

OOP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are programs constructed in FP ?

A

by composing pure functions that transform data without changing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two characteristics of pure functions ?

A

pure functions have no side effects and always return the same output given the same input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the consequences of coding using FP ?

A

code that is easy to reason about and test,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are higher order functions ?

A

functions that take other functions as inputs or return functions as outputs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name 3 main characters in regards to the story of the FP paradigm

A
  • pure functions
  • immutable data
  • higher order functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what are objects ?

A

instances of classes that encapsulate data and behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Name 3 main concepts highlighted via OOP

A
  • abstraction
  • inheritance
  • polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is abstraction ?

A

process of hiding complexity by exposing only relevant information to the user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is inheritance ?

A

process of classes inheriting properties and methods from other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is polymorphism ?

A

objects of different classes to be used interchangeably, as long as they have a common interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how is data and state managed in FP ?

A

data is immutable and state changes are managed through the creation of new data structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how is data and state managed in OOP ?

A

data is encapsulated within objects and state changes are managed through methods that mutate the object’s internal state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how is control flow managed in FP?

A

control flow is managed through the composition of functions using higher-order functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how is control flow managed in OOP ?

A

control flow is managed through method calls and object interactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

how is encapsulation managed in FP ?

A

functions are pure and have no side effects, so there is no need for encapsulation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is encapsulation ?

A

Encapsulation provides a way to control access to the internal state of an object and helps prevent external code from directly manipulating its data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

how is inheritance managed in OOP ?

A

inheritance is used to create new classes that inherit properties and methods from other classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

how are complex programs created in FP ?

A

composition is used to combine functions and data structures to create more complex programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

what is a HOF ?

A

A higher-order function is a function that takes one or more functions as arguments or returns a function as its result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

how can you build complex functions via HOFs ?

A

Higher-order functions are used to build complex functions by combining simpler functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

why is immutability important in FP ?

A

because it makes it easier to reason about the behavior of the program and helps to prevent bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what is recursion ?

A

Recursion is a technique where a function calls itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

why is recursion often used in FP ?

A

Recursion is often used in functional programming because it is a natural way to express computations that involve repeated calculations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

what is a monad ?

A

A monad is a design pattern used to manage side effects in functional programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

why is a monad is a way to encapsulate a computation that has side effects ?

A

so that the side effects can be managed and controlled.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Name two alternative approaches to code reuse and structuring code that can be used instead of inheritance

A

Object composition and function composition

29
Q

how does object composition work ?

A

by creating a new object that contains references to other objects, and delegating behavior to those objects.

30
Q

List two advantages of object composition

A
  • allows for greater flexibility and modularity
  • avoid the problems of tight coupling and inheritance hierarchies that can arise with inheritance.
31
Q

how does function chaining work in function composition ?

A

by chaining functions together so that the output of one function becomes the input of the next function.

32
Q

List 2 disadvantages of inheritance

A

lead to :-
* tight coupling and
* complex inheritance hierarchies that are difficult to understand and maintain.

33
Q

Name two techniques used in functional programming to transform a function with multiple arguments into a series of functions that each take a single argument.

A

currying and partial currying

34
Q
const numbers = [1, 2, 3, 4, 5];

const doubleNumbers = numbers.map(num => num * 2);

console.log(doubleNumbers); // [2, 4, 6, 8, 10]
A

FP using map

35
Q
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
  }
}

const john = new Person('John', 30);
john.greet(); // "Hello, my name is John and I'm 30 years old."
A

OOP

36
Q

how can you achieve encapsulation in JS ?

A

In JavaScript, encapsulation can be achieved using closures or ES6 classes.

37
Q
class Shape {
  constructor(color) {
    this.color = color;
  }

  draw() {
    console.log("Drawing a generic shape");
  }
}

class Circle extends Shape {
  constructor(color, radius) {
    super(color);
    this.radius = radius;
  }

  draw() {
    console.log(`Drawing a ${this.color} circle with radius ${this.radius}`);
  }
}

class Square extends Shape {
  constructor(color, sideLength) {
    super(color);
    this.sideLength = sideLength;
  }

  draw() {
    console.log(`Drawing a ${this.color} square with side length ${this.sideLength}`);
  }
}

const circle = new Circle('blue', 10);
const square = new Square('red', 5);

circle.draw(); // "Drawing a blue circle with radius 10"
square.draw(); // "Drawing a red square with side length 5"
A

polymorphism

38
Q

how can you achieve polymorphism in JS ?

A

In JavaScript, we can achieve polymorphism through inheritance and method overriding.

39
Q

How can you achieve abstraction in JS ?

A

In JavaScript, we can achieve abstraction through encapsulation and inheritance.

40
Q

Which method below is abstract and why ?

class Vehicle {
  constructor(color) {
    this.color = color;
  }

  start() {
    console.log("Starting the vehicle...");
  }

  stop() {
    console.log("Stopping the vehicle...");
  }

 
  move() {
    throw new Error("Abstract method, must be implemented in subclass");
  }
}

class Car extends Vehicle {
  constructor(color) {
    super(color);
  }

  move() {
    console.log(`Driving the ${this.color} car...`);
  }
}

class Boat extends Vehicle {
  constructor(color) {
    super(color);
  }

  move() {
    console.log(`Sailing the ${this.color} boat...`);
  }
}

const car = new Car('blue');
const boat = new Boat('red');

car.start();
car.move();
car.stop();

boat.start();
boat.move();
boat.stop();
A

The move method is declared as an abstract method, which means it must be implemented in subclasses.

41
Q

what will be the console log output ? which one is the HOF ?

function multiplyBy(factor) {
  return function (number) {
    return number * factor;
  };
}

const double = multiplyBy(2);
const triple = multiplyBy(3);

console.log(double(5)); 
console.log(triple(5)); 
A

output is 10 and 15

mutliplyBy is HOF

42
Q

List one or more ways via which we can achieve more concise and modular code in JS?

A

HOF

43
Q

how can you implement monads in JS ?

A

In JavaScript, we can implement monads using objects with a flatMap or bind method.

44
Q
class Maybe {
  constructor(value) {
    this.value = value;
  }

  static of(value) {
    return new Maybe(value);
  }

  map(fn) {
    if (this.value === null || this.value === undefined) {
      return Maybe.of(null);
    }
    return Maybe.of(fn(this.value));
  }

  flatMap(fn) {
    if (this.value === null || this.value === undefined) {
      return Maybe.of(null);
    }
    return fn(this.value);
  }
}

// Example usage
const user = {
  name: "John Doe",
  address: {
    street: "123 Main St",
    city: "Anytown",
    state: "CA",
  },
};

const maybeUser = Maybe.of(user);
const maybeStreet = maybeUser.flatMap((u) => Maybe.of(u.address)).flatMap((a) => Maybe.of(a.street));
console.log(maybeStreet.value); // Output: "123 Main St"

const maybeZip = maybeUser.flatMap((u) => Maybe.of(u.address)).flatMap((a) => Maybe.of(a.zip));
console.log(maybeZip.value); // Output: null
A

monads

45
Q

List out two advantages of currying and partial currying

A

currying and partial currying can make code more flexible and modular, and can simplify function composition in functional programming.

46
Q

Define currying

A

Currying is the process of transforming a function with multiple arguments into a series of functions that each take a single argument.

47
Q

Define partial currying

A

Partial currying is a technique that allows us to partially apply a function with multiple arguments, meaning we can provide some of the arguments and get back a new function that takes the remaining arguments.

48
Q

function multiply(x, y, z) {
return x * y * z;
}

var multiplyByTwo = multiply.bind(null, 2);
console.log(multiplyByTwo(3, 4));

A

output = 24

partial currying

49
Q

What is DI ?

A

Dependency Injection is a design pattern that allows you to inject dependencies into an object rather than having the object create its own dependencies.

50
Q

Name one specific implementation of IOC

A

DI

51
Q

List out the advantages of using DI

A

makes your code more modular, testable, and easier to maintain.

52
Q

What happens in IOC ?

A

instead of your code controlling the creation and flow of objects, the control is handed over to a framework or container that handles the creation and management of objects.

53
Q

Tell me what happens in Reactive Programming

A

data streams are represented as Observable sequences, and transformations are applied to these sequences using operators.

54
Q

List out two features of FRP

A
  • emphasizes the use of pure functions to handle data transformations, and
  • makes use of functional programming concepts such as composition, mapping, and folding to create more declarative and composable code.
55
Q

Name 2 libraries via which you can implement FRP

A

Bacon.js and Most.js

56
Q

What is a Rest API ?

A

A REST API (Representational State Transfer Application Programming Interface) is a type of web service that uses HTTP requests to access and manipulate resources or data.

57
Q

Full form of REST API

A

Representational State Transfer Application Programming Interface

58
Q

What are SOLID principles ?

A

SOLID principles are a set of design principles that aim to make software more maintainable, scalable, and robust.

59
Q

What is SRP ?

A

A class or function should have only one reason to change.
In JavaScript, this can be applied by breaking down large functions into smaller, more focused functions that each handle a specific responsibility.

60
Q

Full form of SRP ?

A

Single Responsibility Principle (SRP)

61
Q

full form of OCP ?

A

Open/Closed Principle (OCP)

62
Q

Explain OCP

A

A class or function should be open for extension but closed for modification.
In JavaScript, this can be achieved by using object composition instead of inheritance, and by using interfaces to define contracts between components.

63
Q

LSP full form ?

A

Liskov Substitution Principle (LSP)

64
Q

Explain LSP

A

Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types.
In JavaScript, this means that any object that implements a certain interface or inherits from a certain class must be able to be used interchangeably with any other object that implements the same interface or inherits from the same class.

65
Q

ISP full form ?

A

Interface Segregation Principle (ISP)

66
Q

Explain ISP

A

A client should not be forced to depend on methods it does not use.
In JavaScript, this means that interfaces should be kept small and focused, with only the methods and properties that are actually needed by the client.

67
Q

DIP full form ?

A

Dependency Inversion Principle (DIP)

68
Q

Explain DIP

A

Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.
In JavaScript, this means that code should depend on interfaces or abstractions instead of concrete implementations, and that dependencies should be injected into classes or functions instead of being hard-coded.

69
Q

State 7 key features of REST API

A
  • Resource identification: Resources are identified by a unique URI (Uniform Resource Identifier).
  • HTTP verbs: REST APIs use standard HTTP verbs like GET, POST, PUT, DELETE, etc. to perform specific operations on resources.
  • Stateless: Each request from the client to the server must contain all the necessary information to complete the request. The server does not maintain any state or session information about the client.
  • Representation-oriented: Resources can be represented in different formats such as JSON, XML, or plain text.
  • Hypermedia-driven: REST APIs use hyperlinks to connect resources and allow clients to navigate through the API.
  • Cacheable: Responses from the server can be cached to improve performance.
  • Layered architecture: REST APIs can be layered to improve scalability and security.