Code Design Flashcards

1
Q

When should you use a singleton?

A
  1. To control access to some share resource. For example, a database object.
  2. To provide global access point to some data without it being overwritten. For example, giving you stricter control to global variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a leaky abstraction?

A

A leaky abstraction refers to any implemented abstraction, intended to reduce (or hide) complexity, where the underlying details are not completely hidden.

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

What are closures?

A

A closure is an inner function along with its enclosing environment that retains the state of its enclosing environment.

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

Why would you need an inner function in python?

A

To create a closure that retains the state of its current environment.

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

What is a high order function?

A

A function that does at least one of the following:

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
6
Q

What is an example of a decorator function?

A

def add_messages(func):
… def _add_messages():
… print(“This is my first decorator”)
… func()
… print(“Bye!”)
… return _add_messages

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

What is an example of a closure?

A

def generate_power(exponent):
def power(base):
return base ** exponent
return power

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

What is an example of an abstraction?

A

The TCP layer over IP https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/

Or strings.

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

What is an example of a leaky abstraction?

A

Networks using TCP getting slower with more traffic. The underlying implementation of TCP becomes clearer and is felt.

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

What is TCP and what does it do?

A

Transmission Control Protocol (TCP) is a communications standard that enables application programs and computing devices to exchange messages over a network.

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

What is IP and what does it do?

A

The Internet Protocol (IP) is the method for sending data from one device to another across the internet.

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

How are TCP and IP related?

A

TCP wraps around IP to guarantee delivery of packets. IP obtains and defines the address—the IP address—of the application or device the data must be sent to. TCP is then responsible for transporting and routing data through the network architecture and ensuring it gets delivered to the destination application or device that IP has defined.

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