Final Review: Practice Questions
For each snippet, write the output. If it prints nothing, write “No Output”.
Database
Every problem starts with a fresh copy of this SQLite database. NULL denotes a SQL NULL value. Rows are shown in id order.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
age INTEGER
);
CREATE TABLE messages (
id integer primary key,
sender_id integer not null,
message text not null,
created_at timestamp not null default current_timestamp
);Initial rows:
users(id, username, password, age):
1 | Trump | Trump | 76
2 | Biden | Biden | 79
3 | Evan | correct horse battery staple | 4
4 | Isaac | soccer | 1
5 | Aaron | guaguagua | 0
6 | Teal | 524euTjrWm6uK2C5iw8mC6aNgX1JI78o | 35
7 | Kristen | Possible-Rich-Absolute-Battle | NULL
messages(id, sender_id, message, created_at):
1 | 1 | I'm a baby | 2021-11-14 14:30:00
2 | 2 | I'm a baby | 2021-11-14 14:30:00
3 | 3 | I'm a baby | 2021-11-14 14:33:01
4 | 4 | I'm a baby | 2021-11-15 14:35:45
5 | 3 | I'm actually a toddler | 2021-11-16 14:35:45
6 | 6 | Today in 1918, the Armistice that effectively ended WWI came into effect. | 2021-11-11 11:00:00
7 | 6 | I'm an adult | 2021-11-17 14:35:45
8 | 6 | SQL is the best!! | 2021-11-17 14:35:45
9 | 7 | I'm an adult | 2021-11-17 14:35:45
10 | 7 | WTF is SQL?! I thought you liked the snake thing. | 2021-11-17 14:35:45
Question 1
Use the database above.
routes = {
('GET', '/notes'): 'display notes',
('POST', '/notes'): 'save a note',
}
requests = [('GET', '/notes'), ('POST', '/notes'), ('GET', '/note')]
for request in requests:
print(routes.get(request, 'not found'))Answer
display notes
save a note
not found
Question 2
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
rows = con.execute('SELECT username, age FROM users WHERE id IN (3, 4) ORDER BY id').fetchall()
labels = [name + ': ' + str(age) for name, age in rows]
print(labels)Answer
['Evan: 4', 'Isaac: 1']
Question 3
Use the database above.
row = (4, 'Isaac', 'soccer')
fields = ['id', 'username', 'password']
record = dict(zip(fields, row))
print(record['username'])
print('<h2>' + str(row[0]) + '</h2>')Answer
Isaac
<h2>4</h2>
Question 4
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
cur = con.execute('SELECT username FROM users WHERE id <= 2 ORDER BY id')
print(cur.fetchone())
print(cur.fetchall())
print(cur.fetchone())Answer
('Trump',)
[('Biden',)]
None
Question 5
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
row = con.execute("SELECT username FROM users WHERE id = 99").fetchone()
if row is None:
print('No user')
else:
print(row[0])
print(row)Answer
No user
None
Question 6
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
row = con.execute("SELECT count(*) FROM users WHERE age > 18").fetchone()
print(row[0])Answer
3
Question 7
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
con.execute("INSERT INTO users (password, username) VALUES ('blue', 'Maya')")
con.commit()
row = con.execute("SELECT username, password FROM users WHERE username = 'Maya'").fetchone()
print(row)Answer
('Maya', 'blue')
Question 8
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
con.execute('UPDATE users SET age = 10 WHERE id = 5')
print(con.execute('SELECT age FROM users WHERE id = 5').fetchone()[0])
con.rollback()
print(con.execute('SELECT age FROM users WHERE id = 5').fetchone()[0])Answer
10
0
Question 9
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
con.execute("UPDATE users SET age = 99 WHERE username = 'Teal'")
con.close()
con = sqlite3.connect('twitter_clone.db')
print(con.execute("SELECT age FROM users WHERE username = 'Teal'").fetchone()[0])
con.execute("UPDATE users SET age = 40 WHERE username = 'Teal'")
con.commit()
con.close()
con = sqlite3.connect('twitter_clone.db')
print(con.execute("SELECT age FROM users WHERE username = 'Teal'").fetchone()[0])
con.close()Answer
35
40
Question 10
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
con.execute('DELETE FROM messages WHERE sender_id = 6')
con.commit()
con.execute('DELETE FROM messages WHERE sender_id = 7')
con.close()
con = sqlite3.connect('twitter_clone.db')
print(con.execute('SELECT count(*) FROM messages').fetchone()[0])Answer
7
Question 11
Use the database above.
name = "Evan' OR age < 2--"
sql = "SELECT username FROM users WHERE username='" + name + "' AND age > 18"
print(sql)Answer
SELECT username FROM users WHERE username='Evan' OR age < 2--' AND age > 18
Question 12
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "Isaac'--"
sql = "SELECT username FROM users WHERE username='" + name + "' AND age > 18"
print(con.execute(sql).fetchall())Answer
[('Isaac',)]
Question 13
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "Isaac' OR username='Teal"
sql = "SELECT username FROM users WHERE username='" + name + "' AND age > 18 ORDER BY id"
print(con.execute(sql).fetchall())Answer
[('Isaac',), ('Teal',)]
Question 14
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "O'Neil"
sql = "SELECT id FROM users WHERE username='" + name + "'"
try:
con.execute(sql)
print('Query ran')
except sqlite3.OperationalError:
print('Invalid query')Answer
Invalid query
Question 15
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "Larch', 'blue'), ('Willow"
sql = "INSERT INTO users (username, password) VALUES ('" + name + "', 'blue')"
con.execute(sql)
rows = con.execute("SELECT username FROM users WHERE password = 'blue' ORDER BY id").fetchall()
print(rows)Answer
[('Larch',), ('Willow',)]
Question 16
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "Teal' OR username='Trump"
sql = "SELECT username, age FROM users WHERE username='" + name + "' ORDER BY id"
rows = con.execute(sql).fetchall()
print([name for name, age in rows if age > 50])Answer
['Trump']
Question 17
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
username = "Teal' OR username='Trump"
sql = 'SELECT count(*) FROM users WHERE username = ?'
print(con.execute(sql, (username,)).fetchone()[0])
print(con.execute(sql, ('Teal',)).fetchone()[0])
con.close()Answer
0
1
Question 18
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "O'Neil"
con.execute('INSERT INTO users (username, password) VALUES (?, ?)', (name, 'blue'))
print(con.execute('SELECT username FROM users WHERE password = ?', ('blue',)).fetchone()[0])Answer
O'Neil
Question 19
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
con.execute('UPDATE users SET age = ? WHERE id = ?', (20, 3))
con.execute('UPDATE users SET age = ? WHERE id = ?', (3, 20))
print(con.execute('SELECT age FROM users WHERE id = 3').fetchone()[0])Answer
20
Question 20
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = 'Teal'
try:
con.execute('SELECT id FROM users WHERE username = ?', (name))
except sqlite3.ProgrammingError:
print('Wrong number of values')
print(con.execute('SELECT id FROM users WHERE username = ?', (name,)).fetchone()[0])Answer
Wrong number of values
6
Question 21
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "Larch', 'blue'), ('Willow"
con.execute('INSERT INTO users (username, password) VALUES (?, ?)', (name, 'blue'))
rows = con.execute("SELECT username FROM users WHERE password = 'blue' ORDER BY id").fetchall()
print(len(rows))
print(rows[0][0] == name)Answer
1
True
Question 22
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
name = "Aaron' OR 1=1--"
con.execute('DELETE FROM users WHERE username = ?', (name,))
print(con.execute('SELECT count(*) FROM users').fetchone()[0])
con.execute('DELETE FROM users WHERE username = ?', ('Aaron',))
print(con.execute('SELECT count(*) FROM users').fetchone()[0])Answer
7
6
Question 23
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
value = 'T%'
print(con.execute('SELECT count(*) FROM users WHERE username LIKE ?', (value,)).fetchone()[0])
print(con.execute('SELECT count(*) FROM users WHERE username = ?', (value,)).fetchone()[0])Answer
2
0
Question 24
Use the database above.
from html import escape
message = '<b>Hi</b> & welcome'
rendered = '<p>' + escape(message) + '</p>'
print(rendered)
print('<b>' in rendered)
print(message)Answer
<p><b>Hi</b> & welcome</p>
False
<b>Hi</b> & welcome
Question 25
Use the database above.
from html import escape
note = 'A & B'
escape(note)
print(note)
note = escape(note)
print(note)Answer
A & B
A & B
Question 26
Use the database above.
from html import escape
note = '<b>Hi</b>'
print(escape('<p>' + note + '</p>'))
print('<p>' + escape(note) + '</p>')Answer
<p><b>Hi</b></p>
<p><b>Hi</b></p>
Question 27
Use the database above.
from html import escape
notes = ['<draft>', 'A & B']
items = ['<li>' + escape(note) + '</li>' for note in notes]
print('<ul>' + ''.join(items) + '</ul>')Answer
<ul><li><draft></li><li>A & B</li></ul>
Question 28
Use the database above.
import sqlite3
from html import escape
con = sqlite3.connect('twitter_clone.db')
text = '<i>Tea</i> & cake'
con.execute('UPDATE messages SET message = ? WHERE id = ?', (text, 1))
stored = con.execute('SELECT message FROM messages WHERE id = 1').fetchone()[0]
print(stored == text)
print(escape(stored))Answer
True
<i>Tea</i> & cake
Question 29
Use the database above.
def find_names(name):
return []
cases = [('Nobody', []), ('Teal', ['Teal']), ("Teal'--", [])]
for name, expected in cases:
print(find_names(name) == expected)Answer
True
False
True
Question 30
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
def find_names(name):
sql = "SELECT username FROM users WHERE username='" + name + "' ORDER BY id"
return [row[0] for row in con.execute(sql).fetchall()]
cases = [('Teal', ['Teal']), ('Nobody', []), ("Teal' OR username='Aaron", [])]
for name, expected in cases:
print(find_names(name) == expected)Answer
True
True
False
Question 31
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
con.execute('UPDATE users SET age = ? WHERE id = ?', (8, 3))
print(con.execute('SELECT age FROM users WHERE id = 3').fetchone()[0] == 8)
con.close()
con = sqlite3.connect('twitter_clone.db')
print(con.execute('SELECT age FROM users WHERE id = 3').fetchone()[0] == 8)Answer
True
False
Question 32
Use the database above.
import sqlite3
con = sqlite3.connect('twitter_clone.db')
def find_names(name):
rows = con.execute('SELECT username FROM users WHERE username = ?', (name,)).fetchall()
return [row[0] for row in rows]
cases = [('Teal', ['Teal']), ('Nobody', []), ("Teal' OR username='Aaron", [])]
passed = 0
for name, expected in cases:
if find_names(name) == expected:
passed += 1
print(passed)Answer
3