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

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

What does debug50 allow a programmer to do?

75 questions

  1. 1

    What does debug50 allow a programmer to do?

    Model answer

    It opens a debugger panel that allows the programmer to step through code and inspect variables.

    Explanation

    Breakpoints and step-by-step execution help reveal how a program behaves while it runs.

  2. 2

    What is the purpose of check50?

    Model answer

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

    Explanation

    It automates many of the correctness tests that programmers might otherwise perform manually.

  3. 3

    How can printf help with debugging?

    Model answer

    It can display variable values, messages, or execution markers so the programmer can see what the program is doing.

    Explanation

    Printing values or messages is especially useful for locating bugs or checking whether a particular section of code executes.

  4. 4

    When should a programmer use breakpoints in a debugger?

    Model answer

    Breakpoints should be placed at important locations where the programmer wants execution to pause and state to be inspected.

    Explanation

    Strategic breakpoints avoid repeatedly stepping through irrelevant code.

  5. 5

    What does style50 check?

    Model answer

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

    Explanation

    It identifies spaces that should be added or removed and reports them visually.

  6. 6

    When should a programmer use help50?

    Model answer

    A programmer should use it when an error message from compiling or running a command is difficult to understand.

    Explanation

    It analyzes the error and provides more accessible, teaching-fellow-style guidance.

  7. 7

    What is rubber duck debugging?

    Model answer

    It is the practice of explaining code aloud, step by step, to a rubber duck or another nonresponsive listener.

    Explanation

    Hearing the reasoning often exposes an incorrect assumption or produces an unexpected insight.

  8. 8

    Why can a program contain a bug even if it runs successfully?

    Model answer

    The program may be accessing invalid memory or relying on behavior that happens to work on one computer but is not guaranteed.

    Explanation

    A program can get lucky because different systems may arrange memory differently.

  9. 9

    What does malloc do?

    Model answer

    It requests a specified amount of memory from the operating system's heap and returns the address of that memory.

    Explanation

    The returned address must be stored in a pointer if the program needs to use the allocated memory.

  10. 10

    Why is sizeof(int) preferable to assuming that an integer occupies four bytes?

    Model answer

    The size of an integer can differ between systems, so sizeof(int) makes the allocation portable.

    Explanation

    Hard-coding a byte count assumes a particular computer architecture.

  11. 11

    What does malloc return conceptually?

    Model answer

    It returns a pointer, or address, to the allocated block of memory.

    Explanation

    The pointer identifies where the requested memory begins.

  12. 12

    Why can a block allocated for several integers be treated like an array?

    Model answer

    The allocated memory is contiguous, meaning its elements occupy consecutive locations.

    Explanation

    Contiguous memory supports indexed access in the same way as an array.

  13. 13

    Why is accessing element 101010 of an array allocated for 101010 integers invalid?

    Model answer

    The valid indexes are 000 through 999; index 101010 is outside the allocated bounds.

    Explanation

    C does not automatically prevent an out-of-bounds access.

  14. 14

    Why might an out-of-bounds memory access appear to work in C?

    Model answer

    The program may accidentally have access to nearby memory that is not immediately protected, so it gets lucky.

    Explanation

    This behavior is still invalid and may fail on another system or under different conditions.

  15. 15

    What does Valgrind detect?

    Model answer

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

    Explanation

    It reports suspicious memory operations and often identifies the relevant source lines and call sequence.

  16. 16

    What does an "invalid read of size 444" generally indicate?

    Model answer

    It indicates that the program tried to read four bytes from memory it was not permitted to access, often an integer outside an array.

    Explanation

    The reported size commonly corresponds to the size of the attempted data access.

  17. 17

    What is a memory leak?

    Model answer

    A memory leak occurs when dynamically allocated memory is no longer needed but is not returned to the operating system with free.

    Explanation

    Repeated leaks can cause a program or computer to slow down, freeze, or crash.

  18. 18

    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

    Memory must not be freed while the program still needs to access it.

  19. 19

    Why is using memory after calling free invalid?

    Model answer

    After free, the program has returned the memory to the operating system and no longer owns it.

    Explanation

    Accessing the released block can produce unpredictable behavior.

  20. 20

    What does it mean when Valgrind reports that all heap blocks were freed?

    Model answer

    It means the dynamically allocated memory was properly released and no memory leaks were detected.

    Explanation

    The heap is the region from which functions such as malloc obtain dynamic memory.

  21. 21

    What happens to freed memory conceptually?

    Model answer

    The operating system marks it as available and returns it to a pool of free memory for later allocation.

    Explanation

    The physical bytes do not disappear; their ownership status changes.

  22. 22

    How should Valgrind be used when a program requires command-line arguments?

    Model answer

    The arguments are supplied normally after the program name, while Valgrind prefixes the command.

    Explanation

    Valgrind runs the program with the same arguments while monitoring its memory behavior.

  23. 23

    What is stored in a program's text segment?

    Model answer

    The text segment stores the machine code instructions of the running program.

    Explanation

    When a program runs, its instructions are loaded from long-term storage into RAM.

  24. 24

    Why is a running program loaded into RAM?

    Model answer

    RAM is faster to access than long-term storage devices such as hard drives or solid-state drives.

    Explanation

    Loading the program into memory allows the processor to execute it efficiently.

  25. 25

    What is stored on the heap?

    Model answer

    The heap stores dynamically allocated memory requested with functions such as malloc.

    Explanation

    Calling free marks heap memory as available again.

  26. 26

    What is stored on the stack?

    Model answer

    The stack stores function call frames and local variables.

    Explanation

    Each function call receives its own region of stack memory.

  27. 27

    What can happen when the stack and heap grow into each other?

    Model answer

    The finite memory space can be exhausted, causing a stack or heap overflow and potentially making the program fail.

    Explanation

    The stack and heap grow dynamically in opposite directions within a limited address space.

  28. 28

    Why did the original value-based swap function fail to swap the caller's variables?

    Model answer

    The function received copies of the values, so it changed only its local copies rather than the original variables.

    Explanation

    Function arguments passed by value are stored in the called function's own stack frame.

  29. 29

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

    Model answer

    The function receives the variables' addresses and dereferences those pointers to change the original memory locations.

    Explanation

    Passing addresses avoids copying the values and gives the function access to the originals.

  30. 30

    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 target.

  31. 31

    Why is dereferencing an uninitialized pointer dangerous?

    Model answer

    An uninitialized pointer contains an unknown garbage address, so dereferencing it may access invalid memory.

    Explanation

    The pointer must be assigned a valid address or NULL before it is dereferenced.

  32. 32

    What is NULL conceptually in pointer operations?

    Model answer

    NULL is a special pointer value indicating that the pointer does not refer to a valid object or memory location.

    Explanation

    It is commonly represented by the address zero and should not be dereferenced.

  33. 33

    What is a struct in C?

    Model answer

    A struct is a programmer-defined data type that groups related fields, which may have different types.

    Explanation

    It allows a program to represent a meaningful entity using one combined data structure.

  34. 34

    What does typedef provide when used with a structure?

    Model answer

    It creates a convenient name for the structure type.

    Explanation

    For example, a structure can be given the shorter type name student.

  35. 35

    How could a student be represented with a structure?

    Model answer

    A student structure could contain fields such as a character pointer for the student's name and another for the dormitory.

    Explanation

    The structure encapsulates related information about one student.

  36. 36

    What does encapsulation mean in the context of structures?

    Model answer

    Encapsulation means grouping related data into one data structure instead of managing many separate variables or arrays.

    Explanation

    It creates a useful abstraction, such as representing a student as one object with several fields.

  37. 37

    How are fields accessed in an array of structures?

    Model answer

    Square brackets select an element of the array, and the dot operator selects a field of that structure.

    Explanation

    For example, an element's name field can be accessed using array indexing followed by .name.

  38. 38

    What is a string in C underneath the abstraction provided by the CS50 library?

    Model answer

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

    Explanation

    The characters occupy consecutive memory locations and normally end with a null terminator.

  39. 39

    What is a major limitation of a C array?

    Model answer

    Its size is fixed after allocation, so it cannot grow without creating or reallocating a larger block of memory.

    Explanation

    Adding an element may require copying all existing elements.

  40. 40

    What is another limitation of a conventional C array?

    Model answer

    All elements must have the same data type.

    Explanation

    An ordinary array cannot directly mix integers, characters, and structures as its elements.

  41. 41

    What is the running time of resizing an array by creating a new array and copying its elements?

    Model answer

    The process takes linear time, O(n)O(n)O(n), because approximately all nnn existing elements must be copied.

    Explanation

    The additional constant amount of space does not change the dominant linear term.

  42. 42

    What does realloc do?

    Model answer

    It changes the size of an allocated memory block and returns a pointer to the resized block.

    Explanation

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

  43. 43

    Why must the return value of realloc be handled carefully?

    Model answer

    realloc may return a different address or NULL if allocation fails, so the program should store and check its return value safely.

    Explanation

    Using the original pointer incorrectly after a failed or relocated allocation can lose memory or cause invalid access.

  44. 44

    What is the trade-off involved in allocating more array capacity than currently needed?

    Model answer

    Extra capacity reduces the frequency of reallocations but wastes memory that is not currently used.

    Explanation

    The best choice depends on whether execution time or memory consumption is more important.

  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 another node.

    Explanation

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

  46. 46

    What fields are typically contained in a singly linked-list node?

    Model answer

    A node typically contains a data value and a pointer to the next node.

    Explanation

    The final node's next pointer is set to NULL to mark the end of the list.

  47. 47

    Why can a linked list grow without copying all existing elements?

    Model answer

    A new node can be allocated independently and connected to the existing list with pointers.

    Explanation

    The nodes need not be stored contiguously, unlike elements in an array.

  48. 48

    What is the purpose of the initial struct node name in a self-referential structure declaration?

    Model answer

    It allows the structure to refer to its own type when declaring a pointer to the next node.

    Explanation

    The compiler must know that the structure name exists before encountering the self-referential pointer field.

  49. 49

    What does a node *next field represent?

    Model answer

    It stores the address of another node.

    Explanation

    The pointer acts as the link between the current node and the next node in the list.

  50. 50

    Why is the first pointer, often called first or numbers, essential for a linked list?

    Model answer

    It provides access to the first node, from which every later node can be reached by following next pointers.

    Explanation

    Losing the first pointer can make the rest of the list inaccessible.

  51. 51

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

    Model answer

    It is constant time, O(1)O(1)O(1), because only the new node's pointer and the list's first pointer must be changed.

    Explanation

    The insertion does not require traversing the existing nodes.

  52. 52

    What is the running time of inserting a node at the end of a singly linked list when only the first pointer is stored?

    Model answer

    It is linear time, O(n)O(n)O(n), because the list must be traversed from the beginning to find the final node.

    Explanation

    A separate last pointer could make insertion at the end faster.

  53. 53

    Why must a new node's next pointer be set before changing the previous node's pointer during a middle insertion?

    Model answer

    The new node must first point to the rest of the list so that changing the previous pointer does not disconnect later nodes.

    Explanation

    Updating the links in the wrong order can lose access to part of the list.

  54. 54

    What is a disadvantage of maintaining a sorted linked list?

    Model answer

    Insertion may require traversing the list to find the correct location, giving worst-case running time O(n)O(n)O(n).

    Explanation

    The new value may belong at the end, requiring a scan through every existing node.

  55. 55

    What advantage does a doubly linked list have over a singly linked list?

    Model answer

    Each node has pointers in both directions, allowing traversal backward as well as forward.

    Explanation

    A backward pointer can simplify operations that need to move to a previous node.

  56. 56

    What does the arrow operator do in an expression such as n->number?

    Model answer

    It follows the pointer n and accesses the structure field named number.

    Explanation

    It is shorthand for dereferencing the pointer and then using the dot operator.

  57. 57

    How is n->number related to (*n).number?

    Model answer

    They are equivalent expressions; the arrow operator is shorthand for dereferencing n and accessing its number field.

    Explanation

    The parentheses ensure that dereferencing occurs before field access.

  58. 58

    Why should a program check whether malloc returned NULL?

    Model answer

    A NULL result means that the requested memory could not be allocated, so continuing to use the pointer would be unsafe.

    Explanation

    A program should handle allocation failure rather than dereference an invalid pointer.

  59. 59

    What is the purpose of traversing a linked list with a pointer variable such as ptr?

    Model answer

    The pointer moves from node to node by repeatedly following each node's next field.

    Explanation

    Traversal continues until the pointer reaches NULL or the desired node is found.

  60. 60

    How do array-based and linked-list-based dynamic storage differ?

    Model answer

    An array provides contiguous memory and fast indexed access but may require costly resizing, while a linked list grows easily but requires pointer traversal.

    Explanation

    The two structures trade random access and locality for flexible growth.

  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 one of 262626 alphabetic buckets.

  62. 62

    What is a hash table?

    Model answer

    A hash table is commonly an array of buckets, where each bucket may contain a linked list of items that hash to that location.

    Explanation

    This combines the array's fast bucket access with the linked list's flexible storage.

  63. 63

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

    Model answer

    Hashing divides the items among buckets, so each bucket's linked list is usually much shorter than the entire collection.

    Explanation

    With roughly uniform distribution among 262626 buckets, the average chain length is about n/26n/26n/26.

  64. 64

    What is the worst-case running time of lookup in a hash table?

    Model answer

    The worst-case running time is O(n)O(n)O(n) if all items are placed in the same bucket.

    Explanation

    A poor or unlucky distribution can reduce the hash table to one long linked list.

  65. 65

    What makes a hash function effective?

    Model answer

    An effective hash function distributes inputs as evenly as possible among the available buckets.

    Explanation

    Uniform distribution avoids long chains and produces more consistent lookup performance.

  66. 66

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

    Model answer

    Some letters are much more common than others, causing uneven bucket sizes and long chains.

    Explanation

    A better function might use more characters, an identifier, or another more uniformly distributed property.

  67. 67

    What is a binary search tree?

    Model answer

    It is a tree in which each node's left descendants contain smaller values and its right descendants contain larger values.

    Explanation

    This ordering allows a search to choose a direction at each node.

  68. 68

    How can a binary search tree be represented in memory?

    Model answer

    Each node can store a value and two pointers: one to a left child and one to a right child.

    Explanation

    A missing child is represented by a NULL pointer.

  69. 69

    Why can searching a balanced binary search tree take logarithmic time?

    Model answer

    Each comparison eliminates roughly half of the remaining search space, so a tree with nnn nodes has height about O(log⁡n)O(\log n)O(logn).

    Explanation

    The search follows one path from the root rather than examining every node.

  70. 70

    What is the root of a tree?

    Model answer

    The root is the top node from which the rest of the tree can be reached.

    Explanation

    Tree searches and traversals normally begin at the root.

  71. 71

    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 comes from retrieval, and the structure supports character-by-character lookup.

  72. 72

    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 representing the next character.

    Explanation

    For a word such as BILLY, the path follows one node for each successive character.

  73. 73

    Why can a trie provide lookup time independent of the number of stored words?

    Model answer

    Lookup follows only the characters of the searched word, so its time depends on word length rather than the total number of words.

    Explanation

    A word of length kkk can be found in approximately O(k)O(k)O(k) time, which is treated as constant for bounded word lengths.

  74. 74

    What is the main cost of using a trie?

    Model answer

    A trie can require a large amount of memory because each node may contain a broad array of possible character links.

    Explanation

    It exchanges increased memory usage for very fast lookup.

  75. 75

    How can a trie distinguish between a word and a longer word that begins with it, such as BOB and BOBBY?

    Model answer

    Each completed word needs an additional marker indicating that a word ends at that node.

    Explanation

    The marker distinguishes stopping at BOB from continuing along the path to BOBBY.

1 of 75
Question 1 of 75

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent