Week 8- Subprogram Flashcards

1
Q

What is abstraction in programming languages?

A

Abstraction in programming languages refers to the concept of hiding low-level details and replacing them with subprogram calls, which enhances readability and manages complexity.

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

What are the two main types of abstraction in programming?

A

The two main types of abstraction are data abstraction and process abstraction.

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

What are the benefits of using subprograms in programming?

A

Subprograms promote reuse, leading to savings in memory space and coding time, and increase readability by hiding low-level details.

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

How are subprograms related to methods in object-oriented programming (OOP)?

A

Subprograms are closely related to methods in OOP, but they differ in how they are called and their associations with classes.

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

What are the fundamental characteristics of subprograms?

A

Subprograms typically have a single entry point, only one subprogram is in execution at a time, and control returns to the caller after termination.

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

What is the difference between procedures and functions in subprograms?

A

Procedures perform actions without returning a value, whereas functions return a value as a result of their execution.

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

What are some design issues related to subprograms?

A

Design issues include local referencing environments, parameter-passing methods, handling parameters that are subprograms, and the use of overloaded and generic subprograms.

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

What is a closure in the context of subprograms?

A

A closure is a subprogram that captures the environment in which it was created, allowing it to access those captured variables even when called outside their scope.

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

What are coroutines and how do they differ from subprograms?

A

Coroutines allow multiple entry points and can suspend and resume execution, unlike traditional subprograms that have a single entry point and run to completion.

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

How does parameter passing improve subprogram flexibility?

A

Parameter passing allows subprograms to be more flexible by not relying extensively on nonlocal variables, which enhances reliability and reduces side effects.

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

What are the common parameter-passing methods in subprograms?

A

Common parameter-passing methods include pass-by-value, pass-by-reference, pass-by-value-result, and pass-by-name.

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

What is the significance of a subprogram header?

A

A subprogram header specifies the name, parameter list (if any), and indicates the start of a subprogram definition.

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

What are user-defined overloaded operators in subprograms?

A

User-defined overloaded operators are custom definitions for existing operators (like +, -, etc.) that allow them to be used with user-defined data types.

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

How do generic subprograms function?

A

Generic subprograms are designed to work with any data type, allowing the same subprogram to handle different types without rewriting the code for each type.

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

How does local referencing environment affect subprograms?

A

The local referencing environment defines the scope of variables and how they are accessed, impacting the subprogram’s behavior and interaction with nonlocal variables.

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

Describe the difference between direct access to nonlocal variables and parameter passing.

A

Direct access to nonlocal variables can reduce reliability due to potential side effects, while parameter passing provides a more controlled and flexible way to access data.

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

What is a protocol of a subprogram, and why is it important?

A

Parameter profile + return type, defining how the subprogram can be called and what it returns, crucial for interface consistency and type checking.

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

What are the challenges of extensive access to nonlocal variables?

A

Extensive access to nonlocal variables can lead to reduced reliability and increased risk of unintended side effects in subprograms.

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

Why is it important to declare subprogram protocols in advance?

A

Declaring subprogram protocols in advance allows for static type checking, ensuring that the subprograms are used correctly according to their defined interfaces.

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

What are parameters in subprograms?

A

Parameters are values or references passed to subprograms to transmit computations.

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

Can the name of a subprogram be used as a parameter?

A

Yes

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

What are formal and actual parameters in Python?

A

Formal parameters are placeholders in the subprogram definition, while actual parameters are the values passed during the call.

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

How are positional parameters bound in Python?

A

Positional parameters are bound by their position in the subprogram call.

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

How are keyword parameters bound in Python?

A

Keyword parameters are bound by name.

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

What is a default parameter in Python?

A

A default parameter is a parameter with a predefined value used if no actual parameter is provided.

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

What are static local variables?

A

Static local variables retain their value between subprogram calls.

22
Q

What are global variables?

A

Global variables are defined outside subprograms and can be accessed without declaration.

23
Q

What is the “out mode” for parameters?

A

Transmits data.

23
Q

What are stack dynamic local variables?

A

Stack dynamic local variables are allocated and deallocated automatically and are flexible for recursive calls.

24
Q

What are nested subprograms?

A

Nested subprograms are defined within other subprograms, creating a hierarchy of logic and scopes.

25
Q

Why are nested subprograms useful?

A

They are useful for hiding subprograms that are only needed within another subprogram.

26
Q

What is the “in mode” for parameters?

A

Receives data.

27
Q

What is the “inout mode” for parameters?

A

Can receive and transmit data.

28
Q

What is pass-by-value?

A

Initializes the formal parameter with a copy of the actual parameter.

29
Q

What is a benefit of pass-by-value?

A

Fast access time for scalars.

30
Q

What is a drawback of pass-by-value?

A

Requires additional storage for copies.

30
Q

What is a benefit of pass-by-reference?

A

Efficient in time and space.

31
Q

What is a con of pass-by-result?

A

Possible parameter collision.

31
Q

What is pass-by-result?

A

Transmits the subprogram’s output back to the caller.

32
Q

What is pass-by-value-result?

A

Combines pass-by-value and pass-by-result.

32
Q

How are parameter passing methods implemented?

A

Typically using a run-time stack.

33
Q

What is pass-by-name?

A

Similar to pass-by-reference, but more complex.

33
Q

What is pass-by-reference?

A

Passes the address of the actual parameter.

34
Q

What is a con of pass-by-reference?

A

Can lead to unintentional changes due to aliases.

35
Q

Do most languages require type checking for parameters?

A

Yes, most modern languages require type checking.

35
Q

How can multidimensional arrays be passed as parameters in C and C++?

A

By passing a pointer to the first element and the dimensions (rows and columns) as separate parameters.

35
Q

How do you get the dimensions of a matrix in C# and Java?

A

Use the length property of the array to get the number of rows or columns.

35
Q

What are two design goals for subprograms related to parameters?

A

Minimize data access outside the subprogram and minimize functional side effects.

35
Q

How does pass-by-value differ from pass-by-reference?

A

Pass-by-value creates a copy of the argument, while pass-by-reference creates an alias to the original variable.

36
Q

When using pass-by-value-result, are the changes reflected in the original variable if the address is computed at the call time?

A

No, changes are not reflected.

37
Q

Consider the function fun(int first, int second). How will the list array be affected by pass-by-value, pass-by-reference, and pass-by-value-result?

A

No change for pass-by-value, list becomes {2, 6} for pass-by-reference, and list becomes {2, 6} for pass-by-value-result (assuming address computation at call time).

37
Q

What are two issues to consider when passing subprograms as parameters?

A

Type checking the subprogram’s protocol and referencing environment handling (shallow binding, deep binding, or ad hoc binding).

38
Q

How can subprograms be called indirectly (e.g., event handling)?

A

By using pointers or references to subprograms.

39
Q

What are overloaded subprograms?

A

Subprograms with the same name but different parameter lists or return types.

40
Q

How do ad hoc polymorphism (overloaded subprograms) and subtype polymorphism (OOP) differ?

A

Ad hoc polymorphism allows functions with the same name but different parameter lists or return types. Subtype polymorphism allows a variable of a type T to access any object of type T and derived from T.

41
Q

How do generic functions interact with overloaded functions?

A

If a specific function exists for the argument types, it takes precedence over a generic function.

42
Q

Can generic functions have multiple template parameters?

A

Yes

42
Q

How can generic functions handle arrays?

A

With template parameters.

43
Q

Why are macros less desirable than generic functions for polymorphism?

A

Macros can cause unintended side effects, especially when arguments have side effects.

44
Q

How can operators be overloaded?

A

Operators can be overloaded by defining them as member functions within a class.

45
Q

What are closures?

A

Closures are functions that can be treated like objects. They can be passed to other functions, returned by functions, and stored in data structures.

46
Q

Can lambda functions create closures?

A

Yes

46
Q

How do nested functions work?

A

Inner functions can access variables from outer functions but cannot be called from outside the outer function.