1.
What is the value of the expression 100 / 25?
Correct Answer
A. 4.0
Explanation
The value of the expression 100 / 25 is 4.0 because when you divide 100 by 25, the result is 4.
2.
In Python, a variable must be declared before it is assigned a value:
Correct Answer
B. Somewhat false
Explanation
In Python, variables do not need to be explicitly declared before they are assigned a value. Unlike some other programming languages, Python allows variables to be created and assigned a value in a single step. This means that a variable can be created and assigned a value on the same line of code without any prior declaration. Therefore, the statement "Somewhat false" accurately reflects the fact that a variable does not necessarily need to be declared before it is assigned a value in Python.
3.
Given the file dog_breeds.txt, which of the following is the correct way to open the file for reading as a text file?
Correct Answer
D. Open('dog_breeds.txt')
Explanation
The correct way to open the file for reading as a text file is by using the "open('dog_breeds.txt')" command.
4.
Given the file jack_russell.png, which of the following is the correct way to open the file for reading as a binary file?
Correct Answer
C. Open('jack_russell.png', 'rb')
Explanation
The correct way to open the file "jack_russell.png" for reading as a binary file is by using the "open('jack_russell.png', 'rb')" command. The 'rb' mode indicates that the file should be opened in binary mode, allowing for reading binary data from the file.
5.
Whenever possible, what is the recommended way to ensure that a file object is properly closed after usage?
Correct Answer
B. By using the with statement
Explanation
The recommended way to ensure that a file object is properly closed after usage is by using the with statement. The with statement automatically takes care of closing the file object, even if an exception occurs during the execution of the code. This helps to prevent resource leaks and ensures that the file is properly closed, regardless of the outcome of the code execution.
6.
When reading a file using the file object, what method is best for reading the entire file into a single string?
Correct Answer
B. .read()
Explanation
The .read() method is the best method for reading the entire file into a single string when using the file object. This method reads the entire contents of the file and returns it as a single string. The .readline() method reads only a single line from the file, while the .readlines() method returns a list of all the lines in the file. The .read_file_to_str() method does not exist and is not a valid method for reading a file.
7.
Consider the following sequence of statements:
n = 300
m = n
Following execution of these statements, Python has created how many objects and how many references?
Correct Answer
C. One object, two reference
Explanation
The given sequence of statements creates one object, which is assigned to both variables "n" and "m". Therefore, there is only one object created. However, there are two references to this object, as both variables "n" and "m" refer to the same object.
8.
Which of the following are valid Python variable names:
Correct Answer
D. Home_address
Explanation
The variable name "home_address" is a valid Python variable name because it follows the rules for naming variables in Python. Variable names in Python can consist of letters (both uppercase and lowercase), numbers, and underscores, but they cannot start with a number. In this case, "home_address" starts with a letter and only contains letters, numbers, and underscores, making it a valid variable name.
9.
Which of the following are Python reserved words (keywords):
Correct Answer
D. Class
Explanation
"class" is a Python reserved word or keyword. It is used to define a class in Python, which is a blueprint for creating objects. The other options "goto", "default", and "And" are not Python reserved words. "goto" is not a valid keyword in Python, "default" is used in some programming languages like C++ but not in Python, and "And" is not a reserved word in Python (the correct keyword is "and" with a lowercase "a").
10.
Using the same directory structure as before:
animals/
│
├── feline/ ← cwd
│ ├── lions.gif
│ └── tigers.gif
│
├── ursine/
│ └── bears.gif
│
└── animals.csv
Assuming that the current working directory(cwd) is in the feline folder, what is the relative path to the file bears.gif?
Correct Answer
C. ../ursine/bears.gif
Explanation
The correct answer is ".. /ursine/bears.gif". The ".." represents going up one level in the directory structure, so ".. /ursine" refers to the ursine folder which is located at the same level as the feline folder. Then, "bears.gif" represents the file name within the ursine folder.