P1(algorithms and coding) mock content Flashcards

1
Q

What is string concatenation?

A

String concatenation is the operation of joining two or more strings end-to-end to form a single string.

Example: “Hello, “ + “World!” results in “Hello, World!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you find the length of a string in Python?

A

You use the len() function to find the number of characters in a string.

Example: len(“Hello”) results in 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a substring?

A

A substring is a portion of a string, extracted from the original string based on specified start and end indices.

Example: “abcdef”[1:4] results in “bcd”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access a specific character in a string in Python?

A

You can access a specific character in a string using its index.

Example: “Hello”[1] results in “e”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What function would you use to replace part of a string in Python?

A

The function used is replace(), which replaces specified parts of the string with a new substring.

Example: “Hello World”.replace(“World”, “Python”) results in “Hello Python”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the strip() function do to a string in Python?

A

The strip() function removes whitespace from both the beginning and end of a string.

Example: “ Hello “.strip() results in “Hello”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you split a string into an array of substrings in Python?

A

You use the split() method, which splits a string based on a delimiter.

Example: “a,b,c”.split(“,”) results in [‘a’, ‘b’, ‘c’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you convert all characters in a string to uppercase in Python?

A

You use the upper() method.

Example: “hello”.upper() results in “HELLO”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the find() method in Python?

A

The find() method returns the index of the first occurrence of a specified substring within a string.

Example: “Hello World”.find(“World”) results in 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is pseudocode?

A

Pseudocode is a simplified, informal way of writing programming logic that uses plain English to describe the steps in an algorithm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is pseudocode useful?

A

Pseudocode helps programmers plan and visualize the logic of their algorithms before writing actual code. It focuses on logic rather than syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Read the following pseudocode and determine the output: SET total TO 0 FOR each number FROM 1 TO 5 SET total TO total + number ENDFOR OUTPUT total

A

The output is 15. This loop adds the numbers 1 through 5, resulting in 1 + 2 + 3 + 4 + 5 = 15.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the following pseudocode do? SET count TO 0 FOR each item IN list IF item > 10 THEN INCREMENT count BY 1 ENDIF ENDFOR OUTPUT count

A

The pseudocode counts the number of items in the list that are greater than 10 and outputs the count.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is decomposition?

A

Decomposition is the process of breaking down a problem into a number of sub-problems, so that each sub-problem accomplishes an identifiable task, which might itself be further subdivided.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is abstraction?

A

The process of removing unnecessary detail for my problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you define a function in Python?

A

You use the def keyword followed by the function name and parentheses.

Example:
def greet(name):
print(f”Hello, {name}”)

17
Q

What is the purpose of the return statement in Python?

A

The return statement exits a function and optionally passes back an expression to the caller.

Example:
def add(a, b):
return a + b

18
Q

How do you call a function in Python?

A

You call a function by using its name followed by parentheses.

Example:
greet(“Alice”)

19
Q

Read the code and determine the output: def add(a, b): return a + b print(add(3, 4))

A

The output is 7.

20
Q

What will the output be for the following code? for i in range(3): print(i)

A

The output will be:
0
1
2

21
Q

What is the purpose of a trace table?

A

A trace table helps track the values of variables at each step of an algorithm to ensure the logic is correct.

22
Q

Complete the trace table for the following code: total = 0 for i in range(1, 4): total += i print(total)

A

i total
1 1
2 3
3 6

23
Q

What is the trace table for the following pseudocode? SET sum TO 0 FOR i FROM 1 TO 3 SET sum TO sum + i ENDFOR

A

i sum
1 1
2 3
3 6

24
Q

Write pseudocode to find the largest number in a list.

A

SET max TO list[0] FOR each number IN list IF number > max THEN SET max TO number ENDIF ENDFOR OUTPUT max

25
Q

Write Python code to find the largest number in a list.

A

def find_max(numbers):
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
print(find_max([3, 7, 2, 9, 5]))

26
Q

What are the key differences between linear search and binary search?

A

Linear search checks each element in the list sequentially until the target element is found or the list ends, making it simple but potentially slow. Binary search, on the other hand, requires a sorted list and divides the search interval in half repeatedly, which makes it much faster but more complex.

27
Q

Identify and correct the syntax error: print(“Hello, World!)

A

Error: Missing closing quotation mark. Corrected code:
print(“Hello, World!”)

28
Q

Identify and correct the syntax error: for i in range(3) print(i)

A

Error: Missing colon after the for loop. Corrected code:
for i in range(3):
print(i)

29
Q

What is a common cause of syntax errors?

A

Syntax errors are often caused by mistakes in the code’s structure, such as missing punctuation, incorrect indentation, or misusing keywords.

30
Q

What is a syntax error?

A

A syntax error occurs when the code violates the rules of the programming language, making it impossible to parse.

31
Q

What is a logic error?

A

A logic error occurs when the code runs without crashing but produces incorrect results due to faulty logic.

32
Q

What is unit testing?

A

Unit testing involves testing individual components or functions of a program to ensure they work correctly.

33
Q

What is integration testing?

A

Integration testing checks how different parts of a program work together.

34
Q

What is system testing?

A

System testing evaluates the entire system’s functionality to ensure it meets requirements.

35
Q

What is acceptance testing?

A

Acceptance testing determines if the system meets the user’s needs and is ready for deployment.

36
Q

What does the mod operator do in Python?

A

The mod operator (%) returns the remainder of a division.