Using Programs with Data - AP Computer Science Principles
- What does 'break' do in a loop?
- Exits the loop immediately. Break statement terminates loop execution completely.
- Identify the error in 'for i in 5: print(i)'
- 'range()' is needed to iterate through integers. Cannot iterate directly over integer; range creates sequence.
- Which data structure uses key-value pairs?
- Dictionary. Dictionaries map unique keys to corresponding values.
- What is a string in programming?
- A sequence of characters. Text data type enclosed in quotes or apostrophes.
- Identify the correct syntax for a Python function call.
- function_name(arguments). Parentheses after name pass values to function parameters.
- Choose the correct syntax to declare an integer variable in Python.
- variable_name = 0. Python uses assignment operator to create variables dynamically.
- What does 'len()' function return?
- The number of items in an object. Built-in function counts elements in sequences and collections.
- What is the purpose of 'continue' in a loop?
- Skips the current iteration and continues with the next. Continue jumps to next iteration without finishing current one.
- Identify the error in the expression:
- Type error: mixing integer and string. Cannot add integer and string directly without type conversion.
- Identify the error: 'if x = 10:'
- Use '==' for comparison, not '='. Assignment operator should be comparison operator for conditions.
- State the syntax to access the third element of a list in Python.
- list_name[2]. List indexing starts at 0, so third element is index 2.
- Identify the error in this function definition: 'def add(a, b): return a + b'
- No error; the function is correct. This function definition follows proper Python syntax rules.
- Identify the error in this loop: 'for i in range(5) print(i)'
- Missing colon after range(5). Python requires colon after for statement header.
- What is the purpose of the 'if' statement in programming?
- To execute code conditionally based on a boolean expression. It evaluates the boolean and runs code only when true.
- What is an 'else' clause used for?
- To execute code when an 'if' condition is false. Else provides alternative code path for false conditions.
- What is the result of in Python?
- 1. Floor division operator returns integer quotient without remainder.
- Which data type is used for true/false values?
- Boolean. Boolean data type represents logical values: True or False.
- Which keyword defines a function in Python?
- 'def'. Keyword that declares new function definitions in Python.
- What does the 'return' statement do in a function?
- It exits the function and optionally returns a value. Return sends a value back to the caller and stops execution.
- Which data type would you use for a decimal number in Python?
- Float. Float type handles numbers with decimal points.
- What is the main difference between a list and a tuple?
- Lists are mutable; tuples are immutable. Lists can be modified; tuples cannot be changed.
- What type is returned by 'input()' function in Python?
- String. Input always returns text even for numeric entries.
- What is the syntax for a for-each loop in Python?
- for item in iterable:. Standard Python loop syntax for iterating collections.
- What is the purpose of a function in programming?
- To encapsulate code for reuse and organization. Functions group code into reusable blocks with optional parameters.
- Which keyword is used to start a loop in Python?
- 'for' or 'while'. Both keywords create repetitive execution structures in Python.
- What function converts a string to an integer in Python?
- 'int()'. Built-in function converts string numbers to integer type.
- What is the purpose of a 'for' loop?
- To iterate over a sequence of items. For loops process each item in collections automatically.
- What does the 'append' method do to a list?
- Adds an element to the end of the list. Append method inserts new item at list's final position.
- Choose the correct way to start a multiline comment in Python.
- Use triple quotes: '''comment'''. Triple quotes allow comments spanning multiple lines.
- What is the value of ?
- 8. Two raised to the third power equals eight.
- What is the output of 'print("Hello, World!")'?
- Hello, World. Print function displays text exactly as written inside quotes.
- How is a comment started in Python?
- Using '#'. Hash symbol creates single-line comments in Python code.
- Which operator is used for exponentiation in Python?
- '**'. Double asterisk performs mathematical power operations.
- Identify the error: 'list.append(x)' when 'list' is not defined.
- 'list' must be defined before use. Variable name must exist before calling methods on it.
- What is a variable in programming?
- A named storage location for data. Variables hold values that can be accessed and modified by name.
- What is an array?
- A collection of elements identified by index. Arrays store multiple values accessible by numeric position.
- Identify the component of a loop that determines when it stops.
- Loop condition or termination condition. This boolean expression controls when the loop continues or stops.
- What is the result of ?
- 20. Order of operations: parentheses first, then multiplication.
- What is a 'while' loop used for?
- To repeat code while a condition is true. While loops continue until condition becomes false.