Untitled
31 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

Given the code import tkinter.messagebox and messagebox.showinfo("Hello"), which two statements are correct regarding the function and its environment?

  • `messagebox` is a module or a package. (correct)
  • The `messagebox()` function can also be invoked using `messagebox()`.
  • The `messagebox()` function can be invoked using `tkinter.messagebox()`.
  • The `showinfo()` function is contained in `the messagebox` module. (correct)

Which two statements about Python packages and modules are incorrect?

  • A programmer is obliged to manually create a directory/folder named `__pycache__` inside every package. (correct)
  • The extension `.pby ` marks files containing Python semi-compiled byte-code. (correct)
  • A variable named `__name__` is a string containing the module name.
  • A source file named `__init__.py` is used to mark a directory/folder as containing a Python package and to initialize the package's state.

Which two statements about Python module search paths and variable privacy are correct?

  • The directory from which the Python code is run is always searched through in order to find the necessary modules. (correct)
  • The directory from which the code is run is never searched through.
  • Variables with names starting with two underscores are considered private inside their home module. (correct)
  • Variables with names ending with two underscores are considered private inside their home module.

What is the output of the following Python code?

import math
x = 1.7
print(-abs(math.floor(x) + math.ceil(x)))

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

Which function is used to list all the names defined in a module?

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

To determine if a hardware platform uses an x86 or ARM CPU, which two functions can be used?

<p><code>platform()</code> (B), <code>processor()</code> (D)</p> Signup and view all the answers

Which two operations in Python will not raise an exception under normal circumstances?

<p>Slicing a string. (A), Decrementing an integer variable by one. (C)</p> Signup and view all the answers

What is the expected output of the code snippet?

<p>out of order (A), turn on (D)</p> Signup and view all the answers

What is the expected output of the following code?

<p>('list index out of range',) (D)</p> Signup and view all the answers

Which of the following statements are true?

<p>Python accepts UTF-8 encoded source files. (C), ASCII is a subset of UNICODE. (D)</p> Signup and view all the answers

Which of the following snippets output 123 to the screen?

<pre><code class="language-tmp">tmp.sort() print(''.join(tmp))``` (C), `print(''.join(sorted(&quot;321&quot;)))` (D) </code></pre> Signup and view all the answers

Which of the following expressions evaluates to True and raises no exception?

<p><code>10 != '1' + '0'</code> (A)</p> Signup and view all the answers

Given the following Python code, which two expressions evaluate to True?

<p><code>selection.my_ID == 2</code> (B), <code>isinstance(start, Button)</code> (C)</p> Signup and view all the answers

What is the output of the code snippet below?

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

Which two of the following lines contain valid Python code?

<p><code>lambda a,b: a if a &lt; b else b</code> (B), <code>lambda a,b: True</code> (C)</p> Signup and view all the answers

The errno.ENOENT symbol corresponds to which error?

<p>No such file or directory (C)</p> Signup and view all the answers

Which function reads one line from a text file?

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

What is the expected output of the code snippet below?

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

What is the output of the code?

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

Given the following code, which two expressions evaluate to True without raising an exception?

<p><code>isinstance(d, Un)</code> (B), <code>Troi in Quatre.__bases__</code> (C)</p> Signup and view all the answers

What is the expected output of the following code snippet?

<p>It outputs <code>261</code> (B)</p> Signup and view all the answers

Which two statements are true about object-oriented programming?

<p>A class may exist without its objects, but an object cannot exist without its class. (A), Encapsulation is a phenomenon which allows you to hide some class traits from the outer world. (B)</p> Signup and view all the answers

Given the class hierarchy, which two class declarations are permissible and will not raise an exception?

<p><code>class Jewel(Adamant, Gem): pass</code> (A), <code>class Jewel(Diamond, Adamant): pass</code> (D)</p> Signup and view all the answers

Which of the following Python classes contains a usable constructor?

<p>All of the above (D)</p> Signup and view all the answers

What would be the result of the following code?

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

Assuming that the following code has been successfully executed, indicate which statement is correct?

<p><code>Volume.dict ['chapter'] != None</code> (D)</p> Signup and view all the answers

Which of the following Python string operations will result in an error, assuming s has been assigned a value? (Select two answers.)

<p><code>S = s[9]</code> given <code>S = 'rhyme'</code> (A), <code>S = s[-3:-5]</code> given <code>S = &quot;12&quot;</code> (C)</p> Signup and view all the answers

Which of the following are not valid Python string literals? (Select two answers.)

<p><code>'Whether ''tis nobler in the mind to suffer'</code> (A), <code>'''To be, or not to be, that is the question&quot;&quot;&quot;</code> (D)</p> Signup and view all the answers

Which of the following expressions evaluate to True and raise no exception? (Select two answers.)

<p><code>'de' not in 'abc'</code> (C), <code>not 'xyz' in 'uvwxyz'</code> (D)</p> Signup and view all the answers

Given the class below, which method will correctly provide the value of the rack variable?

<pre><code class="language-def"> return self.rack``` (D) </code></pre> Signup and view all the answers

Given the code below, which code lines correctly decrement the b variable by one? (Select two answers.)

<p><code>A.b -=1</code> (B), <code>object.b -= 1</code> (D)</p> Signup and view all the answers

Flashcards

messagebox in tkinter

A module or package holding the showinfo() function.

Purpose of .pyc files and __init__.py

Files containing Python semi-compiled byte-code. Also,__init__.py marks a directory as a Python package.

Module search path include current directory?

The directory from which Python code is run is searched for modules.

Output of print(-abs(math.floor(x) + math.ceil(x))) when x = 1.7

The code will output -3. math.floor(1.7) is 1, math.ceil(1.7) is 2, therefore -(1 + 2) is -3.

Signup and view all the flashcards

List module contents

The dir() function lists a module's contents.

Signup and view all the flashcards

Determine hardware platform (x86 or ARM)

processor() and platform() functions reveal CPU architecture.

Signup and view all the flashcards

Operations that don't raise exception

Decrementing an integer and slicing a string never raise exceptions assuming valid syntax.

Signup and view all the flashcards

What does isinstance() do?

Checks if an object is an instance of a class or its subclasses.

Signup and view all the flashcards

What is Method Resolution Order (MRO)?

The order in which base classes are searched when looking for a method or attribute in a class hierarchy. Python uses Depth-First Left-to-Right

Signup and view all the flashcards

What is Polymorphism?

The most specific version of a method that is appropriate for the type of object in hand is used. Determined at runtime.

Signup and view all the flashcards

What is Encapsulation?

Bundling the data (attributes) and methods (functions) that operate on that data into a single unit (class), and restricting access to some of the class's components.

Signup and view all the flashcards

What is a Class?

A blueprint for creating objects (instances). It defines the attributes and methods that the objects will have.

Signup and view all the flashcards

What is an Object?

A specific instance of a class. Each object has its own unique data, but shares the methods defined by the class.

Signup and view all the flashcards

What does __init__ do?

A special method used to initialize the attributes of an object when it is created from a class (__init__).

Signup and view all the flashcards

What is a Subclass?

A class that inherits attributes and methods from another class (the superclass or parent class).

Signup and view all the flashcards

Exception Handling Output

Prints 'turn on' then 'out of order' because the exception is caught and its string representation is printed.

Signup and view all the flashcards

List Index Error Output

Prints ('list index out of range',) because accessing data[-3] attempts to access an index outside the bounds of the list, triggering an exception which prints the exception message.

Signup and view all the flashcards

Assertion Error Handling

Prints an error message because the assertion in attic(x) fails when x is 0, leading to an AssertionError which is not handled, thus crashing the program.

Signup and view all the flashcards

ValueError Handling

Prints 2. The try block attempts to convert the string 'three' to an integer, which raises a ValueError and is caught by the except block, assigning 2 to e.

Signup and view all the flashcards

String Encoding (True Statements)

\n encodes a new-line character and ASCII is a subset of UNICODE.

Signup and view all the flashcards

Sorting Strings

''.join(sorted("321")) and the block of code with list conversion, sorting and joining produce 123.

Signup and view all the flashcards

String Manipulation and Indexing

  1. The code removes commas from the string, resulting in 'John Doe 42', then it prints the character at index -2, which is '4'.
Signup and view all the flashcards

Looping and Membership Testing

  1. The code iterates through '+-+-+'. Because the if statement checks if the character is NOT IN the header, nothing happens so it prints 0.
Signup and view all the flashcards

What is isinstance()?

Checks if an object is an instance of a class or a subclass thereof.

Signup and view all the flashcards

What is a higher-order function?

A function that takes another function as an argument or returns a function.

Signup and view all the flashcards

What is a lambda function?

An anonymous, small, single-expression function.

Signup and view all the flashcards

S[-1]

Accesses the last character of the string 'S'. Negative indices count from the end.

Signup and view all the flashcards

Invalid String Literals

String literals that are not valid include unterminated strings or those with mismatched quotes.

Signup and view all the flashcards

What is errno.ENOENT?

The error code for 'No such file or directory'.

Signup and view all the flashcards

in and not in with strings

not in returns True if the substring is not found. 'T' in 'de' returns False because 'T' is in 'de'.

Signup and view all the flashcards

What does readline() do?

To read a single line from a text file.

Signup and view all the flashcards

What does input() do?

Reads data from the standard input stream (keyboard).

Signup and view all the flashcards

Class vs. Instance Variables

The code outputs False because Content.title is initialized only once and shared across all instances. text1.title remains "None", while text2.name becomes "Article than None".

Signup and view all the flashcards

hasattr() function

hasattr(object, 'attribute') checks if an object has a given attribute.

Signup and view all the flashcards

What are the three pre-opened file streams?

Standard input, standard output, and standard error.

Signup and view all the flashcards

What does readlines() do?

Returns a list of strings, each representing a line from the file.

Signup and view all the flashcards

__str__ Method Error

When a __str__ method tries to use a variable name that is not defined, it produces an error.

Signup and view all the flashcards

Correct get Method definition

The method needs to be properly defined and have a "self" parameter to access the class's variables.

Signup and view all the flashcards

Decrementing Class Variables

A.b -= 1 correctly decrements the class variable b. Instance variables are accessed using object.b.

Signup and view all the flashcards

Study Notes

  • The showinfo() function is in the messagebox module.
  • The messagebox is a module or package.
  • The .pyc extension marks files containing Python semi-compiled byte-code.
  • A source file named init.py marks a directory/folder as containing a Python package and initializes the package's state.
  • Variables starting with two underscores are considered private inside their home module.
  • The directory from which the Python code is run is always searched through to find the necessary modules.
  • The code import math; X = 1.7; print(-abs (math.floor(x) + math.ceil(x))) will output the value -3
  • The dir() function is used to reveal a module's contents.
  • The processor() and platform() functions can determine if a hardware platform utilizes the x86 or arm CPU.
  • Decrementing an integer variable and slicing a string will not raise an exception.
  • The following messages appear when the code is run:
  • turn on
  • out of order
  • The expected output of the code provided is ('list index out of range',)
  • The expected output of the code provided is 2
  • The code will output the value 2
  • ASCII is a subset of UNICODE
  • Python accepts UTF-8 encoded source files.
  • The code print(''.join(sorted("321"))) and tmp = list("321"); tmp.sort(); print(''.join(tmp)) will both output "123"
  • The code provided will print out the character 4
  • The expected output of the following code is the integer 0
  • The expression 10 != '1' + '0' evaluates to True and will not exceptions.
  • String assignments that produce a non-empty string:
  • S = "12"; S = s[1::2]
  • S = "12"; S = s[-1]
  • The code returns the value False.

Valid & Invalid String Literals

  • The following are invalid or not valid string literals
  • "\\"
  • "this is a quote: \""
  • The following expressions evaluate to True and do not raise an exception:
  • T in "T"
  • 'de' not in 'abc'
  • The provided code will output False.
  • The code will output True False.
  • The proper solution to get the class variable is definition Def get (self): return self.rack
  • The code lines that correctly decrement the b variable by one:
  • object.A.b -= 1
  • A.b -= 1
  • The following expressions evaluate to True:
  • isinstance(d, Un)
  • Troi in Quatre.__bases__
  • The expressions that evaluate to True are as follows:
  • Volume.__dict__['chapter'] != None
  • 'paragraph' in edition.__dict__
  • The code will output 261
  • The statements that are true about object oriented programming:
  • A class may exist without its objects, but an object cannot exist without its class.
  • Encapsulation is a phenomenon that allows you to hide some class traits from the outer world.
  • Permissible class declarations that won't raise an exception:
  • class Jewel (Adamant, Gem): pass
  • class Jewel (Adamant, Diamond): pass
  • The following classes contain a usable constructor:
  • class Lower: def __init__(self): self.color = "blue"
  • class Working: def init (self): raise KeyError
  • isinstance (start, Button)
  • selection.my_ID != 2
  • The code will output the number 8 based on the cube(2) call.
  • The following lines contain valid Python code:
  • lambda a,b: a if a < b else b
  • lambda a,b: True
  • The errno.ENOENT symbol refers to an error described as No such file or directory.
  • The function to use to read one line from a text file is readline()
  • The provided code will output the integer 4
  • The expected output of the code provided is 2
  • The provided code will result in an error message on the screen.
  • The statements that are true include:
  • The input() function reads data from the stdin stream.
  • There are three pre-opened file streams.
  • The code will output True.

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Untitled Quiz
6 questions

Untitled Quiz

AdoredHealing avatar
AdoredHealing
Untitled
44 questions

Untitled

ExaltingAndradite avatar
ExaltingAndradite
Untitled Quiz
18 questions

Untitled Quiz

RighteousIguana avatar
RighteousIguana
Untitled Quiz
50 questions

Untitled Quiz

JoyousSulfur avatar
JoyousSulfur
Use Quizgecko on...
Browser
Browser