Download CIT 486 Final Exam Study Guide - Python Programming and more Exams Programming Languages in PDF only on Docsity!
CIT 486 FINAL EXAM QUESTIONS WITH
CORRECT ANSWERS BEST QUALITY EXAM
TESTED AND APPROVED 2025/2026 LATEST
Which of the following statements is false?
- True division (/) divides a numerator by a denominator and yields a floating- point number with a decimal point. Floor division (//) divides a numerator by a denominator, yielding the highest integer that's not greater than the result. Python truncates (discards) the fractional part.
- The expression - 13 / 4 evaluates to - 3.25.
- The expression - 13 // 4 evaluates to - 3. --CORRECT ANSWER--The expression - 13 // 4 evaluates to - 3. Which of the following statements is false?
- Python requires you to indent the statements in suites.
- Incorrect indentation of the statements in a suite can cause errors.
- Using the assignment symbol (=) instead of the equality operator (==) in an if statement's condition is a common logic error.
- Using == in place of = in an assignment statement can lead to subtle problems. --CORRECT ANSWER--Using the assignment symbol (=) instead of the equality operator (==) in an if statement's condition is a common logic error.
DO NOT COPY
Which of statements a), b) or c) is false?
- A variable name, such as x, is an identifier.
- Each identifier may consist of letters, digits and underscores (_) but may not begin with a digit.
- Python is case insensitive, so number and Number are the same identifier despite the fact that one begins with a lowercase letter and the other begins with an uppercase letter.
- All of the above statements are true. --CORRECT ANSWER--Python is case insensitive, so number and Number are the same identifier despite the fact that one begins with a lowercase letter and the other begins with an uppercase letter. Which of the following statements is false?
- A string delimited by single quotes may include double-quote characters: print('Display "hi" in quotes')
- A string delimited by double quotes may include single quote characters: print("Display the name O'Brien")
- A string delimited by double quotes may not include double quotes.
- To avoid using ' and " inside strings, you can enclose such strings in triple quotes: print(""""Display the name O'Brien"""") --CORRECT ANSWER--A string delimited by double quotes may not include double quotes.
DO NOT COPY
The right side of the = symbol always executes first, then the result is assigned to the variable on the symbol's left side. --CORRECT ANSWER--The = symbol is the assignment operator. Which of the following statements is incorrect: min(17, 19, 23, 29, 31, 37, 43) is a valid call to built-in function min, The range of values is simply the minimum through the maximum value. Much of data science is devoted to getting to know your data. All of the above are correct. --CORRECT ANSWER--All of the above are correct. When using IPython in interactive mode to evaluate a simple arithmetic expression in a new session, which of the following is false? The text "In [1]:" is a prompt, indicating that IPython is waiting for your input. You can type? for help or you can begin entering snippets After you type an expression like 45 + 72 and press Enter, IPython reads the snippet, evaluates it and prints its result in Out[1]:.
DO NOT COPY
Then IPython displays the In [2] prompt to show that it's waiting for you to enter your second snippet Python uses x for multiplication and the forward slash (/) for division. -- CORRECT ANSWER--Python uses x for multiplication and the forward slash (/) for division. Which of the following statements is false?
- When you evaluate expressions in interactive mode, the text that print displays is preceded by Out[] with a snippet number in the square brackets.
- print does not display a string's quotes, though there is a way to display quotes in strings.
- You also may enclose a string in double quotes ("), as in: print("Welcome to Python!") but Python programmers generally prefer single quotes.
- When print completes its task, it positions the screen cursor at the beginning of the next line. --CORRECT ANSWER--When you evaluate expressions in interactive mode, the text that print displays is preceded by Out[] with a snippet number in the square brackets. Which of the following statements about libraries is false?
- Using existing libraries helps you avoid "reinventing the wheel," thus leveraging your program-development efforts.
DO NOT COPY
- Parentheses have the highest level of precedence, so expressions in parentheses evaluate first—thus, parentheses may force the order of evaluation to occur in any sequence you desire.
- In expressions with nested parentheses, such as (a / (b - c)), the expression in the innermost parentheses (that is, b - c) evaluates first.
- If an expression contains several exponentiation operations, Python applies them from left to right. --CORRECT ANSWER--If an expression contains several exponentiation operations, Python applies them from left to right. What is the output of the following code for Out[5]: In [4]: x = 'dog'In [5]: type(x)Out[5]: --CORRECT ANSWER--str Which of the following statements about the IPython session below is true? In [1]: gender = 'Female' In [2]: age = 70 In [3]: if gender == 'Female' and age >= 65: ...: print('Senior female') ...: Senior female --CORRECT ANSWER--All of the above statements are true
DO NOT COPY
Which of the following statements is false?
- Augmented assignments abbreviate assignment expressions in which the same variable name appears on the left and right of the assignment's =, as total does in: for number in [1, 2, 3, 4, 5]:total = total + number
- The following code re-implements the preceding for statement using an addition augmented assignment (+=) statement: for number in [1, 2, 3, 4, 5]:total += number
- The statement f = f ** 3 may be replaced by the more concise augmented assignment statement f *= 3.
- All of the above statements are true. --CORRECT ANSWER--The statement f = f ** 3 may be replaced by the more concise augmented assignment statement f *= 3. Which of the following statements is false? The descriptive statistics mean, median and mode are measures of central tendency—each is a way of producing a single value that is in some sense typical of the others. The following session creates a list called grades, then uses the built-in sum and len functions to calculate the median "by hand"—sum calculates the total of the grades (397) and len returns the number of grades (5): In [1]: grades = [85, 93, 45, 89, 85]
DO NOT COPY
- Something in a while statement's suite must ensure that the condition eventually becomes False. Otherwise, a logic error called an infinite loop occurs.
- In applications executed from a Terminal, Command Prompt or shell, type Ctrl
- c or control + c (depending on your keyboard) to terminate an infinite loop. -- CORRECT ANSWER--The following code finds the first power of 3 larger than 50: product = 3while product < 50:product = product * 3 Which of the following statements is false? Function range's one-argument version produces a sequence of consecutive integers from 0 up to, but not including, the argument's value. The following snippet produces the sequence 5 6 7 8 9. for number in range(5, 10):print(number, end=' ') The following snippet produces the sequence 0 2 4 6 8. for number in range(0, 10, 2):print(number, end=' ') The following snippet produces the sequence 10 8 6 4 2 0. for number in range(10, 0, - 2):print(number, end=' ') --CORRECT ANSWER-- The following snippet produces the sequence 10 8 6 4 2 0. for number in range(10, 0, - 2):print(number, end=' ') In the following code segment:
DO NOT COPY
process 10 students
for student in range(10):
get one exam result
result = int(input('Enter result (1=pass, 2=fail): ')) if result == 1: passes = passes + 1 else: failures = failures + 1 --CORRECT ANSWER--The if statement is nested in the for statement. What does the following for statement print? for counter in range(10): print(counter, end=' ') --CORRECT ANSWER--0 1 2 3 4 5 6 7 8 9 What does the following line of code display? print(10, 20, 30, sep=', ') --CORRECT ANSWER--10, 20, 30 Which of the following statements is false? The argument of each of the functions mean, median and mode must be an iterable.
DO NOT COPY
we accidentally write: grade == 85 then grade would be undefined and we'd get a NameError. If grade had been defined before the statement grade == 85, the statement would evaluate to True or False, depending on grade's value, and not perform the intended assignment—fortunately, this is harmless. All of the above statements are true. --CORRECT ANSWER--If grade had been defined before the statement grade == 85, the statement would evaluate to True or False, depending on grade's value, and not perform the intended assignment—fortunately, this is harmless. Which of the following statements is false. You can import all identifiers defined in a module with a wildcard import of the form from modulename import * A wildcard import makes all of the module's identifiers available for use in your code. The following session is an example of safe use of a wildcard import: In [1]: e = 'hello'In [2]: from math import *In [3]: eOut[3]: 2. Importing a module's identifiers with a wildcard import can lead to subtle errors—it's considered a dangerous practice that you should avoid. --CORRECT
DO NOT COPY
ANSWER--The following code calls function square twice. The first call produces the int value 49 and the second call produces the int value 0: In [1]: def square(number):...: """Calculate the square of a number.""" ...: return number ** 2 ...: In [2]: square(7)Out[2]: 49In [3]: square(.1)Out[3]: 0 The following code produces 10 random integers in the range 1-6 to simulate rolling a six-sided die: import randomfor roll in range(10):print(random.randrange(1, 6), end=' ') -- CORRECT ANSWER--False Which of the following statements is false? A function definition begins with the def keyword, followed by the function name, a set of parentheses and a semicolon (;). When you change a function's code, all calls to the function execute the updated version. The following code calls square first, then print displays the result: print('The square of 7 is', square(7)) The required parentheses contain the function's parameter list—a comma- separated list of parameters representing the data that the function needs to perform its task. --CORRECT ANSWER--A function definition begins with the def keyword, followed by the function name, a set of parentheses and a semicolon (;).
DO NOT COPY
- Each function should perform a single, well-defined task.
- The following code calls function square twice. The first call produces the int value 49 and the second call produces the int value 0: In [1]: def square(number):...: """Calculate the square of a number.""" ...: return number ** 2 ...: In [2]: square(7)Out[2]: 49In [3]: square(.1)Out[3]: 0
- The statements defining a function arewritten only once, but may be called "to do their job" from many points in a program and as often as you like.
- Calling square with a non-numeric argument like 'hello' causes a TypeError because the exponentiation operator (**) works only with numeric values. -- CORRECT ANSWER--The following code calls function square twice. The first call produces the int value 49 and the second call produces the int value 0: In [1]: def square(number):...: """Calculate the square of a number.""" ...: return number ** 2 ...: In [2]: square(7)Out[2]: 49In [3]: square(.1)Out[3]: 0 Based on the following function definition: def rectangle_area(length, width):"""Return a rectangle's area.""" return length * width The following call results in results in TypeError because the order of keyword arguments matters—they need to match the corresponding parameters' positions in the function definition: rectangle_area(width=5, length=10) --CORRECT ANSWER--False Which of the following statements is false?
DO NOT COPY
- If randrange truly produces integers at random, every number in its range has an equal probability (or chance or likelihood) of being returned each time we call it.
- We can use Python's underscore (_) digit separator to make a value like 3000000 more readable as 3_000_000.
- The expression range(6,000,000) would be incorrect. Commas separate arguments in function calls, so Python would treat range(6,000,000) as a call to range with the three arguments 6, 0 and 0.
- All of the above statements are true. --CORRECT ANSWER--All of the above statements are true. What is the output of the following code: min('yellow', 'red', 'orange', 'blue', 'green') --CORRECT ANSWER--blue What does the following for statement print? for counter in range(4): print(counter, end=' ') --CORRECT ANSWER--0 1 2 3 For variables x = 'a' and y = 'b', what will the following statement display: print((x + y), 'and', (y + x)) --CORRECT ANSWER--ab and ba Fill in the missing code to replace *** in the following code with a statement : if grade >= 90:
DO NOT COPY
Write the statement that creates a slice consisting of the elements at indices 2 through 6 of a numbers: numbers = [2, 3, 5, 7, 11, 13, 17, 19] --CORRECT ANSWER--numbers[2:6] Which of the following statement(s) will reverse the order of the below items list numbers: numbers = [2, 3, 4, 6, 7, 13, 17, 19]
- reversed_numbers = [item for item in reversed(numbers)]
- The following code concisely creates a new list in reverse order: numbers[::-1]
- To sort a list in descending order, call list method sort with the optional keyword argument reverse=True. numbers.sort(reverse=True)
- All of the above statements are true. --CORRECT ANSWER--All of the above statements are true. Replace the ??? in the following comprehension statement to include in list only the even values produced by the for clause:
DO NOT COPY
list4 = [item for item in range(1, 11) if item ???] --CORRECT ANSWER--% 2 == 0 Which of the following statements is false? Tuples are immutable. Tuples must store heterogeneous data. A tuple's length is its number of elements. A tuple's length cannot change during program execution. --CORRECT ANSWER--Tuples must store heterogeneous data. List comprehensions can replace many for statements that iterate over existing sequences and create new lists, such as: list1 = [] for item in range(1, 6): Write the statement that can accomplish the same task in a single line of code with a list comprehension: --CORRECT ANSWER--list1 = [item for item in range(1, 6)] Consider the list color_names: color_names = ['orange', 'yellow', 'green'] Write the statement that uses the insert method to insert 'red' at index 0. -- CORRECT ANSWER--color_names.insert(0, 'red')
DO NOT COPY