Bjarne Stroustrup Technology Lecture
- 84Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which set of priorities best describes the intended domain of C++ in the lecture?
Which set of priorities best describes the intended domain of C++ in the lecture?
84 questions
- 1
Which set of priorities best describes the intended domain of C++ in the lecture?
- A
Providing one specialized language for scientific calculations and business records
- B
Replacing hardware-oriented programming with a runtime-managed virtual machine
- C
Making web applications with the shortest possible code and few implementation details
- D
Building efficient abstractions for systems, resource-constrained, and large-scale software
Correct answer
DBuilding efficient abstractions for systems, resource-constrained, and large-scale software
Explanation
The lecture characterizes C++ as a language for efficient and elegant abstractions, especially in systems software, resource-constrained applications, and large systems.
- A
- 2
Why does the lecturer consider type safety important in C++?
- A
It ensures that every operation has the same execution time on every machine
- B
It removes the need to understand what operations a program performs
- C
It guarantees that every program will use less memory than an equivalent untyped program
- D
It helps catch invalid uses of values while working within constraints such as performance and program size
Correct answer
DIt helps catch invalid uses of values while working within constraints such as performance and program size
Explanation
The lecture presents type safety as a key goal, while recognizing that improving it must be balanced against constraints such as performance and program size.
- A
- 3
Why are predictable execution times especially important in hard real-time systems?
- A
Predictability ensures that every operation uses the smallest possible amount of memory
- B
Some operations must finish within known time limits, so unpredictable operations may be unacceptable
- C
A predictable program can be compiled without checking its types
- D
Real-time systems must avoid using any data types that represent hardware
Correct answer
BSome operations must finish within known time limits, so unpredictable operations may be unacceptable
Explanation
The speaker explains that hard real-time systems may require operations to have predictable, usually bounded, execution times.
- A
- 4
What does the lecture mean by wanting code complexity to be proportional to task complexity?
- A
The code should be as complex as the programmer is able to manage
- B
A simple task should not require unnecessarily elaborate code, while harder tasks may justify more complexity
- C
Complex tasks should be rewritten as simple tasks before they can be programmed
- D
Every task should be implemented with the same number of statements
Correct answer
BA simple task should not require unnecessarily elaborate code, while harder tasks may justify more complexity
Explanation
The lecturer describes proportional complexity as an ideal: the code needed should reflect the actual difficulty of the task rather than adding avoidable complexity.
- A
- 5
Why does the lecturer argue that C++ should be friendly to novices as well as experts?
- A
Expert-friendly languages cannot support efficient low-level programming
- B
New users should be able to write any program without learning its concepts
- C
A language that only experts can use effectively limits the number of people it can help
- D
Novices need to avoid abstractions, so the language should exclude advanced features
Correct answer
CA language that only experts can use effectively limits the number of people it can help
Explanation
The speaker says that expert-friendliness is not enough if the language is hostile to novices or casual users who could otherwise benefit from it.
- A
- 6
Why does the lecture emphasize readable code even though C++ can also be written in difficult-to-read ways?
- A
Human readability matters only for programs that do not interact with hardware
- B
Readable code is guaranteed automatically whenever a program compiles
- C
The language should make clear, human-readable code possible, even if it cannot prevent every poor style
- D
The language should sacrifice performance whenever that makes code shorter
Correct answer
CThe language should make clear, human-readable code possible, even if it cannot prevent every poor style
Explanation
The lecturer treats readability as an important goal, while acknowledging that C++—like any language—can still be used to write ugly code.
- A
- 7
What trade-off does the lecturer identify between C++ and languages aimed at simpler application development?
- A
C++ avoids performance concerns in order to minimize the amount of code
- B
C++ is intended to replace every language used for specialized application domains
- C
C++ emphasizes real-world systems constraints rather than being the simplest language for every application
- D
C++ is designed only for web applications, while other languages handle systems work
Correct answer
CC++ emphasizes real-world systems constraints rather than being the simplest language for every application
Explanation
The lecture says C++ is not intended to be the simplest language for all purposes; it aims to remain useful under real-world systems constraints.
- A
- 8
What does the lecturer mean by saying that software often outlives hardware?
- A
Programs are generally discarded before the machines they run on
- B
Software may remain in use for many years, so language and system design must account for long-term maintenance
- C
Software longevity makes compatibility between program versions unnecessary
- D
Hardware can only be used after all associated software has been replaced
Correct answer
BSoftware may remain in use for many years, so language and system design must account for long-term maintenance
Explanation
The speaker uses the long life of software to motivate attention to long-term systems design and compatibility.
- A
- 9
Why does the lecturer describe C++ as useful for work in fields such as high-energy physics?
- A
High-energy physics requires no knowledge beyond general-purpose programming
- B
C++ is designed exclusively for particle-physics calculations
- C
C++ programming automatically qualifies someone as a theoretical physicist
- D
Programming can provide a way to contribute to technical projects across many fields
Correct answer
DProgramming can provide a way to contribute to technical projects across many fields
Explanation
The speaker observes that programming can be an effective route into interesting projects in many fields, including high-energy physics.
- A
- 10
Why does the lecturer say that C++ must retain the ability to work close to hardware?
- A
All C++ programs must directly manipulate hardware registers
- B
Low-level programming is possible only when a language has no type system
- C
It is intended to support low-level work as well as abstractions built above that level
- D
Hardware access makes higher-level abstractions unnecessary
Correct answer
CIt is intended to support low-level work as well as abstractions built above that level
Explanation
The lecture presents C++ as aiming to support both direct, hardware-near programming and higher-level abstractions.
- A
- 11
What is the central idea behind C's memory model as described in the lecture?
- A
It maps operations such as addition and memory access onto relatively direct hardware concepts
- B
It prevents programmers from composing objects in memory
- C
It represents every value as an object in a class hierarchy
- D
It hides memory layout behind a mandatory garbage collector
Correct answer
AIt maps operations such as addition and memory access onto relatively direct hardware concepts
Explanation
The lecturer praises C's relatively direct model: operations correspond to hardware actions, and objects can be laid out in memory in straightforward ways.
- A
- 12
What change in programming did Fortran represent, according to the lecture?
- A
It was designed primarily to let business records communicate with physics programs
- B
It introduced the first class hierarchy for representing related concepts
- C
It replaced all hardware-oriented programming with automatic resource management
- D
It raised programming from a machine-focused level toward a level organized around scientists' and engineers' problems
Correct answer
DIt raised programming from a machine-focused level toward a level organized around scientists' and engineers' problems
Explanation
The lecturer describes Fortran as an early language that made programming more problem-focused for scientists and engineers rather than centered solely on the machine.
- A
- 13
What difficulty arose when many specialized programming languages developed?
- A
Programs written for different domains could not easily combine their strengths or work together
- B
Specialized languages could not perform calculations within their intended domains
- C
Each language used exactly the same data representations and therefore lacked specialization
- D
Scientists and business users were forced to write every program in machine code
Correct answer
APrograms written for different domains could not easily combine their strengths or work together
Explanation
The speaker explains that specialized languages could excel in different areas, but combining their capabilities was difficult because they could not readily communicate.
- A
- 14
What contribution does the lecture attribute to Simula?
- A
It solved resource leaks by automatically collecting every kind of resource
- B
It created the first language specifically for scientific numerical calculations
- C
It established C's direct mapping between memory operations and hardware
- D
It introduced the idea of letting programmers build their own abstractions using classes
Correct answer
DIt introduced the idea of letting programmers build their own abstractions using classes
Explanation
The lecture credits Simula with the class concept, allowing programmers to define abstractions such as vectors, matrices, and records.
- A
- 15
How did the combination of C and Simula influence C++?
- A
C's business-record features were combined with Simula's scientific-only operations
- B
C's garbage collector was combined with Simula's machine-code-only interface
- C
C's class hierarchy was combined with Simula's lack of data types
- D
C's relatively direct hardware model was combined with Simula's class-based abstraction ideas
Correct answer
DC's relatively direct hardware model was combined with Simula's class-based abstraction ideas
Explanation
The lecturer describes C++ as bringing together C's direct relationship to hardware and Simula's facilities for classes and abstractions.
- A
- 16
Why were constructors and destructors important to the development of C++ abstractions?
- A
They ensure that every class must inherit from another class
- B
They provide a way to initialize objects and clean up resources associated with them
- C
They automatically make every object shared among all program components
- D
They allow programs to avoid defining how objects are represented
Correct answer
BThey provide a way to initialize objects and clean up resources associated with them
Explanation
The lecture explains that an abstraction may need initialization when an object is created and cleanup when it is no longer needed.
- A
- 17
Why has backward compatibility been a constraint on changing C++?
- A
Large existing codebases depend on old behavior, and developers do not want upgrades to break them
- B
New language features cannot be implemented in a language with classes
- C
Compatibility requires that every new program use the oldest available compiler
- D
Old code cannot be analyzed or transformed by software tools
Correct answer
ALarge existing codebases depend on old behavior, and developers do not want upgrades to break them
Explanation
The lecturer says that existing users often need old code to keep working, which makes removing old features or changing behavior difficult.
- A
- 18
What is meant by calling some old C++ features 'barnacles'?
- A
They are optional libraries that cannot be used in systems programming
- B
They are runtime errors caused only by garbage collection
- C
They are newly added abstractions that eliminate the need for compatibility
- D
They are legacy features that can complicate the language but are difficult to remove without breaking existing code
Correct answer
DThey are legacy features that can complicate the language but are difficult to remove without breaking existing code
Explanation
The speaker uses the barnacle metaphor for historical complications that remain because users and organizations rely on existing behavior.
- A
- 19
Why does the lecturer argue that a completely new language might struggle to replace C++?
- A
It would lack the established community, code, and infrastructure unless it could become embedded in them
- B
Existing C++ programmers cannot learn any new language features
- C
New languages cannot support low-level programming or efficient abstractions
- D
A new language would necessarily have to use Java's design choices
Correct answer
AIt would lack the established community, code, and infrastructure unless it could become embedded in them
Explanation
The lecturer says that a new language might have attractive properties but could fail quickly without access to the existing C++ community and infrastructure.
- A
- 20
Which group consists entirely of examples of resources that a program may need to acquire and later release?
- A
Integers, class names, comments, and function declarations
- B
Loops, predicates, type aliases, and arithmetic expressions
- C
Arrays, algorithms, source files, and language standards
- D
Memory, file handles, locks, and network connections
Correct answer
DMemory, file handles, locks, and network connections
Explanation
The lecture lists memory, file handles, locks, and communication connections as examples of resources that must be returned or released.
- A
- 21
What is a resource leak?
- A
A situation in which a program releases a resource before acquiring it
- B
A situation in which a program stores several values in one container
- C
A situation in which a program uses a resource through a named abstraction
- D
A situation in which a program fails to release a resource after it is finished using it
Correct answer
DA situation in which a program fails to release a resource after it is finished using it
Explanation
A leak occurs when a resource, such as memory or a file handle, is not returned after the program has finished with it.
- A
- 22
Why does the lecturer say that a resource should have a known owner?
- A
Ownership allows every resource to be shared without tracking its users
- B
Ownership ensures that a resource can never be passed between functions
- C
Ownership clarifies which part of the program is responsible for releasing the resource
- D
Ownership prevents an object from having a destructor
Correct answer
COwnership clarifies which part of the program is responsible for releasing the resource
Explanation
The lecture identifies unclear responsibility for cleanup as a source of resource-management problems.
- A
- 23
Why can passing a raw pointer to another part of a program create a resource-management problem?
- A
A raw pointer can represent only an object stored in a local variable
- B
A raw pointer automatically records every part of the program that uses its object
- C
A raw pointer always destroys its object as soon as it is passed
- D
The pointer alone may not establish which part of the program owns and must release the object
Correct answer
DThe pointer alone may not establish which part of the program owns and must release the object
Explanation
The speaker's shape example illustrates that passing a pointer does not by itself make responsibility for cleanup clear.
- A
- 24
How does a standard container such as a vector help manage the resources it uses?
- A
It transfers ownership of its storage to the compiler permanently
- B
It prevents the container from containing objects that need cleanup
- C
It manages its internal storage and releases that storage when the container goes out of scope
- D
It requires the caller to manually delete every element after each operation
Correct answer
CIt manages its internal storage and releases that storage when the container goes out of scope
Explanation
The lecture explains that a container manages its storage and cleans it up when the container's lifetime ends.
- A
- 25
How can cleanup work correctly for a vector whose elements are themselves strings?
- A
The compiler converts the vector and strings into a shared pointer automatically
- B
The vector cleans up its storage, and each string in turn cleans up its own resources
- C
The vector must ignore the strings because only the program can release them
- D
Each string must release the vector's storage before it is destroyed
Correct answer
BThe vector cleans up its storage, and each string in turn cleans up its own resources
Explanation
The lecture describes layered cleanup: the vector is responsible for its elements, and the elements' own destructors manage their resources.
- A
- 26
Why is manually writing cleanup code after operations that can return or throw an exception error-prone?
- A
A return statement automatically releases only the most recently acquired resource
- B
Manual cleanup can be used only for memory, not files or locks
- C
An early exit can bypass cleanup statements that occur later in the function
- D
Exceptions prevent destructors from running for local objects
Correct answer
CAn early exit can bypass cleanup statements that occur later in the function
Explanation
The lecturer's example shows that returns and exceptions can make manually placed cleanup code easy to miss or get wrong.
- A
- 27
How does a local resource-managing object help when a function exits early?
- A
It prevents the function from returning or throwing exceptions
- B
It leaves cleanup to whichever function next receives a pointer to the object
- C
Its destructor runs as its lifetime ends, so cleanup occurs on normal or exceptional exits
- D
It stores the resource indefinitely so that no cleanup decision is needed
Correct answer
CIts destructor runs as its lifetime ends, so cleanup occurs on normal or exceptional exits
Explanation
The lecture relies on destructors to clean up local resources when control leaves a scope, including when an exception is thrown.
- A
- 28
What does 'resource acquisition is initialization' describe in the lecture?
- A
Using a garbage collector to discover resources after they become unreachable
- B
Initializing an object only after its resource has been manually deleted
- C
Acquiring a resource as part of constructing an object that will release it during destruction
- D
Acquiring every resource at program startup and releasing it only at program shutdown
Correct answer
CAcquiring a resource as part of constructing an object that will release it during destruction
Explanation
The lecture presents this idiom as tying a resource's lifetime to an object's lifetime: construction establishes the resource, and destruction cleans it up.
- A
- 29
Why does the lecturer describe exceptions as a useful error-handling mechanism when combined with destructors?
- A
Exceptions guarantee that programs never encounter resource failures
- B
Exceptions can report errors while destructors clean up local resources during the resulting exits
- C
Exceptions make every resource available to every function automatically
- D
Exceptions eliminate the need for constructors and object lifetimes
Correct answer
BExceptions can report errors while destructors clean up local resources during the resulting exits
Explanation
The speaker links exceptions and destructors: errors can propagate while local resource-managing objects still clean up.
- A
- 30
When is a unique pointer a better fit than a shared pointer, as described in the lecture?
- A
When the object must be managed without any ownership relationship
- B
When many independent owners must keep the object alive together
- C
When one part of the program is the sole owner of the object
- D
When no part of the program should know the object's lifetime
Correct answer
CWhen one part of the program is the sole owner of the object
Explanation
The lecture recommends unique ownership when an object belongs to just one owner, avoiding unnecessary shared ownership.
- A
- 31
What happens to a shared pointer's use count as additional shared pointers refer to the same object?
- A
The count stays fixed, and each pointer independently deletes the object
- B
The count decreases, and the first pointer to be destroyed deletes the object
- C
The count records how many bytes the object uses, not how many owners it has
- D
The count increases, and the object is cleaned up when the final shared owner releases it
Correct answer
DThe count increases, and the object is cleaned up when the final shared owner releases it
Explanation
The speaker explains that shared pointers track the number of owners and clean up the object when the count reaches zero.
- A
- 32
Why can shared ownership have a cost even when it prevents an object from being destroyed too early?
- A
Every shared pointer copies the entire object whenever it is passed
- B
The shared ownership count must be maintained, and that shared state can add overhead
- C
Shared pointers prevent destructors from running
- D
Every shared pointer requires a garbage collector to scan all program memory
Correct answer
BThe shared ownership count must be maintained, and that shared state can add overhead
Explanation
The lecture notes that maintaining a shared use count adds work, particularly because the count is shared among users.
- A
- 33
Why does the lecturer caution against sharing an object when sharing is unnecessary?
- A
An object with one owner cannot have a destructor
- B
Shared ownership makes the object's type impossible to determine
- C
Every unshared object must be stored on the free store
- D
Shared state can impose coordination costs, especially in concurrent programs
Correct answer
DShared state can impose coordination costs, especially in concurrent programs
Explanation
The speaker argues that shared resources can cost more than expected, particularly in concurrent systems, so unnecessary sharing should be avoided.
- A
- 34
Why should a function returning a newly created object generally return the object itself rather than a raw pointer to it?
- A
Returning a raw pointer automatically makes the caller the unique owner
- B
Returning an object prevents the caller from using its value
- C
Returning an object preserves a clearer ownership model and avoids forcing callers to manage a raw pointer
- D
Returning an object guarantees that the function never allocates storage
Correct answer
CReturning an object preserves a clearer ownership model and avoids forcing callers to manage a raw pointer
Explanation
The lecture recommends returning a new object as an object, rather than reintroducing unclear ownership and cleanup problems through a raw pointer.
- A
- 35
What is a limitation of using a reference return to avoid returning a large object by value?
- A
A reference can be returned only from a function that has no local variables
- B
A reference guarantees that the object will outlive every caller
- C
A reference always copies the referred-to object before returning
- D
A reference does not explain who owns the referred-to object or who must destroy it
Correct answer
DA reference does not explain who owns the referred-to object or who must destroy it
Explanation
The speaker points out that returning a reference may hide rather than solve the ownership and lifetime problem.
- A
- 36
What ownership role does the lecture assign to raw pointers and iterators?
- A
They can refer to objects owned elsewhere rather than necessarily representing ownership themselves
- B
They are the only safe way to store an object in a container
- C
They always own the object they point to and must delete it
- D
They guarantee that the object remains alive after its owner is destroyed
Correct answer
AThey can refer to objects owned elsewhere rather than necessarily representing ownership themselves
Explanation
The lecture distinguishes ownership from reference: pointers and iterators can be used to refer to objects whose ownership belongs elsewhere.
- A
- 37
What is the core idea of moving a large object, such as a matrix, rather than copying it?
- A
Transfer its representation to the destination instead of duplicating all of its elements
- B
Convert the matrix into a pointer that has no owner
- C
Keep two full copies but assign them the same name
- D
Recalculate every element in the destination from the original inputs
Correct answer
ATransfer its representation to the destination instead of duplicating all of its elements
Explanation
The speaker describes moving as transferring the representation, such as a pointer to allocated storage, rather than copying all the matrix elements.
- A
- 38
Why can copying a large matrix to return it from a function be inefficient?
- A
A copy always requires a separate garbage collector
- B
A matrix cannot be represented as an object returned by a function
- C
Copying changes the mathematical values of the matrix
- D
The copy may duplicate a large number of elements even though the caller only needs the result
Correct answer
DThe copy may duplicate a large number of elements even though the caller only needs the result
Explanation
The lecture uses large matrices to show why duplicating all their elements during a return can be costly.
- A
- 39
How can a move operation leave the source object safe to destroy?
- A
It copies the resource and prevents the source destructor from running
- B
It marks both objects as owners of the same resource without coordination
- C
It delays both objects' destructors until the program terminates
- D
It transfers the resource representation and leaves the source in a state that owns nothing to release
Correct answer
DIt transfers the resource representation and leaves the source in a state that owns nothing to release
Explanation
In the lecture's example, the representation is transferred and the source is cleared, so its destructor has no resource left to release.
- A
- 40
What does a move constructor specify?
- A
How an object is constructed by taking the resources or representation of another object
- B
How a function sorts elements in a container
- C
How an object must be copied element by element from every possible source
- D
How a class selects which virtual function to call at runtime
Correct answer
AHow an object is constructed by taking the resources or representation of another object
Explanation
The lecturer describes a move constructor as defining how an object's representation can be transferred from another object.
- A
- 41
How can a class avoid writing a custom move operation for every member?
- A
By replacing its constructors with a garbage collector
- B
By converting all of its members into raw pointers
- C
By requiring every member to use shared ownership
- D
If its members already know how to move, the language can generate suitable move behavior
Correct answer
DIf its members already know how to move, the language can generate suitable move behavior
Explanation
The lecture explains that when a class is composed of members that already support copying and moving, generated operations can handle the members.
- A
- 42
Why can returning a matrix by value work efficiently when move semantics are available?
- A
The result's representation can be transferred rather than copying every matrix element
- B
A return statement skips the matrix destructor in all cases
- C
Returning by value automatically changes the matrix into a reference
- D
Move semantics make the matrix's elements unnecessary
Correct answer
AThe result's representation can be transferred rather than copying every matrix element
Explanation
Move semantics make value returns practical for large objects by allowing their resources or representation to be transferred efficiently.
- A
- 43
Why does the lecturer object to forcing matrix operations to write into a caller-provided output object?
- A
It prevents programs from using references to matrix elements
- B
It makes it impossible to reuse a matrix in another operation
- C
It guarantees that all intermediate matrices are copied
- D
It makes natural value-based mathematical expressions less direct and can add setup complexity
Correct answer
DIt makes natural value-based mathematical expressions less direct and can add setup complexity
Explanation
The speaker argues that requiring a pre-created output target for an operation such as matrix addition regresses from natural value-based mathematical code.
- A
- 44
What does the lecture say about compiler optimization and move operations?
- A
Move operations work only if the compiler removes every constructor
- B
Compiler optimization is required to make raw-pointer ownership safe
- C
Compilers may optimize some copies, but explicit move support does not have to rely on such optimization
- D
Compilers can never optimize a return of an object
Correct answer
CCompilers may optimize some copies, but explicit move support does not have to rely on such optimization
Explanation
The lecturer distinguishes compiler optimizations from move semantics: a compiler may eliminate copies, but moving provides a direct way to transfer resources.
- A
- 45
What is the lecturer's main criticism of relying on garbage collection for all resource management?
- A
It primarily handles memory, while programs also manage resources such as files, locks, and connections
- B
It cannot identify any memory that is no longer in use
- C
It prevents programs from using containers
- D
It requires every resource to be represented as a class hierarchy
Correct answer
AIt primarily handles memory, while programs also manage resources such as files, locks, and connections
Explanation
The speaker argues that memory collection alone does not manage many other resource types that need explicit cleanup.
- A
- 46
Why does the lecturer compare some garbage-collection work to a shared resource?
- A
Garbage collection makes each object exclusively owned by one thread
- B
Collection can require coordination among program activity rather than being a cost-free local operation
- C
Collection removes the need for any synchronization between threads
- D
Garbage collection stores every object's resources in a separate process
Correct answer
BCollection can require coordination among program activity rather than being a cost-free local operation
Explanation
The lecture emphasizes that garbage collection can impose shared-system costs, particularly in concurrent programs.
- A
- 47
In the shape example, when is shared ownership more appropriate than unique ownership?
- A
When multiple tasks genuinely need to keep the same object alive and no single task is its sole owner
- B
When the program only needs to refer to an object without extending its lifetime
- C
When a container owns each shape exclusively
- D
When one function creates an object and uses it only within its own scope
Correct answer
AWhen multiple tasks genuinely need to keep the same object alive and no single task is its sole owner
Explanation
The lecture presents shared ownership as useful when several users, such as concurrent tasks, must jointly keep an object alive.
- A
- 48
What does the lecturer suggest as a possible way to handle legacy code that cannot readily be converted to modern resource-management practices?
- A
Convert all legacy pointers into references without changing other code
- B
Prevent the legacy program from allocating memory
- C
Remove every destructor so that old cleanup code cannot conflict
- D
Run a garbage collector infrequently to deal with accumulated memory garbage
Correct answer
DRun a garbage collector infrequently to deal with accumulated memory garbage
Explanation
The speaker mentions occasional or infrequent garbage collection as a possible approach for old code that is difficult to modernize.
- A
- 49
When is object-oriented inheritance especially useful, according to the lecture?
- A
When a program must avoid all runtime function calls
- B
When the concepts in a problem domain have meaningful hierarchical relationships
- C
Whenever a function needs to return a value
- D
Whenever a program contains any data structure
Correct answer
BWhen the concepts in a problem domain have meaningful hierarchical relationships
Explanation
The lecturer says inheritance fits domains with real hierarchies, using shapes and their more specific forms as an example.
- A
- 50
What is the purpose of a virtual function in the shape example?
- A
It prevents derived classes from defining their own behavior
- B
It copies each derived object into the base class
- C
It makes every shape object a compile-time constant
- D
It allows a call through a base-class interface to invoke behavior provided by a derived shape
Correct answer
DIt allows a call through a base-class interface to invoke behavior provided by a derived shape
Explanation
The shape example uses virtual behavior so that different shapes can be drawn through a common interface.
- A
- 51
What does the lecture say about the runtime cost of virtual function calls?
- A
They require garbage collection after every call
- B
They are always more expensive than copying an entire object
- C
They have some cost, though improvements such as branch prediction can reduce its practical impact
- D
They have no runtime cost in any implementation
Correct answer
CThey have some cost, though improvements such as branch prediction can reduce its practical impact
Explanation
The speaker describes virtual calls as relatively cheap but notes that they may still matter, especially when inlining is important.
- A
- 52
Why can virtual functions be a concern in high-performance code?
- A
Virtual functions prevent programs from using constructors
- B
Virtual functions require every object to be stored in a vector
- C
Indirect calls always allocate memory for a new object
- D
Indirect calls may make it harder for the compiler to inline a small operation
Correct answer
DIndirect calls may make it harder for the compiler to inline a small operation
Explanation
The lecturer identifies the difficulty of inlining virtual calls as a potential issue when the operation being dispatched is small and performance-sensitive.
- A
- 53
What is one problem the lecturer associates with very deep or large class hierarchies?
- A
They remove the need to define interfaces
- B
They make every operation statically dispatched
- C
They can make code harder to understand and maintain when used without a suitable domain structure
- D
They prevent objects from having different types
Correct answer
CThey can make code harder to understand and maintain when used without a suitable domain structure
Explanation
The speaker cautions against treating enormous inheritance hierarchies as a definition of good programming.
- A
- 54
Why does the lecture warn programmers about using protected data?
- A
It prevents derived classes from accessing any functions
- B
It guarantees that data is cleaned up before the object is destroyed
- C
It exposes representation to derived classes, making it harder to preserve a class's invariants
- D
It makes all data public to every part of the program
Correct answer
CIt exposes representation to derived classes, making it harder to preserve a class's invariants
Explanation
The lecturer warns that protected data lets derived classes modify representation, which can make invariants difficult to maintain.
- A
- 55
What does the lecture distinguish about inheritance in C++?
- A
C++ supports implementation inheritance but not interfaces
- B
C++ inheritance always copies a base class's implementation at runtime
- C
C++ can provide both interface inheritance and implementation inheritance
- D
C++ supports interface inheritance only through raw pointers
Correct answer
CC++ can provide both interface inheritance and implementation inheritance
Explanation
The speaker notes that C++ supports interface inheritance and also implementation inheritance.
- A
- 56
What is an abstract class in the lecture's description?
- A
A class that does not provide a complete implementation and can define a required interface
- B
A class that contains only data and cannot declare operations
- C
A class that can be used only as a template argument
- D
A class that automatically owns every object derived from it
Correct answer
AA class that does not provide a complete implementation and can define a required interface
Explanation
The lecturer describes abstract classes as classes without a full implementation that can provide an interface for derived classes.
- A
- 57
Which statement best reflects the lecture's view of programming paradigms?
- A
Generic programming and object-oriented programming cannot appear in the same code
- B
Object-oriented, generic, and other styles can be combined when they suit the problem
- C
Using a newer paradigm makes all earlier styles obsolete
- D
A program must use only one programming paradigm to be well designed
Correct answer
BObject-oriented, generic, and other styles can be combined when they suit the problem
Explanation
The lecturer argues against language or paradigm wars and gives examples that combine generic algorithms with object-oriented behavior.
- A
- 58
What is generic programming intended to make possible?
- A
Replacing all data structures with class hierarchies
- B
Writing an algorithm that can use only one fixed element type
- C
Writing types and algorithms that can work with many suitable element or container types
- D
Avoiding type checking by converting every value into a raw pointer
Correct answer
CWriting types and algorithms that can work with many suitable element or container types
Explanation
The lecture presents templates as a way to parameterize types and algorithms so they can work with different suitable types.
- A
- 59
Why did the lecturer want generic vectors and algorithms rather than versions fixed to one element type?
- A
The same operations, such as sorting, should be usable for vectors of different element types
- B
Each element type must use a different programming language
- C
Fixed-type algorithms cannot be compiled
- D
Generic algorithms prevent values from being compared
Correct answer
AThe same operations, such as sorting, should be usable for vectors of different element types
Explanation
The speaker wanted a vector and operations such as sorting to be parameterized by the element type rather than tied to one fixed type.
- A
- 60
What is 'duck typing' as described in the lecture?
- A
Using a type according to whether it supports the operations the code attempts to perform
- B
Selecting a type only by its name in a class hierarchy
- C
Converting every argument to the same built-in type before a function call
- D
Checking whether a value has been allocated by a garbage collector
Correct answer
AUsing a type according to whether it supports the operations the code attempts to perform
Explanation
The lecture describes duck typing as relying on whether a type supports the operations used by the generic code.
- A
- 61
What is a major drawback of relying only on template errors to reveal that an argument type is unsuitable?
- A
The program always runs successfully despite the unsuitable type
- B
The resulting compiler diagnostics can be extremely long and difficult to connect to the actual problem
- C
The compiler cannot detect template errors until the program is running
- D
The compiler automatically changes the argument type to a suitable one
Correct answer
BThe resulting compiler diagnostics can be extremely long and difficult to connect to the actual problem
Explanation
The speaker emphasizes that template errors may be detected during compilation but can produce very confusing diagnostics.
- A
- 62
Why do programmers sometimes perform computations at compile time?
- A
Compile-time work guarantees that a program needs no runtime data
- B
It allows errors to be hidden until after a program is shipped
- C
It makes every template independent of its argument types
- D
It can improve performance or express work at the earliest stage when it can be checked
Correct answer
DIt can improve performance or express work at the earliest stage when it can be checked
Explanation
The lecture gives performance and early expression or checking of work as reasons programmers use compile-time computation.
- A
- 63
What is a concept intended to express for a template?
- A
The runtime order in which a program's functions must be called
- B
The requirements that the template places on its type arguments
- C
The exact memory address where every template object must be stored
- D
The number of copies the compiler must make for every template argument
Correct answer
BThe requirements that the template places on its type arguments
Explanation
The lecturer describes concepts as a way to state the intended requirements of a template's arguments.
- A
- 64
Why would a list of integers fail to meet the lecture's stated requirements for a sorting algorithm that needs random access?
- A
A list cannot be traversed from beginning to end
- B
A list does not support direct random access to its elements in the way the algorithm requires
- C
A list has no elements that can be compared
- D
A list cannot contain integers
Correct answer
BA list does not support direct random access to its elements in the way the algorithm requires
Explanation
The lecture explains that the sorting requirements include random access; a list does not provide that capability, so it does not satisfy the constraint.
- A
- 65
How can concepts improve a template error message?
- A
They prevent templates from being used with containers
- B
They replace compile-time checking with runtime warnings
- C
They can report directly that an argument type fails a stated requirement
- D
They silently convert an unsuitable type into a suitable type
Correct answer
CThey can report directly that an argument type fails a stated requirement
Explanation
The lecturer contrasts concept-based diagnostics, which identify an unmet requirement, with the confusing errors common in unconstrained templates.
- A
- 66
What is the role of a predicate in the lecture's sorting example?
- A
It transfers a container's resources to another container
- B
It specifies how two elements should be ordered or compared
- C
It determines how many elements the container can hold
- D
It records which function owns the container's memory
Correct answer
BIt specifies how two elements should be ordered or compared
Explanation
The lecture uses predicates to provide comparison criteria for algorithms such as sorting or finding elements.
- A
- 67
What does a lambda expression provide in the lecture's example?
- A
A declaration that an element type has random access
- B
A way to make every function call virtual
- C
A concise way to supply an operation or predicate directly to an algorithm
- D
A method for transferring ownership of a container
Correct answer
CA concise way to supply an operation or predicate directly to an algorithm
Explanation
The speaker shows a lambda as a compact way to write a comparison operation passed to an algorithm.
- A
- 68
Why are algorithms often expressed using a beginning and ending iterator rather than assuming the whole container?
- A
The same algorithm can then operate on either a full sequence or a selected portion
- B
Iterators automatically determine the container's ownership
- C
A full container cannot be sorted directly
- D
The algorithm can then avoid knowing what operations its elements support
Correct answer
AThe same algorithm can then operate on either a full sequence or a selected portion
Explanation
The lecture explains that iterator ranges allow algorithms to work on subsequences as well as entire sequences.
- A
- 69
What does the lecture say about combining generic programming with object-oriented programming?
- A
Using a template removes the runtime behavior of class hierarchies
- B
A generic algorithm can operate on containers whose elements use object-oriented interfaces
- C
Object-oriented types cannot be stored in generic containers
- D
Generic algorithms cannot call virtual functions
Correct answer
BA generic algorithm can operate on containers whose elements use object-oriented interfaces
Explanation
The speaker demonstrates generic iteration over a container of shapes while using object-oriented drawing behavior.
- A
- 70
Why does the lecturer describe generic programming as ordinary programming rather than a completely separate discipline?
- A
Generic code is executed only by a separate programming language
- B
The same basic ideas of functions, types, and operations still apply, though templates add complexity
- C
Templates do not use types or functions
- D
Every generic program must be written as a class hierarchy
Correct answer
BThe same basic ideas of functions, types, and operations still apply, though templates add complexity
Explanation
The lecturer argues that generic programming uses familiar programming ideas and should not require an entirely different way of reasoning.
- A
- 71
What role can containers such as vectors and maps play in reducing memory-safety problems?
- A
They remove the need to know what data a program stores
- B
They guarantee that no program can contain a logic error
- C
They make all element access valid regardless of index
- D
They can manage storage and expose useful operations without requiring direct manipulation of raw arrays
Correct answer
DThey can manage storage and expose useful operations without requiring direct manipulation of raw arrays
Explanation
The lecture recommends standard containers as a way to manage data and avoid many hazards associated with direct array and pointer use.
- A
- 72
Why does the lecturer connect direct use of C-style arrays with buffer-overflow risks?
- A
Arrays prevent a program from checking an index
- B
Array elements are always allocated by a garbage collector
- C
Array sizes can be lost or overlooked, making out-of-range access easier
- D
Arrays cannot store more than one element
Correct answer
CArray sizes can be lost or overlooked, making out-of-range access easier
Explanation
The speaker explains that C-style arrays can lose their size information, making it easier to access memory outside their bounds.
- A
- 73
What is the lecturer's response to blaming early C designers for every later security flaw?
- A
Modern containers cannot reduce the risks of older programming styles
- B
Early language designers should be blamed whenever a program uses a pointer
- C
The language designers deserve credit for raising programming abstraction, while later programmers should also be responsible for using safer practices
- D
All security flaws are unavoidable consequences of using a compiled language
Correct answer
CThe language designers deserve credit for raising programming abstraction, while later programmers should also be responsible for using safer practices
Explanation
The lecturer defends the historical contribution of C's designers and argues that continuing to write unsafe old-style code is also a source of responsibility.
- A
- 74
What future safety improvement does the lecturer anticipate from hardware range checking?
- A
Hardware support could detect or enforce bounds on accesses that would otherwise be out of range
- B
Range checking could make all pointer ownership unambiguous
- C
Hardware could guarantee that every program is free of logic errors
- D
Hardware support could eliminate the need for type checking
Correct answer
AHardware support could detect or enforce bounds on accesses that would otherwise be out of range
Explanation
The speaker anticipates hardware support for range checking to help enforce safety against out-of-bounds accesses.
- A
- 75
Why does the lecturer consider static analysis valuable for large C++ codebases?
- A
It can help identify problems and support understanding of complex existing code
- B
It guarantees that all legacy code is compatible with every compiler
- C
It removes the need for developers to read code
- D
It automatically rewrites every program into a new language
Correct answer
AIt can help identify problems and support understanding of complex existing code
Explanation
The lecture points to static analysis tools as a way to find problems and help developers understand large codebases.
- A
- 76
How might code transformation help organizations maintain old C++ systems?
- A
It replaces the need to preserve behavior during upgrades
- B
It can guarantee that a program needs no testing after transformation
- C
It can make old code compatible with every compiler automatically
- D
It could help modernize or simplify existing code without requiring every change to be made manually
Correct answer
DIt could help modernize or simplify existing code without requiring every change to be made manually
Explanation
The speaker expresses hope that analysis and transformation tools can help rejuvenate or modernize existing code.
- A
- 77
Why can repeated inclusion of the same header files increase compilation time?
- A
Each inclusion creates a separate copy of every runtime object
- B
The compiler may have to process the header contents repeatedly across many source files
- C
Repeated headers prevent the compiler from checking types
- D
Header files force the program to run its functions during compilation
Correct answer
BThe compiler may have to process the header contents repeatedly across many source files
Explanation
The lecture identifies repeated processing of headers across source files as one contributor to long compilation times.
- A
- 78
How could a modules system reduce some compilation costs?
- A
It could eliminate the need to compile source code
- B
It could prevent programs from using libraries
- C
It could make every function execute at compile time
- D
It could avoid repeatedly processing the same interface information in many source files
Correct answer
DIt could avoid repeatedly processing the same interface information in many source files
Explanation
The speaker describes modules as a possible way to reduce redundant processing of headers and related information.
- A
- 79
Why can programs compiled by different C++ compilers have difficulty linking together?
- A
C++ does not have one universally followed binary linkage and object-format convention across compilers
- B
C++ programs cannot use code compiled in earlier language versions
- C
C++ forbids programs from using more than one source file
- D
Different compilers always assign different meanings to every class
Correct answer
AC++ does not have one universally followed binary linkage and object-format convention across compilers
Explanation
The lecture describes binary compatibility and linking across compilers as a real problem because linkage conventions have not been standardized in a way that ensures universal interoperability.
- A
- 80
Why do compiler vendors often prioritize compatibility with their own previous compiler versions?
- A
Compatibility with earlier versions makes new features impossible
- B
Customers generally require every release to break existing code
- C
A compiler can link only to programs written in the same year
- D
Their customers may have large codebases built with those versions
Correct answer
DTheir customers may have large codebases built with those versions
Explanation
The speaker observes that vendors have strong incentives to preserve compatibility with the code their existing customers already use.
- A
- 81
Why can adoption of a newer C++ standard be slow in some organizations?
- A
Large codebases may need to remain buildable with older compilers or across fragmented teams
- B
New features require every program to abandon its existing algorithms
- C
Modern code cannot be compiled by any commercial compiler
- D
Newer standards cannot support production software
Correct answer
ALarge codebases may need to remain buildable with older compilers or across fragmented teams
Explanation
The lecture notes that organizations with large codebases or many separate user groups may be constrained by older compilers and inconsistent development practices.
- A
- 82
What benefits does the lecturer say can help persuade organizations to adopt modern C++ styles?
- A
They eliminate the need for compatibility with existing systems
- B
They make all programs shorter, regardless of their purpose
- C
They guarantee that developers will not need to learn new tools
- D
They can improve safety and may produce faster code
Correct answer
DThey can improve safety and may produce faster code
Explanation
The speaker argues that modern styles should be promoted by demonstrating their safety and performance benefits.
- A
- 83
Why does the lecturer reject the idea that C++ should simply be replaced by Java for systems programming?
- A
The lecturer considers Java's garbage collection and virtual-machine model unsuitable for some desired systems-programming goals
- B
Java cannot be used to build any large software system
- C
C++ and Java have identical designs and therefore cannot be compared
- D
Java does not support object-oriented programming
Correct answer
AThe lecturer considers Java's garbage collection and virtual-machine model unsuitable for some desired systems-programming goals
Explanation
The lecturer argues that Java's design choices, including garbage collection and a virtual machine, conflict with some of the systems-programming properties desired for C++.
- A
- 84
What is the lecturer's overall position on improving C++?
- A
Remove all historical features immediately, regardless of compatibility
- B
Replace abstractions with direct hardware operations in every program
- C
Make the language specialized for one application domain
- D
Improve the language while preserving its ability to support low-level work, performance, and existing code
Correct answer
DImprove the language while preserving its ability to support low-level work, performance, and existing code
Explanation
The lecturer frames improvement as a balance: C++ should become better while respecting compatibility and its role in both low-level and abstract programming.
- A

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