Font size
WorksheetsReview for Python 3 Test - Loops and Strings (CodeCademy)
Total questions: 100
Worksheet time: 58mins
What will be the output?
name = "Dave"
print (name)
Dave
'Dave'
name
(name)
name = 'Dave'
greeting = "Good morning" + name
print (greeting)
print("Hello" + str(2) + "world!")
What does the following code do? myAge = int (myAge)
Converts the var (variable) myAge to a string
Converts the var (variable) myAge to a integer
Converts the var (variable) myAge from a integer to a string
Converts the var (variable) myAge to if statement
print("Hello world!" * 2)
people = ["John", "Rob", "Bob"]
print (people[1])
what would this result be?
people = ["John", "Rob", "Bob"]
print (people[-1])
what would this result be?
people = ["John", "Rob", "Bob"]
print (people[4])
what would this result be?
Which one will display when this code is run?
If you want to create an empty list called colours in Python, which of the following is correct?
colours = [ ]
colours = ( )
colours = { }
colours = <>
What would this print?
What is the Python built-in function to converts a number to a string?
str()
int()
parseInt()
convert
Which of these is correct Python conditional statement?
IF answer == "Yes":
if answer == "Yes"
if answer == "Yes":
if answer = "yes":
What is a Count-Controlled loop?
for loop
while loop
A count controlled loop will..
Repeat code until a condition is met
Repeat code a specific amount of times
Repeat code a random amount of times
None of the above
What will this code do?
Print Numbers 1-11
Print Numbers 1-10
Print Numbers 2-10
Print Error
Which of these is NOT a loop in python?
for loop
while loop
nested loop
if loop
Which of these operators means EQUAL TO?
'='
'=>'
'<='
'=='
What will be the output of this code?
1,2,3,4,5,6,7,8,9,10,11,12
0,2,4,6,8,10
2,4,6,8,10,12
2,4,6,8,10
What does the '#' allow you to do?
Repeat code
Comment on code
Print code
End code
Which of these will allow me to count from 0-20 in steps of 5?
for x in range(0,20):
print(x)
while x = (0,20,5):
print(x)
for x in range(0,21,5):
print(x)
for x in range(0,21,5):
print(y)
If i want to continuously check for a correct answer, what loop would i use?
for loop
while loop
What is the error in this code?
incorrect variable name
incorrect indentation
incorrect speech marks
incorrect loop
A condition controlled loop is...
A while loop
A for loop
x = 5
y = 6
print(x*y)
The code above displays the following:
11
x*y
Syntax Error
30
x = 5
y = 6
print("x*y")
The code above displays the following:
11
x*y
Syntax Error
30
Operators used in python:
divide, multiply, add, subtract
/, *, +, -
div, mul, +, -
\, x, +, -
~, *, +, -
In programming, what is iteration?
The repetition of steps within a program
The order in which instructions are carried out
A decision point in a program
Testing a program to make sure it works
Which kind of loop would be used?
I need a program that will keep letting me add money to a piggy bank until I get to £100.
FOR Loop
WHILE Loop
Which kind of loop would be used?
I need a program that will keep letting me add a monthly payment for 12 months.
FOR Loop
WHILE Loop
Which kind of loop would be used?
I need a guessing game program that will let me keep guessing until I get the right answer.
FOR Loop
WHILE Loop
Which kind of loop would be used?
I need a revision program that will run through revision questions 4 times.
FOR Loop
WHILE Loop
The correct syntax of the len function for string x:
x.len()
len[x]
LEN[]
len(x)
Let x= "python is a high level language", the output of print(len(x[-1])) is
1
31
Error
25
Let x= "Python", the output of
print(x[0])
print(x[1])
print(x[2])
PYT
P
y
t
T
h
o
n
oh
Let x= "Python", the output of
print(x[0])
print(x[-1])
print(x[-2])
P
N
O
p
O
n
t
h
y
P
n
o
word = "amazing"
For the given string if we run word[2:5] , what would the result be?
mazi
mazin
azi
azin
For strings, the + operator represents
Concatenation
Addition
Appending
Recantation
data = "No Way!" The expression len(data) evaluates to:
7
9
8
6
Which operator checks if a value is equal to another value?
+
=
==
!=
Which operator checks if a value is not equal to another value?
==
+=
!=
=
C
C
for number in range(6):
print(number)
4
3
2
1
3
2
1
0
Is this the correct code for a list?
register = {"Sam", "Pheobe", Georgia", Richard"}
Yes
No
8
6
4
2
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
break
False
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
What is the result of following Program in python3
if(32%2==0):
print("32")
else:
print("2")
2
32
error
What is the result of following Program in python3
for i in range(3,1):
print(i)
3
2
3
2
1
error
2
1
0
What is the result of following Program in python3
for i in range(1,4):
print(i)
if(i==2):
break;
1
1
2
1
2
3
4
error
Terminates the loop statement and transfers execution to the statement immediately following the loop
pass statement
continue statement
break statement
Causes loop to skip the remainder of its body and immediately retest its condition to reiterating
continue statement
break statement
pass statement
break will break out of an iteration
true
false
continue will move to the next iteration
true
false
How long will a loop continue
As long as the condition is true
Forever
What command forces a loop to stop
break
but
broke
breaking
France
Wales
Wales
France
How do you create a variable with the numeric value 5?
x = int(5)
x = 5
Both the other answers are correct
Which method can be used to remove any whitespace from both the beginning and the end of a string?
trim()
ptrim()
strip()
len()
The correct syntax for converting the string x to uppercase is :
X.upper()
upper(x)
x.UPPER[ ]
x.upper()
The correct syntax for converting the string myname to lowercase is :
myNAME.lower[ ]
myname.lower( )
myname.upper( )
myname_lower(12 )
mountain_name = "Mount Kilimanjaro"
print(mountain_name.find("o"))
What prints to the console
0
1
-1
2
fruit = "Strawberry"
print(fruit.replace('r', 'R'))
What prints in the console?
StRawbeRRy
StRawberry
Strawberry
STRAWBERRY
x = "-".join(["Codecademy", "is", "awesome"])
print(x)
what prints in the console?
Codecademy is awesome
Codecademyisawesome
Codecademy-is-awesome
Codecademy.is.awesome
dinosaur = "T-Rex"
print(dinosaur.upper())
What prints to the console?
T-REX
t-rex
T-Rex
text = "Silicon Valley"
print(text.split())
What prints to the console?
['Silicon', 'Valley']
['Silicon Valley']
['Silicon']
['Valley']
text = "Silicon Valley"
print(text.split('i'))
What prints to the console?
['S', 'l', 'con Valley']
['Silicon, 'll']
['l', "ll]
['Silicon', 'Valley']
my_var = "dark knight"
print(my_var.title())
What prints to the console?
Dark Knight
DARK KNIGHT
DARK Knight
dark knight
greeting = "Welcome To Chili's"
print(greeting.lower())
What prints to the console?
welcome to chili's
Welcome to Chili's
WELCOME TO CHILI'S
Welcome to chili's
text1 = ' apples and oranges '
text1.strip()
What prints to the console?
'apples and oranges'
'apples'
'oranges'
' apples and oranges '
text2 = '...+...lemons and limes...-...'
text2.strip('.')
What prints to the console?
'+...lemons and limes...-'
'lemons'
'limes'
'...lemonsandlimes...'
text2 = '...+...lemons and limes...-...'
text2.strip('.+-')
What prints to the console?
'lemons and limes'
'lemons'
'limes'
'...lemons and limes...'
msg1 = 'Fred scored {} out of {} points.'
msg1.format(3, 10)
What prints to the console?
'Fred scored 3 out of 10 points.'
3,10
3,4,5,6,7,8,9
'Fred scored 3,4,5,6,7,8,9,10 points'
colors = ['red', 'yellow', 'green']
print(len(colors))
What prints to the console?
3
3,6,5
14
2
