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

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

What is the primary purpose of a struct in C?

78 questions

  1. 1

    What is the primary purpose of a struct in C?

    1. A

      To combine related values of potentially different data types

    2. B

      To replace every function with a single data structure

    3. C

      To allocate memory only for dynamically sized arrays

    4. D

      To force all variables to use the same data type

    Correct answer
    A

    To combine related values of potentially different data types

    Explanation

    A struct lets programmers define a custom data type that groups related fields, such as a student's name and dorm.

  2. 2

    In the definition of a student structure containing name and dorm, what does each field represent?

    1. A

      A separate piece of information stored within one student object

    2. B

      A separate student stored within the same array position

    3. C

      A function that retrieves information from the user

    4. D

      A pointer that automatically references the next student

    Correct answer
    A

    A separate piece of information stored within one student object

    Explanation

    The fields name and dorm are separate members contained within each student structure.

  3. 3

    Why are structure definitions commonly placed in header files?

    1. A

      Header files ensure that every structure uses dynamic memory

    2. B

      Header files prevent structures from containing character strings

    3. C

      Header files automatically convert structures into executable code

    4. D

      Header files allow the definition to be shared across multiple programs

    Correct answer
    D

    Header files allow the definition to be shared across multiple programs

    Explanation

    Putting a structure definition in a .h file makes it available to multiple source files through inclusion.

  4. 4

    What does the dot operator do when used with a structure variable?

    1. A

      It follows a pointer to a different structure

    2. B

      It accesses a specific field inside that structure

    3. C

      It compares two structures for equality

    4. D

      It allocates memory for the entire structure

    Correct answer
    B

    It accesses a specific field inside that structure

    Explanation

    For example, student.name accesses the name field inside the student structure.

  5. 5

    What does declaring an array with enrollment elements accomplish after the program reads the enrollment value?

    1. A

      It guarantees that the user will enter that many students

    2. B

      It changes the type of the enrollment variable

    3. C

      It creates one student with that many fields

    4. D

      It creates space for the specified number of students

    Correct answer
    D

    It creates space for the specified number of students

    Explanation

    Once the number of students is known, it can be used as the array's size.

  6. 6

    What does fopen return when a file is successfully opened?

    1. A

      The entire contents of the file as a string

    2. B

      A file pointer representing the open file

    3. C

      A Boolean value that is always equal to true

    4. D

      The number of bytes currently in the file

    Correct answer
    B

    A file pointer representing the open file

    Explanation

    fopen returns a FILE *, which represents the opened file and can be used by other file functions.

  7. 7

    What does the mode `

    1. A

      It opens the file only if it already exists

    2. B

      It opens the file for appending without changes

    3. C

      It opens the file for writing

    4. D

      It opens the file only for reading

    Correct answer
    C

    It opens the file for writing

    Explanation

    The w mode indicates writing. The lecture contrasts it with r, which indicates reading.

  8. 8

    Why should a program check whether a file pointer is NULL after calling fopen?

    1. A

      The file may not have been opened successfully

    2. B

      The file pointer may contain a valid address that must be doubled

    3. C

      The file pointer must be converted into an integer first

    4. D

      The file may contain too many comma-separated values

    Correct answer
    A

    The file may not have been opened successfully

    Explanation

    NULL indicates that no valid file address was returned, possibly because the file does not exist, permissions are lacking, or memory is unavailable.

  9. 9

    How does fprintf differ from printf in the file example?

    1. A

      fprintf automatically sorts the data before displaying it

    2. B

      fprintf reads from a file while printf writes to the screen

    3. C

      fprintf prints only integers while printf prints strings

    4. D

      fprintf receives a file pointer and writes to that file

    Correct answer
    D

    fprintf receives a file pointer and writes to that file

    Explanation

    fprintf works like printf, but its first argument identifies the destination file.

  10. 10

    What is the purpose of calling fclose after writing to a file?

    1. A

      It closes the file and completes the saving process

    2. B

      It prints the file's contents to the screen

    3. C

      It deletes the file from the computer

    4. D

      It converts the file into a binary executable

    Correct answer
    A

    It closes the file and completes the saving process

    Explanation

    fclose closes the open file; in the lecture, this is described as effectively completing the save.

  11. 11

    Why is a CSV file described as a simplistic database format?

    1. A

      It stores data only in encrypted binary form

    2. B

      It prevents programs such as spreadsheets from reading the data

    3. C

      It organizes tabular values using separators such as commas

    4. D

      It automatically creates indexes for every stored value

    Correct answer
    C

    It organizes tabular values using separators such as commas

    Explanation

    CSV files represent rows and columns by separating values with commas, allowing spreadsheet programs to display them as tables.

  12. 12

    Why do databases eventually become preferable to large CSV files?

    1. A

      CSV files can only be opened once before becoming invalid

    2. B

      CSV files cannot store any character data

    3. C

      Databases require every record to have exactly two fields

    4. D

      Databases provide more powerful and efficient ways to search data

    Correct answer
    D

    Databases provide more powerful and efficient ways to search data

    Explanation

    As CSV files grow, searching them becomes increasingly slow, motivating the use of more powerful databases.

  13. 13

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

    1. A

      The compiled zeros and ones that make up the program

    2. B

      The dynamically allocated memory returned by malloc

    3. C

      The user names and passwords stored outside the program

    4. D

      The local variables created whenever a function runs

    Correct answer
    A

    The compiled zeros and ones that make up the program

    Explanation

    The text segment contains the compiled instructions of the program, represented as machine-code bits rather than ordinary ASCII text.

  14. 14

    Where are global variables generally stored in the memory layout described?

    1. A

      Only in the stack frames of called functions

    2. B

      Inside the return address of the calling function

    3. C

      In initialized or uninitialized data regions

    4. D

      Only in the heap after a call to malloc

    Correct answer
    C

    In initialized or uninitialized data regions

    Explanation

    Global variables are placed in initialized data if given initial values and uninitialized data otherwise.

  15. 15

    What is stored in an environment variable?

    1. A

      A duplicate copy of every file opened by the program

    2. B

      The next instruction that a function should execute after returning

    3. C

      The address of every local variable in the current stack frame

    4. D

      A value such as a password that a program can access without embedding it in source code

    Correct answer
    D

    A value such as a password that a program can access without embedding it in source code

    Explanation

    Environment variables can store values such as user names and passwords outside the program's source code.

  16. 16

    What generally happens when a function is called?

    1. A

      The function automatically allocates all memory from the heap

    2. B

      The function receives a new text segment containing all program instructions

    3. C

      The function receives a new stack frame for its local variables and parameters

    4. D

      The function permanently replaces the stack frame of its caller

    Correct answer
    C

    The function receives a new stack frame for its local variables and parameters

    Explanation

    Each called function receives a portion of stack memory for its parameters, local variables, and related information.

  17. 17

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

    1. A

      It used a temporary variable that could not store integers

    2. B

      It passed the variables by their addresses but ignored them

    3. C

      It attempted to modify the program's text segment

    4. D

      It changed copies of the values in its own stack frame

    Correct answer
    D

    It changed copies of the values in its own stack frame

    Explanation

    Without pointers, the function received copies of the values, so changing its local parameters did not change main's variables.

  18. 18

    What does a declaration such as int *p indicate?

    1. A

      It declares an array containing one integer pointer per element

    2. B

      It declares p as an integer whose value must always be multiplied

    3. C

      It declares a function that returns an integer automatically

    4. D

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

    Correct answer
    D

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

    Explanation

    The asterisk in the declaration indicates that p is a pointer to an int, meaning it stores an integer's address.

  19. 19

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

    1. A

      The number of bytes allocated for the pointer

    2. B

      The address of the pointer variable p itself

    3. C

      The next pointer declared after p

    4. D

      The value stored at the address contained in p

    Correct answer
    D

    The value stored at the address contained in p

    Explanation

    The dereference operator follows the address in p and accesses the value stored there.

  20. 20

    Why do pointers allow swap to modify variables in main?

    1. A

      They prevent swap from creating local parameters

    2. B

      They convert both variables into global variables

    3. C

      They copy the variables into a larger stack frame automatically

    4. D

      They give swap the addresses of the variables that must be changed

    Correct answer
    D

    They give swap the addresses of the variables that must be changed

    Explanation

    Passing addresses lets swap dereference those pointers and modify the original memory locations.

  21. 21

    What happens to a function's stack memory after the function finishes?

    1. A

      It is automatically converted into heap memory

    2. B

      It is permanently moved into the text segment

    3. C

      It is erased physically from the computer's hardware

    4. D

      It becomes available for reuse by later function calls

    Correct answer
    D

    It becomes available for reuse by later function calls

    Explanation

    The memory remains physically present but is no longer trustworthy as that function's storage and can be reused.

  22. 22

    What is a garbage value in an uninitialized local variable?

    1. A

      Residual bits left in memory by earlier uses of that memory

    2. B

      A special value that always represents zero

    3. C

      A pointer that automatically refers to the heap

    4. D

      A value inserted by the compiler to indicate a syntax error

    Correct answer
    A

    Residual bits left in memory by earlier uses of that memory

    Explanation

    C generally does not clear reused stack memory, so uninitialized variables may contain leftover bits from previous activity.

  23. 23

    Why would returning the address of a local array from a function be unsafe?

    1. A

      Addresses of arrays cannot be represented by pointers

    2. B

      Returning an address automatically frees the heap

    3. C

      The array's stack memory becomes available for reuse when the function returns

    4. D

      Local arrays are always stored in read-only memory

    Correct answer
    C

    The array's stack memory becomes available for reuse when the function returns

    Explanation

    Once the function returns, its stack frame is no longer trustworthy, so the returned address may refer to overwritten data.

  24. 24

    What is the main distinction between stack memory and heap memory in this lecture?

    1. A

      The stack supports short-term function storage, while the heap supports longer-lived allocations

    2. B

      The stack stores only machine instructions, while the heap stores only global variables

    3. C

      The stack persists until manually freed, while the heap disappears after each function call

    4. D

      The stack is dynamically allocated, while the heap is reserved only for function parameters

    Correct answer
    A

    The stack supports short-term function storage, while the heap supports longer-lived allocations

    Explanation

    Stack frames are associated with active function calls, whereas heap allocations can remain available until explicitly freed.

  25. 25

    What is the role of malloc?

    1. A

      It clears every byte of the entire computer's memory

    2. B

      It opens a file for writing

    3. C

      It allocates a requested amount of memory from the heap

    4. D

      It releases a previously allocated stack frame

    Correct answer
    C

    It allocates a requested amount of memory from the heap

    Explanation

    malloc allocates heap memory and returns its address.

  26. 26

    What distinguishes calloc from malloc?

    1. A

      calloc allocates memory and initializes the allocated bytes to zero

    2. B

      calloc increases an allocation without copying existing contents

    3. C

      calloc releases memory while malloc allocates it

    4. D

      calloc allocates memory only on the stack and never on the heap

    Correct answer
    A

    calloc allocates memory and initializes the allocated bytes to zero

    Explanation

    calloc is like malloc but clears the allocated memory, preventing initial garbage values.

  27. 27

    What is the purpose of realloc?

    1. A

      It converts a pointer into a structure field

    2. B

      It removes every pointer from a linked list

    3. C

      It changes the size of an existing memory allocation

    4. D

      It opens an existing file in read-only mode

    Correct answer
    C

    It changes the size of an existing memory allocation

    Explanation

    realloc can grow or otherwise resize a previously allocated block, handling relocation and copying when necessary.

  28. 28

    What can happen if a pointer is dereferenced before it has been assigned a valid address?

    1. A

      The program may access unauthorized memory and crash

    2. B

      The pointer automatically receives a valid heap address

    3. C

      The pointed-to integer is initialized to zero

    4. D

      The operating system converts the pointer into NULL

    Correct answer
    A

    The program may access unauthorized memory and crash

    Explanation

    An uninitialized pointer contains a garbage address, so dereferencing it can access memory the program does not own.

  29. 29

    Why does assigning y = x for two pointers not copy the pointed-to data?

    1. A

      It changes both pointers into ordinary integer variables

    2. B

      It frees the memory previously referenced by both pointers

    3. C

      It copies the address so both pointers refer to the same memory

    4. D

      It allocates a new block and copies every value into it

    Correct answer
    C

    It copies the address so both pointers refer to the same memory

    Explanation

    Pointer assignment copies the address. Both pointers then refer to the same memory location.

  30. 30

    What is a memory leak?

    1. A

      A pointer that refers to two structures at once

    2. B

      Allocated memory that is no longer needed but has not been freed

    3. C

      A file that has been closed before being written

    4. D

      A stack variable that contains a valid initialized value

    Correct answer
    B

    Allocated memory that is no longer needed but has not been freed

    Explanation

    If allocated heap memory is not released with free, it remains unavailable for reuse and can cause memory exhaustion.

  31. 31

    What is a buffer?

    1. A

      A function that automatically sorts an array

    2. B

      A special pointer that always stores NULL

    3. C

      A file mode used exclusively for reading

    4. D

      A chunk of memory used to store data or values

    Correct answer
    D

    A chunk of memory used to store data or values

    Explanation

    A buffer is a region of memory, often an array, used to hold data temporarily or permanently.

  32. 32

    What causes a buffer overflow?

    1. A

      Reading a value from the first valid position of an array

    2. B

      Writing more data into a buffer than the buffer can hold

    3. C

      Freeing memory after it is no longer needed

    4. D

      Allocating a buffer whose size matches the input

    Correct answer
    B

    Writing more data into a buffer than the buffer can hold

    Explanation

    A buffer overflow occurs when an operation writes beyond the allocated boundaries of a memory region.

  33. 33

    Why does copying a 13-byte string into a 12-byte array create a problem?

    1. A

      The thirteenth byte is written outside the array's valid bounds

    2. B

      The compiler converts the array into a linked list

    3. C

      The string loses its terminating null character before copying

    4. D

      The array automatically changes its size to 13 bytes

    Correct answer
    A

    The thirteenth byte is written outside the array's valid bounds

    Explanation

    A 12-byte array has valid positions from 0 through 11, so writing byte 12 exceeds its boundary.

  34. 34

    How can a buffer overflow become a security exploit?

    1. A

      An attacker may force the program to use only constant-time algorithms

    2. B

      An attacker may prevent the heap from storing any pointers

    3. C

      An attacker may cause every local variable to become automatically initialized

    4. D

      An attacker may overwrite a return address with data interpreted as executable instructions

    Correct answer
    D

    An attacker may overwrite a return address with data interpreted as executable instructions

    Explanation

    If input overwrites a return address and contains carefully crafted machine-code bits, control may be redirected to the attacker's code.

  35. 35

    What does a segmentation fault generally indicate in the pointer examples?

    1. A

      The program successfully freed every heap allocation

    2. B

      The program attempted to access memory it was not permitted to access

    3. C

      The program searched a sorted tree in logarithmic time

    4. D

      The program opened a CSV file in write mode

    Correct answer
    B

    The program attempted to access memory it was not permitted to access

    Explanation

    A segmentation fault occurs when a program touches an invalid or unauthorized memory location.

  36. 36

    What error exists when code writes to x[10] after allocating space for 10 integers?

    1. A

      It writes one integer beyond the valid range, which ends at x[9]

    2. B

      It writes to the first integer because arrays begin at index 10

    3. C

      It reads the tenth integer, which is always valid

    4. D

      It creates an additional integer without using extra memory

    Correct answer
    A

    It writes one integer beyond the valid range, which ends at x[9]

    Explanation

    For an array of 10 elements, valid indexes are 0 through 9. Index 10 is outside the allocation.

  37. 37

    What kind of problems is Valgrind designed to detect?

    1. A

      Only logical errors in sorting algorithms

    2. B

      Errors in the visual layout of spreadsheet columns

    3. C

      Only spelling errors in comments and variable names

    4. D

      Memory-related errors such as invalid accesses and leaks

    Correct answer
    D

    Memory-related errors such as invalid accesses and leaks

    Explanation

    Valgrind helps identify invalid memory operations, memory leaks, and related memory-management problems.

  38. 38

    What does an “invalid write of size 4” message commonly mean for an integer array?

    1. A

      A four-byte integer was written to an invalid memory location

    2. B

      The program successfully wrote four integers into one array position

    3. C

      The program read four bytes from a valid integer location

    4. D

      The program allocated exactly four additional array elements

    Correct answer
    A

    A four-byte integer was written to an invalid memory location

    Explanation

    An int commonly occupies four bytes in the described environment, so the message indicates an invalid four-byte write.

  39. 39

    What does hexadecimal notation such as 0x... commonly represent in debugging output?

    1. A

      A compact representation of a number, often a memory address

    2. B

      A decimal representation of a file's line count

    3. C

      A special notation used only for Boolean values

    4. D

      A character encoding that cannot represent addresses

    Correct answer
    A

    A compact representation of a number, often a memory address

    Explanation

    Debuggers and memory tools commonly display addresses in hexadecimal notation.

  40. 40

    How does rubber duck debugging help a programmer?

    1. A

      The duck detects memory leaks without access to the source code

    2. B

      The duck automatically executes the program with corrected pointers

    3. C

      Explaining code aloud increases the allocated buffer size

    4. D

      Explaining the code aloud can reveal inconsistencies in the programmer's reasoning

    Correct answer
    D

    Explaining the code aloud can reveal inconsistencies in the programmer's reasoning

    Explanation

    Verbalizing what the code does can make a mismatch between the intended and actual behavior apparent.

  41. 41

    Why can dynamically resizing an array be inefficient?

    1. A

      Array resizing can occur only when the program has no variables

    2. B

      A new allocation always deletes the existing elements without copying

    3. C

      A new allocation may require copying all existing elements, an O(n)O(n)O(n) operation

    4. D

      Array resizing requires searching every possible memory address

    Correct answer
    C

    A new allocation may require copying all existing elements, an O(n)O(n)O(n) operation

    Explanation

    When an array must move to a larger block, its existing elements must be copied, requiring linear time.

  42. 42

    What problem does memory fragmentation create for arrays?

    1. A

      Every free byte is automatically converted into a pointer

    2. B

      Enough total free memory may exist, but not in one contiguous block

    3. C

      Fragmentation guarantees that all allocations are constant time

    4. D

      Arrays become unable to store values of the same data type

    Correct answer
    B

    Enough total free memory may exist, but not in one contiguous block

    Explanation

    Arrays require contiguous memory, so separated free regions may not support one large array allocation.

  43. 43

    What is the defining feature of a linked list?

    1. A

      Each node stores data and a pointer to another node

    2. B

      The list can be accessed directly using array indexes

    3. C

      Every node contains exactly two numeric values and no pointers

    4. D

      Every element must occupy adjacent memory locations

    Correct answer
    A

    Each node stores data and a pointer to another node

    Explanation

    A linked list connects nodes through pointers, allowing nodes to be located anywhere in memory.

  44. 44

    What does the next field in a linked-list node store?

    1. A

      The index of the current node in a contiguous array

    2. B

      The address of the program's text segment

    3. C

      The number of elements remaining in the list

    4. D

      The address of the next node in the list

    Correct answer
    D

    The address of the next node in the list

    Explanation

    The next pointer links the current node to the following node.

  45. 45

    Why does a linked list not require contiguous memory?

    1. A

      The operating system automatically moves every node next to the previous one

    2. B

      Linked lists do not use memory for their stored values

    3. C

      Pointers connect nodes even when the nodes are located in separate memory regions

    4. D

      Each node is stored inside the stack frame of the first node

    Correct answer
    C

    Pointers connect nodes even when the nodes are located in separate memory regions

    Explanation

    The links between nodes preserve the logical order even when the nodes are physically separated in memory.

  46. 46

    What special pointer is used to identify the beginning of a linked list?

    1. A

      A pointer that always stores the address of the final node

    2. B

      A pointer that refers to the middle node for faster indexing

    3. C

      A pointer that stores the number of nodes as an integer

    4. D

      A pointer such as first or head that stores the first node's address

    Correct answer
    D

    A pointer such as first or head that stores the first node's address

    Explanation

    Because linked-list nodes are not indexed like array elements, the list is accessed through a pointer to its first node.

  47. 47

    What should the next pointer of the final node in a linked list contain?

    1. A

      The number of nodes currently stored in the list

    2. B

      The address of the first node, regardless of list structure

    3. C

      NULL, indicating that there is no following node

    4. D

      A garbage value indicating that the list is complete

    Correct answer
    C

    NULL, indicating that there is no following node

    Explanation

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

  48. 48

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

    1. A

      O(n2)O(n^2)O(n2) because every pair of nodes must be compared

    2. B

      O(1)O(1)O(1) because only a constant number of pointers must be updated

    3. C

      O(log⁡n)O(\log n)O(logn) because the list must be divided in half

    4. D

      O(n)O(n)O(n) because every existing node must be copied

    Correct answer
    B

    O(1)O(1)O(1) because only a constant number of pointers must be updated

    Explanation

    Insertion at the beginning requires updating the new node's pointer and the list's first pointer, regardless of list length.

  49. 49

    Why can inserting into a sorted linked list require O(n)O(n)O(n) time?

    1. A

      The new node must be allocated on the stack rather than the heap

    2. B

      Every node must be copied into a contiguous array

    3. C

      The list may need to be traversed to locate the correct position

    4. D

      The list must always be sorted using binary search

    Correct answer
    C

    The list may need to be traversed to locate the correct position

    Explanation

    Maintaining sorted order may require walking through many nodes before finding the insertion point.

  50. 50

    What is the main trade-off of using a linked list instead of an array?

    1. A

      Linked lists eliminate all traversal time but cannot store integers

    2. B

      Linked lists use less memory but cannot grow after initialization

    3. C

      Linked lists gain flexible growth but use extra pointer memory and more complex code

    4. D

      Linked lists provide direct indexing but require contiguous allocation

    Correct answer
    C

    Linked lists gain flexible growth but use extra pointer memory and more complex code

    Explanation

    Linked lists support dynamic growth and avoid shifting or copying all elements, but pointers consume memory and complicate operations.

  51. 51

    Why must a linked list be traversed using pointers rather than array indexing?

    1. A

      Its nodes may be separated in memory and therefore lack meaningful numeric indexes

    2. B

      Its nodes are always stored in reverse order

    3. C

      Array indexing works only for character data

    4. D

      Pointers cannot refer to structures stored in arrays

    Correct answer
    A

    Its nodes may be separated in memory and therefore lack meaningful numeric indexes

    Explanation

    A linked list is not a contiguous array, so traversal follows the pointers from node to node.

  52. 52

    When freeing an entire linked list, why should the next pointer be saved before freeing the current node?

    1. A

      The next pointer always contains a copy of the current node's data

    2. B

      Saving the pointer changes the list into an array

    3. C

      Freeing the current node makes its stored pointer unsafe to use afterward

    4. D

      Freeing a node automatically frees every later node

    Correct answer
    C

    Freeing the current node makes its stored pointer unsafe to use afterward

    Explanation

    After freeing the current node, its fields should no longer be accessed, so the address of the next node must be saved first.

  53. 53

    What is technical debt in software development?

    1. A

      The future cost created by choosing quick, poorly designed solutions

    2. B

      The number of pointers contained in a linked list

    3. C

      The amount of heap memory returned by free

    4. D

      The time required to execute a constant-time algorithm

    Correct answer
    A

    The future cost created by choosing quick, poorly designed solutions

    Explanation

    Shortcuts may save time initially but create additional maintenance, debugging, or redesign work later.

  54. 54

    What does LIFO mean for a stack?

    1. A

      The first item inserted is the first item removed

    2. B

      The largest item inserted is the first item removed

    3. C

      Items are removed in alphabetical order

    4. D

      The last item inserted is the first item removed

    Correct answer
    D

    The last item inserted is the first item removed

    Explanation

    A stack follows last in, first out: the most recently pushed item is popped first.

  55. 55

    Which operations are conventionally associated with a stack?

    1. A

      malloc to add an item and free to remove an item

    2. B

      enqueue to add an item and dequeue to remove an item

    3. C

      insert to add an item and search to remove an item

    4. D

      push to add an item and pop to remove an item

    Correct answer
    D

    push to add an item and pop to remove an item

    Explanation

    Stacks conventionally use push and pop, although an implementation could choose different names.

  56. 56

    What limitation results from implementing a stack with a fixed-size array?

    1. A

      The stack automatically requires a linked list for every operation

    2. B

      The stack cannot store integers or other simple values

    3. C

      The stack cannot hold more items than its predefined capacity

    4. D

      The stack must remove its oldest item first

    Correct answer
    C

    The stack cannot hold more items than its predefined capacity

    Explanation

    A fixed array provides only a predetermined amount of storage unless the implementation is changed or resized.

  57. 57

    What does FIFO mean for a queue?

    1. A

      The largest item inserted is the first item removed

    2. B

      Items are removed from the position with the most memory

    3. C

      The last item inserted is the first item removed

    4. D

      The first item inserted is the first item removed

    Correct answer
    D

    The first item inserted is the first item removed

    Explanation

    A queue follows first in, first out, like people being served in line.

  58. 58

    Why should an efficient array-based queue track the position of the front?

    1. A

      It ensures that the queue removes the newest item first

    2. B

      It allows the queue to avoid storing its size

    3. C

      It can remove the front item without shifting every remaining item

    4. D

      It converts the queue into a binary search tree

    Correct answer
    C

    It can remove the front item without shifting every remaining item

    Explanation

    Tracking the front avoids the unnecessary cost of moving all remaining elements forward after each dequeue.

  59. 59

    What is an API in the context of a data structure?

    1. A

      The physical arrangement of every byte inside the data structure

    2. B

      The compiler's private representation of all local variables

    3. C

      The set of operations and behaviors exposed to users of the implementation

    4. D

      The number of lines used to implement the data structure

    Correct answer
    C

    The set of operations and behaviors exposed to users of the implementation

    Explanation

    An API exposes operations such as push and pop while hiding implementation details.

  60. 60

    What is the root of a tree?

    1. A

      The topmost node from which the rest of the tree can be reached

    2. B

      The node with the largest stored value

    3. C

      The final node visited during a search

    4. D

      Any node that has no children and no parent

    Correct answer
    A

    The topmost node from which the rest of the tree can be reached

    Explanation

    The root is the starting node of a tree and is analogous to the first pointer in a linked list.

  61. 61

    What is a leaf in a tree?

    1. A

      A pointer that stores the tree's height

    2. B

      A node with no children

    3. C

      A node that must have exactly two children

    4. D

      The node at the top of the tree

    Correct answer
    B

    A node with no children

    Explanation

    Leaves are nodes at the bottom of a tree that have no outgoing child links.

  62. 62

    What makes a tree a binary tree?

    1. A

      Each node has at most two children

    2. B

      The tree must always be perfectly balanced

    3. C

      The tree must contain exactly two nodes

    4. D

      Every node must contain two integers

    Correct answer
    A

    Each node has at most two children

    Explanation

    A binary tree permits each node to have zero, one, or two children.

  63. 63

    What additional property makes a binary tree a binary search tree?

    1. A

      Values in the left subtree are smaller and values in the right subtree are larger

    2. B

      Every node has exactly two children with equal values

    3. C

      The tree must contain the same number of nodes on every level

    4. D

      All values are stored in one contiguous array

    Correct answer
    A

    Values in the left subtree are smaller and values in the right subtree are larger

    Explanation

    A binary search tree maintains the ordering property that smaller values go left and larger values go right at every node.

  64. 64

    What is the search time for a balanced binary search tree?

    1. A

      O(log⁡n)O(\log n)O(logn) because each comparison can eliminate roughly half the remaining tree

    2. B

      O(n)O(n)O(n) because every node must always be examined

    3. C

      O(n2)O(n^2)O(n2) because each node must be compared with every other node

    4. D

      O(1)O(1)O(1) because every node has a direct numeric index

    Correct answer
    A

    O(log⁡n)O(\log n)O(logn) because each comparison can eliminate roughly half the remaining tree

    Explanation

    In a balanced binary search tree, each decision chooses one of two substantially smaller subtrees, producing logarithmic search time.

  65. 65

    Why can a binary search tree have linear search time despite its search property?

    1. A

      The left and right pointers prevent comparisons between values

    2. B

      Binary search trees cannot store more than two values

    3. C

      Leaves always contain duplicate values that require repeated searches

    4. D

      If the tree becomes highly unbalanced, it can resemble a linked list

    Correct answer
    D

    If the tree becomes highly unbalanced, it can resemble a linked list

    Explanation

    A poorly shaped binary search tree may have nodes arranged almost entirely along one path, requiring linear traversal.

  66. 66

    Why are the left and right fields of a tree leaf set to NULL?

    1. A

      A leaf has no child nodes to which those pointers could refer

    2. B

      A leaf stores its value only in the text segment

    3. C

      A leaf must always point back to the root

    4. D

      A leaf is not allowed to contain a structure field

    Correct answer
    A

    A leaf has no child nodes to which those pointers could refer

    Explanation

    Since leaves have no children, both child pointers are null.

  67. 67

    What is hashing?

    1. A

      Copying an array into a larger contiguous array

    2. B

      Using a function to map an input to a bucket or hash value

    3. C

      Sorting every input by repeatedly comparing adjacent elements

    4. D

      Following pointers until a null value is reached

    Correct answer
    B

    Using a function to map an input to a bucket or hash value

    Explanation

    A hash function examines an input and produces a value identifying the bucket where the input should be placed.

  68. 68

    What is a collision in a hash table?

    1. A

      A pointer becomes NULL at the end of a linked list

    2. B

      Two different inputs map to the same bucket

    3. C

      A tree node has both a left and right child

    4. D

      An array contains fewer elements than its capacity

    Correct answer
    B

    Two different inputs map to the same bucket

    Explanation

    A collision occurs when multiple inputs produce the same hash value.

  69. 69

    How does linear probing handle a hash-table collision?

    1. A

      It creates a new linked list for every input immediately

    2. B

      It recursively divides the table into two balanced trees

    3. C

      It discards the new input and keeps only the earlier one

    4. D

      It searches subsequent positions until it finds an available slot

    Correct answer
    D

    It searches subsequent positions until it finds an available slot

    Explanation

    Linear probing checks positions sequentially until it finds an empty location.

  70. 70

    What is separate chaining in a hash table?

    1. A

      Using a linked list at each bucket to store colliding inputs

    2. B

      Searching every bucket in order after each insertion

    3. C

      Storing only one input per bucket and rejecting collisions

    4. D

      Using a second array to replace every occupied bucket

    Correct answer
    A

    Using a linked list at each bucket to store colliding inputs

    Explanation

    Separate chaining combines an array of buckets with linked lists that hold values sharing a hash value.

  71. 71

    Why should a hash function distribute inputs uniformly?

    1. A

      Uniform distribution guarantees that collisions never occur

    2. B

      Uniform distribution eliminates the need for memory allocation

    3. C

      Uniform distribution keeps the chains or buckets from becoming unnecessarily long

    4. D

      Uniform distribution ensures that every input has the same number of characters

    Correct answer
    C

    Uniform distribution keeps the chains or buckets from becoming unnecessarily long

    Explanation

    If values are spread evenly, searches examine shorter chains on average.

  72. 72

    What is the practical benefit of dividing data among many hash-table buckets?

    1. A

      Each search may examine only a fraction of the total data

    2. B

      The table no longer requires any memory for pointers

    3. C

      Every search becomes independent of the input's value

    4. D

      All collisions are eliminated regardless of the hash function

    Correct answer
    A

    Each search may examine only a fraction of the total data

    Explanation

    A well-designed table distributes data among buckets, reducing the amount of data examined during lookup in practical terms.

  73. 73

    What is a trie?

    1. A

      A tree-like structure that stores sequences through arrays of character links

    2. B

      A fixed-size array that stores every word in alphabetical order

    3. C

      A linked list that stores only the final character of each word

    4. D

      A queue in which each item contains two numeric priorities

    Correct answer
    A

    A tree-like structure that stores sequences through arrays of character links

    Explanation

    A trie stores words by following a sequence of pointers indexed by successive characters.

  74. 74

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

    1. A

      By storing the entire word in one array slot as a single string

    2. B

      By following one character link for each successive letter

    3. C

      By placing every character in a separate sorted binary tree

    4. D

      By hashing only the final letter into a linked list

    Correct answer
    B

    By following one character link for each successive letter

    Explanation

    The trie follows links for M, then a, then x, and so on, creating a path for the word.

  75. 75

    What does a special end-of-word marker accomplish in a trie?

    1. A

      It forces all later characters to be ignored

    2. B

      It indicates that a complete word ends at that node

    3. C

      It identifies the root of every separate trie

    4. D

      It stores the number of words below the node

    Correct answer
    B

    It indicates that a complete word ends at that node

    Explanation

    An end marker distinguishes a complete word from a prefix that continues into additional characters.

  76. 76

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

    1. A

      The total number of words stored in the trie

    2. B

      The number of times the trie has been resized

    3. C

      The number of unused buckets in the trie

    4. D

      The number of characters in the word

    Correct answer
    D

    The number of characters in the word

    Explanation

    Lookup follows one link per character, so its time depends on word length rather than the total number of stored words.

  77. 77

    Why is trie lookup described as effectively constant time for ordinary names?

    1. A

      A trie stores every possible word in one memory location

    2. B

      A trie performs no comparisons or pointer accesses

    3. C

      A trie automatically converts every word into a single integer

    4. D

      Typical name lengths are bounded, so lookup does not grow with the total dictionary size

    Correct answer
    D

    Typical name lengths are bounded, so lookup does not grow with the total dictionary size

    Explanation

    Although lookup is technically proportional to word length, ordinary word lengths are relatively limited and do not depend on the number of dictionary entries.

  78. 78

    What is the principal trade-off of a trie?

    1. A

      It offers very fast lookup but can consume substantial memory

    2. B

      It provides fixed capacity but avoids all pointer storage

    3. C

      It supports only numeric data but not character sequences

    4. D

      It uses minimal memory but requires linear lookup through every word

    Correct answer
    A

    It offers very fast lookup but can consume substantial memory

    Explanation

    Tries can provide effectively constant lookup with respect to dictionary size, but their arrays of pointers require considerable memory.

Question 1 of 78
Question 1 of 78

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent