P1(algorithms and coding) mock content Flashcards
What is string concatenation?
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 do you find the length of a string in Python?
You use the len() function to find the number of characters in a string.
Example: len(“Hello”) results in 5
What is a substring?
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 do you access a specific character in a string in Python?
You can access a specific character in a string using its index.
Example: “Hello”[1] results in “e”
What function would you use to replace part of a string in Python?
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”
What does the strip() function do to a string in Python?
The strip() function removes whitespace from both the beginning and end of a string.
Example: “ Hello “.strip() results in “Hello”
How do you split a string into an array of substrings in Python?
You use the split() method, which splits a string based on a delimiter.
Example: “a,b,c”.split(“,”) results in [‘a’, ‘b’, ‘c’]
How can you convert all characters in a string to uppercase in Python?
You use the upper() method.
Example: “hello”.upper() results in “HELLO”
What is the purpose of the find() method in Python?
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
What is pseudocode?
Pseudocode is a simplified, informal way of writing programming logic that uses plain English to describe the steps in an algorithm.
Why is pseudocode useful?
Pseudocode helps programmers plan and visualize the logic of their algorithms before writing actual code. It focuses on logic rather than syntax.
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
The output is 15. This loop adds the numbers 1 through 5, resulting in 1 + 2 + 3 + 4 + 5 = 15.
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
The pseudocode counts the number of items in the list that are greater than 10 and outputs the count.
What is decomposition?
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.
What is abstraction?
The process of removing unnecessary detail for my problem.