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 (A)</p> Signup and view all the answers

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

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

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

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

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

<p>String (C)</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 (D)</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 (C)</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) (C)</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') (D)</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. (B)</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. (A)</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! (C)</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') (D)</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. (C)</p> Signup and view all the answers

Flashcards

Function Call (Line 12)

Evaluating a function with supplied inputs. It calculates a value based on the input.

Return Value of 10 (mystery function)

Specific input values to the function mystery resulting in the function returning 10.

Opening a file ('test_answers.txt')

Reading data from the file 'test_answers.txt'.

next() function (file reading)

Moves the file pointer to the next line in the file.

Signup and view all the flashcards

Dictionaries

Data structure storing key-value pairs.

Signup and view all the flashcards

Tracing Code

Following the execution of a code segment, step-by-step.

Signup and view all the flashcards

Function definition in Python

Describes the behaviour of a function, specifying its parameters, actions, and output.

Signup and view all the flashcards

File modes (Python)

Specify how you will use the file (e.g., reading, writing, appending).

Signup and view all the flashcards

CSV Unpacking Issue

Problem in code where data from a CSV reader isn't correctly extracted into separate variables.

Signup and view all the flashcards

CSV Data Type

Data read from CSV using a reader, often stored as strings, unless explicitly converted.

Signup and view all the flashcards

Dictionary Iteration (Keys & Values)

The items() method is used to iterate through both keys and values simultaneously in a dictionary.

Signup and view all the flashcards

Function Arguments

Values provided to a function when it's called.

Signup and view all the flashcards

Dictionary Non-Key Type

A list cannot be used as a key in a Python dictionary.

Signup and view all the flashcards

Function Output (Example)

The output of a particular function depends on the code within the function, not just assumptions.

Signup and view all the flashcards

Dictionary Key Iteration

To iterate through all keys in a dictionary use either keys() or a for loop with dict.items()

Signup and view all the flashcards

File Write Data Type

When writing to a text file, the values need to be strings.

Signup and view all the flashcards

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

Use Quizgecko on...
Browser
Browser