Please help with these review questions
Which of the following is not a legal identifier?
PASS_WORD
pASSWORD
Password
2Password
Which of the following is a legal variable name?
global
name
class
break
An accessor method that returns the value of instance variable X is typically named?
setX
accessX
X
GetX
Keeping the details of how an object is represented inside a class definition is called?
a) Piracy
b) Seclusion
c) Encapsulation
d) Hording
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]:
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)
All of the above
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
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)
In this code,dog, is an example of a(n):
def animal(dog):
Print (“running “, dog)
method
program
function
parameter
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
What is output of the following Python code fragment?
s = “John Doe”
print(s[-3])
h
John
D
N
The variables within an instance of a class:
are called global variables
are called instance variables
are called instance methods
none of the above
A Boolean expression
evaluates to either True or False
cannot be used in a condition
evaluates to 0 & 1
all of the above
Python exceptions can be caught using which one of the following?
if-else
try-except
try-catching
try-accept
Which of the following is not a Python data type?
int
float
Rational
String
Which type of errors must be fixed before the program can be compiled?
syntax errors
logical errors
violations
exceptions
Which of the following data types would you use to store the number 25.62?
str
int
num
float
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
What is the value of my_num after the following statement executes?
my_num = (50 + 2 * 10 – 4) / 2
258
33
156
29
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.
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
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
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)
The identifier can’t start with number. So 2Password is not a legal identifier.
2)
Global, class, break are keywords. Variable name can’t be any keyword.
So name is legal variable name.
3)
An accessor method that returns the value of instance variable x, is typically named x.
4)
Piracy, Seclusion, Hording are not even keywords.
So, how an object is represented inside a class definition is Encapsulation.
5)
List named numbers
The statement is sum=sum+number
The statement not summing up with index, like, sum=sum+numbers[i].
So, the option is
For number in numbers:
Number represent the each number in the list, for every iteration
[Note: please post remaining quesions separately]