1. Home
  2. Explore
  3. Computer Science
  4. CS50 Debugging Tools

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

CS50 Debugging Tools

  • 77Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Debugging Tools

What does debug50 allow a programmer to do?

77 questions

  1. 1

    What does debug50 allow a programmer to do?

    Model answer

    debug50 allows a programmer to pause execution at breakpoints, step through code, and inspect variables as the program runs.

    Explanation

    Its side panel supports step-by-step debugging and makes the program's changing state visible.

  2. 2

    What is the purpose of check50?

    Model answer

    check50 automatically runs tests to check whether a program is correct.

    Explanation

    It automates many of the correctness checks that students or teaching fellows might otherwise perform manually.

  3. 3

    What does printf help a programmer investigate during debugging?

    Model answer

    printf can display variable values, messages, or execution markers to show what the program is doing and where a bug occurs.

    Explanation

    It is especially useful for checking whether execution reached a particular point or observing values inside loops.

  4. 4

    Why can printf be more convenient than repeatedly stepping through a loop with a debugger?

    Model answer

    A strategically placed printf can show information on many loop iterations without requiring the programmer to click through each iteration manually.

    Explanation

    This is useful for nested or lengthy loops where repeated debugger steps would be tedious.

  5. 5

    What does style50 check?

    Model answer

    style50 checks the formatting and style of code, such as spacing, to improve readability.

    Explanation

    It identifies places where spaces should be added or removed.

  6. 6

    When should a programmer use help50?

    Model answer

    A programmer should use help50 when an unfamiliar or cryptic error message appears while compiling or running a command.

    Explanation

    It analyzes the command's output and provides more understandable, teaching-fellow-style guidance.

  7. 7

    What is rubber duck debugging?

    Model answer

    Rubber duck debugging is the practice of explaining code aloud, step by step, to an object such as a rubber duck in order to identify a logical mistake.

    Explanation

    Hearing oneself describe the code can reveal an incorrect assumption or produce an unexpected insight.

  8. 8

    Why can a program contain a memory bug even if it appears to work correctly?

    Model answer

    C may allow an invalid memory operation to succeed by chance, so the program can produce the expected output even though its behavior is unsafe.

    Explanation

    Different computers or memory layouts may cause the same bug to crash or behave differently.

  9. 9

    What is Valgrind used to detect?

    Model answer

    Valgrind detects memory errors such as invalid reads, invalid writes, and memory leaks.

    Explanation

    It can expose problems that ordinary compilation and sample testing do not reveal.

  10. 10

    What does malloc do, and what must a program do after using the allocated memory?

    Model answer

    malloc requests a block of memory from the heap; the program should release that memory with free when it is no longer needed.

    Explanation

    Failing to call free can cause a memory leak.

  11. 11

    Why can failing to free allocated memory cause a computer or program to slow down, freeze, or crash?

    Model answer

    Unfreed allocations remain marked as in use, so the program continually consumes available memory until insufficient memory remains.

    Explanation

    The operating system cannot reuse leaked blocks until they are released.

  12. 12

    What does an 'invalid read of size 4' generally indicate when an int is involved?

    Model answer

    It indicates that the program attempted to read four bytes from a memory location it was not allowed to access, often because it went outside an array's bounds.

    Explanation

    On the example system, an int occupies four bytes.

  13. 13

    Why is accessing element 10 invalid when memory was allocated for 10 integers?

    Model answer

    The valid indexes are 000 through 999; index 101010 is one position beyond the allocated array.

    Explanation

    Array indexing begins at zero, so an array of length 101010 has ten valid positions.

  14. 14

    Why might an out-of-bounds access not immediately cause a crash in C?

    Model answer

    The program may have received extra adjacent memory or may access memory that is not currently protected, allowing the invalid operation to appear to work.

    Explanation

    This accidental success is not guaranteed across systems or executions.

  15. 15

    What does Valgrind report when allocated memory is not released?

    Model answer

    It reports that a number of bytes in one or more blocks are definitely lost, indicating a memory leak.

    Explanation

    The report usually identifies the allocation site so the programmer can add the appropriate free.

  16. 16

    When should dynamically allocated memory be freed?

    Model answer

    It should be freed when the program is finished using it, commonly near the end of the relevant computation or program.

    Explanation

    Freeing memory before its final use creates an invalid access.

  17. 17

    Why is using memory after calling free incorrect?

    Model answer

    free returns the memory to the operating system's available pool, so accessing it afterward uses memory that the program no longer owns.

    Explanation

    The released block may be reused or contain invalid data.

  18. 18

    What do the heap and the stack generally store?

    Model answer

    The heap stores dynamically allocated memory requested with functions such as malloc, while the stack stores local variables and function-call frames.

    Explanation

    Each function call receives its own region of stack memory.

  19. 19

    What happens conceptually when memory is freed?

    Model answer

    The memory is marked as available and returned to the pool of free memory for later allocations; its physical bytes do not disappear.

    Explanation

    The operating system tracks which regions are in use and which are available.

  20. 20

    What is stored in a program's text segment when the program is running?

    Model answer

    The text segment stores the program's machine-code instructions loaded into RAM.

    Explanation

    The executable is copied from long-term storage into faster memory so the processor can run it.

  21. 21

    Why are programs loaded from storage into RAM before execution?

    Model answer

    RAM is generally faster to access than hard drives or solid-state storage, so loading code into RAM improves execution performance.

    Explanation

    RAM is electronic and has no moving mechanical parts.

  22. 22

    What is a string in C at a low level?

    Model answer

    A string is a sequence or array of characters and is represented by a char *, a pointer to a character.

    Explanation

    The pointer refers to the beginning of the character data in memory.

  23. 23

    Why did passing ordinary variables to the original swap function fail to swap the variables in main?

    Model answer

    The function received copies of the values, so it swapped its local copies rather than the original variables in main.

    Explanation

    Function parameters are stored in a separate stack frame.

  24. 24

    How do pointers allow a function to swap variables belonging to its caller?

    Model answer

    The caller passes the variables' addresses, and the function dereferences those pointers to modify the original memory locations.

    Explanation

    The function changes the data at the addresses rather than changing copied values.

  25. 25

    What does dereferencing a pointer mean?

    Model answer

    Dereferencing means accessing the value stored at the memory address contained in the pointer.

    Explanation

    In C, the * operator is used to follow a pointer to its referent.

  26. 26

    Why is dereferencing an uninitialized pointer unsafe?

    Model answer

    An uninitialized pointer contains an indeterminate or garbage address, so dereferencing it may access invalid memory and cause unpredictable behavior.

    Explanation

    A pointer should be assigned a valid address or NULL before it is dereferenced.

  27. 27

    What is the difference between a null pointer and an uninitialized pointer?

    Model answer

    A null pointer is deliberately set to NULL, meaning it points to no valid object, whereas an uninitialized pointer contains an unknown garbage value.

    Explanation

    Neither should be dereferenced, but a null pointer can safely be tested for explicitly.

  28. 28

    What is the purpose of a struct in C?

    Model answer

    A struct groups related values, possibly of different types, into one custom data structure.

    Explanation

    It lets programmers represent a real-world concept, such as a student, as a single code object.

  29. 29

    How can a student be represented using a structure?

    Model answer

    A student structure can contain fields such as a char * for the student's name and a char * for the student's dorm.

    Explanation

    The structure encapsulates related student information in one data type.

  30. 30

    What is the purpose of typedef when defining a structure?

    Model answer

    typedef creates a convenient name for a type, allowing a structure definition to be referred to by a shorter custom name such as student or node.

    Explanation

    It makes declarations easier to read and use.

  31. 31

    How are fields accessed in an ordinary structure variable?

    Model answer

    The dot operator is used, such as student.name or student.dorm.

    Explanation

    The dot operator accesses a field directly through a structure object.

  32. 32

    Why is encapsulating related values in a structure preferable to using separate parallel arrays?

    Model answer

    A structure keeps related data together, reducing the risk of mismatching values such as a name from one person with a dorm from another.

    Explanation

    It provides a clearer abstraction for the entity being represented.

  33. 33

    What is a major limitation of an ordinary C array regarding data types?

    Model answer

    All elements of an ordinary array must have the same data type.

    Explanation

    For example, an array must contain all integers, all characters, or all structures of the same type.

  34. 34

    Why can an ordinary C array not simply grow when another element is needed?

    Model answer

    Its allocated size is fixed, so adding capacity requires allocating a larger region and copying the existing elements into it.

    Explanation

    The original array has no extra capacity unless it was deliberately overallocated.

  35. 35

    What is the time complexity of resizing an array by copying its nnn existing elements?

    Model answer

    The resizing operation takes O(n)O(n)O(n) time because each of the nnn existing elements must be copied.

    Explanation

    The additional constant work is ignored in asymptotic notation.

  36. 36

    What does realloc do?

    Model answer

    realloc changes the size of a previously allocated memory block and returns a pointer to the resized block.

    Explanation

    It may extend the block in place or allocate a new block, copy the data, and return the new address.

  37. 37

    Why might realloc return a different address from the original allocation?

    Model answer

    If adjacent memory is unavailable, realloc must create a larger block elsewhere, copy the old contents, and return the new block's address.

    Explanation

    The original block may no longer be large enough to expand in place.

  38. 38

    Why should the return value of realloc usually be stored in a temporary pointer first?

    Model answer

    If realloc fails and returns NULL, assigning directly to the original pointer can lose the only reference to the original block.

    Explanation

    A temporary pointer permits failure checking before replacing the original pointer.

  39. 39

    What is the trade-off between allocating extra unused array capacity and resizing one element at a time?

    Model answer

    Extra capacity uses more memory but reduces the number of costly reallocations, while exact resizing saves memory but may require more copying.

    Explanation

    The better choice depends on the available resources and the expected input size.

  40. 40

    What do the variables size and capacity represent in a dynamically growing array?

    Model answer

    size is the number of elements currently stored, while capacity is the number of elements the allocated memory can hold.

    Explanation

    When size equals capacity, additional memory is needed before inserting another element.

  41. 41

    Why might a program use Control-D when reading an unknown number of integers?

    Model answer

    Control-D signals end-of-file, allowing the user to indicate that input is finished without entering another integer.

    Explanation

    A function designed to read only integers cannot use a letter such as q as a quit command.

  42. 42

    Why can getInt not directly support entering q to end numeric input?

    Model answer

    getInt expects an integer and immediately rejects nonnumeric input rather than returning a string command such as q.

    Explanation

    Supporting q would require reading a string first and then converting valid numeric strings.

  43. 43

    What is a sentinel value?

    Model answer

    A sentinel value is a special reserved value used to signal a condition such as the end of input.

    Explanation

    A program can test for the reserved value and stop processing when it appears.

  44. 44

    Why must a dynamically allocated array eventually be freed?

    Model answer

    The allocated block remains in use until free is called, so failing to release it causes a memory leak.

    Explanation

    Valgrind can report the leaked bytes and allocation location.

  45. 45

    What is a linked list?

    Model answer

    A linked list is a sequence of nodes in which each node stores data and a pointer to the next node.

    Explanation

    The nodes need not occupy adjacent memory locations because pointers connect them.

  46. 46

    What fields are typically included in a singly linked-list node storing integers?

    Model answer

    The node contains an integer value and a pointer to another node, often represented as int number and struct node *next.

    Explanation

    The pointer stores the address of the next node in the list.

  47. 47

    Why must a self-referential structure use the structure tag before its complete definition?

    Model answer

    The structure tag declares the type name early enough for a pointer field to refer to another structure of the same type.

    Explanation

    A pointer to the type has a known size even while the structure's full contents are still being defined.

  48. 48

    What does a NULL next pointer signify in a singly linked list?

    Model answer

    It signifies that the node is the last node and that no subsequent node exists.

    Explanation

    Traversal stops when the next pointer equals NULL.

  49. 49

    What pointer must be retained to access every element of a singly linked list?

    Model answer

    The program must retain a pointer to the first node, commonly called the head or first pointer.

    Explanation

    Every later node is reached by following next pointers from the first node.

  50. 50

    How is a new node inserted at the beginning of a linked list?

    Model answer

    First set the new node's next pointer to the current first node, then update the first pointer to the new node.

    Explanation

    Updating the new node before the first pointer prevents losing access to the existing list.

  51. 51

    Why must a new middle node point to its successor before its predecessor points to it?

    Model answer

    The new node must first preserve the successor address; otherwise, changing the predecessor's pointer could disconnect the remaining list.

    Explanation

    The safe order is to connect the new node to the old successor and then connect the predecessor to the new node.

  52. 52

    What is the worst-case time complexity of inserting into a sorted singly linked list?

    Model answer

    The worst-case time complexity is O(n)O(n)O(n) because the program may need to traverse all nnn nodes to find the insertion position.

    Explanation

    A sorted insertion may place the new node at the end of the list.

  53. 53

    What is the time complexity of inserting a node at the beginning of an unsorted linked list?

    Model answer

    Insertion at the beginning takes O(1)O(1)O(1) time.

    Explanation

    It requires only a fixed number of pointer updates, regardless of list length.

  54. 54

    What is the main advantage of a linked list over a fixed-size array?

    Model answer

    A linked list can grow or shrink dynamically without requiring all existing elements to be copied into a larger contiguous block.

    Explanation

    Each new node can be allocated separately as needed.

  55. 55

    What is a disadvantage of storing linked-list nodes in separate allocations?

    Model answer

    The nodes are not contiguous, so the list does not provide constant-time random access by index.

    Explanation

    The program must follow pointers sequentially from the first node.

  56. 56

    What is a doubly linked list, and what trade-off does it introduce?

    Model answer

    A doubly linked list stores both next and previous pointers, allowing traversal in either direction but requiring additional memory and pointer maintenance.

    Explanation

    The previous pointer makes it possible to move backward when a traversal goes too far.

  57. 57

    How can keeping a pointer to the last node improve linked-list insertion?

    Model answer

    A stored last pointer allows insertion at the end in constant time rather than requiring a traversal from the first node.

    Explanation

    The new node can be linked directly after the current last node.

  58. 58

    What does the arrow operator, ->, mean when used with a structure pointer?

    Model answer

    It follows the pointer to a structure and accesses the named field in that structure.

    Explanation

    The expression p->field is shorthand for (*p).field.

  59. 59

    Why is the arrow operator used instead of the dot operator when n is a pointer to a node?

    Model answer

    The dot operator requires a structure object, whereas n is an address; the arrow operator first follows the address and then accesses the field.

    Explanation

    For a node pointer n, n->number accesses the node's number field.

  60. 60

    What should a program do if malloc returns NULL while allocating a node?

    Model answer

    It should detect the failure and terminate or handle the error gracefully rather than dereferencing the null pointer.

    Explanation

    A NULL result means that the requested memory could not be allocated.

  61. 61

    What is a hash function?

    Model answer

    A hash function maps an input, such as a name, to an index or bucket used to organize data.

    Explanation

    For example, a function might map the first letter of a name to an index from 000 through 252525.

  62. 62

    How can an array of linked lists form a hash table?

    Model answer

    The array provides buckets for hash values, and each bucket stores a linked list containing all items that map to that bucket.

    Explanation

    This combines fast bucket access from arrays with dynamic storage from linked lists.

  63. 63

    Why can hashing improve name lookup compared with searching one long list?

    Model answer

    Hashing narrows the search to the linked list in one bucket rather than requiring a scan of every stored name.

    Explanation

    If names are distributed evenly, each bucket contains approximately n/26n/26n/26 names when using the first letter over the English alphabet.

  64. 64

    What is the worst-case time complexity of lookup in a hash table using linked-list buckets?

    Model answer

    The worst-case time complexity is O(n)O(n)O(n) if all items map to the same bucket.

    Explanation

    In that case, the hash table degenerates into one long linked list.

  65. 65

    What is the goal of a well-designed hash function?

    Model answer

    Its goal is to distribute inputs as evenly as possible among the available buckets.

    Explanation

    Even distribution keeps the bucket lists short and produces more consistent lookup times.

  66. 66

    Why might using only the first letter of a name be a poor hash function?

    Model answer

    Letters are not equally common in names, so some buckets may become much longer than others and lookup performance may be uneven.

    Explanation

    A hash function could use more information, such as multiple letters, an identification number, or another relatively uniform attribute.

  67. 67

    How can a hash table have a practical advantage even when its worst-case complexity remains O(n)O(n)O(n)?

    Model answer

    If the hash function distributes data evenly, the average search examines a much shorter bucket list, reducing actual running time despite the same worst-case classification.

    Explanation

    Ignoring constant factors in asymptotic notation does not mean those factors have no practical effect.

  68. 68

    What is a binary search tree?

    Model answer

    A binary search tree is a tree in which each node has up to a left and right child, with smaller values on the left and larger values on the right.

    Explanation

    This ordering allows searches to eliminate one subtree at each decision.

  69. 69

    How can a binary search tree be represented in C?

    Model answer

    Each node can contain a value and two pointers, one to a left child and one to a right child.

    Explanation

    A null child pointer indicates that the corresponding subtree does not exist.

  70. 70

    Why does searching a balanced binary search tree take O(log⁡n)O(\log n)O(logn) time?

    Model answer

    Each comparison chooses either the left or right subtree, roughly halving the remaining search space at every level.

    Explanation

    A balanced tree with nnn nodes has height proportional to log⁡n\log nlogn.

  71. 71

    What is the root of a tree?

    Model answer

    The root is the topmost node and the starting point for traversing the tree.

    Explanation

    All other nodes are reached by following child pointers from the root.

  72. 72

    What is a trie?

    Model answer

    A trie is a tree whose nodes contain arrays used to represent successive characters of stored strings.

    Explanation

    The name is associated with retrieval, and the structure supports efficient word lookup.

  73. 73

    How is a word stored in a trie?

    Model answer

    Each character selects an entry in the current node's array, leading to the node for the next character in the word.

    Explanation

    For a word such as BILLY, the traversal follows entries for BBB, III, LLL, LLL, and YYY.

  74. 74

    Why can a trie reuse memory when storing words with shared prefixes?

    Model answer

    Words with the same initial characters follow the same nodes until their characters diverge.

    Explanation

    For example, words beginning with B can share the root's B branch.

  75. 75

    What is the lookup time for a word of fixed length in a trie?

    Model answer

    Lookup takes time proportional to the word's length, which is effectively O(1)O(1)O(1) when the maximum word length is treated as bounded independently of the number of stored words.

    Explanation

    The number of stored words does not determine how many character links must be followed.

  76. 76

    What is the main cost of achieving very fast lookup with a trie?

    Model answer

    A trie can consume substantial memory because each node may contain a large array of character pointers.

    Explanation

    The design trades additional space for faster retrieval.

  77. 77

    How could a trie distinguish between a word such as BOB and a longer word such as BOBBY?

    Model answer

    Each node needs an indication of whether a complete word ends there, in addition to pointers for continuing characters.

    Explanation

    The structure must distinguish a valid word endpoint from a prefix that continues into another word.

1 of 77
Question 1 of 77

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent