21. If you want to read a file called "logs.txt", which line of code allows you to open this file for purposes of reading it and store it in a variable called file?

  • with open(“logs.txt”, file, “r”):
  • with file.open(“logs.txt”, “r”):
  • with open(“logs.txt”, “r”) as file:
  • with open(“logs.txt”) as file:

22. You’ve read a log file into the variable file_text. The file_text variable contains a string of 50 usernames of employees at your company. In order to pass it into a function that checks the login count of each user, the string should be divided into a list of separate usernames. How do you convert this string into a list and store it in a variable usernames?

  • file_text.split() as usernames
  • usernames = usernames.split(file_text)
  • usernames = file_text.split()
  • usernames = split(usernames, file_text)

23. What are the three types of errors you will encounter while debugging?

  • Logic errors, comment errors, and iterative errors
  • Exceptions, logic errors, iterative errors
  • Syntax errors, exceptions, and comment errors
  • Syntax errors, logic errors, and exceptions

24. The purpose of the following code is to print the characters in a device ID. Run this code, analyze its output, and then debug it. (If you want to undo your changes to the code, you can click the Reset button.)

What is the error related to?

  • A misspelled variable
  • A missing double equals sign (==)
  • A missing quotation mark ()
  • A missing colon (:)

25. When debugging code, what are effective ways to determine which sections of code are working properly? Select all that apply.

  • Add comments in the code
  • Use a debugger
  • Delete blank lines from the code
  • Add print statements

26. What does the following code do?

with open("logs.txt", "r") as file:

  • It copies a file called “logs.txt” into a new file “r”.
  • It opens a file called “logs.txt” in write mode and stores it in a variable called file.
  • It copies a file called “r” into a new file “logs.txt”.
  • It opens a file called “logs.txt” in read mode and stores it in a variable called file.

27. What is parsing?

  • The process of reading data line by line
  • The process of copying data to other files
  • The process of writing data to a new file
  • The process of converting data into a more readable format

28. What is the practice of identifying and fixing errors in code?

  • Parsing
  • Slicing
  • Debugging
  • Splitting

Shuffle Q/A 3

29. The purpose of this code is to print "user flagged" if the username is "jhill", and otherwise to print "user okay". Run this code, analyze its output, and debug it. (If you want to undo your changes to the code, you can click the Reset button.)

How can you fix this error?

  • Call check_user() before the function definition.
  • Remove indentation from the line that prints “user okay” so that it is not part of the conditional.
  • Use the != operator instead of the == operator in the conditional header.
  • Add an else statement before the line that prints “user okay”.

30. You did not define a function before calling it. What type of error is this?

  • Index out of bounds
  • Syntax error
  • Logic error
  • Exception

Leave a Reply