1. Home
  2. Explore
  3. Computer Science
  4. Image Filters in C

Study methods

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

Image Filters in C

  • 86Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Structures and Files

What is a struct in C, and why is it useful?

86 questions

  1. 1

    What is a struct in C, and why is it useful?

    Model answer

    A struct is a user-defined data type that groups related fields, which may have different types, into one object.

    Explanation

    Structures allow programmers to model entities such as students by combining information like names, dorms, IDs, and contact details.

  2. 2

    What does the declaration typedef struct student { ... } student; accomplish?

    Model answer

    It defines a structure type and creates student as an alias for that type, allowing variables to be declared using the name student.

    Explanation

    The typedef avoids repeatedly writing the longer struct student form.

  3. 3

    How would a student structure represent a student's name and dorm?

    Model answer

    It could contain fields such as string name and string dorm, with additional fields added for information such as an ID or email address.

    Explanation

    A structure can encapsulate multiple related values in one data object.

  4. 4

    Why are structure definitions commonly placed in header files?

    Model answer

    A header file allows the structure definition to be shared across multiple source files and programs.

    Explanation

    Including the header makes the custom type available wherever it is needed.

  5. 5

    What does students[i].name mean in C?

    Model answer

    It accesses the name field of the structure stored at index i in the students array.

    Explanation

    The dot operator enters a structure and selects one of its fields.

  6. 6

    How does a variable-length array of students get created when the enrollment is known only at runtime?

    Model answer

    The program reads the enrollment value and declares an array with that value as its size.

    Explanation

    The array size can be determined after input is received rather than being fixed in the source code.

  7. 7

    What are the two arguments passed to fopen?

    Model answer

    The first argument is the filename, and the second specifies the access mode, such as read or write.

    Explanation

    The mode tells the operating system what operations the program intends to perform on the file.

  8. 8

    What do the file modes r and w mean?

    Model answer

    r opens a file for reading, while w opens a file for writing.

    Explanation

    Writing allows the program to save data rather than merely retrieve it.

  9. 9

    Why should a program check whether fopen returned NULL?

    Model answer

    A NULL result indicates that the file could not be opened, possibly because it does not exist, permission is denied, or resources are unavailable.

    Explanation

    NULL represents the absence of a valid address or successfully opened file.

  10. 10

    How does fprintf differ from printf?

    Model answer

    fprintf takes an open file as an additional argument and writes formatted output to that file instead of automatically writing to the screen.

    Explanation

    The remaining format string and values are used similarly to printf.

  11. 11

    What is the purpose of fclose after writing a file?

    Model answer

    fclose closes the file and ensures that the program's output is finalized and saved.

    Explanation

    Closing the file releases the associated resources and completes the file operation.

  12. 12

    What is a CSV file, and how was it created in the example?

    Model answer

    A CSV file stores values separated by commas; the program created one by printing each student's fields with commas between them.

    Explanation

    Spreadsheet programs can interpret each comma-separated field as a separate column.

  13. 13

    Why do CSV files become less useful as the amount of data grows?

    Model answer

    Searching increasingly large CSV files becomes slower, so databases are a more powerful solution for managing larger datasets.

    Explanation

    A CSV file is a simple storage format rather than a system optimized for efficient searching and data management.

  14. 14

    What is the text segment of a program's memory?

    Model answer

    The text segment contains the compiled machine instructions of the program, represented as zeros and ones.

    Explanation

    Despite its name, the text segment does not contain ordinary human-readable text.

  15. 15

    What is stored in initialized and uninitialized data segments?

    Model answer

    They store global variables; initialized data contains globals given values, while uninitialized data contains globals without explicitly assigned values.

    Explanation

    The compiler places these two categories of global variables in separate memory areas.

  16. 16

    What are environment variables used for?

    Model answer

    They store values such as usernames, passwords, or configuration data that programs need to access without placing those values directly in the source code.

    Explanation

    The operating system or server makes environment variables available to the program.

  17. 17

    What is the stack used for during function calls?

    Model answer

    Each called function receives a stack frame containing its parameters, local variables, and related execution information.

    Explanation

    A new frame is placed on the stack when a function is called and becomes available for reuse when the function returns.

  18. 18

    Why did the original swap function change a and b but not x and y?

    Model answer

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

    Explanation

    The local parameters occupied a different stack frame from x and y.

  19. 19

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

    Model answer

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

    Explanation

    The function operates on the caller's data rather than on copies of the values.

  20. 20

    What does int *p declare?

    Model answer

    It declares p as a pointer that stores the address of an integer.

    Explanation

    The asterisk in a declaration indicates that the variable stores an address rather than an ordinary integer value.

  21. 21

    What does the expression *p mean when p is an integer pointer?

    Model answer

    It means to follow the address stored in p and access the integer located there.

    Explanation

    The asterisk is the dereference operator when used with an existing pointer.

  22. 22

    What is the difference between assigning y = x and assigning *y = *x when x and y are pointers?

    Model answer

    y = x copies the address stored in x, whereas *y = *x copies the value located at the address stored in x into the location pointed to by y.

    Explanation

    The first operation changes where y points; the second changes data in memory.

  23. 23

    Why can a function's local variables not safely be returned by address?

    Model answer

    Their stack frame becomes invalid for reliable use after the function returns, so the returned address refers to memory that may be reused or overwritten.

    Explanation

    The bytes may remain physically present, but their contents are no longer trustworthy.

  24. 24

    What are garbage values in C?

    Model answer

    Garbage values are leftover bit patterns in memory that has not been initialized by the current program operation.

    Explanation

    C generally does not spend time automatically clearing reused stack memory.

  25. 25

    What is the main difference between stack and heap memory?

    Model answer

    The stack provides short-term storage for function calls and local variables, while the heap provides dynamically allocated storage that can remain available until explicitly freed.

    Explanation

    Heap allocation is appropriate when data must outlive the function that created it.

  26. 26

    Why does GetString need to store its result on the heap rather than in its stack frame?

    Model answer

    The string must remain valid after GetString returns, but memory in its stack frame is no longer reliable after the function ends.

    Explanation

    Heap memory can remain allocated until the program explicitly releases it.

  27. 27

    What does malloc return?

    Model answer

    malloc allocates a requested number of bytes on the heap and returns the address of the allocated block.

    Explanation

    The returned address is typically stored in a pointer.

  28. 28

    What is a memory leak?

    Model answer

    A memory leak occurs when allocated heap memory is no longer needed but is not released with free.

    Explanation

    Repeated leaks can waste available memory and eventually slow or terminate a program.

  29. 29

    How does calloc differ from malloc?

    Model answer

    calloc allocates memory like malloc but also initializes every byte in the allocated block to zero.

    Explanation

    The clearing behavior can prevent uninitialized values without requiring a separate loop.

  30. 30

    What does realloc do?

    Model answer

    realloc changes the size of an allocated memory block, copying the data to a new location if necessary and releasing the old block.

    Explanation

    It can be used to grow or shrink dynamically allocated storage.

  31. 31

    Why does dereferencing an uninitialized pointer cause a segmentation fault?

    Model answer

    An uninitialized pointer contains an unpredictable address, so dereferencing it may access memory the program is not permitted to use.

    Explanation

    Declaring a pointer allocates space for the address itself but does not allocate the memory to which it should point.

  32. 32

    What is a segmentation fault?

    Model answer

    A segmentation fault occurs when a program accesses a memory region it is not allowed to access.

    Explanation

    Dereferencing invalid pointers and writing beyond allocated memory are common causes.

  33. 33

    How can the statement y = x fix a pointer example involving x and y?

    Model answer

    It makes y store the same valid address as x, so both pointers refer to the same allocated memory block.

    Explanation

    The assignment copies an address; it does not allocate a second block.

  34. 34

    What is a stack overflow in the technical sense?

    Model answer

    A stack overflow occurs when excessive function calls or local storage exhaust the stack and cause the program to access neighboring memory incorrectly.

    Explanation

    The stack can grow into memory used by the heap or other program data.

  35. 35

    What is a heap overflow?

    Model answer

    A heap overflow occurs when excessive dynamic allocation or an invalid write causes heap data to extend into memory that does not belong to the allocation.

    Explanation

    The heap can grow toward the stack, and incorrect access can corrupt other memory.

  36. 36

    What is a buffer?

    Model answer

    A buffer is a region of memory used to store data, such as an array of characters or downloaded video bytes.

    Explanation

    Strings, arrays, and video playback storage can all function as buffers.

  37. 37

    Why does video buffering occur?

    Model answer

    Buffering occurs when a video player has consumed the downloaded bytes in its memory buffer and must wait for more data to arrive.

    Explanation

    A sufficiently full buffer lets playback continue temporarily even if the network connection slows.

  38. 38

    What does Valgrind help programmers detect?

    Model answer

    Valgrind detects memory-related problems such as invalid reads or writes and memory leaks.

    Explanation

    It is especially useful for finding errors that are difficult to identify by inspecting a large program manually.

  39. 39

    What does Valgrind mean by an invalid write of size four in an integer program?

    Model answer

    It means the program attempted to write four bytes, the typical size of an integer in the example environment, to an invalid memory location.

    Explanation

    The invalid location may be outside an allocated array or block.

  40. 40

    What is hexadecimal notation commonly used to represent in debugging output?

    Model answer

    It is commonly used to represent memory addresses and other numerical values compactly.

    Explanation

    A hexadecimal address represents the same underlying number as its decimal or binary form.

  41. 41

    What is rubber-duck debugging?

    Model answer

    Rubber-duck debugging is the practice of explaining code aloud to an object or person so that inconsistencies in one's reasoning become apparent.

    Explanation

    Verbalizing what the code should do can reveal differences between intended and actual behavior.

  42. 42

    Why is writing to x[10] invalid when x points to space for ten integers?

    Model answer

    The valid indices are x[0] through x[9], so x[10] writes one integer beyond the allocated block.

    Explanation

    C arrays use zero-based indexing, and the out-of-range write is a buffer overflow.

  43. 43

    What is the main danger of copying an unchecked command-line string into a fixed-size array?

    Model answer

    If the input is longer than the array, the copy overwrites adjacent memory, potentially including a function's return address.

    Explanation

    The program must verify that the input length fits within the destination buffer.

  44. 44

    How can a buffer overflow become a security exploit?

    Model answer

    An attacker can overwrite control data such as a return address and redirect execution to attacker-supplied machine instructions.

    Explanation

    Because code and data are ultimately represented as bits, carefully crafted input may be interpreted as executable code.

  45. 45

    Why is overwriting a return address particularly dangerous?

    Model answer

    The return address tells a function where execution should resume, so changing it can make the program jump to an unintended location.

    Explanation

    An attacker may redirect execution to malicious instructions rather than back to the caller.

  46. 46

    What fundamental programming practice helps prevent buffer overflow exploits?

    Model answer

    Programs must check array boundaries and ensure that the amount of copied or written data fits within the allocated memory.

    Explanation

    Out-of-bounds access is the underlying error that makes these exploits possible.

  47. 47

    Why is an array's fixed, contiguous layout inconvenient when it must grow?

    Model answer

    The program may need to allocate a larger contiguous block, copy all existing elements, and release the old block.

    Explanation

    The copying takes linear time and may temporarily require additional memory.

  48. 48

    What is memory fragmentation, and why can it prevent a larger array allocation?

    Model answer

    Fragmentation occurs when free memory is divided into separated pieces; even if the total free space is sufficient, there may be no single contiguous block large enough for the array.

    Explanation

    Arrays require their elements to occupy one contiguous region.

  49. 49

    What is a linked list?

    Model answer

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

    Explanation

    The nodes can be located anywhere in memory because pointers connect them logically.

  50. 50

    What fields does a typical singly linked-list node contain?

    Model answer

    It contains a data value and a pointer named something like next that stores the address of the next node.

    Explanation

    The pointer replaces the implicit next-position relationship provided by an array.

  51. 51

    Why does a linked list require a special pointer such as first or head?

    Model answer

    The pointer stores the address of the first node, allowing the program to locate the rest of the list by following next pointers.

    Explanation

    Unlike an array, the list has no single contiguous base region that can be indexed directly.

  52. 52

    What value should the next pointer of the final linked-list node contain?

    Model answer

    It should contain NULL, indicating that there is no next node.

    Explanation

    A NULL pointer marks the end of the list.

  53. 53

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

    Model answer

    A linked list can grow or shrink by allocating or freeing individual nodes without reallocating and copying the entire collection.

    Explanation

    Only the relevant pointers need to be changed when nodes are added or removed.

  54. 54

    Why can inserting at the beginning of an unsorted linked list take constant time?

    Model answer

    The program only needs to make the new node point to the current first node and update the head pointer to the new node.

    Explanation

    The number of existing nodes does not affect these pointer updates.

  55. 55

    Why does inserting into a sorted linked list generally take linear time?

    Model answer

    The program may need to follow the list from the beginning until it finds the correct insertion position.

    Explanation

    Linked lists do not support direct indexing, so the search proceeds through nodes one at a time.

  56. 56

    How does the arrow operator differ from the dot operator in C?

    Model answer

    The dot operator accesses a field directly through a structure, while the arrow operator follows a pointer to a structure and accesses one of its fields.

    Explanation

    For a pointer p to a structure, p->field is equivalent in meaning to (*p).field.

  57. 57

    How can a program free an entire linked list safely?

    Model answer

    It should save the current node's next pointer, free the current node, and then continue with the saved pointer until the pointer is NULL.

    Explanation

    Saving next first prevents the program from losing access to the remainder of the list.

  58. 58

    What is a major trade-off of linked lists compared with arrays?

    Model answer

    Linked lists provide flexible growth and insertion but use extra memory for pointers and require more complex code and sequential traversal.

    Explanation

    Arrays provide direct indexing and compact storage but have less flexible resizing.

  59. 59

    What does realloc allow a dynamic array implementation to do?

    Model answer

    It allows the program to resize an existing allocation, such as increasing the capacity by one or more elements.

    Explanation

    When necessary, realloc moves the data to a larger block and returns the new address.

  60. 60

    What is technical debt in software development?

    Model answer

    Technical debt is the future cost created by choosing a quick or poorly designed implementation instead of a cleaner solution.

    Explanation

    The short-term time savings may lead to later debugging, maintenance, or redesign work.

  61. 61

    What is the defining behavior of a stack data structure?

    Model answer

    A stack is a LIFO structure: the last item pushed is the first item popped.

    Explanation

    The behavior resembles a stack of trays, where the top tray is removed first.

  62. 62

    What are the conventional operations on a stack?

    Model answer

    The conventional operations are push, which adds an item, and pop, which removes the most recently added item.

    Explanation

    The operations describe the interface independently of the implementation.

  63. 63

    How can a stack be implemented with an array?

    Model answer

    It can store elements in an array while tracking the current size and a maximum capacity.

    Explanation

    The implementation must prevent the size from exceeding the allocated capacity.

  64. 64

    What is a disadvantage of implementing a stack with a fixed-size array?

    Model answer

    The stack cannot store more elements than its predetermined capacity without allocating additional storage or using another structure.

    Explanation

    A fixed capacity may waste space or prevent growth.

  65. 65

    What is the defining behavior of a queue data structure?

    Model answer

    A queue is a FIFO structure: the first item inserted is the first item removed.

    Explanation

    This behavior models a line in which the person who arrives first is served first.

  66. 66

    What are the conventional operations on a queue?

    Model answer

    The conventional operations are enqueue, which adds an item, and dequeue, which removes the oldest item.

    Explanation

    The names may vary in specific programs, but the FIFO behavior remains the same.

  67. 67

    Why should a queue track the position of its front?

    Model answer

    Tracking the front allows the next item to be removed without shifting every remaining element forward.

    Explanation

    Avoiding repeated shifts improves the efficiency of queue operations.

  68. 68

    How does an API separate a data structure's design from its implementation?

    Model answer

    An API specifies the operations and expected behavior, while hiding whether those operations are implemented with an array, linked list, or another structure.

    Explanation

    Users can rely on documented operations without needing to know the underlying representation.

  69. 69

    What is the root of a tree?

    Model answer

    The root is the topmost node of a tree and the node from which the rest of the structure can be reached.

    Explanation

    A tree is often accessed through a pointer to its root.

  70. 70

    What is a leaf in a tree?

    Model answer

    A leaf is a node with no children.

    Explanation

    In a pointer representation, a leaf's child pointers are NULL.

  71. 71

    What makes a tree a binary tree?

    Model answer

    Each node in a binary tree has at most two children.

    Explanation

    A node may have zero, one, or two children.

  72. 72

    What property makes a binary tree a binary search tree?

    Model answer

    For every node, values in the left subtree are smaller and values in the right subtree are larger.

    Explanation

    The property applies recursively to every subtree.

  73. 73

    How does searching a balanced binary search tree achieve logarithmic time?

    Model answer

    Each comparison eliminates approximately half of the remaining tree, producing a search time of O(log⁡n)O(\log n)O(logn).

    Explanation

    The process is analogous to binary search on a sorted array.

  74. 74

    Why can a binary search tree degrade to linear search time?

    Model answer

    If the tree is badly unbalanced, each node may have only one child, making the structure resemble a linked list.

    Explanation

    In that case, searching may require visiting nearly every node.

  75. 75

    What are the three recursive cases when searching a binary search tree?

    Model answer

    Return false for a NULL tree, search left when the target is smaller, search right when it is larger, and return true when it equals the current node.

    Explanation

    These cases reduce the problem to progressively smaller subtrees.

  76. 76

    How are the children represented in a binary-tree node structure?

    Model answer

    The node contains its data and two pointers, commonly named left and right, each pointing to another node or NULL.

    Explanation

    The pointers connect the node to its two possible subtrees.

  77. 77

    What is a hash function?

    Model answer

    A hash function maps an input value to a hash value or bucket index used to organize and locate data.

    Explanation

    For example, a name's first letter can determine which bucket stores it.

  78. 78

    What is a collision in a hash table?

    Model answer

    A collision occurs when two different inputs produce the same hash value or bucket index.

    Explanation

    A hash-table design must provide a way to store and retrieve both values.

  79. 79

    How does linear probing handle hash collisions?

    Model answer

    It searches sequentially through later positions until it finds an available slot.

    Explanation

    Although this uses array space efficiently, clusters can form and searches may approach linear time.

  80. 80

    What is separate chaining?

    Model answer

    Separate chaining stores an array of buckets in which each bucket points to a linked list of values that hash to that bucket.

    Explanation

    Colliding values are kept together in the same chain rather than moved to unrelated array positions.

  81. 81

    Why does a uniform hash function improve a hash table?

    Model answer

    It distributes values relatively evenly among buckets, keeping the chains short and making lookups faster in practice.

    Explanation

    Uneven distribution creates long chains and reduces the benefit of hashing.

  82. 82

    What is the practical advantage of using many buckets in a hash table?

    Model answer

    More buckets can divide the data into smaller groups, reducing the average number of items examined during a lookup.

    Explanation

    The improvement may be substantial in real elapsed time even if the asymptotic classification remains unchanged.

  83. 83

    What is a trie?

    Model answer

    A trie is a tree-like structure that stores strings by following a sequence of character-indexed pointers.

    Explanation

    Each level represents a character position in the stored words.

  84. 84

    How is a word such as Maxwell stored in a trie?

    Model answer

    The program follows or creates a pointer for M, then a, then x, and so on through every character, marking the endpoint as a complete word.

    Explanation

    The characters are represented by the sequence of choices rather than being stored as one ordinary string at a single node.

  85. 85

    What determines the lookup time for a word in a trie?

    Model answer

    Lookup takes time proportional to the word's number of characters, such as seven character steps for Maxwell, regardless of how many other words are stored.

    Explanation

    Adding more words does not increase the number of character steps needed for an existing word.

  86. 86

    Why can a trie provide effectively constant-time lookup?

    Model answer

    If the maximum word length is bounded, lookup depends on that bounded length rather than on the total number of stored words, so it is treated as O(1)O(1)O(1) in practice.

    Explanation

    The advantage comes at the cost of using substantially more memory.

1 of 86
Question 1 of 86

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent