Image Filters in C
- 78Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary purpose of a struct in C?
What is the primary purpose of a struct in C?
78 questions
- 1
What is the primary purpose of a
structin C?- A
To combine related values of potentially different data types
- B
To replace every function with a single data structure
- C
To allocate memory only for dynamically sized arrays
- D
To force all variables to use the same data type
Correct answer
ATo combine related values of potentially different data types
Explanation
A
structlets programmers define a custom data type that groups related fields, such as a student's name and dorm. - A
- 2
In the definition of a
studentstructure containingnameanddorm, what does each field represent?- A
A separate piece of information stored within one student object
- B
A separate student stored within the same array position
- C
A function that retrieves information from the user
- D
A pointer that automatically references the next student
Correct answer
AA separate piece of information stored within one student object
Explanation
The fields
nameanddormare separate members contained within eachstudentstructure. - A
- 3
Why are structure definitions commonly placed in header files?
- A
Header files ensure that every structure uses dynamic memory
- B
Header files prevent structures from containing character strings
- C
Header files automatically convert structures into executable code
- D
Header files allow the definition to be shared across multiple programs
Correct answer
DHeader files allow the definition to be shared across multiple programs
Explanation
Putting a structure definition in a
.hfile makes it available to multiple source files through inclusion. - A
- 4
What does the dot operator do when used with a structure variable?
- A
It follows a pointer to a different structure
- B
It accesses a specific field inside that structure
- C
It compares two structures for equality
- D
It allocates memory for the entire structure
Correct answer
BIt accesses a specific field inside that structure
Explanation
For example,
student.nameaccesses thenamefield inside thestudentstructure. - A
- 5
What does declaring an array with
enrollmentelements accomplish after the program reads the enrollment value?- A
It guarantees that the user will enter that many students
- B
It changes the type of the enrollment variable
- C
It creates one student with that many fields
- D
It creates space for the specified number of students
Correct answer
DIt 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.
- A
- 6
What does
fopenreturn when a file is successfully opened?- A
The entire contents of the file as a string
- B
A file pointer representing the open file
- C
A Boolean value that is always equal to
true - D
The number of bytes currently in the file
Correct answer
BA file pointer representing the open file
Explanation
fopenreturns aFILE *, which represents the opened file and can be used by other file functions. - A
- 7
What does the mode `
- A
It opens the file only if it already exists
- B
It opens the file for appending without changes
- C
It opens the file for writing
- D
It opens the file only for reading
Correct answer
CIt opens the file for writing
Explanation
The
wmode indicates writing. The lecture contrasts it withr, which indicates reading. - A
- 8
Why should a program check whether a file pointer is
NULLafter callingfopen?- A
The file may not have been opened successfully
- B
The file pointer may contain a valid address that must be doubled
- C
The file pointer must be converted into an integer first
- D
The file may contain too many comma-separated values
Correct answer
AThe file may not have been opened successfully
Explanation
NULLindicates that no valid file address was returned, possibly because the file does not exist, permissions are lacking, or memory is unavailable. - A
- 9
How does
fprintfdiffer fromprintfin the file example?- A
fprintfautomatically sorts the data before displaying it - B
fprintfreads from a file whileprintfwrites to the screen - C
fprintfprints only integers whileprintfprints strings - D
fprintfreceives a file pointer and writes to that file
Correct answer
Dfprintfreceives a file pointer and writes to that fileExplanation
fprintfworks likeprintf, but its first argument identifies the destination file. - A
- 10
What is the purpose of calling
fcloseafter writing to a file?- A
It closes the file and completes the saving process
- B
It prints the file's contents to the screen
- C
It deletes the file from the computer
- D
It converts the file into a binary executable
Correct answer
AIt closes the file and completes the saving process
Explanation
fclosecloses the open file; in the lecture, this is described as effectively completing the save. - A
- 11
Why is a CSV file described as a simplistic database format?
- A
It stores data only in encrypted binary form
- B
It prevents programs such as spreadsheets from reading the data
- C
It organizes tabular values using separators such as commas
- D
It automatically creates indexes for every stored value
Correct answer
CIt 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.
- A
- 12
Why do databases eventually become preferable to large CSV files?
- A
CSV files can only be opened once before becoming invalid
- B
CSV files cannot store any character data
- C
Databases require every record to have exactly two fields
- D
Databases provide more powerful and efficient ways to search data
Correct answer
DDatabases 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.
- A
- 13
What is the text segment of a program's memory?
- A
The compiled zeros and ones that make up the program
- B
The dynamically allocated memory returned by
malloc - C
The user names and passwords stored outside the program
- D
The local variables created whenever a function runs
Correct answer
AThe 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.
- A
- 14
Where are global variables generally stored in the memory layout described?
- A
Only in the stack frames of called functions
- B
Inside the return address of the calling function
- C
In initialized or uninitialized data regions
- D
Only in the heap after a call to
malloc
Correct answer
CIn initialized or uninitialized data regions
Explanation
Global variables are placed in initialized data if given initial values and uninitialized data otherwise.
- A
- 15
What is stored in an environment variable?
- A
A duplicate copy of every file opened by the program
- B
The next instruction that a function should execute after returning
- C
The address of every local variable in the current stack frame
- D
A value such as a password that a program can access without embedding it in source code
Correct answer
DA 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.
- A
- 16
What generally happens when a function is called?
- A
The function automatically allocates all memory from the heap
- B
The function receives a new text segment containing all program instructions
- C
The function receives a new stack frame for its local variables and parameters
- D
The function permanently replaces the stack frame of its caller
Correct answer
CThe 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.
- A
- 17
Why did the original
swapfunction fail to change the variables inmain?- A
It used a temporary variable that could not store integers
- B
It passed the variables by their addresses but ignored them
- C
It attempted to modify the program's text segment
- D
It changed copies of the values in its own stack frame
Correct answer
DIt 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. - A
- 18
What does a declaration such as
int *pindicate?- A
It declares an array containing one integer pointer per element
- B
It declares
pas an integer whose value must always be multiplied - C
It declares a function that returns an integer automatically
- D
It declares
pas a variable that stores the address of an integer
Correct answer
DIt declares
pas a variable that stores the address of an integerExplanation
The asterisk in the declaration indicates that
pis a pointer to anint, meaning it stores an integer's address. - A
- 19
What does the expression
*pmean whenpis an integer pointer?- A
The number of bytes allocated for the pointer
- B
The address of the pointer variable
pitself - C
The next pointer declared after
p - D
The value stored at the address contained in
p
Correct answer
DThe value stored at the address contained in
pExplanation
The dereference operator follows the address in
pand accesses the value stored there. - A
- 20
Why do pointers allow
swapto modify variables inmain?- A
They prevent
swapfrom creating local parameters - B
They convert both variables into global variables
- C
They copy the variables into a larger stack frame automatically
- D
They give
swapthe addresses of the variables that must be changed
Correct answer
DThey give
swapthe addresses of the variables that must be changedExplanation
Passing addresses lets
swapdereference those pointers and modify the original memory locations. - A
- 21
What happens to a function's stack memory after the function finishes?
- A
It is automatically converted into heap memory
- B
It is permanently moved into the text segment
- C
It is erased physically from the computer's hardware
- D
It becomes available for reuse by later function calls
Correct answer
DIt 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.
- A
- 22
What is a garbage value in an uninitialized local variable?
- A
Residual bits left in memory by earlier uses of that memory
- B
A special value that always represents zero
- C
A pointer that automatically refers to the heap
- D
A value inserted by the compiler to indicate a syntax error
Correct answer
AResidual 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.
- A
- 23
Why would returning the address of a local array from a function be unsafe?
- A
Addresses of arrays cannot be represented by pointers
- B
Returning an address automatically frees the heap
- C
The array's stack memory becomes available for reuse when the function returns
- D
Local arrays are always stored in read-only memory
Correct answer
CThe 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.
- A
- 24
What is the main distinction between stack memory and heap memory in this lecture?
- A
The stack supports short-term function storage, while the heap supports longer-lived allocations
- B
The stack stores only machine instructions, while the heap stores only global variables
- C
The stack persists until manually freed, while the heap disappears after each function call
- D
The stack is dynamically allocated, while the heap is reserved only for function parameters
Correct answer
AThe 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.
- A
- 25
What is the role of
malloc?- A
It clears every byte of the entire computer's memory
- B
It opens a file for writing
- C
It allocates a requested amount of memory from the heap
- D
It releases a previously allocated stack frame
Correct answer
CIt allocates a requested amount of memory from the heap
Explanation
mallocallocates heap memory and returns its address. - A
- 26
What distinguishes
callocfrommalloc?- A
callocallocates memory and initializes the allocated bytes to zero - B
callocincreases an allocation without copying existing contents - C
callocreleases memory whilemallocallocates it - D
callocallocates memory only on the stack and never on the heap
Correct answer
Acallocallocates memory and initializes the allocated bytes to zeroExplanation
callocis likemallocbut clears the allocated memory, preventing initial garbage values. - A
- 27
What is the purpose of
realloc?- A
It converts a pointer into a structure field
- B
It removes every pointer from a linked list
- C
It changes the size of an existing memory allocation
- D
It opens an existing file in read-only mode
Correct answer
CIt changes the size of an existing memory allocation
Explanation
realloccan grow or otherwise resize a previously allocated block, handling relocation and copying when necessary. - A
- 28
What can happen if a pointer is dereferenced before it has been assigned a valid address?
- A
The program may access unauthorized memory and crash
- B
The pointer automatically receives a valid heap address
- C
The pointed-to integer is initialized to zero
- D
The operating system converts the pointer into
NULL
Correct answer
AThe 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.
- A
- 29
Why does assigning
y = xfor two pointers not copy the pointed-to data?- A
It changes both pointers into ordinary integer variables
- B
It frees the memory previously referenced by both pointers
- C
It copies the address so both pointers refer to the same memory
- D
It allocates a new block and copies every value into it
Correct answer
CIt 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.
- A
- 30
What is a memory leak?
- A
A pointer that refers to two structures at once
- B
Allocated memory that is no longer needed but has not been freed
- C
A file that has been closed before being written
- D
A stack variable that contains a valid initialized value
Correct answer
BAllocated 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. - A
- 31
What is a buffer?
- A
A function that automatically sorts an array
- B
A special pointer that always stores
NULL - C
A file mode used exclusively for reading
- D
A chunk of memory used to store data or values
Correct answer
DA 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.
- A
- 32
What causes a buffer overflow?
- A
Reading a value from the first valid position of an array
- B
Writing more data into a buffer than the buffer can hold
- C
Freeing memory after it is no longer needed
- D
Allocating a buffer whose size matches the input
Correct answer
BWriting 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.
- A
- 33
Why does copying a 13-byte string into a 12-byte array create a problem?
- A
The thirteenth byte is written outside the array's valid bounds
- B
The compiler converts the array into a linked list
- C
The string loses its terminating null character before copying
- D
The array automatically changes its size to 13 bytes
Correct answer
AThe 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.
- A
- 34
How can a buffer overflow become a security exploit?
- A
An attacker may force the program to use only constant-time algorithms
- B
An attacker may prevent the heap from storing any pointers
- C
An attacker may cause every local variable to become automatically initialized
- D
An attacker may overwrite a return address with data interpreted as executable instructions
Correct answer
DAn 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.
- A
- 35
What does a segmentation fault generally indicate in the pointer examples?
- A
The program successfully freed every heap allocation
- B
The program attempted to access memory it was not permitted to access
- C
The program searched a sorted tree in logarithmic time
- D
The program opened a CSV file in write mode
Correct answer
BThe 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.
- A
- 36
What error exists when code writes to
x[10]after allocating space for 10 integers?- A
It writes one integer beyond the valid range, which ends at
x[9] - B
It writes to the first integer because arrays begin at index 10
- C
It reads the tenth integer, which is always valid
- D
It creates an additional integer without using extra memory
Correct answer
AIt 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.
- A
- 37
What kind of problems is Valgrind designed to detect?
- A
Only logical errors in sorting algorithms
- B
Errors in the visual layout of spreadsheet columns
- C
Only spelling errors in comments and variable names
- D
Memory-related errors such as invalid accesses and leaks
Correct answer
DMemory-related errors such as invalid accesses and leaks
Explanation
Valgrind helps identify invalid memory operations, memory leaks, and related memory-management problems.
- A
- 38
What does an “invalid write of size 4” message commonly mean for an integer array?
- A
A four-byte integer was written to an invalid memory location
- B
The program successfully wrote four integers into one array position
- C
The program read four bytes from a valid integer location
- D
The program allocated exactly four additional array elements
Correct answer
AA four-byte integer was written to an invalid memory location
Explanation
An
intcommonly occupies four bytes in the described environment, so the message indicates an invalid four-byte write. - A
- 39
What does hexadecimal notation such as
0x...commonly represent in debugging output?- A
A compact representation of a number, often a memory address
- B
A decimal representation of a file's line count
- C
A special notation used only for Boolean values
- D
A character encoding that cannot represent addresses
Correct answer
AA compact representation of a number, often a memory address
Explanation
Debuggers and memory tools commonly display addresses in hexadecimal notation.
- A
- 40
How does rubber duck debugging help a programmer?
- A
The duck detects memory leaks without access to the source code
- B
The duck automatically executes the program with corrected pointers
- C
Explaining code aloud increases the allocated buffer size
- D
Explaining the code aloud can reveal inconsistencies in the programmer's reasoning
Correct answer
DExplaining 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.
- A
- 41
Why can dynamically resizing an array be inefficient?
- A
Array resizing can occur only when the program has no variables
- B
A new allocation always deletes the existing elements without copying
- C
A new allocation may require copying all existing elements, an O(n) operation
- D
Array resizing requires searching every possible memory address
Correct answer
CA new allocation may require copying all existing elements, an O(n) operation
Explanation
When an array must move to a larger block, its existing elements must be copied, requiring linear time.
- A
- 42
What problem does memory fragmentation create for arrays?
- A
Every free byte is automatically converted into a pointer
- B
Enough total free memory may exist, but not in one contiguous block
- C
Fragmentation guarantees that all allocations are constant time
- D
Arrays become unable to store values of the same data type
Correct answer
BEnough 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.
- A
- 43
What is the defining feature of a linked list?
- A
Each node stores data and a pointer to another node
- B
The list can be accessed directly using array indexes
- C
Every node contains exactly two numeric values and no pointers
- D
Every element must occupy adjacent memory locations
Correct answer
AEach 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.
- A
- 44
What does the
nextfield in a linked-list node store?- A
The index of the current node in a contiguous array
- B
The address of the program's text segment
- C
The number of elements remaining in the list
- D
The address of the next node in the list
Correct answer
DThe address of the next node in the list
Explanation
The
nextpointer links the current node to the following node. - A
- 45
Why does a linked list not require contiguous memory?
- A
The operating system automatically moves every node next to the previous one
- B
Linked lists do not use memory for their stored values
- C
Pointers connect nodes even when the nodes are located in separate memory regions
- D
Each node is stored inside the stack frame of the first node
Correct answer
CPointers 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.
- A
- 46
What special pointer is used to identify the beginning of a linked list?
- A
A pointer that always stores the address of the final node
- B
A pointer that refers to the middle node for faster indexing
- C
A pointer that stores the number of nodes as an integer
- D
A pointer such as
firstorheadthat stores the first node's address
Correct answer
DA pointer such as
firstorheadthat stores the first node's addressExplanation
Because linked-list nodes are not indexed like array elements, the list is accessed through a pointer to its first node.
- A
- 47
What should the
nextpointer of the final node in a linked list contain?- A
The number of nodes currently stored in the list
- B
The address of the first node, regardless of list structure
- C
NULL, indicating that there is no following node - D
A garbage value indicating that the list is complete
Correct answer
CNULL, indicating that there is no following nodeExplanation
The final node's
nextpointer is set toNULLto mark the end of the list. - A
- 48
What is the time complexity of inserting a node at the beginning of an unsorted linked list?
- A
O(n2) because every pair of nodes must be compared
- B
O(1) because only a constant number of pointers must be updated
- C
O(logn) because the list must be divided in half
- D
O(n) because every existing node must be copied
Correct answer
BO(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.
- A
- 49
Why can inserting into a sorted linked list require O(n) time?
- A
The new node must be allocated on the stack rather than the heap
- B
Every node must be copied into a contiguous array
- C
The list may need to be traversed to locate the correct position
- D
The list must always be sorted using binary search
Correct answer
CThe 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.
- A
- 50
What is the main trade-off of using a linked list instead of an array?
- A
Linked lists eliminate all traversal time but cannot store integers
- B
Linked lists use less memory but cannot grow after initialization
- C
Linked lists gain flexible growth but use extra pointer memory and more complex code
- D
Linked lists provide direct indexing but require contiguous allocation
Correct answer
CLinked 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.
- A
- 51
Why must a linked list be traversed using pointers rather than array indexing?
- A
Its nodes may be separated in memory and therefore lack meaningful numeric indexes
- B
Its nodes are always stored in reverse order
- C
Array indexing works only for character data
- D
Pointers cannot refer to structures stored in arrays
Correct answer
AIts 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.
- A
- 52
When freeing an entire linked list, why should the next pointer be saved before freeing the current node?
- A
The next pointer always contains a copy of the current node's data
- B
Saving the pointer changes the list into an array
- C
Freeing the current node makes its stored pointer unsafe to use afterward
- D
Freeing a node automatically frees every later node
Correct answer
CFreeing 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.
- A
- 53
What is technical debt in software development?
- A
The future cost created by choosing quick, poorly designed solutions
- B
The number of pointers contained in a linked list
- C
The amount of heap memory returned by
free - D
The time required to execute a constant-time algorithm
Correct answer
AThe future cost created by choosing quick, poorly designed solutions
Explanation
Shortcuts may save time initially but create additional maintenance, debugging, or redesign work later.
- A
- 54
What does LIFO mean for a stack?
- A
The first item inserted is the first item removed
- B
The largest item inserted is the first item removed
- C
Items are removed in alphabetical order
- D
The last item inserted is the first item removed
Correct answer
DThe last item inserted is the first item removed
Explanation
A stack follows last in, first out: the most recently pushed item is popped first.
- A
- 55
Which operations are conventionally associated with a stack?
- A
mallocto add an item andfreeto remove an item - B
enqueueto add an item anddequeueto remove an item - C
insertto add an item andsearchto remove an item - D
pushto add an item andpopto remove an item
Correct answer
Dpushto add an item andpopto remove an itemExplanation
Stacks conventionally use
pushandpop, although an implementation could choose different names. - A
- 56
What limitation results from implementing a stack with a fixed-size array?
- A
The stack automatically requires a linked list for every operation
- B
The stack cannot store integers or other simple values
- C
The stack cannot hold more items than its predefined capacity
- D
The stack must remove its oldest item first
Correct answer
CThe 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.
- A
- 57
What does FIFO mean for a queue?
- A
The largest item inserted is the first item removed
- B
Items are removed from the position with the most memory
- C
The last item inserted is the first item removed
- D
The first item inserted is the first item removed
Correct answer
DThe first item inserted is the first item removed
Explanation
A queue follows first in, first out, like people being served in line.
- A
- 58
Why should an efficient array-based queue track the position of the front?
- A
It ensures that the queue removes the newest item first
- B
It allows the queue to avoid storing its size
- C
It can remove the front item without shifting every remaining item
- D
It converts the queue into a binary search tree
Correct answer
CIt 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.
- A
- 59
What is an API in the context of a data structure?
- A
The physical arrangement of every byte inside the data structure
- B
The compiler's private representation of all local variables
- C
The set of operations and behaviors exposed to users of the implementation
- D
The number of lines used to implement the data structure
Correct answer
CThe set of operations and behaviors exposed to users of the implementation
Explanation
An API exposes operations such as
pushandpopwhile hiding implementation details. - A
- 60
What is the root of a tree?
- A
The topmost node from which the rest of the tree can be reached
- B
The node with the largest stored value
- C
The final node visited during a search
- D
Any node that has no children and no parent
Correct answer
AThe 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.
- A
- 61
What is a leaf in a tree?
- A
A pointer that stores the tree's height
- B
A node with no children
- C
A node that must have exactly two children
- D
The node at the top of the tree
Correct answer
BA node with no children
Explanation
Leaves are nodes at the bottom of a tree that have no outgoing child links.
- A
- 62
What makes a tree a binary tree?
- A
Each node has at most two children
- B
The tree must always be perfectly balanced
- C
The tree must contain exactly two nodes
- D
Every node must contain two integers
Correct answer
AEach node has at most two children
Explanation
A binary tree permits each node to have zero, one, or two children.
- A
- 63
What additional property makes a binary tree a binary search tree?
- A
Values in the left subtree are smaller and values in the right subtree are larger
- B
Every node has exactly two children with equal values
- C
The tree must contain the same number of nodes on every level
- D
All values are stored in one contiguous array
Correct answer
AValues 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.
- A
- 64
What is the search time for a balanced binary search tree?
- A
O(logn) because each comparison can eliminate roughly half the remaining tree
- B
O(n) because every node must always be examined
- C
O(n2) because each node must be compared with every other node
- D
O(1) because every node has a direct numeric index
Correct answer
AO(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.
- A
- 65
Why can a binary search tree have linear search time despite its search property?
- A
The left and right pointers prevent comparisons between values
- B
Binary search trees cannot store more than two values
- C
Leaves always contain duplicate values that require repeated searches
- D
If the tree becomes highly unbalanced, it can resemble a linked list
Correct answer
DIf 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.
- A
- 66
Why are the
leftandrightfields of a tree leaf set toNULL?- A
A leaf has no child nodes to which those pointers could refer
- B
A leaf stores its value only in the text segment
- C
A leaf must always point back to the root
- D
A leaf is not allowed to contain a structure field
Correct answer
AA leaf has no child nodes to which those pointers could refer
Explanation
Since leaves have no children, both child pointers are null.
- A
- 67
What is hashing?
- A
Copying an array into a larger contiguous array
- B
Using a function to map an input to a bucket or hash value
- C
Sorting every input by repeatedly comparing adjacent elements
- D
Following pointers until a null value is reached
Correct answer
BUsing 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.
- A
- 68
What is a collision in a hash table?
- A
A pointer becomes
NULLat the end of a linked list - B
Two different inputs map to the same bucket
- C
A tree node has both a left and right child
- D
An array contains fewer elements than its capacity
Correct answer
BTwo different inputs map to the same bucket
Explanation
A collision occurs when multiple inputs produce the same hash value.
- A
- 69
How does linear probing handle a hash-table collision?
- A
It creates a new linked list for every input immediately
- B
It recursively divides the table into two balanced trees
- C
It discards the new input and keeps only the earlier one
- D
It searches subsequent positions until it finds an available slot
Correct answer
DIt searches subsequent positions until it finds an available slot
Explanation
Linear probing checks positions sequentially until it finds an empty location.
- A
- 70
What is separate chaining in a hash table?
- A
Using a linked list at each bucket to store colliding inputs
- B
Searching every bucket in order after each insertion
- C
Storing only one input per bucket and rejecting collisions
- D
Using a second array to replace every occupied bucket
Correct answer
AUsing 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.
- A
- 71
Why should a hash function distribute inputs uniformly?
- A
Uniform distribution guarantees that collisions never occur
- B
Uniform distribution eliminates the need for memory allocation
- C
Uniform distribution keeps the chains or buckets from becoming unnecessarily long
- D
Uniform distribution ensures that every input has the same number of characters
Correct answer
CUniform distribution keeps the chains or buckets from becoming unnecessarily long
Explanation
If values are spread evenly, searches examine shorter chains on average.
- A
- 72
What is the practical benefit of dividing data among many hash-table buckets?
- A
Each search may examine only a fraction of the total data
- B
The table no longer requires any memory for pointers
- C
Every search becomes independent of the input's value
- D
All collisions are eliminated regardless of the hash function
Correct answer
AEach 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.
- A
- 73
What is a trie?
- A
A tree-like structure that stores sequences through arrays of character links
- B
A fixed-size array that stores every word in alphabetical order
- C
A linked list that stores only the final character of each word
- D
A queue in which each item contains two numeric priorities
Correct answer
AA 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.
- A
- 74
How is a word such as
Maxwellrepresented in a trie?- A
By storing the entire word in one array slot as a single string
- B
By following one character link for each successive letter
- C
By placing every character in a separate sorted binary tree
- D
By hashing only the final letter into a linked list
Correct answer
BBy 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.
- A
- 75
What does a special end-of-word marker accomplish in a trie?
- A
It forces all later characters to be ignored
- B
It indicates that a complete word ends at that node
- C
It identifies the root of every separate trie
- D
It stores the number of words below the node
Correct answer
BIt 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.
- A
- 76
What determines the lookup time for a word in a trie?
- A
The total number of words stored in the trie
- B
The number of times the trie has been resized
- C
The number of unused buckets in the trie
- D
The number of characters in the word
Correct answer
DThe 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.
- A
- 77
Why is trie lookup described as effectively constant time for ordinary names?
- A
A trie stores every possible word in one memory location
- B
A trie performs no comparisons or pointer accesses
- C
A trie automatically converts every word into a single integer
- D
Typical name lengths are bounded, so lookup does not grow with the total dictionary size
Correct answer
DTypical 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.
- A
- 78
What is the principal trade-off of a trie?
- A
It offers very fast lookup but can consume substantial memory
- B
It provides fixed capacity but avoids all pointer storage
- C
It supports only numeric data but not character sequences
- D
It uses minimal memory but requires linear lookup through every word
Correct answer
AIt 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.
- A

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