CS50 Debugging Tools
- 58Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which CS50 IDE feature allows a programmer to pause execution, step through code, and inspect variables?
Which CS50 IDE feature allows a programmer to pause execution, step through code, and inspect variables?
58 questions
- 1
Which CS50 IDE feature allows a programmer to pause execution, step through code, and inspect variables?
- A
The check50 tester
- B
The style50 formatter
- C
The help50 interpreter
- D
The debug50 debugger
Correct answer
DThe debug50 debugger
Explanation
debug50 opens a debugging panel that supports breakpoints, step-by-step execution, and variable inspection.
- A
- 2
What is the primary purpose of check50?
- A
To display variable values during program execution
- B
To run automated tests that check code correctness
- C
To explain unfamiliar compiler error messages
- D
To identify inconsistent spacing in source code
Correct answer
BTo run automated tests that check code correctness
Explanation
check50 automates tests designed to evaluate whether a program produces the expected results.
- A
- 3
When is printf especially useful during debugging?
- A
When a programmer wants to allocate a dynamically sized memory block
- B
When a programmer wants to automatically correct a syntax error
- C
When a programmer wants to observe values or confirm that execution reached a point
- D
When a programmer wants to replace every breakpoint in a program
Correct answer
CWhen a programmer wants to observe values or confirm that execution reached a point
Explanation
printf can reveal variable values, trace loop execution, or confirm that a particular point in the program was reached.
- A
- 4
What does style50 primarily evaluate?
- A
The correctness of the program's algorithm
- B
The formatting and readability of source code
- C
The memory leaks produced during program execution
- D
The validity of command-line arguments
Correct answer
BThe formatting and readability of source code
Explanation
style50 checks code style, including spacing and formatting that affect readability.
- A
- 5
When should a programmer instinctively use help50?
- A
When source code contains inconsistent indentation
- B
When a command or compiler error message is difficult to understand
- C
When an array needs to be enlarged during execution
- D
When a program needs to be stepped through one statement at a time
Correct answer
BWhen a command or compiler error message is difficult to understand
Explanation
help50 interprets cryptic error messages and provides more understandable, teaching-fellow-like guidance.
- A
- 6
What is the main purpose of rubber duck debugging?
- A
To convert a text-based program into Scratch blocks
- B
To test whether a program handles invalid input
- C
To uncover mistakes by explaining code aloud step by step
- D
To detect memory leaks by monitoring heap usage
Correct answer
CTo uncover mistakes by explaining code aloud step by step
Explanation
Rubber duck debugging requires verbalizing the logic of the program, which can expose an incorrect assumption or unexpected step.
- A
- 7
How can Valgrind be used with a program that requires command-line arguments?
- A
The arguments must be supplied only after the program terminates
- B
The usual arguments can follow the program name in the Valgrind command
- C
The arguments cannot be used because Valgrind accepts only interactive input
- D
The arguments must be placed inside the source code before compilation
Correct answer
BThe usual arguments can follow the program name in the Valgrind command
Explanation
Valgrind prefixes the normal execution command, so command-line arguments can be supplied in the usual way.
- A
- 8
What does Valgrind primarily help detect?
- A
Invalid memory accesses and memory leaks
- B
Incorrect user-interface prompts
- C
Poor variable names and inconsistent indentation
- D
Unoptimized arithmetic expressions
Correct answer
AInvalid memory accesses and memory leaks
Explanation
Valgrind reports errors such as invalid reads or writes and memory that was allocated but never freed.
- A
- 9
What can happen when memory allocated with malloc is never freed?
- A
The allocated memory immediately becomes read-only
- B
The compiler automatically copies the memory into a smaller block
- C
The operating system automatically converts the memory into stack space
- D
The program can leak memory and eventually slow down, freeze, or crash
Correct answer
DThe program can leak memory and eventually slow down, freeze, or crash
Explanation
Failing to call free leaves allocated memory unavailable for reuse, potentially causing a memory leak and resource exhaustion.
- A
- 10
If a program allocates space for 10 integers, which expression requests the required number of bytes without hard-coding the size of an integer?
- A
10÷sizeof(int)
- B
sizeof(10×int)
- C
10+sizeof(int)
- D
10×sizeof(int)
Correct answer
D10×sizeof(int)
Explanation
Multiplying the number of integers by sizeof(int) accounts for the platform-specific size of each integer.
- A
- 11
What does malloc return when it successfully allocates a block of memory?
- A
A copy of the first value stored in the block
- B
A pointer to the beginning of the allocated block
- C
The number of bytes remaining on the heap
- D
The index of the final byte in the block
Correct answer
BA pointer to the beginning of the allocated block
Explanation
malloc returns an address, represented in C by a pointer, to the newly allocated memory.
- A
- 12
Why is accessing element 10 of an array containing 10 elements invalid?
- A
The valid indices range from 0 through 9
- B
Index 10 always refers to the first element
- C
Arrays can contain only even-numbered indices
- D
The valid indices range from 1 through 10
Correct answer
AThe valid indices range from 0 through 9
Explanation
An array of 10 elements has indices 0, 1, through 9, so index 10 is outside its bounds.
- A
- 13
Why might an out-of-bounds memory access appear to work in C?
- A
Valgrind silently repairs the access before execution
- B
The program may accidentally access memory that is available but not part of the array
- C
The compiler changes the invalid index into the final valid index
- D
C automatically expands every array when an invalid index is used
Correct answer
BThe program may accidentally access memory that is available but not part of the array
Explanation
C may not immediately detect an out-of-bounds access, and the neighboring memory may happen to be accessible, producing seemingly correct behavior.
- A
- 14
What does an 'invalid read of size 4' commonly indicate when an integer is being accessed?
- A
The program has used a four-character string instead of an integer
- B
The program is reading four bytes from an invalid memory location
- C
The program has printed four integers in the wrong order
- D
The program has allocated four arrays instead of one
Correct answer
BThe program is reading four bytes from an invalid memory location
Explanation
An integer commonly occupies four bytes on the discussed systems, so the message indicates an invalid four-byte read.
- A
- 15
When should dynamically allocated memory generally be freed?
- A
Whenever a pointer is passed to another function
- B
After the program has finished using the memory
- C
Immediately after malloc returns, before any use
- D
Before the memory is initialized with a value
Correct answer
BAfter the program has finished using the memory
Explanation
Memory should be freed only after its final use; freeing it earlier and then accessing it creates a use-after-free error.
- A
- 16
What does freeing memory conceptually tell the operating system?
- A
The physical memory has been erased from the computer
- B
The pointer has been converted into an integer
- C
The memory has been moved permanently to the stack
- D
The memory is available for reuse by other allocations
Correct answer
DThe memory is available for reuse by other allocations
Explanation
free marks the allocated region as available again; it does not necessarily erase the physical bytes immediately.
- A
- 17
Which memory region is used by malloc?
- A
The text segment
- B
The register segment
- C
The stack
- D
The heap
Correct answer
DThe heap
Explanation
Dynamic memory requested with malloc comes from the heap.
- A
- 18
What is stored in the text segment when a program runs?
- A
The machine-code instructions of the program
- B
The dynamically allocated variables created by malloc
- C
The local variables belonging to active functions
- D
The unused bytes returned by free
Correct answer
AThe machine-code instructions of the program
Explanation
The text segment contains the program's machine code after it is loaded from long-term storage into RAM.
- A
- 19
What is typically stored on the stack?
- A
Only memory allocated with malloc
- B
The executable machine code
- C
Files stored permanently on disk
- D
Function call frames and local variables
Correct answer
DFunction call frames and local variables
Explanation
The stack contains regions associated with active function calls and their local variables.
- A
- 20
What can occur if the stack and heap grow into one another?
- A
All local variables are copied into the text segment
- B
A memory exhaustion or overflow problem can occur
- C
The operating system converts pointers into strings
- D
The program automatically becomes a linked list
Correct answer
BA memory exhaustion or overflow problem can occur
Explanation
Because memory is finite, excessive growth of either region can cause the stack and heap to collide or exhaust available memory.
- A
- 21
Why does passing ordinary variables to a swap function fail to change the originals in main?
- A
The function cannot contain a temporary variable
- B
The function receives copies of the values rather than the original variables
- C
The compiler automatically reverses the requested assignments
- D
The stack prevents functions from changing integers
Correct answer
BThe function receives copies of the values rather than the original variables
Explanation
Function arguments are passed by value in this situation, so the function modifies local copies named a and b.
- A
- 22
How can a swap function modify the original variables in its caller?
- A
By receiving pointers to the variables and dereferencing them
- B
By storing the variables in the text segment
- C
By declaring the function with return type void
- D
By copying the variables into a second array
Correct answer
ABy receiving pointers to the variables and dereferencing them
Explanation
Passing addresses lets the function access and modify the original memory locations through dereferencing.
- A
- 23
What does the dereference operator do to a pointer?
- A
It calculates the pointer's position in the source file
- B
It accesses the value stored at the pointer's address
- C
It allocates a new memory block automatically
- D
It converts the pointed-to value into a function
Correct answer
BIt accesses the value stored at the pointer's address
Explanation
Dereferencing follows the address stored in a pointer and accesses the data located there.
- A
- 24
Why is dereferencing an uninitialized pointer unsafe?
- A
It may contain an unpredictable address that does not refer to valid memory
- B
It automatically frees the pointer's target
- C
It changes the pointer into a null pointer
- D
It always contains the address of the first array element
Correct answer
AIt may contain an unpredictable address that does not refer to valid memory
Explanation
An uninitialized pointer contains an indeterminate garbage value, so dereferencing it can access an invalid location.
- A
- 25
What does a null pointer represent?
- A
A special value indicating that the pointer refers to no valid object
- B
A pointer that always refers to the most recent variable
- C
A pointer to the first byte of every allocated block
- D
A pointer containing an arbitrary leftover memory address
Correct answer
AA special value indicating that the pointer refers to no valid object
Explanation
NULL represents the absence of a valid target address and is commonly used to mark the end of a structure.
- A
- 26
What is the purpose of a struct in C?
- A
To make every variable dynamically allocated
- B
To group related values into a custom data type
- C
To automatically sort all variables in memory
- D
To replace all pointer operations with arithmetic
Correct answer
BTo group related values into a custom data type
Explanation
A struct lets programmers define a custom type containing related fields, such as a student's name and dorm.
- A
- 27
What does encapsulation mean in the context of a student struct?
- A
Keeping related student data together in one data structure
- B
Hiding all variables by placing them on the stack
- C
Converting every student field into a function
- D
Storing every student's data in a separate program
Correct answer
AKeeping related student data together in one data structure
Explanation
Encapsulation groups related information, such as name and dorm, into one abstraction representing a student.
- A
- 28
Which operator accesses a field of a structure value?
- A
The scope-resolution operator
- B
The dot operator
- C
The square-bracket operator
- D
The percent operator
Correct answer
BThe dot operator
Explanation
For a structure value, the dot operator accesses fields such as student.name or student.dorm.
- A
- 29
Why does a pointer to a struct commonly use the arrow operator?
- A
It converts a structure into an array of integers
- B
It follows two pointers simultaneously
- C
It allocates a new structure and initializes every field
- D
It combines dereferencing the pointer with accessing a structure field
Correct answer
DIt combines dereferencing the pointer with accessing a structure field
Explanation
n->field is shorthand for (*n).field: follow the pointer and then access the specified field.
- A
- 30
What is a major limitation of a C array?
- A
Its size cannot be changed directly after it is created
- B
Its elements cannot contain integers
- C
Its elements cannot be accessed using indices
- D
Its elements must always be stored on the heap
Correct answer
AIts size cannot be changed directly after it is created
Explanation
A fixed-size C array cannot grow in place; enlarging it requires another allocation and copying.
- A
- 31
What is the time complexity of resizing an array by creating a new array and copying all existing elements?
- A
O(n2)
- B
O(logn)
- C
O(1)
- D
O(n)
Correct answer
DO(n)
Explanation
Copying all n existing elements requires a number of operations proportional to n.
- A
- 32
What does realloc do when enlarging an allocated block?
- A
It always adds exactly one byte without moving anything
- B
It converts the block into a stack allocation
- C
It frees the block and returns a random address
- D
It may extend the block in place or allocate a new block and copy the contents
Correct answer
DIt may extend the block in place or allocate a new block and copy the contents
Explanation
realloc can enlarge the block at its current location if possible, or move the contents to a larger block and return its new address.
- A
- 33
Why should the return value of realloc often be stored in a temporary pointer first?
- A
realloc returns a value that cannot be assigned directly to a pointer
- B
A failed realloc can return NULL, and the original pointer may still be needed
- C
The temporary pointer automatically doubles the allocation
- D
The original pointer is always invalid after any realloc call
Correct answer
BA failed realloc can return NULL, and the original pointer may still be needed
Explanation
Using a temporary pointer allows the program to check for NULL before replacing the original pointer and potentially losing access to the allocation.
- A
- 34
What is the distinction between an array's size and its capacity in the dynamic-array example?
- A
Size is the number of bytes allocated, while capacity is the number of loops
- B
Size is the number of elements used, while capacity is the space available
- C
Size is the maximum possible value, while capacity is the current index
- D
Size refers to the heap, while capacity refers to the stack
Correct answer
BSize is the number of elements used, while capacity is the space available
Explanation
Size records how many elements are currently stored, whereas capacity records how many elements can fit.
- A
- 35
What keystroke was described as signaling end-of-file input in the terminal?
- A
Control-Z
- B
Control-Q
- C
Control-D
- D
Control-C
Correct answer
CControl-D
Explanation
Control-D sends an end-of-file signal, indicating that the user is finished providing input.
- A
- 36
Why cannot getInt directly support a user typing q to quit?
- A
getInt automatically converts q into zero
- B
getInt accepts only commands beginning with Control-D
- C
getInt expects integer input rather than an arbitrary string
- D
getInt terminates the operating system when q is entered
Correct answer
CgetInt expects integer input rather than an arbitrary string
Explanation
Because getInt is designed to obtain an integer, a letter such as q is not a valid value for that input function.
- A
- 37
What is the defining representation of a node in a singly linked list?
- A
A value together with pointers to every previous node
- B
A value together with a pointer to the next node
- C
A single pointer with no stored data
- D
A fixed array containing every list value
Correct answer
BA value together with a pointer to the next node
Explanation
Each node stores its own value and an address pointing to the next node in the list.
- A
- 38
Why is the first pointer in a linked list especially important?
- A
It provides access to the first node and therefore to the rest of the list
- B
It stores the number of nodes without pointing to any node
- C
It always points to the final node in the list
- D
It contains a copy of every value in the list
Correct answer
AIt provides access to the first node and therefore to the rest of the list
Explanation
Following the first pointer allows traversal from the beginning through every subsequent node.
- A
- 39
What value normally marks the end of a singly linked list?
- A
The largest integer in the list
- B
The address of the first node
- C
NULL
- D
The number of nodes in the list
Correct answer
CNULL
Explanation
The final node's next pointer is set to NULL to indicate that no further node exists.
- A
- 40
What is a key advantage of linked lists over fixed-size arrays?
- A
They require no pointers or dynamic memory
- B
They guarantee that all values occupy adjacent memory
- C
They can grow by allocating additional nodes without moving existing nodes
- D
They provide faster random access to every element
Correct answer
CThey can grow by allocating additional nodes without moving existing nodes
Explanation
New linked-list nodes can be allocated separately and connected with pointers, avoiding the need to resize and copy the entire structure.
- A
- 41
What is the major access-time disadvantage of a singly linked list?
- A
Finding a later element requires following the links from the beginning
- B
The first element cannot be accessed without sorting the list
- C
The list can contain only one data type
- D
Each node must be copied before it can be read
Correct answer
AFinding a later element requires following the links from the beginning
Explanation
Because nodes may be scattered in memory, the program must traverse the chain sequentially to reach a later node.
- A
- 42
What is the worst-case time complexity of inserting into a sorted singly linked list when the insertion point must be found?
- A
O(1)
- B
O(logn)
- C
O(n2)
- D
O(n)
Correct answer
DO(n)
Explanation
The new value may belong at the end, requiring traversal through all n existing nodes.
- A
- 43
What is the time complexity of inserting a new node at the beginning of an unsorted linked list?
- A
O(logn)
- B
O(n2)
- C
O(1)
- D
O(n)
Correct answer
CO(1)
Explanation
Insertion at the front requires only updating the new node's next pointer and the list's first pointer.
- A
- 44
Why must a new node point to the old successor before the predecessor points to the new node?
- A
Otherwise the list would automatically become sorted
- B
Otherwise NULL would be converted into an integer
- C
Otherwise the only pointer to the old successor could be lost
- D
Otherwise the new node would be allocated on the stack
Correct answer
COtherwise the only pointer to the old successor could be lost
Explanation
The new node must first preserve the old link; changing the predecessor's pointer too early can disconnect the remainder of the list.
- A
- 45
What additional capability does a doubly linked list provide compared with a singly linked list?
- A
Each node can store values of multiple unrelated data types
- B
The list no longer requires a pointer to its first node
- C
The list automatically provides constant-time search
- D
Each node can point both to the next node and to the previous node
Correct answer
DEach node can point both to the next node and to the previous node
Explanation
A doubly linked list stores forward and backward links, allowing traversal in both directions.
- A
- 46
What is a hash function in the discussed hash-table design?
- A
A function that maps an input, such as a name, to a bucket index
- B
A function that copies an array into a larger array
- C
A function that converts every pointer into NULL
- D
A function that sorts every linked list in descending order
Correct answer
AA function that maps an input, such as a name, to a bucket index
Explanation
The hash function uses an input property, such as the first letter of a name, to select one of several array buckets.
- A
- 47
How is the described hash table commonly structured?
- A
An array whose entries point to linked lists
- B
A single array containing every item in sorted order
- C
A linked list whose entries each contain a complete array
- D
A tree whose nodes contain no pointers
Correct answer
AAn array whose entries point to linked lists
Explanation
The array provides bucket access, while each bucket's linked list stores items that hash to that bucket.
- A
- 48
Why can hashing names by their first letter improve lookup performance?
- A
It removes the need to store names in memory
- B
It divides the names among shorter lists instead of one long list
- C
It guarantees that every lookup takes exactly one operation
- D
It sorts all names without comparing any characters
Correct answer
BIt divides the names among shorter lists instead of one long list
Explanation
The first letter distributes names across buckets, so each linked list is typically much shorter than one list containing everyone.
- A
- 49
What is the worst-case lookup time for a hash table if every item hashes to the same bucket?
- A
O(logn)
- B
O(1)
- C
O(n)
- D
O(nlogn)
Correct answer
CO(n)
Explanation
If all items occupy one linked list, lookup may require examining all n items.
- A
- 50
What is the goal of a well-designed hash function?
- A
To place every item in the first bucket
- B
To distribute items as evenly as possible among buckets
- C
To eliminate the need for an array of buckets
- D
To ensure that all linked lists have different lengths
Correct answer
BTo distribute items as evenly as possible among buckets
Explanation
A good hash function reduces collisions by distributing inputs relatively uniformly across the available buckets.
- A
- 51
What ordering property defines a binary search tree in the lecture's example?
- A
Values in the left subtree are smaller and values in the right subtree are larger
- B
Every node has exactly two children with equal values
- C
Values are stored in the tree in the order they were inserted
- D
Values increase only from the root toward the left subtree
Correct answer
AValues in the left subtree are smaller and values in the right subtree are larger
Explanation
For each node, smaller values are placed to the left and larger values to the right, enabling guided searches.
- A
- 52
How many pointer fields are needed in a binary-tree node to represent left and right children?
- A
One pointer field
- B
Three pointer fields
- C
Two pointer fields
- D
No pointer fields
Correct answer
CTwo pointer fields
Explanation
A binary-tree node needs one pointer for its left child and one pointer for its right child.
- A
- 53
What is the worst-case search time in a balanced binary search tree with n nodes?
- A
O(logn)
- B
O(n)
- C
O(n2)
- D
O(1)
Correct answer
AO(logn)
Explanation
Each comparison chooses the left or right subtree, approximately halving the remaining search space in a balanced tree.
- A
- 54
Why can a tree provide faster searching than an unsorted linked list?
- A
The ordering allows each comparison to eliminate an entire subtree
- B
Tree nodes are always stored contiguously in memory
- C
A tree stores every value in a single array index
- D
Trees never require dynamic memory allocation
Correct answer
AThe ordering allows each comparison to eliminate an entire subtree
Explanation
The binary-search-tree ordering lets the search choose a direction and discard many candidates at each step.
- A
- 55
What is a trie?
- A
A hash table that uses only the last character of each word
- B
A tree in which each node contains an array used to select the next character
- C
A linked list whose nodes each store three integers
- D
A fixed-size array containing all dictionary words directly
Correct answer
BA tree in which each node contains an array used to select the next character
Explanation
A trie represents strings character by character, with each node providing array-based links to possible next characters.
- A
- 56
Why can a trie support lookup in time related to word length rather than dictionary size?
- A
Lookup compares the word against every stored word simultaneously
- B
Lookup stores only the first letter of each word
- C
Lookup always uses binary search over one contiguous array
- D
Lookup follows one character link per letter in the word
Correct answer
DLookup follows one character link per letter in the word
Explanation
A trie follows the sequence of characters directly, so the number of steps depends primarily on the word's length.
- A
- 57
What is the primary cost of using a trie for fast word lookup?
- A
It can consume a large amount of memory
- B
It makes insertion impossible after initialization
- C
It requires every word to have the same length
- D
It cannot store words with repeated letters
Correct answer
AIt can consume a large amount of memory
Explanation
Each node may contain an array of possible character links, so a trie can use substantially more memory than simpler structures.
- A
- 58
How can a trie distinguish between a complete word and a prefix of a longer word?
- A
The trie compares only the first and last letters
- B
The trie must delete every node after a word is found
- C
Every prefix is automatically treated as a complete word
- D
Each node can record whether a word ends at that point
Correct answer
DEach node can record whether a word ends at that point
Explanation
A separate end-of-word marker allows the structure to distinguish a complete word such as Bob from a longer word such as Bobby.
- A

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