Week 3: Arrays & Computation Complexity and Analysis of Algorithms Flashcards
Which of the following statements about arrays is true?
A) Arrays can store elements of different types.
B) Arrays are mutable and can change size dynamically.
C) Arrays store a fixed-size sequential collection of elements of the same type.
D) Arrays are unordered collections.
C) Arrays store a fixed-size sequential collection of elements of the same type.
Explanation: Arrays are a basic data structure that stores a fixed-size sequential collection of elements of the same type. They are not mutable and cannot change size dynamically.
Which of the following is a characteristic of a list in Python?
A) Lists are immutable.
B) Lists are unordered collections.
C) Lists can change size as items are added or removed.
D) Lists do not allow duplicate values.
C) Lists can change size as items are added or removed.
Explanation: Lists in Python are mutable sequence containers that can change size as items are added or removed. They are ordered collections and can contain duplicate values.
Which method is used to add an item to the end of a list in Python?
A) insert()
B) append()
C) extend()
D) add()
B) append()
Explanation: The append() method adds an element at the end of the list.
Which method is used to remove the first occurrence of a specified value from a list in Python?
A) pop()
B) remove()
C) delete()
D) clear()
B) remove()
Explanation: The remove() method removes the first item with the specified value from the list.
Which method returns the number of elements with the specified value in a list?
A) count()
B) index()
C) len()
D) find()
A) count()
Explanation: The count() method returns the number of elements with the specified value in a list.
Which of the following statements about tuples is true?
A) Tuples are mutable.
B) Tuples are unordered collections.
C) Tuples allow duplicate values.
D) Tuples can change size dynamically.
C) Tuples allow duplicate values.
Explanation: Tuples are ordered collections that allow duplicate values. They are immutable, meaning their size and content cannot be changed after creation.
Which of the following best describes a dictionary in Python?
A) A collection of ordered elements.
B) A collection of key-value pairs.
C) A collection of unique values.
D) A collection of immutable elements.
B) A collection of key-value pairs.
Explanation: A dictionary in Python is a collection of key-value pairs, where each key is unique and is used to store and retrieve values.
Which method returns a list of all the keys in a dictionary?
A) keys()
B) values()
C) items()
D) get()
A) keys()
Explanation: The keys() method returns a list of all the keys in the dictionary.
Which of the following best describes an algorithm?
A) A random sequence of steps to solve a problem.
B) A systematic procedure that produces the answer to a question in a finite number of steps.
C) A collection of unordered steps to solve a problem.
D) A sequence of steps that may or may not produce a solution.
B) A systematic procedure that produces the answer to a question in a finite number of steps.
Explanation: An algorithm is an organized sequence of clear steps or operations needed to solve a given programming problem.
Which of the following factors is used to choose between different algorithms?
A) The number of lines of code.
B) The efficiency, usually noted by how long an algorithm takes to produce its result.
C) The programming language used.
D) The number of comments in the code.
B) The efficiency, usually noted by how long an algorithm takes to produce its result.
Explanation: Efficiency, often measured by the time an algorithm takes to produce its result, is a key factor in choosing between different algorithms.
What does computational complexity measure?
A) The number of lines of code in an algorithm.
B) The amount of resources (time and space) required to run an algorithm.
C) The number of comments in the code.
D) The programming language used.
B) The amount of resources (time and space) required to run an algorithm.
Explanation: Computational complexity measures the amount of resources, such as time and space, required to run an algorithm.
What is time complexity?
A) The number of lines of code in an algorithm.
B) The amount of memory required to solve a problem.
C) The number of times an algorithm runs.
D) The number of comments in the code.
C) The number of times an algorithm runs.
Explanation: Time complexity is expressed as the number of times an algorithm runs, indicating how the execution time grows with the input size.
What does Big-O notation describe?
A) The number of lines of code in an algorithm.
B) The limiting behavior of a function when the argument tends toward a particular value or infinity.
C) The number of comments in the code.
D) The programming language used.
B) The limiting behavior of a function when the argument tends toward a particular value or infinity.
Explanation: Big-O notation describes the limiting behavior of a function, used to classify the efficiency of algorithms according to how their space and time complexity grows as the input size grows.
hich Big-O notation represents constant time complexity?
A) O(n)
B) O(1)
C) O(n^2)
D) O(log n)
B) O(1)
Explanation: O(1) represents constant time complexity, where the time required to execute the algorithm is constant, regardless of the input size.
Which Big-O notation represents linear time complexity?
A) O(n)
B) O(1)
C) O(n^2)
D) O(log n)
A) O(n)
Explanation: O(n) represents linear time complexity, where the time required to execute the algorithm is proportional to the input size.
Which Big-O notation represents polynomial time complexity?
A) O(n)
B) O(1)
C) O(n^2)
D) O(log n)
C) O(n^2)
Explanation: O(n^2) represents polynomial time complexity, where the time complexity increases based on the number of nested loops.
Which type of algorithm analysis considers the maximum number of steps required?
A) Best-case
B) Average-case
C) Worst-case
D) Random-case
C) Worst-case
Explanation: Worst-case analysis considers the maximum number of steps required for an algorithm to complete, providing an upper bound on the time complexity.
Which type of algorithm analysis considers the minimum number of steps required?
A) Best-case
B) Average-case
C) Worst-case
D) Random-case
A) Best-case
Explanation: Best-case analysis considers the minimum number of steps required for an algorithm to complete, providing a lower bound on the time complexity.