Exam Questions and CORRECT Answers
[SHORT ANSWER] Assume the string variables firstName and userID are assigned with values.
use string slicing and concatenation to create a username based on the first two letters of the
firstName, concatenated with the last four digits of the userID as shown in the example below.
For Example: Suppose firstName = "Mary" and userID = "W12345678" then, the username
should be:
[SHORT ANSWER] Assume the string variables firstName and userID are assigned with values.
use string slicing and concatenation to create a username based on the first two letters of the
firstName, concatenated with the last four digits of the userID as shown in the example below.
For Example: Suppose firstName = "Mary" and userID = "W12345678" then, the username
should be:
Ma5678 - CORRECT ANSWER - username = firstName[:2] + userID[-4:]
SHORT ANSWER Assume variable gpa has already been assigned a value. Write python code
that displays invalid GPA if the value associated with variable gpa is not in the range [0,4] -
CORRECT ANSWER - if gpa < 0 or gpa > 4:
print("Invalid GPA")
Which type of error prevents the program from running?
human
syntax
grammatical
logical - CORRECT ANSWER - syntax
The smallest storage location in a computer's memory is known as a
,switch
byte
ketter
bit - CORRECT ANSWER - bit
A Python line comment begins with ________.
a. /*
b. $$
c. //
d. # - CORRECT ANSWER - d. #
Which of the following are valid camel case identifier naming styles (Choose all that apply)?
(a) dollar_amount
(b) dollarAmount
(c) dollar$Amount
(d) amtDollar
(e) dollarAmt$
(f) dollarAmt
(g) amountDollar
(h) Dollar_Amount - CORRECT ANSWER - (b) dollarAmount
(d) amtDollar
(f) dollarAmt
(g) amountDollar
Assume the following:
val = 99
,val += 1
Match the code with its output:
a. print('The value is', 'val')
b. print('The value is', val)
print("The value is:", 'val')
The value is val
The value is 100
The value is: val - CORRECT ANSWER - (a) The value is val
(b) The value is 100
(c) The value is: val
Match the relational operators with its meaning:
a. >
b. <
c. >=
d. <=
e. ==
f. != - CORRECT ANSWER - a. Greater than
b. less than
c. Greater than or equal to
d. Less than or equal to
e. Equal to (equality operator)
f. Not equal to
Which of the following will assign a random integer in the range of 1 through 50 to the variable
number?
, (a) number = random(range(1, 50))
(b) random(1, 50) = number
c) number = random.randint(1, 50)
(d) randint(1, 50) = number - CORRECT ANSWER - c) number = random.randint(1, 50)
x < y or z > x What is the result of the following Boolean expression, given that x = 5, y = 3, and
z= 8?
not (x < y or z > x) and y < z
(a) 5
(b) False
(c) 8
(d) True - CORRECT ANSWER - (b) False
What is the result of the following Boolean expression, given that x = 5, y = 3, and z = 8?
x < y or z > x
(a) False
(b) 8
(c) True
(d) 5 - CORRECT ANSWER - (c) True
Consider the following statements. If the input is 85, the output of the following code will be:
score=0