Comp sci exam python
Which operator symbol is used by Python to assign values to variables?
=
Which of the following are rules for naming a function?
A function name cannot begin with a number. A function name can contain letters, numbers, or underscores.
What's the purpose of the break statement in Python? (Select all that apply.)
It allows you to break out of a while loop. It allows you to break out of a for loop.
%
Modulus, calculates the remainder of a division operation
* operand
Multiplies one operand by itself the number of times specified by the right operand, in exponential
What's the output of the following program? # text = "Hi there!" print(text)
NameError: name 'text' is not defined
In Python, variables are:
Numbers you can perform calculations on. Snippets of text that you can display on the screen. Names that you can assign to different objects and use to reference those objects throughout your code
Which of the following code examples will cause a runtime error when executed? phrase = "Hello, world" print(phrase) text = "Hello, world" print(text) Phrase = "Hello, world" print(phrase)
Option 3
- operand
Subtracts the right operand from the left one
Write the Python code that assigns the value "super snake" to the variable hero and then prints it:
hero = "super snake" print(hero)
Write a Python if ... else statement in the code box below that sets the variable m to the smaller of an and b.
if a < b: m= a else: m= b
Which of the following keywords are used to create a loop in Python?
while for
Which of the following are valid operators for manipulating numbers in Python?
- / % ** * // +
What's the file extension used for text files containing Python code?
.py
Consider the following loop: for num in range(1, 5): print(num) What's its output?
1 2 3 4
x = 0 while x < 10: x= x+ 1 What's the value of x after the above code snippet executes?
10
+ operator
Adds the left operand to the right operand
/ operand
Divides the left operand by the right operand, yielding a floating point number
Comments are lines of text that subtly change the way a Python program runs
False
Python's float data type guarantees 100 percent accuracy for all numerical operations.
False
Which Boolean value does this Python expression evaluate to? 1 != 1
False
Which Boolean value does this Python expression evaluate to? 123 == "123"
False
In Boolean logic, you compare two items' values and... get one of three values: True, False, or Undefined
Get one of two values: true or false
The basic mechanism that Python uses to control program flow is the _____ statement.
If
// operand
Integer or floor division, divides the left operand by the right operand then rounds the result to the nearest whole number
Which of the following numeric types are built into Python's syntax? Fractions
Integers Floating-point Numbers
When you use a built-in function in your code, what steps occur? The function evaluates, the function produces a side effect, and the function closes.
The function is called, the function executes, and the function returns.
How do you get the absolute value of a number in Python? abs(-3.14)
True
Operator precedence ensures that... 2 * 3 - 1 returns 5 and not 4
True
The Python interpreter takes the code that you write and converts it to the language that the computer's hardware understands. Is this statement True or False?
True
Which Boolean value does this Python expression evaluate to? 1 <= 1
True
Which Boolean value does this Python expression evaluate to? ("A" != "A") or not (2 >= 3)
True
Python provides a Boolean data type. Objects of Boolean type may have one of two values,
True or false
Suppose you have the following variables defined:
a = 100 b = 50
The Boolean logical operators are: and, neither, nor
and, or, not
Which of the following lines of code will create an empty dictionary named captains?
captains = {}
Consider the following function: def square(num): num_squared = num ** 2 return num_squared Which of the following lines of code is the function's signature?
def square(num):
Delete a key from this dictionary: captains = { "Enterprise": "Picard", "Voyager": "Janeway", "Defiant": "Sisko", "Discovery": "unknown", } What statement will remove the entry for the key "Discovery"?
del(captains["Discovery"]) Correct del captains["Discovery"] Correct
Which of the following keywords are for handling errors?
except try
What's the result of the following code? >>> file_content = title_cased_file_content.lower()
file_content is a lowercase version of title_cased_file_content Correct
What's the name of the function for rounding numbers in Python?
round
Write the Python code that prints out the text Hello, world
print("Hello, world")