1. Home
  2. Explore
  3. Computer Science
  4. CS50 Week 7: Data Tools

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

CS50 Week 7: Data Tools

  • 68Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Data and CSV

Why might a programmer choose SQL instead of Python for working with very large datasets?

68 questions

  1. 1

    Why might a programmer choose SQL instead of Python for working with very large datasets?

    1. A

      SQL automatically converts all data into machine code before execution

    2. B

      SQL is designed specifically to query and manipulate database data efficiently

    3. C

      SQL eliminates the need to store data in files or database tables

    4. D

      SQL is the only language that can represent numerical values

    Correct answer
    B

    SQL is designed specifically to query and manipulate database data efficiently

    Explanation

    SQL is a database-centric language designed for querying and manipulating data, often allowing database tasks to be expressed more concisely than in general-purpose Python code.

  2. 2

    What is a flat-file database in the context of the lecture?

    1. A

      A binary database containing only numerical values

    2. B

      A database that stores every value in a separate program

    3. C

      A spreadsheet that must contain exactly one row

    4. D

      A text file containing data, often separated by delimiters

    Correct answer
    D

    A text file containing data, often separated by delimiters

    Explanation

    A flat-file database is essentially a text file containing data, commonly organized with delimiters such as commas or tabs.

  3. 3

    How do CSV files represent rows and columns using plain text?

    1. A

      Spaces separate rows, and quotation marks separate columns

    2. B

      Commas separate rows, and newline characters separate columns

    3. C

      Newline characters separate rows, and commas commonly separate columns

    4. D

      Binary markers separate rows, and tabs always separate columns

    Correct answer
    C

    Newline characters separate rows, and commas commonly separate columns

    Explanation

    CSV stands for comma-separated values. Newline characters separate records, while commas commonly separate values within each record.

  4. 4

    Why might a CSV exporter place quotation marks around a field such as "Hello, World"?

    1. A

      The field contains a comma that could otherwise be mistaken for a column delimiter

    2. B

      The field is always the first value in every CSV record

    3. C

      The field contains a number that must be converted into text

    4. D

      The field contains a newline that separates two database tables

    Correct answer
    A

    The field contains a comma that could otherwise be mistaken for a column delimiter

    Explanation

    Quotation marks protect commas that are part of a field's actual text so that a CSV reader does not interpret them as separators between columns.

  5. 5

    What was the purpose of the header row in the exported favorites CSV file?

    1. A

      It stored the first student's responses before the other rows

    2. B

      It recorded the database password needed to open the file

    3. C

      It converted the CSV file from text into binary format

    4. D

      It identified the meanings of the columns, such as timestamp, language, and problem

    Correct answer
    D

    It identified the meanings of the columns, such as timestamp, language, and problem

    Explanation

    The header row contained column names and therefore described the data rather than representing an ordinary response.

  6. 6

    What does Python's csv.reader return for each row of a CSV file?

    1. A

      A list whose elements correspond to the row's columns

    2. B

      A tuple containing only the first and last columns

    3. C

      A single string containing the entire file

    4. D

      A dictionary whose keys are automatically generated database names

    Correct answer
    A

    A list whose elements correspond to the row's columns

    Explanation

    The CSV reader yields rows as lists. For the favorites data, the three columns could be accessed using indices 000, 111, and 222.

  7. 7

    Why was next(reader) used before looping through the rows with Python's CSV reader?

    1. A

      It advanced directly to the last row of the file

    2. B

      It skipped the header row so the program would process only response data

    3. C

      It converted each row from a list into a dictionary

    4. D

      It closed the file before the loop began

    Correct answer
    B

    It skipped the header row so the program would process only response data

    Explanation

    The first row was metadata describing the columns, so next(reader) consumed it before the program processed the actual data rows.

  8. 8

    What advantage does Python's DictReader provide over the basic CSV reader?

    1. A

      It converts every field into an integer automatically

    2. B

      It automatically sorts every row alphabetically before returning it

    3. C

      It lets code access values by column name instead of fixed numeric position

    4. D

      It removes all duplicate values from the CSV file

    Correct answer
    C

    It lets code access values by column name instead of fixed numeric position

    Explanation

    DictReader uses the header row as dictionary keys, allowing code such as row["language"] and making it less dependent on column order.

  9. 9

    Why is code using row[1] considered fragile when reading a spreadsheet-derived CSV file?

    1. A

      Python lists cannot contain strings at index 111

    2. B

      Moving or deleting columns can cause index 111 to refer to different data

    3. C

      The second column is always ignored by CSV readers

    4. D

      Numeric indices can be used only with binary database files

    Correct answer
    B

    Moving or deleting columns can cause index 111 to refer to different data

    Explanation

    A positional index assumes that the desired field will always remain in the same column. Accessing a named dictionary key is more robust when columns are rearranged.

  10. 10

    What happens when Python code uses with open(...) as file:?

    1. A

      The file remains open until the entire Python program terminates

    2. B

      The file is automatically closed when execution leaves the with block

    3. C

      The file is automatically converted into a database table

    4. D

      The file can only be opened for writing inside the block

    Correct answer
    B

    The file is automatically closed when execution leaves the with block

    Explanation

    The with statement manages the file resource and automatically closes it after the indented block finishes, including in many error situations.

  11. 11

    What is the purpose of a dictionary used as a counting structure?

    1. A

      It preserves only the most recently encountered item

    2. B

      It stores every item only once and prevents numerical updates

    3. C

      It associates each item, such as a language, with its current count

    4. D

      It automatically replaces all strings with their ASCII codes

    Correct answer
    C

    It associates each item, such as a language, with its current count

    Explanation

    A dictionary can map keys such as Python, C, and Scratch to integer counts that are initialized and incremented as rows are processed.

  12. 12

    When using a dictionary to count values without Counter, why should a newly encountered item usually be assigned a count of 111 rather than 000?

    1. A

      Python dictionaries reject values equal to zero

    2. B

      The first row is always the header and must be counted

    3. C

      A value of zero causes the dictionary key to be deleted

    4. D

      The current row is already the first occurrence of that item

    Correct answer
    D

    The current row is already the first occurrence of that item

    Explanation

    If an item is not yet in the dictionary, encountering it means it has already appeared once, so its initial count should be 111.

  13. 13

    What does Python's Counter class provide?

    1. A

      A sorting algorithm that orders values by their spelling

    2. B

      A database connection that automatically stores every Python variable

    3. C

      A mapping with counting behavior that initializes unseen items to zero

    4. D

      A file reader that understands only comma-free text

    Correct answer
    C

    A mapping with counting behavior that initializes unseen items to zero

    Explanation

    Counter is a specialized dictionary-like object that allows a program to increment counts even for keys that have not previously been encountered.

  14. 14

    What does the most_common method of a Python Counter return?

    1. A

      Rows from the original CSV file in their original order

    2. B

      Only the alphabetically first item in the counter

    3. C

      Items paired with their counts in descending order of frequency

    4. D

      All items with their values replaced by zero

    Correct answer
    C

    Items paired with their counts in descending order of frequency

    Explanation

    most_common returns key-count pairs ordered from the most frequent item to the least frequent item.

  15. 15

    What does Python's sorted function do by default when given a dictionary for iteration?

    1. A

      It sorts the dictionary's values by numerical size

    2. B

      It sorts the dictionary's keys

    3. C

      It removes keys that have duplicate values

    4. D

      It reverses the order in which keys were inserted

    Correct answer
    B

    It sorts the dictionary's keys

    Explanation

    By default, sorted orders dictionary keys. Supplying a key function such as counts.get makes it sort according to the associated values instead.

  16. 16

    What effect does reverse=True have when passed to Python's sorted function?

    1. A

      It reverses the resulting sort order

    2. B

      It sorts according to dictionary keys instead of values

    3. C

      It removes the sorting operation entirely

    4. D

      It causes the function to modify the original file

    Correct answer
    A

    It reverses the resulting sort order

    Explanation

    reverse=True changes ascending order to descending order, or reverses whatever ordering the sort would otherwise produce.

  17. 17

    What distinguishes a positional argument from a named parameter in Python?

    1. A

      A positional argument is optional, while a named parameter is always required

    2. B

      A positional argument is identified by location, while a named parameter is identified explicitly by name

    3. C

      A positional argument must be a number, while a named parameter must be a string

    4. D

      A positional argument belongs to SQL, while a named parameter belongs only to C

    Correct answer
    B

    A positional argument is identified by location, while a named parameter is identified explicitly by name

    Explanation

    Positional arguments are interpreted according to their order. Named parameters, such as reverse=True, explicitly identify which option is being set.

  18. 18

    What does SQL stand for?

    1. A

      Sequential Question Logic

    2. B

      Structured Query Language

    3. C

      System Query Library

    4. D

      Stored Quantitative List

    Correct answer
    B

    Structured Query Language

    Explanation

    SQL is the abbreviation for Structured Query Language, a language designed for working with databases.

  19. 19

    Why is SQL described as a declarative language?

    1. A

      The programmer must write a loop for every row returned

    2. B

      The database executes only commands written in a graphical interface

    3. C

      The programmer describes the desired data rather than specifying every procedural step

    4. D

      The language can only declare variables and cannot query data

    Correct answer
    C

    The programmer describes the desired data rather than specifying every procedural step

    Explanation

    SQL generally expresses what data should be returned or changed, leaving the database system to determine how to perform the operation.

  20. 20

    What does the CRUD acronym represent in database operations?

    1. A

      Connect, Retrieve, Use, and Disconnect

    2. B

      Count, Rank, Understand, and Display

    3. C

      Compile, Run, Upload, and Debug

    4. D

      Create, Read, Update, and Delete

    Correct answer
    D

    Create, Read, Update, and Delete

    Explanation

    CRUD summarizes the four fundamental categories of database operations: creating, reading, updating, and deleting data.

  21. 21

    Which SQL keywords correspond most directly to the CRUD operations?

    1. A

      OPEN, READ, WRITE, and CLOSE

    2. B

      IMPORT, GROUP, ORDER, and LIMIT

    3. C

      INSERT, SELECT, UPDATE, and DELETE

    4. D

      CREATE, COUNT, SORT, and DROP

    Correct answer
    C

    INSERT, SELECT, UPDATE, and DELETE

    Explanation

    SQL commonly uses INSERT to add rows, SELECT to read data, UPDATE to modify data, and DELETE to remove rows. DROP can remove an entire table.

  22. 22

    In a relational database, what is a table most analogous to in a spreadsheet?

    1. A

      A chart generated from several sheets

    2. B

      A sheet containing rows and columns

    3. C

      A file extension used to export data

    4. D

      A single formula in a cell

    Correct answer
    B

    A sheet containing rows and columns

    Explanation

    A database table stores related data in rows and columns, much like a spreadsheet sheet.

  23. 23

    What does SELECT * FROM favorites; request?

    1. A

      The number of unique rows in the favorites table

    2. B

      Only the first column from the favorites table

    3. C

      Every column and row from the favorites table

    4. D

      Every table whose name contains the word favorites

    Correct answer
    C

    Every column and row from the favorites table

    Explanation

    In SQL, * is a wildcard meaning all columns. Without a filtering condition, the query returns all rows from the named table.

  24. 24

    What is the purpose of the SQL LIMIT clause?

    1. A

      It restricts the number of columns that may exist in a table

    2. B

      It restricts the number of rows returned by a query

    3. C

      It limits values to integers rather than text

    4. D

      It prevents users from inserting more rows permanently

    Correct answer
    B

    It restricts the number of rows returned by a query

    Explanation

    LIMIT 10, for example, asks SQL to return no more than 101010 rows in the result set.

  25. 25

    What does COUNT(*) calculate in a query over a table?

    1. A

      The sum of all numerical values in the table

    2. B

      The number of columns in the selected table

    3. C

      The number of rows in the selected result

    4. D

      The number of distinct values in every column

    Correct answer
    C

    The number of rows in the selected result

    Explanation

    COUNT(*) counts rows, regardless of the particular values stored in their columns.

  26. 26

    What does the SQL keyword DISTINCT do?

    1. A

      It deletes duplicate rows from the original table

    2. B

      It changes all text values to uppercase

    3. C

      It sorts values from smallest to largest

    4. D

      It removes duplicate values from the query result

    Correct answer
    D

    It removes duplicate values from the query result

    Explanation

    DISTINCT returns only unique values in the selected result. It does not delete records from the underlying table.

  27. 27

    How does a SQL WHERE clause function?

    1. A

      It creates a backup copy before a query runs

    2. B

      It filters rows according to a specified condition

    3. C

      It groups every row with the same column name

    4. D

      It permanently changes the table's column order

    Correct answer
    B

    It filters rows according to a specified condition

    Explanation

    A WHERE clause selects only rows satisfying a condition, such as language = 'C'.

  28. 28

    Which SQL expression correctly filters rows where the language is C and the problem is Hello, World?

    1. A

      WHERE language = 'C' AND problem = 'Hello, World'

    2. B

      WHERE language := 'C' PLUS problem := 'Hello, World'

    3. C

      WHERE language == C && problem == Hello, World

    4. D

      WHERE language IN C OR problem IN Hello, World

    Correct answer
    A

    WHERE language = 'C' AND problem = 'Hello, World'

    Explanation

    SQL uses AND for conjunction and generally uses a single equals sign for equality comparisons. Text values are written as quoted strings.

  29. 29

    What is the default ordering produced by ORDER BY in SQL?

    1. A

      Order of insertion only

    2. B

      Ascending order

    3. C

      Random order

    4. D

      Descending order

    Correct answer
    B

    Ascending order

    Explanation

    SQL orders ascending by default. The keyword DESC requests descending order.

  30. 30

    What does GROUP BY language accomplish when combined with a count?

    1. A

      It sorts all individual responses alphabetically without combining them

    2. B

      It deletes every language that appears more than once

    3. C

      It combines rows by language so the number of rows for each language can be counted

    4. D

      It limits the result to one language regardless of its frequency

    Correct answer
    C

    It combines rows by language so the number of rows for each language can be counted

    Explanation

    GROUP BY forms groups of rows sharing a value. Counting each group produces frequencies such as the number of responses for each language.

  31. 31

    What is the purpose of the SQL keyword AS in a query such as COUNT(*) AS n?

    1. A

      It converts the expression into a string

    2. B

      It sorts the expression in ascending order

    3. C

      It restricts the expression to one row

    4. D

      It assigns an alias to a column or expression in the result

    Correct answer
    D

    It assigns an alias to a column or expression in the result

    Explanation

    AS n gives the computed column a shorter, more convenient name: n.

  32. 32

    What is the difference between DELETE FROM favorites; and DELETE FROM favorites WHERE timestamp IS NULL;?

    1. A

      The first deletes every row, while the second deletes only rows meeting the condition

    2. B

      There is no difference because WHERE is ignored by SQL

    3. C

      The first deletes the table itself, while the second deletes only its columns

    4. D

      The first deletes only null rows, while the second deletes every row

    Correct answer
    A

    The first deletes every row, while the second deletes only rows meeting the condition

    Explanation

    Without a WHERE clause, DELETE affects all rows. With the condition, only rows whose timestamp is null are deleted.

  33. 33

    What does a SQL NULL value represent?

    1. A

      An empty database file

    2. B

      A pointer to the beginning of a table

    3. C

      The absence of a value

    4. D

      The numerical value zero

    Correct answer
    C

    The absence of a value

    Explanation

    NULL indicates that no value is present. In SQL, it is not a pointer or memory address.

  34. 34

    What is the effect of an UPDATE statement without a WHERE clause?

    1. A

      It changes the table schema but not any stored values

    2. B

      It changes the specified columns in every row of the table

    3. C

      It fails because SQL requires a WHERE clause for updates

    4. D

      It changes only the first row returned by the table

    Correct answer
    B

    It changes the specified columns in every row of the table

    Explanation

    An UPDATE without a condition applies to all rows, which can be highly destructive.

  35. 35

    Why is database normalization useful in the TV-show example?

    1. A

      It eliminates the need for relationships between tables

    2. B

      It forces every table to contain exactly the same number of columns

    3. C

      It stores every repeated name in a longer text field for readability

    4. D

      It factors out repeated information so each show and person can be stored canonically

    Correct answer
    D

    It factors out repeated information so each show and person can be stored canonically

    Explanation

    Normalization reduces redundant strings by storing entities such as shows and people once and relating them through identifiers.

  36. 36

    Why was using separate star columns for each television show considered poor design?

    1. A

      Every star must have the same name as the show

    2. B

      Different shows may require different numbers of star columns, creating sparse and uneven rows

    3. C

      A spreadsheet cannot store names in more than one column

    4. D

      Separate columns prevent the database from storing numerical IDs

    Correct answer
    B

    Different shows may require different numbers of star columns, creating sparse and uneven rows

    Explanation

    A variable number of star columns produces a jagged design in which some rows have many unused or missing fields.

  37. 37

    Why is repeating a show's title across multiple rows inefficient?

    1. A

      It wastes storage and creates opportunities for inconsistent copies of the same text

    2. B

      It prevents the show from having more than one star

    3. C

      It causes every repeated title to become a separate database table

    4. D

      It makes all identifiers automatically invalid

    Correct answer
    A

    It wastes storage and creates opportunities for inconsistent copies of the same text

    Explanation

    Repeated strings consume space and can become inconsistent if one copy is edited while others are not.

  38. 38

    What is the purpose of a primary key?

    1. A

      It uniquely identifies each row or entity in a table

    2. B

      It records the order in which rows were inserted

    3. C

      It guarantees that every column contains text

    4. D

      It stores the names of all tables related to the current table

    Correct answer
    A

    It uniquely identifies each row or entity in a table

    Explanation

    A primary key, such as a show's unique ID, identifies one particular entity uniquely within its table.

  39. 39

    What is a foreign key?

    1. A

      A key used only to encrypt data stored outside the database

    2. B

      A duplicate primary key that is invalid in its original table

    3. C

      A column containing values that refer to primary keys in another table

    4. D

      A column that must contain a different data type from every other column

    Correct answer
    C

    A column containing values that refer to primary keys in another table

    Explanation

    A foreign key creates a relationship by storing identifiers that correspond to primary keys in another table.

  40. 40

    Why is an intermediary table such as stars needed for a many-to-many relationship?

    1. A

      It ensures that each show can have only one person

    2. B

      It stores all show titles in one large text field

    3. C

      It records multiple associations between shows and people without repeating complete entity descriptions

    4. D

      It replaces every primary key with a random string

    Correct answer
    C

    It records multiple associations between shows and people without repeating complete entity descriptions

    Explanation

    A show can have many people, and a person can appear in many shows. A linking table stores each show-person association using foreign keys.

  41. 41

    What relationship exists between the shows and ratings tables in the described IMDb design?

    1. A

      Each show must have many corresponding rating rows

    2. B

      Each rating belongs to many unrelated shows simultaneously

    3. C

      Shows and ratings have no shared identifying value

    4. D

      Each show has one corresponding rating row

    Correct answer
    D

    Each show has one corresponding rating row

    Explanation

    The design describes a one-to-one relationship: each show has one row in the ratings table, linked by the show's ID.

  42. 42

    What relationship does the genres table represent for shows?

    1. A

      One genre can be associated with only one show

    2. B

      Genres can be stored only as columns in the shows table

    3. C

      One show can be associated with multiple genres

    4. D

      Each show must have exactly one genre

    Correct answer
    C

    One show can be associated with multiple genres

    Explanation

    The genres table uses multiple rows with the same show ID to represent associations such as comedy, adventure, and family.

  43. 43

    What is the effect of joining tables with a one-to-many relationship?

    1. A

      The original tables are permanently merged and all duplicate data is stored

    2. B

      The result set may temporarily repeat the one-side data for each matching many-side row

    3. C

      The query can return only the primary keys and no descriptive fields

    4. D

      The database must delete all rows on the many side

    Correct answer
    B

    The result set may temporarily repeat the one-side data for each matching many-side row

    Explanation

    For a show with several genres, a join can return the show's title once per genre. This repetition exists in the temporary result set, not necessarily in the stored tables.

  44. 44

    What is the main purpose of an SQL JOIN?

    1. A

      To sort one table without examining any of its values

    2. B

      To remove all foreign keys from a database

    3. C

      To convert a table into a CSV file

    4. D

      To combine related data from multiple tables using matching columns

    Correct answer
    D

    To combine related data from multiple tables using matching columns

    Explanation

    A join combines rows from related tables, such as matching shows.id with ratings.show_id.

  45. 45

    Why are table-qualified column names such as shows.id useful in a join?

    1. A

      They are required whenever a column contains a number

    2. B

      They force SQL to return only one row

    3. C

      They convert the column into a foreign key automatically

    4. D

      They make clear which table a column belongs to and avoid ambiguity

    Correct answer
    D

    They make clear which table a column belongs to and avoid ambiguity

    Explanation

    Table qualification clarifies which column is intended, especially when multiple tables contain similarly named columns.

  46. 46

    Which SQL query correctly combines show titles with their ratings for ratings of at least 6.06.06.0?

    1. A

      SELECT title, rating FROM shows WHERE shows.id JOIN ratings = ratings.show_id AND rating 6.0;

    2. B

      SELECT title, rating FROM shows JOIN ratings ON shows.id = ratings.show_id WHERE rating >= 6.0;

    3. C

      SELECT title, rating FROM ratings GROUP shows ON title WHERE rating <= 6.0;

    4. D

      SELECT title, rating FROM shows, ratings DELETE rating >= 6.0;

    Correct answer
    B

    SELECT title, rating FROM shows JOIN ratings ON shows.id = ratings.show_id WHERE rating >= 6.0;

    Explanation

    The query selects the desired columns, joins the tables on their related identifiers, and filters ratings using WHERE rating >= 6.0.

  47. 47

    What is the main benefit of using SQLite in the described course context?

    1. A

      It eliminates the need to learn any SQL syntax

    2. B

      It is a programming language that replaces Python for every task

    3. C

      It stores data only in human-readable spreadsheets

    4. D

      It provides a lightweight implementation containing core SQL database functionality

    Correct answer
    D

    It provides a lightweight implementation containing core SQL database functionality

    Explanation

    SQLite is a lightweight SQL implementation commonly used on computers and phones while still supporting core relational database operations.

  48. 48

    Which SQLite command displays the schema of a database or table?

    1. A

      .columns

    2. B

      .design

    3. C

      .schema

    4. D

      .layout

    Correct answer
    C

    .schema

    Explanation

    The SQLite-specific .schema command displays the SQL definitions used to create tables and their columns.

  49. 49

    Which data type is most appropriate for a value such as a decimal rating?

    1. A

      BLOB

    2. B

      TEXT

    3. C

      REAL

    4. D

      INTEGER

    Correct answer
    C

    REAL

    Explanation

    A REAL represents a number that may contain a decimal point, such as a rating of 8.78.78.7.

  50. 50

    What does the SQL data type BLOB generally represent?

    1. A

      A text field that cannot contain spaces

    2. B

      A binary large object containing data such as raw file bytes

    3. C

      A relationship between two primary keys

    4. D

      A column containing only whole-number counts

    Correct answer
    B

    A binary large object containing data such as raw file bytes

    Explanation

    BLOB stands for Binary Large Object and generally refers to binary data. The lecture noted that files are often better stored in a file system than directly in a database.

  51. 51

    What constraint prevents a column from accepting missing values?

    1. A

      NO EMPTY

    2. B

      NOT NULL

    3. C

      UNIQUE ONLY

    4. D

      REQUIRED TEXT

    Correct answer
    B

    NOT NULL

    Explanation

    NOT NULL requires a value to be present in the column.

  52. 52

    What does a UNIQUE constraint enforce?

    1. A

      The column must contain at least one null value

    2. B

      Values in the constrained column cannot be duplicated

    3. C

      Rows must be returned in alphabetical order

    4. D

      Every value must be a floating-point number

    Correct answer
    B

    Values in the constrained column cannot be duplicated

    Explanation

    A UNIQUE constraint prevents duplicate values, such as duplicate email addresses in a user table.

  53. 53

    What is the purpose of an index in a database?

    1. A

      It provides a data structure that can make searches on selected columns faster

    2. B

      It permanently sorts every table and prevents future updates

    3. C

      It replaces the need for primary and foreign keys

    4. D

      It compresses all text into a single database value

    Correct answer
    A

    It provides a data structure that can make searches on selected columns faster

    Explanation

    An index is an auxiliary data structure, commonly a B-tree, that helps the database locate matching values more efficiently than scanning every row.

  54. 54

    Why can a B-tree index speed up a database search?

    1. A

      It scans every row several times to guarantee accuracy

    2. B

      It stores every possible query result in the user's program

    3. C

      It converts all searches into constant-time operations regardless of the data

    4. D

      Its short, broad structure reduces the number of steps needed to reach a value

    Correct answer
    D

    Its short, broad structure reduces the number of steps needed to reach a value

    Explanation

    A B-tree has many children per node, keeping its height relatively small and reducing the number of comparisons needed to find a value.

  55. 55

    What indexing behavior was described for primary and foreign keys in SQLite?

    1. A

      Primary keys are indexed automatically, while foreign keys are not indexed by default

    2. B

      Both types are always unindexed unless stored as text

    3. C

      Foreign keys are indexed automatically, while primary keys cannot be indexed

    4. D

      Both types are automatically indexed only after a SELECT query

    Correct answer
    A

    Primary keys are indexed automatically, while foreign keys are not indexed by default

    Explanation

    The lecture stated that SQLite automatically indexes primary keys, but foreign-key columns generally require indexes to be created explicitly.

  56. 56

    What is the main trade-off of creating database indexes?

    1. A

      Indexes make searches slower but make database design simpler

    2. B

      Indexes use additional space and can slow inserts, updates, and deletes

    3. C

      Indexes prevent tables from being joined together

    4. D

      Indexes reduce storage space and make every write instantaneous

    Correct answer
    B

    Indexes use additional space and can slow inserts, updates, and deletes

    Explanation

    Indexes improve read performance but require storage and maintenance whenever indexed data changes.

  57. 57

    Why might an index on a frequently searched title column improve a large website's performance?

    1. A

      The index automatically creates a new copy of every website page

    2. B

      The database can locate matching titles without linearly scanning the entire column

    3. C

      The index changes all title values into primary keys

    4. D

      The index prevents users from searching for titles that are not present

    Correct answer
    B

    The database can locate matching titles without linearly scanning the entire column

    Explanation

    An index allows searches to use an efficient data structure rather than examining every title from top to bottom.

  58. 58

    Why can Python and SQL be used together in one application?

    1. A

      SQL can query the database while Python handles interaction or application logic

    2. B

      Python and SQL are interchangeable names for the same language

    3. C

      Python automatically turns every SQL query into a spreadsheet formula

    4. D

      SQL can create user interfaces but cannot read stored data

    Correct answer
    A

    SQL can query the database while Python handles interaction or application logic

    Explanation

    A program can use Python for input, output, and application behavior while sending SQL queries to retrieve or modify database data.

  59. 59

    Why are placeholders such as ? used when passing user input into SQL queries?

    1. A

      They allow the database library to handle user input safely rather than concatenating it into SQL text

    2. B

      They make SQL queries execute without any database connection

    3. C

      They convert every user input into a database table

    4. D

      They force the database to ignore all user input

    Correct answer
    A

    They allow the database library to handle user input safely rather than concatenating it into SQL text

    Explanation

    Parameterized queries separate SQL structure from user-provided values and allow the library to escape dangerous characters appropriately.

  60. 60

    What is a race condition in the likes-counter example?

    1. A

      Concurrent operations read and update shared data in an order that can cause updates to be lost

    2. B

      A query takes longer because an index contains too many values

    3. C

      A database refuses to store two rows with the same primary key

    4. D

      A user enters a password containing an invalid character

    Correct answer
    A

    Concurrent operations read and update shared data in an order that can cause updates to be lost

    Explanation

    If several servers read the same old like count before any writes finish, they may all write the same incremented value, losing some likes.

  61. 61

    If three users simultaneously read a like count of 1,000,0001{,}000{,}0001,000,000 and each writes back 1,000,0011{,}000{,}0011,000,001, what is the result?

    1. A

      The database may record only one new like instead of three

    2. B

      The database necessarily records exactly three new likes

    3. C

      The database multiplies the count by three before storing it

    4. D

      The database automatically records zero new likes

    Correct answer
    A

    The database may record only one new like instead of three

    Explanation

    Because all three operations may use the same old value, each can overwrite the others with 1,000,0011{,}000{,}0011,000,001, losing two increments.

  62. 62

    What does it mean for a database transaction to be atomic?

    1. A

      All operations in the transaction happen together, or none of them take effect

    2. B

      Only numerical operations can occur inside the transaction

    3. C

      Each operation is permanently stored before the next operation begins

    4. D

      The transaction automatically creates a separate database for each user

    Correct answer
    A

    All operations in the transaction happen together, or none of them take effect

    Explanation

    Atomicity prevents a multi-step operation from being interrupted halfway through, helping preserve consistency when concurrent requests occur.

  63. 63

    How can a transaction help prevent lost updates in the likes example?

    1. A

      It causes the read-and-update sequence to execute without harmful interleaving

    2. B

      It replaces the likes count with a randomly generated value

    3. C

      It converts every like into a separate database table

    4. D

      It permanently disables all other users from accessing the database

    Correct answer
    A

    It causes the read-and-update sequence to execute without harmful interleaving

    Explanation

    Wrapping related operations in a transaction makes the sequence behave as a single atomic unit, reducing errors caused by concurrent requests.

  64. 64

    What is a SQL injection attack?

    1. A

      An attack that sorts a database in the wrong order

    2. B

      An attack that physically inserts a new database server into a network

    3. C

      An attack that occurs only when a database contains numeric columns

    4. D

      An attack in which malicious input changes the meaning of a dynamically constructed SQL query

    Correct answer
    D

    An attack in which malicious input changes the meaning of a dynamically constructed SQL query

    Explanation

    SQL injection occurs when untrusted input is concatenated into SQL and is interpreted as part of the query rather than merely as data.

  65. 65

    In SQLite, what does -- signify when used in a SQL statement?

    1. A

      It forces the database to delete the current table

    2. B

      It begins a comment, causing the remainder of the line to be ignored

    3. C

      It marks the beginning of a password field

    4. D

      It subtracts one value from another

    Correct answer
    B

    It begins a comment, causing the remainder of the line to be ignored

    Explanation

    In SQLite, two hyphens begin a comment. In an injection attack, this can cause the rest of a query, such as a password check, to be ignored.

  66. 66

    Why is constructing a SQL query with a Python f-string unsafe when it includes user input?

    1. A

      F-strings automatically delete every row returned by a query

    2. B

      A user can insert quotes and SQL syntax that alter the intended query

    3. C

      F-strings can contain only integers and therefore corrupt all text input

    4. D

      SQL databases cannot execute queries containing Python strings

    Correct answer
    B

    A user can insert quotes and SQL syntax that alter the intended query

    Explanation

    Direct string interpolation allows user input to become executable SQL syntax. Parameterized placeholders should be used instead.

  67. 67

    How could a username containing a closing quote followed by -- bypass an unsafe password check?

    1. A

      The input causes SQL to compare the password numerically instead of as text

    2. B

      The input changes the username into a primary key without querying the database

    3. C

      The input automatically encrypts the password before comparison

    4. D

      The quote can terminate the username string, and -- can comment out the password condition

    Correct answer
    D

    The quote can terminate the username string, and -- can comment out the password condition

    Explanation

    In an improperly interpolated query, the injected quote closes the string literal and -- comments out the remaining password-checking expression.

  68. 68

    Which practice best prevents the SQL injection vulnerability described in the lecture?

    1. A

      Place user input directly inside an f-string surrounded by quotes

    2. B

      Use parameterized queries with placeholders supplied through a database library

    3. C

      Allow users to submit only values that look like numbers

    4. D

      Remove all quotation marks from SQL string comparisons

    Correct answer
    B

    Use parameterized queries with placeholders supplied through a database library

    Explanation

    Parameterized queries let the database library distinguish data from SQL syntax and safely escape potentially dangerous input.

Question 1 of 68
Question 1 of 68

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent