Please help with this review
1. Which of the following is not a legal identifier?
PASS_WORD
pASSWORD
Password
2Password
2. Which of the following is a legal variable name?
global
name
class
break
3. An accessor method that returns the value of instance variable X is typically named?
setX
accessX
X
getX
4. Keeping the details of how an object is represented inside a class definition is called?
a) Piracy
b) Seclusion
c) Encapsulation
d) Hording
5. What is the missing line from the following fragment of code designed to add up the numbers in a list?
numbers = []
sum =
#missing here
sum = sum + number
for number in range(len(nums)):
for number in range(nums):
for number in numbers::
for number in nums[i]:
6. Suppose myList = [1,2,3, 4]. Which of the following correctly deletes the value 2 from myList?
myList.remove(2)
del myList[1]
myList.pop(1)
myList.delete(1)
7 Python lists are different from arrays found in other languages because
lists can be accessed by indexing
lists grow and shrink as needed
lists items can be changed via assignment
lists can store a large collection in a single variable
8. Given the code below, suppose that ‘words’ is a list of characters (string). The following code is supposed to count the number of occurrences of each characters. What is the missing line?
counts = { }
words = ‘Here we go again’
for c in words:
counts[c] = counts[c] + 1
counts[c] = counts.get(c,0) + 1
counts[c].add(1)
counts.append(c)
9. In this code,dog, is an example of a(n):
def animal(dog):
Print (“running “, dog)
method
program
function
parameter
10. Suppose that the variable x currently has integer value 20. What statement would cause the value of x to become 15?
x = 20
x = -x + -5
x = x- + -5
x = x – -5
x = x + -5
OOD stands for:
open object development
other object design
object oriented design
none of the above
11. What is output of the following Python code fragment?
s = “John Doe”
print(s[-3])
h
John
D
N
12. The variables within an instance of a class:
are called global variables
are called instance variables
are called instance methods
none of the above
13. A Boolean expression
evaluates to either True or False
cannot be used in a condition
evaluates to 0 & 1
all of the above
14. Python exceptions can be caught using which one of the following?
if-else
try-except
try-catching
try-accept
15. Which of the following is not a Python data type?
int
float
Rational
String
16. Which type of errors must be fixed before the program can be compiled?
syntax errors
logical errors
violations
exceptions
17. Which of the following data types would you use to store the number 25.62?
str
int
num
float
18. Which of the following defines a constructor that initializes one attribute?
def __init__(name):
this.name = name
def __init__(this, name):
name = this.name
def __init__(name):
self.name = name
def __init__(self, name):
self.name = name
19. What is the value of my_num after the following statement executes?
my_num = (50 + 2 * 10 – 4) / 2
258
33
156
29
20. Which of the following in this expression is evaluated first?
age >= 65 or age <= 18 or status == “retired”
age >= 65
status == “retired”
age <= 18
age >= 65 or age <= 18
Which of the following is true for an if statement that has both elif and else clauses?
If the condition in an elif clause is true, the statements in that clause are executed.
If the condition in the if clause is true, the statements in that clause are executed.
If the condition in the else clause is true, the statements in that clause are executed.
The statements in the else clause are always executed.
21. Which of the following is not an acceptable way to code a nested structure?
nest a for loop inside a while loop
nest an if statement inside a for loop
nest a while loop inside an elif clause
nest an else clause within an elif clause
22. Before you can use a standard module like the random module, you need to
import the module
import the module into a custom namespace
import the module into the global namespace
import the module into its default namespace
23. To work with a file when you’re using Python, you must do all but one of the following.
open the file
decode the data in the file
write data to or read data from the file
close the file
Expert Answer
1. Which of the following is not a legal identifier?
PASS_WORD
pASSWORD
Password
2Password [The first character must be a letter or special symbol underscore( _ )].
2. Which of the following is a legal variable name?
Global [The following shows the reserved words and these reserved words may not be used as constant or variable or any other identifier names. These are name, class, break.
name
class
break
3. An accessor method that returns the value of instance variable X is typically named?
setX
accessX
X
getX [ in Accessor methods getRed, getGreen, and getBlue these methods usually access a value].
4. Keeping the details of how an object is represented inside a class definition is called?
a) Piracy
b) Seclusion
c) Encapsulation [Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse.].
d) Hording
5. What is the missing line from the following fragment of code designed to add up the numbers in a list?
numbers = []
sum = sum(numbers[0:len(numbers)]) or sum = sum(numbers)
#missing here
sum = sum + number
for number in range(len(nums)):
for number in range(nums):
for number in numbers::
for number in nums[i]:
6. Suppose myList = [1,2,3, 4]. Which of the following correctly deletes the value 2 from myList?
myList.remove(2)
del myList[1]
myList.pop(1)
myList.delete(1)
[The above 3 will give the correct answer 4th syntax is array.remove(array[0])]
7 Python lists are different from arrays found in other languages because
lists can be accessed by indexing
lists grow and shrink as needed [list2 = [1, 2, 3, 4, 5 ];]
lists items can be changed via assignment
lists can store a large collection in a single variable
8. Given the code below, suppose that ‘words’ is a list of characters (string). The following code is supposed to count the number of occurrences of each characters. What is the missing line?
counts = { }
words = ‘Here we go again’
for c in words:
counts[c] = counts[c] + 1
counts[c] = counts.get(c,0) + 1
counts[c].add(1)
counts.append(c)
9. In this code,dog, is an example of a(n):
def animal(dog):
Print (“running “, dog)
method
program
function
parameter
10. Suppose that the variable x currently has integer value 20. What statement would cause the value of x to become 15?
x = 20
x = -x + -5 =-25
x = x- + -5 =25
x = x – -5 =25
x = x + -5 =15
OOD stands for:
open object development
other object design
object oriented design
none of the above
11. What is output of the following Python code fragment?
s = “John Doe”
print(s[-3])
h
John
D
N
12. The variables within an instance of a class:
are called global variables
are called instance variables
are called instance methods
none of the above
13. A Boolean expression
evaluates to either True or False
cannot be used in a condition
evaluates to 0 & 1
all of the above
14. Python exceptions can be caught using which one of the following?
if-else
try-except
try-catching
try-accept
15. Which of the following is not a Python data type?
int
float
Rational
String
16. Which type of errors must be fixed before the program can be compiled?
syntax errors
logical errors
violations
exceptions
17. Which of the following data types would you use to store the number 25.62?
str
int
num
float
18. Which of the following defines a constructor that initializes one attribute?
def __init__(name):
this.name = name
def __init__(this, name):
name = this.name
def __init__(name):
self.name = name
def __init__(self, name):
self.name = name
19. What is the value of my_num after the following statement executes?
my_num = (50 + 2 * 10 – 4) / 2
258
33
156
29
20. Which of the following in this expression is evaluated first?
age >= 65 or age <= 18 or status == “retired”
age >= 65
status == “retired”
age <= 18
age >= 65 or age <= 18
Which of the following is true for an if statement that has both elif and else clauses?
If the condition in an elif clause is true, the statements in that clause are executed.
If the condition in the if clause is true, the statements in that clause are executed.
If the condition in the else clause is true, the statements in that clause are executed.
The statements in the else clause are always executed.
21. Which of the following is not an acceptable way to code a nested structure?
nest a for loop inside a while loop
nest an if statement inside a for loop
nest a while loop inside an elif clause
nest an else clause within an elif clause
22. Before you can use a standard module like the random module, you need to
import the module
import the module into a custom namespace
import the module into the global namespace
import the module into its default namespace
23. To work with a file when you’re using Python, you must do all but one of the following.
open the file
decode the data in the file
write data to or read data from the file
close the file