Python Data Types and Functions Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What will be the output of the expression f1(4)?

  • ERROR
  • 6
  • 4
  • 8 (correct)

What will be the value of the expression f1(2) + f1(1)?

  • ERROR
  • 6 (correct)
  • 4
  • 5

What will be the result of the expression f1(x + 1) where x is assigned the value 4?

  • 10
  • 8
  • 9 (correct)
  • ERROR

What will be the value of x after the expression x = 4 is evaluated?

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

What does the function f2 return when called with f2(3, 2)?

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

What will the variable r be after executing the code snippet that includes the definition of f2?

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

What will the output be of the expression 'cat' in L after executing L = ['c', 'a', 't']?

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

Which Python expression correctly translates the propositional logic (¬(¬p)) ⇔ p?

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

What is the result of the expression len(L) == len('cat') after L = ['c', 'a', 't']?

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

Which of the following Python expressions accurately represents the propositional logic expression ((p ∨ q) ∧ ¬p) ⇒ q?

<p>((p or q) and not p) or q (B)</p> Signup and view all the answers

After executing d = {x: x * 3 for x in [1, 2, 3]}, what will be the contents of the dictionary d?

<p>{1: 3, 2: 6, 3: 9} (A)</p> Signup and view all the answers

What will be the output of the statement d == {2: 6, 3: 9, 1: 3} after d has been assigned the dictionary created previously?

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

Which function will return the smallest value in the list defined as numbers_list = [x for x in range(5, 50)]?

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

What does the output of max(numbers_list) represent after executing numbers_list = [x for x in range(5, 50)]?

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

What is the expected output of str.upper(gl) where gl = 'good luck, we're rooting for you!'?

<p>'GOOD LUCK, WE'RE ROOTING FOR YOU!' (D)</p> Signup and view all the answers

What will be the output of the expression 'good' in gl?

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

What does the function get_uppercase return when given the input ['Good', 'CODE', 'is its own', 'BEST', 'documentation.']?

<p>['BEST', 'CODE'] (C)</p> Signup and view all the answers

In the check_scores function, what will it return if the input is [95, 115]?

<p>'Good work' (C)</p> Signup and view all the answers

What is the purpose of the any() function in the check_scores function?

<p>To determine if at least one score meets a criterion (B)</p> Signup and view all the answers

What will the function return when provided with an input of [5, 10, 15, 20] in check_scores?

<p>'Better luck next time' (D)</p> Signup and view all the answers

Which of the following statements about the check_scores function is true?

<p>It processes a list of integers and generates messages based on their values. (C)</p> Signup and view all the answers

What is the expected output of check_scores([115, 160, 130])?

<p>'Great work' (D)</p> Signup and view all the answers

In the context of the function get_uppercase, what is a key requirement for a string to be included in the resulting list?

<p>The string must be entirely in uppercase. (B)</p> Signup and view all the answers

What does the check_all variable in the check_scores function signify?

<p>It checks if all scores are above 100. (D)</p> Signup and view all the answers

What is the expected output of the function get_common_numbers if both inputs do not have common elements?

<p>An empty set (B)</p> Signup and view all the answers

Which statement correctly describes the Fibonacci sequence as noted in the content?

<p>Each number is the sum of the two previous numbers. (D)</p> Signup and view all the answers

What is the precondition for the function is_fibonacci to operate correctly?

<p>The length of the list must be greater than 2. (B)</p> Signup and view all the answers

In the function get_common_numbers, what type of data structure should the return type be?

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

What is a potential reason that the current implementation of is_fibonacci might fail for certain inputs?

<p>It does not correctly evaluate the Fibonacci condition. (A)</p> Signup and view all the answers

What would be a correct sample input for the test case test_is_fibonacci_error?

<p>[0, 1, 1, 2, 3, 4] (C)</p> Signup and view all the answers

Which of the following statements about built-in functions/methods in Python can be inferred from the hints given in the content?

<p>Built-in functions can aid in writing efficient functionality. (B)</p> Signup and view all the answers

What Python function will check if all elements in a sequence meet a specific condition, as used in is_fibonacci?

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

What is the expected output of calling is_fibonacci([0, 1, 1, 2, 3, 5, 8, 13, 21])?

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

What is a precondition stated for the is_fibonacci function?

<p>len(s) must be greater than 2 (A)</p> Signup and view all the answers

What mistake is potentially present in the is_fibonacci implementation?

<p>The initial values of the Fibonacci sequence are reversed. (A)</p> Signup and view all the answers

If the function is_fibonacci is correctly implemented, what output will is_fibonacci([0, 1, 4, 9, 16, 25, 36, 49, 64, 81]) produce?

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

In terms of Fibonacci sequence definitions, which of the following statements is true?

<p>Every number after the second is the sum of the previous two numbers. (B)</p> Signup and view all the answers

What does the term 'divisibility predicate' refer to in the given content?

<p>A statement that defines a condition where one integer is divisible by another. (B)</p> Signup and view all the answers

Which of the following represents an example of a number n being divisible by n squared, as stated in the context?

<p>n | n² + n (C)</p> Signup and view all the answers

What does the output of the function assert actual == expected signify?

<p>It checks if the actual output matches the expected output. (D)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Data Types and Expressions

  • 'cat' in L evaluates to False because the list L does not contain the string 'cat', only the characters 'c', 'a', and 't'.
  • len(L) == len('cat') evaluates to True because both L and 'cat' have a length of 3.
  • d == {2: 6, 3: 9, 1: 3} evaluates to True due to the dictionary comprehension assignment where each element x in the list [1, 2, 3] is mapped to x * 3.
  • d evaluates to {1: 3, 2: 6, 3: 9} because dictionaries are unordered.
  • min(numbers_list) evaluates to 5 as numbers_list contains numbers from 5 to 49.
  • max(numbers_list) evaluates to 49 as numbers_list contains numbers from 5 to 49.
  • str.upper(gl) evaluates to 'GOOD LUCK, WE'RE ROOTING FOR YOU!' because the upper() method converts all characters in the string to uppercase.
  • 'good' in gl evaluates to True as the substring 'good' is present in the string gl.

Function Basics

  • f1(4) evaluates to 8 due to the function definition f1(x: int) -> int: return x * 2.
  • f1(2) + f1(1) evaluates to 6 because f1(2) returns 4 and f1(1) returns 2, summing to 6.
  • f1(x + 1) evaluates to 10 because x is 4, so x + 1 is 5, and f1(5) returns 10.
  • x still evaluates to 4 as the function f1 does not modify the global variable x.
  • f2(3, 2) evaluates to True because f1(3) returns 6, 6 % 2 is 0, and r == 0 is True.
  • r is undefined in this context as it is a local variable within the f2 function.

Symbolic Logic and Python Translation

  • All cute animals are fluffy: ∀a ∈ A, IsCute(a) ⇒ IsFluffy(a)
  • At least one cute animal is fluffy: ∃a ∈ A, IsCute(a) ∧ IsFluffy(a)
  • (¬(¬p)) ⇔ p: (not (not p)) == p
  • ((p ∨ q) ∧ ¬p) ⇒ q: ((p or q) and not p) == q

Filtering Collections and String Methods

  • def get_uppercase(lst: list[str]) -> list[str]:
    • return sorted([s for s in lst if s.isupper()])

If Statements

  • def check_scores(scores: list[int]) -> str:
    • check_all = all([score >= 100 for score in scores])
    • check_at_least_one = any([score >= 100 for score in scores])
    • if check_all:
      • return 'Great work'
    • elif check_at_least_one:
      • return 'Good work'
    • else:
      • return 'Better luck next time'

Function Design Recipe - get_common_numbers

  • def get_common_numbers(L1: list[int], L2: list[int]) -> set[int]:
    • """Return a set containing the common numbers in L1 and L2 (i.e., those numbers present in both lists).
    • >>> L1 = [10, 20, 30, 40, 50]
    • >>> L2 = [60, 70, 80, 30, 10]
    • >>> numbers = get_common_numbers(L1, L2)
    • >>> numbers == {10, 30}
    • True
    • >>> L1 = [10, 20, 30, 40, 50]
    • >>> L2 = [60, 70, 80]
    • >>> get_common_numbers(L1, L2)
    • set()
    • """
    • return set(L1) & set(L2)

Debugging and Testing - is_fibonacci

  • def test_is_fibonacci_error() -> None:
    • """A test case for is_fibonacci that reveals an error in the given implementation.If we ran this test with pytest and the current implementation, we would expect
    • this test to *fail* because of the error in the function body."""
    • actual = is_fibonacci([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
    • expected = False
    • assert actual == expected
  • Error: The original implementation fails to validate the first two elements of the list.
  • def is_fibonacci(s: list[int]) -> bool:
    • """Return whether list s contains the Fibonacci sequence of length len(s).The Fibonacci sequence of length n is the sequence of n numbers
    • 0, 1, 1, 2, 3, 5, 8, 13,...Every number after the second is the sum of the previous two numbers.The first number must be 0 and the second number must be 1.Preconditions:
    • - len(s) > 2
    • >>> is_fibonacci([0, 1, 1, 2, 3, 5, 8, 13, 21])
    • True
    • >>> is_fibonacci([0, 1, 4, 9, 16, 25, 36, 49, 64, 81])
    • False
    • """
    • return s[0] == 0 and s[1] == 1 and all([s[i + 2] == s[i] + s[i + 1] for i in range(0, len(s) - 2)])

Proofs

  • Statement (1) rewritten: ∃k ∈ Z, n = k * n
  • Proof: To prove the statement, we need to show that there exists an integer k for any integer n such that n = k * n.
    • Choosing k = 1, we get: n = 1 * n which holds true for any integer n.
    • Therefore, the statement ∀n ∈ Z, n | n2 + n is proven.

Studying That Suits You

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

Quiz Team

Related Documents

CSC110Y1F Term Test 1 PDF
Use Quizgecko on...
Browser
Browser