zuai-logo
zuai-logo
  1. AP Computer Science Principles
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What does the following code output?

python
def is_prime(n):
  for i in range(2, n):
    if n % i == 0:
      return False
  return True

print(is_prime(4))```

False

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What does the following code output?

python
def is_prime(n):
  for i in range(2, n):
    if n % i == 0:
      return False
  return True

print(is_prime(4))```

False

What does the following code output?

python
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

print(factorial(3))```

6

Identify the error in the following code:

python
def find_max(list):
  max_value = 0
  for num in list:
    if num > max_value
      max_value = num
  return max_value```

Missing colon after the if condition. It should be if num > max_value:

What does the following code output?

python
def linear_search(list, target):
    for i in range(len(list)):
        if list[i] == target:
            return i
    return -1

my_list = [10, 20, 30, 40, 50]
print(linear_search(my_list, 30))```

2

What does the following code output?

python
def example_function(n):
    count = 0
    for i in range(n):
        for j in range(n):
            count += 1
    return count

print(example_function(4))```

16

Identify the error in the following code:

python
def calculate_average(numbers):
    sum = 0
    for number in numbers:
        sum += number
    average = sum / len(numbers)
    return average

print(calculate_average([]))```

ZeroDivisionError: division by zero. The code attempts to divide by the length of the list, which is zero when the list is empty.

What does the following code output?

python
def mystery_function(arr):
    n = len(arr)
    for i in range(n):
        for j in range(i+1, n):
            if arr[i] > arr[j]:
                arr[i], arr[j] = arr[j], arr[i]
    return arr

print(mystery_function([5, 2, 8, 1, 9]))```

[1, 2, 5, 8, 9]

Identify the error in the following code:

python
def recursive_function(n):
    if n > 0:
        return recursive_function(n-1)

print(recursive_function(5))```

The function lacks a base case that returns a concrete value when n reaches a certain condition (e.g., n == 0). This will result in infinite recursion and eventually a stack overflow error.

What does the following code output?

python
def binary_search(arr, target):
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

print(binary_search([2, 5, 7, 8, 11, 12], 13))```

-1

Identify the error in the following code:

python
def calculate_sum(numbers):
    total = 0
    for i in range(1, len(numbers)):
        total += numbers[i]
    return total

print(calculate_sum([1, 2, 3, 4, 5]))```

The loop starts at index 1 instead of 0, causing the first element of the list to be skipped. The loop should start with for i in range(len(numbers)):

How is pathfinding (shortest path) applied in real-world scenarios?

GPS navigation systems, network routing, logistics optimization.

How are sorting algorithms applied in real-world scenarios?

Database management, search engine results, e-commerce product listings.

How are heuristics used in AI?

Game playing (e.g., chess), machine learning model training, spam filtering.

How is cryptography related to computer science?

It uses algorithms to secure data and communications, ensuring confidentiality and integrity.

How is dynamic programming applied in bioinformatics?

Sequence alignment for DNA and protein analysis.

How are graph algorithms used in social networks?

Friend recommendation, community detection, influence analysis.

How is machine learning used in fraud detection?

Identifying patterns of fraudulent behavior based on transaction data.

How are search algorithms used in web search engines?

Indexing and retrieving relevant web pages based on search queries.

How is data compression used in multimedia applications?

Reducing the size of images, audio, and video files for efficient storage and transmission.

How are scheduling algorithms used in operating systems?

Managing and prioritizing processes to optimize CPU utilization and system performance.

What is the significance of polynomial time?

Indicates an algorithm is generally efficient and scalable.

Why are exponential time algorithms considered inefficient?

Their runtime grows very quickly, making them impractical for large inputs.

When should heuristics be used?

When finding the optimal solution is too computationally expensive.

How does input size affect algorithm efficiency?

Larger inputs generally require more computational resources, impacting efficiency.

What is the trade-off when using heuristics?

Accuracy is sacrificed for speed and feasibility.

What is the goal of algorithm design?

To solve a problem efficiently using minimal resources.

Explain the relationship between problem type and algorithm efficiency.

Optimization problems can be more computationally intensive than decision problems.

What does it mean for an algorithm to be 'scalable'?

The algorithm maintains reasonable efficiency as the input size increases.

What is the importance of understanding algorithm efficiency?

Helps in choosing the best approach for solving a problem within resource constraints.

How do heuristics relate to unreasonable time complexity?

Heuristics provide practical solutions when a problem has unreasonable time complexity.