Programming Paradigms
- 60Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does it mean to understand a programming concept as an abstraction rather than as syntax?
60 questions
- 1
What does it mean to understand a programming concept as an abstraction rather than as syntax?
Model answer
It means understanding the underlying idea independently of how a particular language writes it. For example, an array is a concept that can be recognized across languages with different syntax.
Explanation
Learning several languages can help separate a programming idea from the specific notation used to express it.
- 2
Why can learning several programming languages make it easier to learn additional languages?
Model answer
Once the underlying concepts are familiar, new syntax is easier to learn. It also becomes easier to recognize concepts that recur across different languages.
Explanation
Conditions, collections, arithmetic, and memory operations appear in many languages, even when expressed differently.
- 3
Why are different programming languages or paradigms suited to different problems?
Model answer
They organize programs and express solutions in different ways, so some approaches fit a particular problem better than others. The change-making example, for instance, is awkward as an object-oriented demonstration but fits other paradigms more naturally.
Explanation
Choosing an approach depends partly on how well its way of expressing a solution matches the problem.
- 4
What is procedural programming, and why can it feel intuitive to many programmers?
Model answer
Procedural programming describes a sequence of steps to perform, such as doing one task, then another, and then a final task. It can feel intuitive because people often think about tasks as sequences of actions.
Explanation
Simple programs such as scripts and a basic Hello World program can follow this step-by-step style.
- 5
What common programming concepts can be found across different languages and paradigms?
Model answer
Common concepts include conditionals, collections, arithmetic, and loading and storing values in memory. Their syntax and roles in a program may vary by language or paradigm.
Explanation
Recognizing these recurring concepts helps programmers transfer knowledge between languages.
- 6
How can implementing the same problem in several languages help someone compare programming paradigms?
Model answer
It makes the different ways each paradigm organizes and expresses a solution visible. It also helps distinguish shared programming concepts from paradigm-specific approaches.
Explanation
Comparing multiple implementations highlights both common abstractions and meaningful differences.
- 7
How does the idea of a general-purpose language relate to programming paradigms?
Model answer
A language can support more than one paradigm, even if it is particularly well suited to demonstrating one. Racket and Ruby, for example, can be used in more than one programming style.
Explanation
A language’s common use or strengths do not necessarily restrict it to a single paradigm.
- 8
In Ruby, what is an object, and what do its state and behavior represent?
Model answer
An object encapsulates state and behavior. State is its data, such as fields or instance variables, while behavior is what it does, often through methods.
Explanation
An object brings data and the operations associated with that data together.
- 9
What examples illustrate Ruby’s claim that everything is an object?
Model answer
False and null are objects, and classes themselves are instances of the Object class. Ruby therefore treats even these values and class definitions as objects.
Explanation
This illustrates Ruby’s comparatively pure object-oriented approach.
- 10
How does an object typically modify its own internal state in object-oriented programming?
Model answer
The object uses its own methods to modify its internal state. In Ruby,
selfrefers to the object performing that operation.Explanation
Keeping state modification within the object supports encapsulation.
- 11
How does message passing allow objects to interact in Ruby?
Model answer
One object sends another object a message, typically by calling one of its methods. The receiving object performs the requested behavior and may return a result.
Explanation
For example, sending
lengthto an array asks the array object to calculate and return its length. - 12
How does the bank-account example use encapsulation to protect an account’s balance?
Model answer
The account stores its balance as internal state and exposes a reader rather than allowing outside code to set it directly. Its initialization, deposit, and withdrawal methods manage the balance.
Explanation
Deposits increase the balance, withdrawals decrease it, and the reader allows the balance to be inspected.
- 13
Why should money be represented as an integer number of cents rather than as a floating-point value?
Model answer
The recommended representation is an integer number of cents. Floating-point values are discouraged for representing money.
Explanation
Using integer cents avoids relying on floating-point values for monetary amounts.
- 14
How can an object-oriented design model an everyday object such as a chair?
Model answer
A chair object could have state such as its color and behavior such as holding someone up. This represents the object as a combination of attributes and actions.
Explanation
Object-oriented modeling aligns with the way people often group an entity’s properties and behavior.
- 15
How do objects support code reuse, and what standard-library examples illustrate this?
Model answer
Reusable object types can serve many different programs, avoiding repeated implementations of the same behavior. Arrays, hashes, and strings are examples of commonly reused objects.
Explanation
For instance, programmers can reuse array behavior such as obtaining its length rather than implementing it repeatedly.
- 16
How can object-oriented design support testing at both the component and system levels?
Model answer
An object can be tested in isolation to check its own behavior and state changes. The interactions between objects can then be tested as part of the larger system.
Explanation
Testing individual objects and the connections between them addresses different parts of a system.
- 17
What assumptions and steps underlie the cash-register change-making algorithm?
Model answer
It subtracts the amount owed from the amount tendered, then works through the drawer’s denominations from largest to smallest. It uses a denomination when it does not exceed the remaining change and skips to a smaller one otherwise; the drawer must be sorted largest to smallest.
Explanation
The algorithm tracks the remaining difference and records which bills and coins are used.
- 18
What is pure functional programming, and how does it treat input data?
Model answer
Pure functional programming uses functions that produce output from input without storing state or mutating the input data. A function that transforms an array returns a new array rather than changing the original.
Explanation
This style avoids changes to shared data as part of a function’s operation.
- 19
Why does the output of a pure function depend on its input rather than on program history?
Model answer
A pure function does not rely on stored or changing state, so the same input produces the same output regardless of what happened earlier.
Explanation
The absence of state makes the function’s result independent of prior operations.
- 20
How does functional programming distinguish data from procedures compared with object-oriented programming?
Model answer
Functional programming treats data and procedures as separate, while object-oriented programming commonly encapsulates state and behavior together in objects.
Explanation
This is a useful general distinction, although it is not an absolute description of every language.
- 21
What is prefix notation, and what advantages does it offer for expressing operations?
Model answer
Prefix notation places the operator before its arguments, as in
(+ 3 5). It can accept more than two arguments to an operator and makes nested operation order explicit with parentheses.Explanation
For example, one multiplication operator can be applied to several values, and nested expressions show their grouping directly.
- 22
How are a function and a function call expressed in Racket?
Model answer
A function can be defined with
define, followed by its name and argument list, then its body. It is called by placing the function name and its argument inside parentheses.Explanation
A square function, for instance, can take one argument and multiply it by itself when called.
- 23
How does Racket’s
condconstruct select a result, and how can it calculate an absolute value?Model answer
condchecks conditions in order and evaluates the corresponding result for the first true condition, with anelsecase for the remaining possibilities. For absolute value, it returns a positive input unchanged, returns zero for zero, and negates a negative input.Explanation
This gives the function a result for positive, zero, and negative inputs.
- 24
How are lists represented and manipulated in Racket?
Model answer
A list is written as space-separated items inside parentheses, with a leading tick to distinguish list data from a function call.
carreturns the first item,cdrreturns the rest, andconsadds an item to the front of a list.Explanation
These operations provide basic ways to inspect and build lists.
- 25
How does recursion calculate a factorial in the functional example?
Model answer
The function returns one when its input is one or less. Otherwise, it multiplies the input by a recursive call that calculates the factorial of one less than that input.
Explanation
The base case ends the recursion, while the recursive case reduces the problem.
- 26
How does the recursive Fibonacci function calculate the nth Fibonacci number?
Model answer
It returns zero when the input is less than or equal to zero and one when the input is one. For other inputs, it adds the results for the two preceding inputs.
Explanation
The recursive rule expresses each later Fibonacci number as the sum of the previous two.
- 27
Why can pure functional programming simplify concurrent or threaded programs?
Model answer
When functions do not mutate shared state, programs do not need to coordinate changes to that state with locks. This reduces concerns associated with concurrent access to shared memory.
Explanation
Pure functions perform data-in/data-out operations rather than modifying shared data.
- 28
Why can pure functions be straightforward to test and reuse?
Model answer
Tests can provide input and check the resulting output without first setting up an object’s state. A function can also be reused elsewhere without needing surrounding state or context.
Explanation
The function’s behavior depends on its input rather than on a particular program’s history.
- 29
What makes functional programs, especially Lisp-family programs, appealing to some programmers?
Model answer
They can be concise, and some programmers value their compactness even though others dislike it.
Explanation
Conciseness is presented as a stylistic strength rather than a benefit everyone shares.
- 30
How does the Racket change-making function handle zero, an empty denomination list, and a denomination too large to use?
Model answer
For an amount of zero, it returns an empty list; with no denominations, it returns false. If the first denomination is larger than the amount, it recursively tries the remaining denominations.
Explanation
These cases handle the base conditions and skip a denomination that cannot contribute to the change.
- 31
How does the Racket change-making function represent using a denomination?
Model answer
It adds the first denomination to the result with
consand recursively makes change for the remaining amount using the same denomination list. Reusing the list allows the same denomination to be selected more than once.Explanation
The recursive call reduces the amount by the value of the denomination just used.
- 32
What is the relationship between Racket and Lisp, and what does the name Lisp refer to?
Model answer
Racket belongs to the Lisp family of languages. Lisp stands for “List Processor” and was described as the second-oldest multipurpose high-level programming language, after Fortran.
Explanation
Racket and Lisp were presented as useful languages for demonstrating functional programming, though they are not limited to it.
- 33
What is the foundation of Prolog, and how does a Prolog program differ from a list of instructions?
Model answer
Prolog is based on formal logic and is made up of facts and clauses rather than a sequence of instructions. A program describes relationships and conditions that must hold.
Explanation
Its logical basis includes ideas such as conjunctions, disjunctions, implications, and negation.
- 34
How does describing “what” rather than “how” distinguish Prolog programming?
Model answer
A Prolog program states facts and constraints about what must be true, rather than specifying step-by-step how to solve the problem. Prolog searches for values that satisfy those statements.
Explanation
The system uses the supplied facts and clauses to find a consistent solution.
- 35
How are variables, constants, and facts written in Prolog?
Model answer
Variables begin with capital letters, while constants begin with lowercase letters. Facts end with a period.
Explanation
For example, a statement that a place is a state can be written as a fact using a lowercase constant.
- 36
What role does a Prolog rule play, and what does the
:-symbol indicate?Model answer
A rule specifies a relationship that holds when its conditions hold. The
:-symbol indicates a logical implication from the conditions to the relationship being defined.Explanation
For example, a rule can define two places as adjacent when a border fact connects them.
- 37
How does pattern matching allow Prolog to answer queries containing variables?
Model answer
Prolog searches its facts and rules for matches and assigns values to variables that make the query true. A query with an unknown can therefore ask which value satisfies a relationship.
Explanation
The system tries possible constants against the available facts and clauses.
- 38
Why might a Prolog rule based only on
border(X, Y)fail to show that adjacency works in both directions?Model answer
Prolog follows the facts and rules exactly as written; a fact that Washington borders Oregon does not by itself establish that Oregon borders Washington. The rule must also account for the reversed order.
Explanation
Prolog does not automatically infer that a relationship is symmetric.
- 39
How can facts about borders be used to define adjacency in Prolog?
Model answer
A rule can say that two places are adjacent if a border fact connects them. To make the relation work in either direction, the rule must also allow the border fact with the places reversed.
Explanation
The rule connects a general relationship, adjacency, to the specific border facts in the program.
- 40
How can Prolog answer family-relationship queries in either direction?
Model answer
Facts such as
father(homer, bart)allow a query to find Bart’s father by asking which value fills the first argument. Placing a variable in the second argument can instead find children, and Prolog can provide further answers when more than one fact matches.Explanation
Variables can appear in different argument positions to ask different questions about the same facts.
- 41
How can Prolog define that two people are siblings?
Model answer
Two people are siblings if they share a mother or share a father, provided they are not the same person.
Explanation
The rule searches for a shared parent and includes a condition excluding self-sibling matches.
- 42
What kinds of lists can Prolog represent?
Model answer
Prolog can represent empty lists, lists of values, heterogeneous lists containing different kinds of items, and nested lists.
Explanation
The examples include lists of numbers or constants as well as lists containing other lists.
- 43
How does Prolog’s bar notation split a list for pattern matching?
Model answer
In a pattern such as
[F|R],Fmatches the first element andRmatches the rest of the list. This corresponds to the first-and-rest distinction expressed bycarandcdrin Racket.Explanation
For a list containing one, two, and three,
Fmatches one andRmatches the list containing two and three. - 44
What does the underscore mean in a Prolog rule?
Model answer
The underscore represents a value that the rule does not need to use. It indicates that any value can occupy that position without affecting the logic.
Explanation
This is useful when a list element or argument is irrelevant to a particular rule.
- 45
How does the recursive Prolog
memberrule test whether an item occurs in a list?Model answer
It succeeds if the item is the list’s first element. Otherwise, it ignores the first element and recursively checks whether the item is in the rest of the list.
Explanation
The rule checks the head first and then searches the remaining list.
- 46
How can Prolog use constraints to solve a scheduling problem with limited resources?
Model answer
The program can represent constraints such as experiment durations, repetition intervals, resource availability, and participants’ existing schedules. Prolog can search for a schedule that satisfies the constraints.
Explanation
This approach was illustrated with scheduling experiments that shared a limited resource, described as a dolphin.
- 47
How does a Prolog change-making rule represent a solution?
Model answer
It relates three arguments: the amount to make, the available bills or coins, and the list used to make that amount. A zero amount matches an empty change list, while recursive cases either use the first denomination or skip it.
Explanation
The relationships are expressed as conditions that must hold, rather than as a procedure with a return statement.
- 48
How can a Prolog logic puzzle use clues to determine an arrangement?
Model answer
Clues can be encoded as facts and constraints, such as the person in the yellow house not owning the fish or the person who likes rum living next to the cat owner. Prolog searches for an arrangement in which all the encoded statements are true.
Explanation
This illustrates how a logic program can fill in unknowns by finding a world consistent with its clauses.
- 49
What makes the simple assembly language described in the content a low-level language?
Model answer
It has a very small vocabulary and standard library, with only a limited set of operations and no built-in multiplication, division, or list structure. Programs must work with registers and memory directly.
Explanation
The assembly language described has fewer than thirty operations, making its capabilities deliberately limited.
- 50
What are the roles of the A, D, and M values in the assembly language?
Model answer
A and D are the two registers on which operations are performed. A can hold data or an address, while M refers to the memory cell at the address held in A.
Explanation
When the code uses M, it accesses memory through A rather than using A’s value as ordinary data.
- 51
What kinds of computations can the described assembly language perform?
Model answer
It supports addition and subtraction between A, D, and M; incrementing and decrementing them; negation; bitwise NOT, AND, and OR; and assignments of computed values. It does not provide multiplication or division.
Explanation
The language’s limited operation set requires more complex tasks to be built from simpler instructions.
- 52
How does the assembly language load constants and refer to program locations?
Model answer
The
@notation loads a constant into the A register. A label names a program location that can be placed in A and used as a jump destination.Explanation
The A register is used both to hold values and to identify memory addresses or program locations.
- 53
How do conditional and unconditional jumps provide branching in the assembly language?
Model answer
A jump instruction transfers control to the location in A when its condition is met; conditions such as greater than, equal to, or less than are tested against zero.
JMPjumps unconditionally.Explanation
These jump instructions are the language’s mechanism for branching and looping.
- 54
How does the assembly addition example add two values and store the result?
Model answer
It loads the first value into A and copies it to D, then loads the second value into A and adds it to D. It places address zero in A and stores the result in the memory cell addressed by A.
Explanation
The example demonstrates using registers for arithmetic and memory for storing the result.
- 55
How does the assembly example sum the integers from one through five?
Model answer
It uses one memory cell as an accumulator and another as a counter initialized to five. Each loop adds the counter to the accumulator, decrements the counter, and repeats until the counter reaches zero.
Explanation
A conditional jump exits the loop when the counter is no longer positive; otherwise, an unconditional jump returns to the loop’s start.
- 56
How can memory cells be used to represent a list in an assembly program without built-in list support?
Model answer
A program can assign related values to consecutive or designated memory cells and use them by their addresses. In the change-making example, cells hold the amount, denominations, and counts of coins to use.
Explanation
This represents list-like data explicitly in memory rather than relying on a list data type.
- 57
How does the assembly change-making example decide whether to use a coin denomination?
Model answer
It subtracts the denomination from the remaining amount. If the result is negative, it skips to the next denomination; otherwise, it saves the new remainder, increments the count of that coin, and tries again.
Explanation
This repeats the test for each denomination, beginning with quarters in the example.
- 58
Why can procedural programming be useful even when assembly itself is rarely needed for everyday work?
Model answer
Procedural programming is useful for expressing straightforward sequences of actions and is common in scripts and simple programs. Assembly is a separate, much lower-level tool that most programmers do not need daily.
Explanation
The usefulness of a programming style does not imply that every language demonstrating it is routinely needed.
- 59
How does assembly illustrate the relationship between high-level programming ideas and low-level machine operations?
Model answer
Tasks such as arithmetic, iteration, and branching can be implemented using registers, memory assignments, and jumps, even when the language lacks higher-level structures. The programmer must build those behaviors from the limited operations available.
Explanation
The examples implement addition, loops, and change-making with explicit memory and control flow.
- 60
What does comparing the change-making solutions reveal about how paradigms express the same problem?
Model answer
The object-oriented version organizes the task around a cash register and its drawer, the functional version uses recursive functions and returned lists, the logic version states relationships among amounts and denominations, and the assembly version uses explicit memory and jumps.
Explanation
The goal is the same, but each paradigm emphasizes a different way of organizing the solution.

Ask anything
We've read this whole study set, so questions about it get the best answers.