Menu

1.2 Algorithms

Card 1 of 79 0 got it 0 still learning
Question
TapClick or press space to see the answer
Answer
All 79 cards in this set
What is the primary purpose of pseudocode in algorithm design?
To represent an algorithm in a simplified, language-agnostic format
In pseudocode, what does the statement 'print("Hello World")' primarily illustrate?
Output - displaying text on the screen
What is the role of 'IfElseifElse' statements in pseudocode?
To represent decision-making based on conditions
In pseudocode, what is the significance of the 'While' loop?
To execute a set of instructions as long as a condition is met
Which statement best describes the syntax requirement for pseudocode?
It should make logical sense without needing precise syntax
Given the pseudocode: 'FOR i = 1 to 5: print(i) END FOR', what will be the output?
1, 2, 3, 4, 5
What is the output of the following pseudocode? 'x = 10; WHILE x > 5: print(x); x = x - 2 END WHILE'
10, 8, 6
Consider the pseudocode: 'IF x > 10 THEN print("High") ELSE print("Low") ENDIF' What is printed if x = 8?
Low
Given the pseudocode with a nested loop: 'FOR i = 1 to 3: FOR j = 1 to 2: print(i * j) END FOR END FOR', what is the output?
1, 2, 2, 4, 3, 6
What will be the output of this pseudocode? 'i = 5; WHILE i >= 0: IF i % 2 == 0 THEN print(i) ENDIF; i = i - 1 END WHILE'
4, 2, 0
What is the primary purpose of a flowchart in algorithm design?
To visually represent the steps of an algorithm
How is a loop typically represented in a flowchart?
By arrows circling back to an earlier step
What is the result of 8 + 2 in Python?
10
In Python, what does the expression 9 - 3 evaluate to?
6
If you have 14 * 2 in Python, what will be the result?
28
How does Python evaluate 15 // 2?
7
What will 17 % 5 return in Python?
2
What does the ** operator calculate in 2 ** 3?
8
In Python, what is the result of 5 + 3 * 2?
11
What does 4 ** 0.5 yield in Python?
2
What is the outcome of -5 // 2 in Python?
-3
What is the result of 8 % 3?
2
In Python, how is 9 + 4 - 2 evaluated?
11
What does the expression 10 / 5 / 2 return?
1
If you perform 11 // 3 in Python, what will be the result?
3
What will be the result of 7 ** 2 in Python?
49
Which operator gives a True result only when both of its inputs are True?
AND
What is meant by the operator OR?
It gives a True result when at least one of its inputs is True
Which operator reverses the value of a single Boolean input?
NOT
What is meant by the term Boolean expression?
An expression that evaluates to either True or False
What is the name for a table that shows the output for every possible combination of inputs to a Boolean expression?
Truth table
What is meant by the term Boolean operator?
An operator such as AND, OR or NOT that combines or changes True and False values
What is the output of the expression NOT True?
False
What is the value of (2 > 3) OR (5 == 5)?
True
A program accepts a password only when it is longer than 7 characters AND contains a digit. Which pair of values allows the password to be accepted?
Length is 10, contains digit is True
In the rule IF day == 'Saturday' OR day == 'Sunday' THEN shop is closed, on which days is the shop closed?
Saturday and Sunday
What does the expression NOT (x > 10) evaluate to when x is 15?
False
What does the NOT operator do to its input?
It takes a single input and reverses its truth value, so True becomes False and False becomes True.
A ticket is sold when age is less than 18 OR the person is a student. Which person can buy a ticket?
A 25 year old who is a student
What is the primary purpose of a trace table in programming?
To track the values of variables to debug or understand the flow of an algorithm
At which stage of the software development cycle are trace tables most commonly used?
Debugging and Testing
Which of the following is NOT typically included in a trace table?
The source code of the program
Which term describes an error where the code written does not conform to the rules or grammar of the language?
Syntax error
Which term describes an error where a program runs but does not work as the programmer intended?
Logic error
Which term describes an error that only occurs while a program is running, such as dividing a number by zero?
Runtime error
What is meant by a syntax error?
An error where the code does not follow the rules or grammar of the programming language
What is meant by a logic error?
An error where the program compiles and runs but does not work as the programmer intended
What is meant by a runtime error?
An error that occurs while the program is running, such as an attempt to divide by zero
A program contains the line print("Hello". What type of error is this?
Syntax error
A program asks for two numbers and divides the first by the second. The user enters 10 and then 0. What happens?
A runtime error occurs because division by zero is not allowed
A program should work out the area of a square but uses area = length * 2 instead of length ** 2. What type of error is this?
Logic error
Which of these is a runtime error?
The program tries to divide a number by zero while it is running
A programmer uses a trace table to find an error. What is the main purpose of a trace table?
To record the value of each variable as it changes while the program runs
A program should output the largest of three numbers but always outputs the first number. Which error type is this and why?
Logic error, because the program runs but does not give the intended result
What is the main advantage of using a linear search algorithm?
It can be used on unordered arrays
In what scenario is a binary search algorithm more efficient than a linear search?
When searching for an item in a large, ordered array
Why can't a binary search be used on an unordered array?
Because it relies on the array being ordered to eliminate half of the search space at each step
What is the worst-case scenario for a linear search algorithm?
Finding the desired item last in the array
How does a binary search algorithm find a value in a sorted array?
By repeatedly dividing the search interval in half
What is a key limitation of the binary search algorithm?
It requires the array to be sorted
Which algorithm would be best for searching a small, unordered list of names?
Linear search
How is the middle element of the array found in a binary search algorithm?
By calculating the midpoint of the current search interval
What happens after each comparison in a binary search?
The search space is halved
Which search algorithm would you use for a dataset that changes frequently and does not remain sorted?
Linear search
What is the best case performance scenario for a linear search algorithm?
Finding the desired item on the first check
Why might a linear search be preferred over binary search in some cases?
Because of its simplicity and lack of requirement for the array to be sorted
In a binary search, if the middle value is greater than the value being searched for, what is the next step?
Create a new array with values to the left of the middle and repeat the search
What makes binary search more performant than linear search on large, sorted arrays?
Its ability to eliminate half of the search space with each comparison
How is performance generally measured for search algorithms?
By the number of comparisons or iterations needed to find the target value
What is the primary purpose of using sorting algorithms in computer science?
To organize data into a specified order for more efficient searching and processing
Which sorting algorithm is considered most efficient when dealing with large datasets?
Merge Sort
In Bubble Sort, what action is taken when the current element is greater than the next element?
The elements are swapped
What is a key characteristic of Merge Sort that differentiates it from other sorting algorithms?
It breaks down the array into smaller arrays and merges them in order
How does the complexity of Bubble Sort change with the size of the array?
It increases quadratically, making it less efficient for larger arrays
What advantage does Merge Sort have over Bubble Sort?
Better performance on larger arrays due to its divide-and-conquer approach
Why is Bubble Sort considered inefficient for sorting large datasets?
Because its time complexity is quadratic, requiring many passes through the data
How does Merge Sort ensure elements are sorted correctly when reassembling the array?
By merging smaller arrays in order while ensuring each is sorted
What is a scenario where Bubble Sort might still be a viable option despite its inefficiencies?
When sorting an array that is nearly sorted, where minimal swaps are needed
Which sorting algorithm employs a divide-and-conquer strategy for sorting elements?
Merge Sort