Programming Function Calls Quiz
16 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What problem might arise with the code if the csv reader object is not created properly?

  • The data from the csv reader is not being unpacked properly and will cause an error.
  • The csv reader object is not being created properly and will cause an error. (correct)
  • There is nothing wrong with the code.
  • The csv file is not being opened properly and will cause an error.
  • When using the csv reader object, what data type is typically read for one entry in the row?

  • Float
  • Depends on the data being read in. (correct)
  • Integer
  • String
  • What dictionary method allows you to access both keys and values in a dictionary simultaneously?

  • values()
  • keys()
  • items() (correct)
  • pairs()
  • Which of the following cannot be used as a key in a dictionary?

    <p>List</p> Signup and view all the answers

    What is the correct term for values passed into a function during its call?

    <p>Arguments</p> Signup and view all the answers

    What method correctly allows you to iterate through all keys in a dictionary?

    <p>keys()</p> Signup and view all the answers

    When writing to a text file, what type must the value you are writing be?

    <p>String</p> Signup and view all the answers

    Which of the following options can be used to update values in a dictionary?

    <p>Direct assignment using an existing key</p> Signup and view all the answers

    What value will be returned from the function call on line 12 if the function is defined to return 3?

    <p>3</p> Signup and view all the answers

    Which of the following function calls will produce a return value of 10 based on its definition?

    <p>mystery(10,1,0)</p> Signup and view all the answers

    Which line of code correctly opens a file 'test_answers.txt' for reading only?

    <p>fn = open('test_answers.txt', 'r')</p> Signup and view all the answers

    What is the primary function of the next() function when reading from a file?

    <p>It allows lines in the file to be skipped.</p> Signup and view all the answers

    Which statement accurately describes what a dictionary is in programming?

    <p>Dictionaries are similar to lists but store data in key-value pairs.</p> Signup and view all the answers

    What will be the output of the following code segment that prints colors? Assuming the print function is called correctly.

    <p>Go green!Go white!Boo blue!</p> Signup and view all the answers

    Which line does NOT correctly utilize the open function to access a file?

    <p>fn = open('test_answers.txt', 'write')</p> Signup and view all the answers

    What type of structure is a dictionary in programming?

    <p>A mutable structure to hold data in unordered key-value pairs.</p> Signup and view all the answers

    Study Notes

    Exam 3 Sample Questions

    • Sample questions similar to those on the exam are provided.
    • These questions are not exhaustive, and other topics may appear on the exam.
    • Use this document as supplemental study material.

    Question 1: Function Call

    • Problem: Determine the returned value from a function call.
    • Function: mystery(x, y, z)
      • Takes three arguments x, y, and z
      • If x > 0, then:
        • If y < z, return x
        • Otherwise, return y + z
      • Otherwise, return y
    • Values: a = 3, b = 4, c = -2
    • Call: mystery(a, b, c)
    • Solution: The code returns 3 because a is 3, a > 0is true, and b is 4, and c is -2, and 4 > -2 is true, so a = 3 is returned from the function

    Question 2: Return Values

    • Problem: Identify function calls that return a value of 10.
    • Function: mystery(a, b, c)
      • If a > b, then:
        • If b < c, return a + c
        • Otherwise, return b + c
      • Otherwise, return a + b

    Question 3: Files

    • Problem: Identify the correct code for opening a file for reading only.
    • Solution: fn = open("test_answers.txt", 'r')

    Question 4: Files – next() Function

    • Problem: Describe the function of the next() function in file reading.
    • Solution: The next() function skips to the next line in the file, useful for skipping headers.

    Question 5: Dictionaries

    • Problem: Define a dictionary.
    • Solution: A dictionary stores data in key-value pairs.

    Question 6: Tracing Code

    • Problem: Determine the output from a given code segment.
    • Code: Defines functions sparty and wolvy
      • sparty prints "Go green!" if its input (n) is even, and "Go white!" if odd
      • wolvy returns its input multiplied by "Boo blue"
      • Code loops for 5 iterations
    • Output: The output shows a repeating pattern of "Go white!" and "Go green!" interspersed with "Boo blue"

    Question 7: CSV Unpacking

    • Problem: Identify the issue within a CSV file reading program.
    • Solution: The data from the CSV reader isn't unpacked correctly.

    Question 8: File Data

    • Problem: Determine the type of the data read from a CSV file using csv.reader().
    • Solution: The data type is dependent on the data. Numbers are floats or integers, otherwise they're strings

    Question 9: Dictionaries – Method for Iteration

    • Problem: Identify the dictionary method used for iterating over both keys and values.
    • Solution: items()

    Question 10: Function Terms

    • Problem: Identify the term for values passed into a function.
    • Solution: Arguments

    Question 11: Dictionaries – Invalid Keys

    • Problem: Determine which data type cannot be used as a key in a dictionary.
    • Solution: Lists

    Question 12: Functions – Output

    • Problem: Determine the output from a given function.
    • Solution: The function prints messages about opening and closing, and it returns the value assigned to the variable "check" within the function.

    Question 13: Dictionaries – Key Iteration

    • Problem: Identify options for iterating through dictionary keys.
    • Methods: dictionary.keys(), for key in dictionary:

    Question 14: Files – Data Type

    • Problem: Determine the required data type for writing to a text file.
    • Solution: String

    Question 15: Dictionaries – Value Update

    • Problem: Identify options that update the values in a dictionary.
    • Solution: Iterating over the keys (dictionary.keys()) and updating the values using the key as index, for example dictionary[‘key’] = {value}.

    Answer Key

    • Numerical answers to the corresponding question are provided.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Exam 3 Sample Questions PDF

    Description

    Test your understanding of function calls and return values with this exam sample. The questions focus on conditional logic in function definitions and help you prepare for your upcoming exam. Use this supplement as a part of your study strategy.

    More Like This

    Microprocessor Addressing Modes Quiz
    10 questions
    Function Call with Arguments and Parameters
    18 questions
    Langerhans Cells Function in Epidermis
    25 questions
    Types of Recursion in Programming
    10 questions
    Use Quizgecko on...
    Browser
    Browser