Programming Chapter 9: Subprograms
106 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What distinguishes C# 2005's generic methods from those in Java 5.0?

  • C# supports type constraints on generic parameters.
  • C# allows wildcards in generic methods.
  • Actual type parameters can be omitted in C# if inferred. (correct)
  • Java allows type inference in all cases.
  • Which of the following correctly illustrates parametric polymorphism in Java?

  • void printCollection(Collection c) { for (Object e : c) { ... } } (correct)
  • void print(int value) { System.out.println(value); }
  • void print(List l) { for (Element e : l) { ... } }
  • void add(T element) { list.add(element); }
  • What is the purpose of the apostrophe and single letter notation used in F# generic types?

  • To define a fixed type parameter.
  • To specify a static type.
  • To declare a type constraint.
  • To denote an inferred generic type. (correct)
  • Which feature is NOT supported in F# regarding generic functions?

    <p>User-defined conversions between types.</p> Signup and view all the answers

    What does the format code %A signify in an F# function?

    <p>It formats any type for printing.</p> Signup and view all the answers

    How can operators be overloaded in programming languages mentioned?

    <p>Through defining methods with specific names.</p> Signup and view all the answers

    What is a notable limitation of generic functions in F# compared to Java and C++?

    <p>F# generic functions have less utility due to type inference restrictions.</p> Signup and view all the answers

    What feature of Java's generic methods is exemplified in the printCollection method?

    <p>They can work with any collection class.</p> Signup and view all the answers

    What is a key advantage of using stack-dynamic local variables?

    <p>They allow for the storage of local variables shared among several subprograms.</p> Signup and view all the answers

    Which parameter passing mode initializes the formal parameter with the value of the actual parameter?

    <p>In mode</p> Signup and view all the answers

    In which programming languages are local variables primarily stack dynamic by default?

    <p>C and C++</p> Signup and view all the answers

    What is a disadvantage of passing parameters by physical move?

    <p>It requires additional storage due to copies.</p> Signup and view all the answers

    What is true regarding the support for closures in programming languages that allow nested subprograms?

    <p>Support for closures depends on the language's implementation of nested subprograms.</p> Signup and view all the answers

    Which of the following defines parametric polymorphism?

    <p>A method that can operate on multiple data types.</p> Signup and view all the answers

    What differentiates C# generic methods from Java generics?

    <p>C# supports type inference directly.</p> Signup and view all the answers

    Which benefit of local static variables contrasts with stack-dynamic local variables?

    <p>They maintain their values between invocations.</p> Signup and view all the answers

    What is a characteristic of pass-by-result?

    <p>The formal parameter acts as a local variable and transmits the value back only at the end.</p> Signup and view all the answers

    Which of the following describes a disadvantage of pass-by-reference?

    <p>It can lead to unwanted side effects and aliasing.</p> Signup and view all the answers

    What aspect of pass-by-name allows for flexibility?

    <p>Binding occurs at the time of reference or assignment, enabling late binding.</p> Signup and view all the answers

    What is a potential problem when using pass-by-result?

    <p>The previous value of the parameter may be lost if not handled correctly.</p> Signup and view all the answers

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

    <p>Pass-by-value-result involves returning the final value back to the caller.</p> Signup and view all the answers

    Which notation represents pass-by-reference effectively?

    <p>An access path to the actual parameter is provided during the call.</p> Signup and view all the answers

    What is one common issue associated with using pass-by-name?

    <p>Increased risk of unwanted side effects.</p> Signup and view all the answers

    What is a main benefit of using pass-by-reference in programming?

    <p>Elimination of copying values, leading to efficiency.</p> Signup and view all the answers

    What is the key advantage of using overloaded subprograms in programming?

    <p>They enable multiple functionalities under the same function name.</p> Signup and view all the answers

    What is a significant trade-off between using static and dynamic local variables?

    <p>Static variables retain their value between function calls, while dynamic variables do not.</p> Signup and view all the answers

    In which scenario would using static local variables be more efficient than dynamic local variables?

    <p>When the subprogram is called frequently and needs to maintain state.</p> Signup and view all the answers

    How do closures differentiate from regular functions?

    <p>Closures capture their surrounding state, while regular functions do not.</p> Signup and view all the answers

    What is a major benefit of using generic subprograms?

    <p>They simplify the coding process by reducing the number of functions needed.</p> Signup and view all the answers

    Which of the following best describes a coroutine?

    <p>It is a function that can be interrupted and resumed at a later time.</p> Signup and view all the answers

    What is a unique characteristic of closures in programming?

    <p>They can retain access to variables that are out of scope.</p> Signup and view all the answers

    What is a key design issue associated with overloaded subprograms?

    <p>Parameter ambiguity can arise when distinguishing between overloads.</p> Signup and view all the answers

    Why are coroutines considered advantageous for I/O-bound tasks?

    <p>They allow for non-blocking execution and can yield control efficiently.</p> Signup and view all the answers

    What is the primary difference between pass-by-value and pass-by-reference?

    <p>Pass-by-value uses a copy of the data, while pass-by-reference uses the original data.</p> Signup and view all the answers

    Which parameter passing method retains its value across function calls?

    <p>Static local variables</p> Signup and view all the answers

    In which scenario is pass-by-name particularly advantageous?

    <p>When the exact value passed needs to be evaluated multiple times within the function.</p> Signup and view all the answers

    Which of the following is a disadvantage of using pass-by-reference?

    <p>It can lead to unexpected side effects on the original data.</p> Signup and view all the answers

    What is a characteristic feature of pass-by-result?

    <p>It returns a modified value of the formal parameter to the caller.</p> Signup and view all the answers

    Which parameter passing method requires the actual parameter to be evaluated only once?

    <p>Pass-by-value-result</p> Signup and view all the answers

    What semantic model is used when a parameter allows both input and output?

    <p>Inout mode</p> Signup and view all the answers

    What is a primary consideration when selecting a parameter passing method for a function?

    <p>Whether the data needs to be modified within the function.</p> Signup and view all the answers

    Static local variables in a function retain their value between function calls.

    <p>True</p> Signup and view all the answers

    Coroutines can only be executed in a preemptive multitasking environment.

    <p>False</p> Signup and view all the answers

    Closures allow functions to access variables from their local environment even when those variables are no longer in scope.

    <p>True</p> Signup and view all the answers

    Function overloading can lead to confusion due to having multiple functions with the same name but different behavior.

    <p>False</p> Signup and view all the answers

    Generic subprograms provide polymorphic behavior without sacrificing type safety.

    <p>True</p> Signup and view all the answers

    Using dynamic local variables is generally more efficient than static local variables due to memory allocation flexibility.

    <p>False</p> Signup and view all the answers

    Overloaded functions in C++ can have the same parameter types as long as their return types differ.

    <p>False</p> Signup and view all the answers

    In programming, coroutines can be used to manage I/O-bound tasks effectively.

    <p>True</p> Signup and view all the answers

    Static local variables in subprograms are automatically destroyed once the subprogram execution finishes.

    <p>False</p> Signup and view all the answers

    Closures do not have any impact on the behavior of functional programming.

    <p>False</p> Signup and view all the answers

    Pass-by-reference allows modifications to the actual variable used in the function call.

    <p>True</p> Signup and view all the answers

    Static local variables lose their value after the function call ends.

    <p>False</p> Signup and view all the answers

    Pass-by-value-result combines both the characteristics of pass-by-value and pass-by-reference.

    <p>True</p> Signup and view all the answers

    Dynamic local variables are not suitable for recursive function calls.

    <p>False</p> Signup and view all the answers

    Pass-by-name requires the actual parameter to be evaluated multiple times during the function execution.

    <p>True</p> Signup and view all the answers

    Pass-by-value copies the value of the actual parameter, so changes made inside the function do not affect the original variable.

    <p>True</p> Signup and view all the answers

    In pass-by-result, the actual parameter is not evaluated before entering the function.

    <p>False</p> Signup and view all the answers

    Using static local variables can lead to unexpected results in recursive functions due to their retained values.

    <p>True</p> Signup and view all the answers

    What effect does pass-by-value have on the original variable when modified in a subprogram?

    <p>The original variable remains unchanged regardless of modifications.</p> Signup and view all the answers

    Which parameter passing method is most memory-efficient for large data types?

    <p>Pass-by-reference</p> Signup and view all the answers

    In which programming scenario would you mainly choose pass-by-value?

    <p>When the original data should remain unchanged.</p> Signup and view all the answers

    What is a potential risk of using pass-by-reference?

    <p>Unintended side effects on the original variable can occur.</p> Signup and view all the answers

    How does pass-by-name differ from other parameter passing methods?

    <p>It evaluates the actual parameter each time it is accessed.</p> Signup and view all the answers

    What outcome can be expected when using pass-by-value with a complex data type, such as an array in Java?

    <p>A new copy of the array is created and modifications don't affect the original.</p> Signup and view all the answers

    What functionality does pass-by-result provide in parameter passing?

    <p>The actual parameter is initialized with the results after execution.</p> Signup and view all the answers

    Which parameter passing method might be the most restrictive in terms of flexibility during execution?

    <p>Pass-by-value</p> Signup and view all the answers

    Pass-by-value creates a copy of the original parameter and allows modifications to affect the original.

    <p>False</p> Signup and view all the answers

    In pass-by-reference, the original argument can be modified directly within the subprogram.

    <p>True</p> Signup and view all the answers

    Pass-by-value is generally more memory-efficient compared to pass-by-reference.

    <p>False</p> Signup and view all the answers

    Using pass-by-value is appropriate when changes to the original parameter are not intended.

    <p>True</p> Signup and view all the answers

    Languages like C and Java utilise pass-by-reference for all parameter types.

    <p>False</p> Signup and view all the answers

    Pass-by-result initializes the formal parameter with a copy of the actual parameter value.

    <p>False</p> Signup and view all the answers

    In Python, lists are passed by reference, meaning modifications inside a function affect the original list.

    <p>True</p> Signup and view all the answers

    Pass-by-name evaluates the actual parameter every time it is accessed within the subprogram.

    <p>True</p> Signup and view all the answers

    Dynamic local variables support recursion by allowing the allocation of new storage with each subprogram invocation.

    <p>True</p> Signup and view all the answers

    Static local variables are suitable for recursive subprograms since they maintain their values between invocations.

    <p>False</p> Signup and view all the answers

    Dynamic allocation of local variables can lead to significant overhead primarily due to runtime memory management.

    <p>True</p> Signup and view all the answers

    Static local variables are frequently used in scenarios that require maintaining state, such as caching computed values.

    <p>True</p> Signup and view all the answers

    Programming languages like Python and Java primarily use static local variables by default.

    <p>False</p> Signup and view all the answers

    What is a significant disadvantage of using dynamic local variables in subprograms?

    <p>They require runtime memory management leading to overhead.</p> Signup and view all the answers

    In which scenario are static local variables particularly useful?

    <p>When caching computed values across subprogram invocations.</p> Signup and view all the answers

    Which programming technique relies heavily on dynamic local variables?

    <p>Depth-first search in graph traversal.</p> Signup and view all the answers

    Which of the following is NOT a characteristic of static local variables?

    <p>They are suitable for recursive function implementations.</p> Signup and view all the answers

    What is a primary benefit of stack-dynamic local variables in modern programming languages?

    <p>They automatically release memory upon function exit.</p> Signup and view all the answers

    What is the primary benefit of using overloaded subprograms in software design?

    <p>They enhance code readability and API design.</p> Signup and view all the answers

    Which programming languages commonly utilize overloaded subprograms?

    <p>C++, Java, and C#</p> Signup and view all the answers

    How does overloading simplify API design?

    <p>By enabling the same function name for different parameter types.</p> Signup and view all the answers

    What is a potential downside of using overloaded functions?

    <p>They can confuse programmers with similar function signatures.</p> Signup and view all the answers

    What is one significant advantage of generic subprograms?

    <p>They allow functions to handle various data types without rewriting code.</p> Signup and view all the answers

    What is a key feature that differentiates coroutines from traditional functions?

    <p>Coroutines can suspend and resume execution at specific points.</p> Signup and view all the answers

    Which programming languages are mentioned as having built-in support for coroutines?

    <p>Lua, Python, and C#</p> Signup and view all the answers

    What advantage do coroutines offer over threads in programming?

    <p>Coroutines reduce overhead by allowing functions to pause and resume.</p> Signup and view all the answers

    What keyword in Python allows a coroutine to yield control back to the caller?

    <p>await</p> Signup and view all the answers

    Why are coroutines particularly useful in I/O-bound applications?

    <p>They can suspend operations while waiting for I/O without blocking.</p> Signup and view all the answers

    What is a common misconception about the execution of coroutines?

    <p>Coroutines require preemptive multitasking to function.</p> Signup and view all the answers

    In which scenario are coroutines widely applied in real-world programming?

    <p>Animating characters in game development.</p> Signup and view all the answers

    What is a potential disadvantage of using coroutines for concurrency?

    <p>Coroutines can't implement complex task parallelism as reliably as threads.</p> Signup and view all the answers

    What is a closure primarily defined as?

    <p>A combination of a function and its environment</p> Signup and view all the answers

    How do closures simplify code, especially in functional programming?

    <p>They eliminate the need for additional objects to maintain state</p> Signup and view all the answers

    In what scenario are closures particularly useful?

    <p>In event handling to preserve state across calls</p> Signup and view all the answers

    Which of the following examples illustrates a closure mechanism?

    <p>A function returning another function that can access its variables</p> Signup and view all the answers

    What benefit do closures provide when building factory functions?

    <p>They allow functions to keep unique references to creation parameters</p> Signup and view all the answers

    Which programming language does NOT implement closures in a first-class way?

    <p>Java 7</p> Signup and view all the answers

    What is a significant drawback of using closures in programming?

    <p>They often lead to increased memory consumption due to retained variables</p> Signup and view all the answers

    What is one of the primary advantages of using closures in decorators within Python?

    <p>They preserve the original function's state while modifying behavior</p> Signup and view all the answers

    Study Notes

    Chapter 9: Subprograms

    • Subprograms are fundamental programming tools
    • They define actions of the subprogram abstraction
    • Python functions are executable, while others are not (e.g. non-executable)
    • Ruby functions can appear inside or outside class definitions
    • Lua functions are anonymous
    • Subprogram calls are explicit requests for execution
    • A subprogram header defines the subprogram, kind, and formal parameters

    Subprograms: Basic Definitions

    • The parameter profile (signature) of a subprogram contains the number, order, and data types of the parameters
    • A function's protocol is its parameter profile and, if applicable, the return type
    • Function declarations in C and C++ are often called prototypes
    • A subprogram declaration provides the protocol but not the body
    • Formal parameters act as dummy variables in the subprogram header
    • Actual parameters are values or addresses used within the subprogram call statement

    Parameter Correspondence

    • Positional binding: parameters match based on their position within the argument list (first to first, etc.)
    • Keyword binding: explicit mapping of actual parameters to formal parameters.

    Formal Parameter Default Values

    • In some languages (like C++, Python, Ruby, and PHP), formal parameters can have default values.
    • In C++, default parameters must appear last
    • Variable number of parameters in C# are an array preceded by params
    • Ruby uses hash literals, with preceding asterisk

    Procedures and Functions

    • Procedures define parameterized computations
    • Functions resemble procedures but semantically model mathematical functions
    • They are ideally intended to have no side effects

    Design Issues for Subprograms

    • Are local variables static or dynamic?
    • Can subprogram definitions appear in other subprogram definitions?
    • What parameter passing mechanisms are used?
    • Are parameter types checked?
    • If subprograms can be parameters, what is the referencing environment of parameters?
    • Are functional side effects allowed?
    • What types of values are returned from functions?

    Local Referencing Environments

    • Local variables are generally stack-dynamic in contemporary languages
    • Advantages: support for recursion and shared local storage between subprograms
    • Disadvantages: allocation/de-allocation time, indirect addressing, cannot be history sensitive
    • Local variables can also be static

    Parameter Passing Methods

    • In Mode: physical copy of value, less efficient
    • Out Mode: values are passed back without transmission
    • Inout Mode: combination, inout or two way

    Conceptual Models of Transfer

    • Physically move a value
    • Move an access path to a value

    Parameter Passing Methods of Major Languages

    • C: pass-by-value and address (pointer)
    • C++: pass-by-value and reference type
    • Java: non-object parameters are passed by value, objects by reference
    • Fortran 95+: parameters can be declared as in, out, or inout mode
    • C#: default pass-by-value, ref can be used for pass-by-reference
    • PHP: similar to C#, formal/actual with ref (optional)
    • Swift: default is pass-by-value but inout is possible
    • Perl: arguments placed in the predefined array @
    • Python and Ruby: pass-by-assignment to objects

    Type Checking Parameters

    • Type checking is critical for reliability but relatively new language do not require it(e.g , Perl, Javascript, and PHP)

    Multidimensional Arrays

    • Multidimensional arrays passed to subprograms require the compiler to know the declared size for storage mapping

    Overloaded Subprograms

    • Overloaded subprograms share the same name but have different protocols (e.g., parameters, return type)

    Generic Subprograms

    • Generic subprograms can operate on parameters of different types
    • Subtype polymorphism is available in object-oriented languages that allows a variable of one type to reference objects of itself or any derived type
    • Parametric polymorphism provides a convenient compile time alternative to dynamic polymorphism.

    User-Defined Overloaded Operators

    • User-defined operators can be overloaded and used in language like: Ada, C++, Python, and Ruby

    Closures

    • A closure combines a subprogram and its referencing environment
    • It is needed in statically scoped languages with nested subprograms access to variables in enclosing block.

    Coroutines

    • Coroutines are subprograms with multiple entries, controlled by themselves (e.g., symmetric control).
    • Different from ordinary subprograms, coroutines usually are more equal with each other in invoking, and can be executed repeatedly.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    Explore the fundamentals of subprograms, including their definitions, signatures, and protocols across different programming languages like Python, Ruby, and Lua. This quiz will test your understanding of function declarations and parameter correspondence in various programming contexts.

    More Like This

    Nested Subprograms and Blocks Quiz
    34 questions
    Subprogramas do Pronaf
    12 questions

    Subprogramas do Pronaf

    EnergyEfficientQuasimodo avatar
    EnergyEfficientQuasimodo
    Database Subprograms and Triggers
    30 questions
    Use Quizgecko on...
    Browser
    Browser