CS50 Debugging Tools
- 54Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which feature does debug50 provide in CS50 IDE?
Which feature does debug50 provide in CS50 IDE?
54 questions
- 1
Which feature does
debug50provide in CS50 IDE?- A
It steps through code and displays variable values
- B
It automatically rewrites C code as Scratch blocks
- C
It translates compiler errors into natural language
- D
It checks whether code follows prescribed style rules
Correct answer
AIt steps through code and displays variable values
Explanation
debug50opens a debugger panel that allows programmers to execute code step by step and inspect variables. - A
- 2
What is the primary purpose of
check50?- A
It allocates additional memory for a running program
- B
It displays the contents of variables during execution
- C
It reformats source code according to style rules
- D
It runs automated tests to check program correctness
Correct answer
DIt runs automated tests to check program correctness
Explanation
check50automates tests that evaluate whether a program produces the expected results. - A
- 3
When is
printfespecially useful during debugging?- A
When a programmer wants to prevent all runtime errors automatically
- B
When a programmer wants to allocate memory without using the heap
- C
When a programmer wants to inspect values or confirm execution reached a point
- D
When a programmer wants to resize an array without copying data
Correct answer
CWhen a programmer wants to inspect values or confirm execution reached a point
Explanation
A programmer can use
printfto display variable values or indicate that execution reached a particular location, especially inside loops. - A
- 4
When should a programmer use
style50?- A
When locating memory leaks caused by missing
freecalls - B
When checking spacing and formatting for code readability
- C
When stepping through instructions one statement at a time
- D
When testing whether a program handles all expected inputs
Correct answer
BWhen checking spacing and formatting for code readability
Explanation
style50identifies spacing and formatting issues so that code is more readable. - A
- 5
When should a programmer instinctively use
help50?- A
When a program needs to check its indentation and spacing
- B
When a program needs to print the value of a variable
- C
When a program needs to allocate a new block of memory
- D
When an unfamiliar or cryptic command-line error appears
Correct answer
DWhen an unfamiliar or cryptic command-line error appears
Explanation
help50analyzes confusing error messages and provides feedback intended to help diagnose the problem. - A
- 6
What is the main purpose of Valgrind in the lecture?
- A
It replaces all compiler warnings with corrected code
- B
It converts compiled machine code into source code
- C
It detects invalid memory accesses and memory leaks
- D
It automatically sorts values stored in linked lists
Correct answer
CIt detects invalid memory accesses and memory leaks
Explanation
Valgrind helps identify invalid reads or writes and memory that was allocated but never freed.
- A
- 7
Why can a program that accesses memory beyond an allocated array appear to work?
- A
The operating system always initializes neighboring memory to valid array elements
- B
C automatically enlarges every array when an out-of-bounds access occurs
- C
The compiler converts every out-of-bounds access into a safe operation
- D
The program may get lucky and access memory that is temporarily available
Correct answer
DThe program may get lucky and access memory that is temporarily available
Explanation
C does not necessarily stop an out-of-bounds access. The program may accidentally access nearby memory without immediately crashing, but the behavior is unsafe.
- A
- 8
What does
mallocreturn when it successfully allocates memory?- A
A pointer containing the address of the allocated memory
- B
A copy of every value currently stored in the allocated memory
- C
A Boolean indicating whether the allocation was successful
- D
The number of bytes that remain available in the heap
Correct answer
AA pointer containing the address of the allocated memory
Explanation
mallocreturns the address of the allocated block, which is stored in a pointer. - A
- 9
Why is
sizeof(int)preferred over assuming that an integer always occupies four bytes?- A
The size of an integer can vary across computer systems
- B
Using
sizeofprevents integers from being stored in the heap - C
The value returned by
sizeofchanges whenever an integer is assigned - D
The C language does not allow numeric constants in memory expressions
Correct answer
AThe size of an integer can vary across computer systems
Explanation
Although an integer is often four bytes, its size is implementation-dependent, so
sizeof(int)is safer and portable. - A
- 10
What must generally happen to memory allocated by
mallocwhen it is no longer needed?- A
It should be overwritten with zeroes before being reused
- B
It should be copied into a string before the program ends
- C
It should be returned to the system with
free - D
It should be moved automatically from the heap to the stack
Correct answer
CIt should be returned to the system with
freeExplanation
Calling
freemarks dynamically allocated memory as available again and prevents a memory leak. - A
- 11
Why is freeing memory and then using it again an error?
- A
Calling
freeautomatically changes all pointers into integers - B
Freed memory can only be used to store characters
- C
The
freefunction permanently deletes the entire operating system - D
The program no longer owns that memory after it has been freed
Correct answer
DThe program no longer owns that memory after it has been freed
Explanation
After memory is freed, it may be reused for another purpose, so dereferencing the old pointer is unsafe.
- A
- 12
What does Valgrind report when allocated memory is not freed?
- A
It reports that the program contains an invalid function prototype
- B
It reports that the compiler cannot identify the program's entry point
- C
It reports that every array has been resized successfully
- D
It reports that memory was definitely lost
Correct answer
DIt reports that memory was definitely lost
Explanation
Valgrind identifies allocated heap memory that the program failed to release as memory that was definitely lost.
- A
- 13
What happens to memory after it is freed, conceptually?
- A
The operating system marks it as available for future use
- B
The memory becomes permanently inaccessible to every program
- C
The memory is automatically converted into stack memory
- D
The memory is physically removed from the computer
Correct answer
AThe operating system marks it as available for future use
Explanation
Freeing memory generally returns it to the pool of available memory; the physical bytes remain present.
- A
- 14
What does rubber duck debugging encourage a programmer to do?
- A
Explain the code aloud step by step to expose faulty reasoning
- B
Ask a debugging tool to rewrite the program automatically
- C
Run the program repeatedly without examining its source code
- D
Replace every pointer with a global variable before testing
Correct answer
AExplain the code aloud step by step to expose faulty reasoning
Explanation
Explaining code aloud can reveal contradictions or incorrect assumptions, even if the listener is only a rubber duck.
- A
- 15
What is a C string underneath the abstraction provided by the CS50 library?
- A
A special primitive type that stores an entire sentence directly
- B
A pointer to an integer containing the string's length only
- C
A pointer to a character, commonly written as
char * - D
A fixed-size structure that cannot contain a terminating character
Correct answer
CA pointer to a character, commonly written as
char *Explanation
A string is represented as a sequence of characters, and a
char *points to the first character. - A
- 16
What is stored in the text segment of a running program?
- A
Only local variables created inside currently executing functions
- B
The list of all memory blocks that have been freed
- C
Only dynamically allocated variables requested by
malloc - D
The machine instructions that implement the program
Correct answer
DThe machine instructions that implement the program
Explanation
The text segment contains the program's compiled machine code loaded into memory for execution.
- A
- 17
What type of data is typically stored in the heap?
- A
Dynamically allocated memory requested with
malloc - B
The compiled instructions of the running program
- C
Only function parameters passed by value
- D
The return addresses of every completed function
Correct answer
ADynamically allocated memory requested with
mallocExplanation
The heap is the region from which dynamic memory is allocated and to which it is returned with
free. - A
- 18
What type of data is typically stored on the stack?
- A
The permanent contents of the computer's hard drive
- B
Local variables and function call information
- C
Only memory obtained through
malloc - D
Only string literals from the program's source code
Correct answer
BLocal variables and function call information
Explanation
Function calls use stack frames containing local variables and related call information.
- A
- 19
What can happen if the stack and heap grow toward each other too far?
- A
They can collide because the computer has a finite amount of memory
- B
The compiler expands the physical RAM without changing the program
- C
All pointers become null before any data is overwritten
- D
The operating system automatically converts the heap into an array
Correct answer
AThey can collide because the computer has a finite amount of memory
Explanation
The stack and heap share finite memory space, so excessive growth can cause an overflow or collision.
- A
- 20
Why did the original value-based
swapfunction fail to change the caller's variables?- A
The function received copies of the values rather than their addresses
- B
The stack cannot store two integer variables at the same time
- C
The function used a temporary variable, which prevents swapping in C
- D
The function attempted to modify variables declared as constants
Correct answer
AThe function received copies of the values rather than their addresses
Explanation
Parameters passed by value create copies, so changes to the function's local parameters do not affect the original variables.
- A
- 21
How can a function change the caller's variables during a swap?
- A
It can pass the values through a second function by value
- B
It can declare new local variables with the same names
- C
It can receive pointers to those variables and dereference them
- D
It can print the variables before returning from the function
Correct answer
CIt can receive pointers to those variables and dereference them
Explanation
Passing addresses allows the function to access and modify the original memory locations.
- A
- 22
What does dereferencing a pointer mean?
- A
Accessing the value stored at the pointer's address
- B
Changing the pointer into an ordinary integer variable
- C
Copying the pointer's address into every nearby variable
- D
Deleting the memory block associated with the pointer
Correct answer
AAccessing the value stored at the pointer's address
Explanation
Dereferencing follows the pointer to the memory location it identifies and accesses the value there.
- A
- 23
Why is dereferencing an uninitialized pointer unsafe?
- A
An uninitialized pointer always contains the address of the first array element
- B
Dereferencing is safe as long as the pointer variable has a name
- C
It may contain an indeterminate address that does not identify valid memory
- D
The pointer is automatically converted into a read-only string
Correct answer
CIt may contain an indeterminate address that does not identify valid memory
Explanation
An uninitialized pointer may contain a garbage value, so dereferencing it can access an invalid or unintended location.
- A
- 24
What is the purpose of
NULLin pointer operations?- A
It represents the largest possible address in physical memory
- B
It represents a pointer that does not refer to a valid object
- C
It guarantees that the pointer refers to newly allocated memory
- D
It stores the first character of a string in a pointer
Correct answer
BIt represents a pointer that does not refer to a valid object
Explanation
NULLis a special pointer value used to indicate that a pointer does not currently refer to a valid object. - A
- 25
What does a
structallow a C programmer to do?- A
Guarantee that every field uses the same primitive data type
- B
Group related values of potentially different types into one data type
- C
Replace all pointers with values that are stored only on the stack
- D
Create an array whose size automatically changes after every assignment
Correct answer
BGroup related values of potentially different types into one data type
Explanation
A structure encapsulates related fields, such as a student's name and dorm, into one custom data type.
- A
- 26
What does encapsulation mean in the student structure example?
- A
Keeping related information about one entity together in one structure
- B
Hiding all variables from the compiler during program execution
- C
Storing every field of an entity in a separate array
- D
Converting every field into a pointer regardless of its original type
Correct answer
AKeeping related information about one entity together in one structure
Explanation
Encapsulation combines related data, such as a student's name and dorm, into one coherent abstraction.
- A
- 27
Why can an array be inconvenient when the number of elements is unknown?
- A
Its elements cannot be accessed using indices
- B
Its values cannot be copied into another array
- C
Its elements are always stored at unrelated memory addresses
- D
Its capacity is fixed after allocation and cannot grow in place normally
Correct answer
DIts capacity is fixed after allocation and cannot grow in place normally
Explanation
A conventional C array has a fixed allocated size, so adding elements may require a larger replacement array.
- A
- 28
What is the time complexity of resizing an array by allocating a new array and copying its elements?
- A
Big O of n, because all existing elements may need to be copied
- B
Big O of one, because only the new element is affected
- C
Big O of log n, because the array is divided during copying
- D
Big O of n squared, because every pair of elements must be compared
Correct answer
ABig O of n, because all existing elements may need to be copied
Explanation
Copying an array with n existing elements requires work proportional to n.
- A
- 29
What does
reallocattempt to do?- A
Resize an allocated memory block and return its possibly new address
- B
Convert a linked list into a fixed-size array without copying
- C
Initialize every byte in a memory block to zero automatically
- D
Release a block of memory without changing its allocation status
Correct answer
AResize an allocated memory block and return its possibly new address
Explanation
realloccan enlarge or shrink an allocated block; it may keep the same address or move the data and return a new address. - A
- 30
Why must a program use the return value of
realloccarefully?- A
The resized block may be moved, and
reallocmay returnNULLon failure - B
The original pointer remains valid even when
reallocreturns a new address - C
The return value indicates the size of the stack rather than the heap
- D
The return value is always the number of elements currently in the array
Correct answer
AThe resized block may be moved, and
reallocmay returnNULLon failureExplanation
reallocmay relocate the block, so the program needs the returned pointer; failure can produceNULL. - A
- 31
What is the difference between an array's size and its capacity?
- A
Size is the number of stored elements, while capacity is the available storage
- B
Size is the maximum possible length, while capacity is the current element count
- C
Size refers to the pointer address, while capacity refers to the array's first value
- D
Size is the number of bytes in the heap, while capacity is the number of stack frames
Correct answer
ASize is the number of stored elements, while capacity is the available storage
Explanation
Size tracks how many elements are currently stored; capacity tracks how many elements can fit before resizing is required.
- A
- 32
What keyboard combination was described as signaling end of input rather than terminating the program?
- A
Control-D, which represents end of file
- B
Control-S, which represents successful allocation
- C
Control-Z, which represents a completed integer input
- D
Control-C, which represents end of file
Correct answer
AControl-D, which represents end of file
Explanation
Control-D sends an end-of-file signal, allowing a program to recognize that the user has finished entering input.
- A
- 33
Why cannot
getIntdirectly support a user typingqto quit?- A
It can read strings but cannot return numeric values
- B
It automatically interprets every letter as a valid integer
- C
It expects the input to represent an integer rather than an arbitrary string
- D
It only accepts input after the program has freed its array
Correct answer
CIt expects the input to represent an integer rather than an arbitrary string
Explanation
getIntis designed to obtain integers, so a quit command such asqwould require reading a string instead. - A
- 34
Why is a linked list more flexible than a fixed-size array for adding elements?
- A
All linked-list nodes must occupy adjacent memory addresses
- B
New nodes can be allocated individually without moving all existing nodes
- C
A linked list automatically sorts every new element in constant time
- D
A linked list stores every element directly inside the first node
Correct answer
BNew nodes can be allocated individually without moving all existing nodes
Explanation
Linked lists grow by allocating new nodes and connecting them with pointers, so existing nodes need not be relocated.
- A
- 35
What is the key structural difference between an array and a linked list?
- A
Array elements contain pointers, while linked-list nodes cannot contain pointers
- B
Arrays are stored on the heap, while linked lists are always stored on the stack
- C
Arrays cannot store structures, while linked lists can store only integers
- D
Array elements are contiguous, while linked-list nodes may be scattered in memory
Correct answer
DArray elements are contiguous, while linked-list nodes may be scattered in memory
Explanation
Arrays use contiguous storage, whereas linked-list nodes can be located anywhere and connected by pointers.
- A
- 36
What information does a basic linked-list node typically contain?
- A
A fixed array containing every value in the list
- B
Only a pointer to the first node in the list
- C
A data value and the physical address of the computer's hard drive
- D
A data value and a pointer to the next node
Correct answer
DA data value and a pointer to the next node
Explanation
A node commonly stores its own value and a
nextpointer leading to another node. - A
- 37
What does a
nextpointer containingNULLindicate in a singly linked list?- A
The current node has been freed and cannot be accessed
- B
The current node is the final node in the list
- C
The current node contains an invalid integer value
- D
The current node is the first node in a circular list
Correct answer
BThe current node is the final node in the list
Explanation
A
NULLnext pointer marks the end of a singly linked list. - A
- 38
What is the running time of inserting a new node at the beginning of an unsorted linked list when the first pointer is known?
- A
Big O of n, because every existing node must be inspected
- B
Big O of n squared, because every node must be compared with the new node
- C
Big O of one, because only a constant number of pointers must change
- D
Big O of log n, because the list is divided into two parts
Correct answer
CBig O of one, because only a constant number of pointers must change
Explanation
Insertion at the front requires setting the new node's next pointer and updating the first pointer.
- A
- 39
What is the worst-case running time of inserting into a linked list while preserving sorted order?
- A
Big O of n squared, because each insertion requires sorting the entire list
- B
Big O of log n, because linked lists support binary search directly
- C
Big O of one, because linked lists always insert instantly
- D
Big O of n, because the correct position may be at the end
Correct answer
DBig O of n, because the correct position may be at the end
Explanation
A sorted insertion may require traversing the entire list to locate the correct position.
- A
- 40
Why must pointer updates be performed in the correct order when inserting into a linked list?
- A
The new node cannot store a value until every old node has been copied
- B
Linked lists require all pointer assignments to occur in alphabetical order
- C
Overwriting a link too early can cause the program to lose access to part of the list
- D
The operating system rejects pointer assignments made after a node is allocated
Correct answer
COverwriting a link too early can cause the program to lose access to part of the list
Explanation
The existing link should be preserved before redirecting a pointer; otherwise, nodes may become unreachable.
- A
- 41
What additional pointer would make appending to a linked list more efficient?
- A
A pointer that identifies the middle node regardless of list length
- B
A pointer that replaces every node's
nextfield - C
A pointer that always identifies the last node
- D
A pointer that stores the number of bytes in every node
Correct answer
CA pointer that always identifies the last node
Explanation
Maintaining a pointer to the last node allows a new node to be appended without traversing from the beginning.
- A
- 42
What is a doubly linked list?
- A
A list whose nodes are always sorted in increasing and decreasing order
- B
A list whose nodes contain pointers to both the next and previous nodes
- C
A list that stores every value twice in the same memory block
- D
A list that uses two arrays instead of dynamically allocated nodes
Correct answer
BA list whose nodes contain pointers to both the next and previous nodes
Explanation
A doubly linked list adds a pointer to the previous node, allowing traversal in both directions.
- A
- 43
What is the basic strategy of a hash table?
- A
Place every value at a random address without storing any linking information
- B
Store every value in one sorted array and search it linearly
- C
Use a hash function to assign data to an array bucket containing a list
- D
Use one linked list and repeatedly divide it in half during every search
Correct answer
CUse a hash function to assign data to an array bucket containing a list
Explanation
A hash table commonly combines an array of buckets with linked lists that hold values assigned to each bucket.
- A
- 44
In the name example, what did the hash function use to choose a bucket?
- A
The last letter of the person's name only
- B
The first letter of the person's name
- C
The order in which the person entered the room
- D
The number of characters in the person's name
Correct answer
BThe first letter of the person's name
Explanation
The example mapped names to buckets according to their first letters, such as A through Z.
- A
- 45
Why can a hash table be faster than one long linked list in practice?
- A
It guarantees that every lookup examines exactly one element
- B
It stores all values contiguously and therefore never uses pointers
- C
It eliminates the need to compare a searched value with stored values
- D
It distributes values across multiple shorter lists that can be reached through an array
Correct answer
DIt distributes values across multiple shorter lists that can be reached through an array
Explanation
The array provides quick access to a bucket, and the linked list in that bucket is usually shorter than one list containing every value.
- A
- 46
What is the worst-case lookup time for a hash table with a poor or unlucky distribution?
- A
Big O of log n, because every bucket is automatically balanced
- B
Big O of n squared, because every bucket must be searched twice
- C
Big O of n, if all values end up in one bucket
- D
Big O of one, regardless of the hash function
Correct answer
CBig O of n, if all values end up in one bucket
Explanation
If all values collide in the same bucket, lookup degenerates into searching a list of length n.
- A
- 47
What is a desirable property of a hash function?
- A
It assigns every input to the same bucket to simplify storage
- B
It requires the input values to be stored in sorted order beforehand
- C
It produces a different number of buckets for every input
- D
It distributes inputs relatively evenly among the available buckets
Correct answer
DIt distributes inputs relatively evenly among the available buckets
Explanation
A well-designed hash function reduces collisions by distributing data relatively uniformly.
- A
- 48
What is the defining ordering property of a binary search tree?
- A
Values increase from left to right only at the root node
- B
Values are stored in the same contiguous order as in an array
- C
Values in the left subtree are smaller and values in the right subtree are larger
- D
Every node has exactly two children with equal values
Correct answer
CValues in the left subtree are smaller and values in the right subtree are larger
Explanation
In a binary search tree, each node is greater than values in its left subtree and less than values in its right subtree.
- A
- 49
How is a tree node designed differently from a singly linked-list node?
- A
It contains no data value because trees store values only in arrays
- B
It typically contains two child pointers instead of one next pointer
- C
It can store only character data rather than integers or structures
- D
It must contain a pointer to every other node in the tree
Correct answer
BIt typically contains two child pointers instead of one next pointer
Explanation
A binary tree node commonly stores a value plus pointers to a left child and a right child.
- A
- 50
What is the search time in a balanced binary search tree?
- A
Big O of n squared, because each subtree is searched repeatedly
- B
Big O of log n, because each comparison chooses a subtree
- C
Big O of n, because every node must always be visited
- D
Big O of one, because every node is directly indexed
Correct answer
BBig O of log n, because each comparison chooses a subtree
Explanation
A balanced binary search tree reduces the remaining search space at each level, producing logarithmic search time.
- A
- 51
What is the root of a tree?
- A
The top node from which traversal begins
- B
The node with the largest value in the entire tree
- C
The final node reached after following all right pointers
- D
The node that has no parent but must have two children
Correct answer
AThe top node from which traversal begins
Explanation
The root is the starting node of a tree and has no parent.
- A
- 52
What is a trie?
- A
A fixed-size array containing every dictionary word as a complete string
- B
A binary search tree that stores only numerical values
- C
A linked list whose nodes are sorted by the length of each word
- D
A tree in which each node contains an array used to follow characters
Correct answer
DA tree in which each node contains an array used to follow characters
Explanation
A trie stores words through paths of character choices, with each node providing an array of possible next characters.
- A
- 53
Why can a trie provide lookup time that depends on word length rather than the number of stored words?
- A
Every word is stored in alphabetical order inside one contiguous array
- B
The trie checks every word simultaneously using a separate processor
- C
Lookup follows one character path for the word instead of scanning all words
- D
The trie automatically compresses every word into a single integer
Correct answer
CLookup follows one character path for the word instead of scanning all words
Explanation
A lookup follows the sequence of characters in the target word, so the number of stored words does not directly determine the path length.
- A
- 54
What is the main trade-off of using a trie?
- A
It provides constant memory usage but cannot represent shared prefixes
- B
It eliminates pointers but requires every word to have the same length
- C
It reduces memory usage but requires linear lookup for every word
- D
It can provide fast lookup at the cost of substantial memory usage
Correct answer
DIt can provide fast lookup at the cost of substantial memory usage
Explanation
Tries use many arrays and nodes, consuming considerable memory in exchange for very fast word lookup.
- A

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