Programming 1 final

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will be displayed after the following code is executed? counter = 1 while counter <= 20: --print(counter, end=" ") --counter *= 3 print("\nThe loop has ended.") a. 1 3 9 The loop has ended. b. 1 3 9 The loop has ended. c. 1 3 9 12 15 18 The loop has ended. d. 1 3 9 The loop has ended

a. 1 3 9 The loop has ended.

To code a property that gets a private attribute, you use which of the following annotations? a. @property b. @propertyName.getter c. @propertyName d. @propertyName.setter

a. @property

class Product: --def __init__(self, name="", price=0.0): ----self.name = name ----self.price = price --def getDescription(self): ----return self.name class Book(Product): --def __init__(self, name="", price=0.0, author=""): ----Product.__init__(self, name, price) ----self.author = author --def getDescription(self): ----return Product.getDescription(self) + " by " + self.author class Movie(Product): --def __init__(self, name="", price=0.0, year=0): ----Product.__init__(self, name, price) self.year = year --def getDescription(self): ----return Product.getDescription(self) + " (" + str(self.year) + ")" The superclass is named a. Product b. Book c. Movie d. self

a. Product

Which of the following is a toolkit that you can use to build and display a GUI with Python? a. Tkinter b. Tk c. Tk() d. ttk

a. Tkinter

Which of the following in this expression is evaluated first? age >= 65 or age <= 18 or status == "retired" a. age >= 65 b. age <= 18 c. status == "retired" d. age >= 65 or age <= 18

a. age >= 65

You can't access a private attribute a. by using the dot operator followed by the attribute's name b. by calling a read-only property from the object c. by calling a method from the object d. by using a constructor to create the object

a. by using the dot operator followed by the attribute's name

If you have a class named Vehicle, and you want to code a class named Truck that inherits the Vehicle class, you can begin by writing this code: a. class Truck(Vehicle): b. class Truck : Vehicle c. class Truck inherits Vehicle: d. class Truck extends Vehicle:

a. class Truck(Vehicle):

Which of the following will create a read-only property? a. coding only a getter method with the @property annotation b. coding only a setter method with the @property annotations c. coding getter and setter methods with the @readonly annotation d. coding an attribute with the @readonly annotation

a. coding only a getter method with the @property annotation

Refer to the below code example, class Rectangle: --def __init__(self, height, width): ----self.height = height ----self.width = width --def getPerimeter(self): ----perimeter = self.height * 2 + self.width * 2 ----return perimeter --def getArea(self): ----area = self.height * self.width ----return area --def getStr(self): ----return str(self.height) + " by " + str(self.width) Which of the following is the code for the constructor? a. def __init__(self, height, width): --self.height = height --self.width = width b. def getPerimeter(self): --perimeter = self.height * 2 + self.width * 2 --return perimeter c. def getArea(self): --area = self.height * self.width --return area d. def getStr(self): --return str(self.height) + " by " + str(self.width)

a. def __init__(self, height, width): --self.height = height --self.width = width

The Unified Modeling Language a. describes the classes and objects of an application b. can be used to write the pseudo code for a class c. can be used to write the code that defines a class d. is a scripting language for working with objects

a. describes the classes and objects of an application

Which of the following can use the range() function to loop through a block of statements? a. for statement b. an if statement c. while statement d. a break statement

a. for statement

To lay out components in a series of rows and columns, you can use the __________ method a. grid() b. pack() c. table() d. layout()

a. grid()

The and operator has a. higher precedence than the or operator, but lower precedence than the not operator b. higher precedence than the or and not operators c. lower precedence than the or operator, but higher precedence than the not operator d. lower precedence than the or and not operators

a. higher precedence than the or operator, but lower precedence than the not operator

Which of the following statements imports a module into the default namespace? a. import temperature b. import temperature as temp c. from temperature import * d. import temperature as t

a. import temperature

Before you can use a standard module like the random module, you need to a. import the module b. import the module into a custom namespace c. import the module into the global namespace d. import the module into its default namespace

a. import the module

A local variable is defined a. inside a function b. inside the main() function c. inside an if statement d. outside of all functions

a. inside a function

A file that contains reusable code is called a a. module b. hierarchy chart c. function d. namespace

a. module

Refer to the below code example, class Multiplier: --def __init__(self): ----self.num1 = 0 ----self.num2 = 0 --def getProduct(self): ----return self.num1 * self.num2 --def main(): ----m = Multiplier() ----m.num1 = 7 ----m.num2 = 3 ----print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": --main() What is (are) the attributes of the class? a. num1, num2 b. m c. getProduct() d. __init__()

a. num1, num2

Most tkinter components, like frames and buttons, have a constructor that accepts __________ as its first argument. a. the parent component b. the child component c. the root window d. a frame

a. the parent component

For the following code, what will the result be if the user enters 5 at the prompt? sum = 0 end_value = int(input("Enter a number: ")) for i in range(1, end_value): --sum = sum + i --print(sum, end=", ") a. 10, b. 1, 3, 6, 10, c. 1, 3, 6, 10, 16, d. 1, 2, 3, 4, 5,

b. 1, 3, 6, 10,

The five steps for designing an object-oriented program, in the order they should be followed, are: a. 1. Identify the classes 2. Identify the methods 3. Identify the data attributes 4. Subdivide each attribute 5. Refine classes, attributes, methods b. 1. Identify the data attributes 2. Subdivide each attribute 3. Identify the classes 4. Identify the methods 5. Refine classes, attributes, methods c. 1. Identify the classes 2. Identify the data attributes 3. Subdivide each attribute 4. Identify the methods 5. Refine classes, attributes, methods d. 1. Identify the methods 2. Identify the classes 3. Identify the data attributes 4. Subdivide each attribute 5. Refine classes, attributes, methods

b. 1. Identify the data attributes 2. Subdivide each attribute 3. Identify the classes 4. Identify the methods 5. Refine classes, attributes, methods

Refer to the below code example, class Rectangle: --def __init__(self, height, width): ----self.height = height ----self.width = width --def getPerimeter(self): ----perimeter = self.height * 2 + self.width * 2 ----return perimeter --def getArea(self): ----area = self.height * self.width ----return area --def getStr(self): ----return str(self.height) + " by " + str(self.width) How many public attributes does this class define? a. 1 b. 2 c. 3 d. 4

b. 2

What is the value of my_num after the following statements execute? my_num = 5 my_num += 20 my_num -= 12 my_num *= 0.5 a. 0.5 b. 6.5 c. 6 d. 12.5

b. 6.5

When an object of one class stores one or more objects of another class, this is known as a. Instantiation b. Composition c. Encapsulation d. annotation

b. Composition

A component that only displays text is known as a a. Button b. Label c. Frame d. title

b. Label

When you use a for loop to iterate through each object in a list of objects, Python automatically calls these two methods: a. __iter__(), __for__() b. __iter__(), __next__() c. __for__(), __next__() d. __for__(), __in__()

b. __iter__(), __next__()

A return statement a. must be coded within every function b. can be used to return a local variable to the calling function c. can be used to allow the function to modify the value of a global variable d. can only be used once in each function

b. can be used to return a local variable to the calling function

. Given a class named Customer, which of the following creates a Customer object and assigns it to the variable named cust1: a. cust1 = new Customer() b. cust1 = Customer() c. cust1 = Customer.init() d. cust1 = Customer.create()

b. cust1 = Customer()

After you identify the data attributes for an object, you can identify the methods that a. store the data for the object b. define the behavior of an object c. access the object d. can be deleted

b. define the behavior of an object

In a three-tier architecture, the business tier stores the code that a. controls the user interface b. defines the business objects c. accesses the file or database d. handles all monetary transactions

b. defines the business objects

Which of the following creates a Boolean variable? a. flag = True or False b. flag = True c. if flag == True: d. flag == "False"

b. flag = True

import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Escape from the Maze") root.geometry("600x300") frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True) def click_button1(): --root.title("Wrong way!") def click_button2(): --root.destroy() button1 = ttk.Button(frame, text="Go Left", command=click_button1) button2 = ttk.Button(frame, text="Go Right", command=click_button2) button1.pack() button2.pack() root.mainloop() Which code snippet adds the frame to the root window? a. root = tk.Tk() root.title("Escape from the Maze") root.geometry("600x300") b. frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True) c. import tkinter as tk from tkinter import ttk d. root.mainloop()

b. frame = ttk.Frame(root, padding="200 100 200 100")

Which function provides a way to check an object's type? a. istype() b. isinstance() c. showtype() d. instance()

b. isinstance()

Refer to Code Example 4-1: What is the scope of the variable named s? Code Example 4-1 def get_username(first, last): --s = first + "." + last --return s.lower() def main(): --first_name = input("Enter your first name: ") --last_name = input("Enter your last name: ") --username = get_username(first_name, last_name) --print("Your username is: " + username) if __name__ == "__main__": --main() a. global b. local c. global in main() but local in get_username() d. local in main() but global in get_username()

b. local

To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the a. upper() method to convert the first character in each string to uppercase. b. lower() method to convert all characters in each string to lowercase. c. lower() method to convert all characters after the first character in each string to lowercase. d. upper() method to convert all characters after the first character in each string to uppercase.

b. lower() method to convert all characters in each string to lowercase.

Which of the following defines the tasks that an object can perform? a. data structures b. methods of a class c. attributes of a class d. instances of a class

b. methods of a class

Given that pi = 3.1415926535, which of the following print() functions displays: pi = 3.14 a. print("pi= ", round(pi, 2)) b. print("pi = " + round(pi, 2)) c. print("pi = ", float(pi, 2)) d. print("pi = ", round(pi))

b. print("pi = " + round(pi, 2))

If an attribute can be calculated from other attributes in the class, you can a. merge the attribute with the other attributes b. remove the attribute and add a method that calculates it from the other attributes c. add it to the constructor d. redesign the system to avoid this calculation

b. remove the attribute and add a method that calculates it from the other attributes

Which of the following will get a floating-point number from the user? a- my_number = input(float("Enter a number:")) b- my_number = float(input "Enter a number:") c- my_number = float(input("Enter a number:")) d- my_number = input("Enter a number:") float_num = my_number

c- my_number = float(input("Enter a number:"))

Python will sort the strings that follow in this sequence: Peach peach 1peach 10Peaches a. Peach, peach, 1peach, 10Peaches b. 1peach, 10Peaches, Peach, peach c. 10Peaches, 1peach, Peach, peach d. 10peaches, 1peach, peach, Peach

c. 10Peaches, 1peach, Peach, peach

Refer to the below code example, class Rectangle: --def __init__(self, height, width): ----self.height = height ----self.width = width --def getPerimeter(self): ----perimeter = self.height * 2 + self.width * 2 ----return perimeter --def getArea(self): ----area = self.height * self.width ----return area --def getStr(self): ----return str(self.height) + " by " + str(self.width) How many public methods does this class define? a. 1 b. 2 c. 3 d. 4

c. 3

class Multiplier: --def __init__(self): ----self.num1 = 0 ----self.num2 = 0 --def getProduct(self): ----return self.num1 * self.num2 --def main(): ----m = Multiplier() ----m.num1 = 7 ----m.num2 = 3 ----print(m.num1, "X", m.num2, "=", m.getProduct()) if __name__ == "__main__": --main() When this code is executed, what does it print to the console? a. 7 X 3 = 0 b. 3 X 7 = 0 c. 7 X 3 = 21 d. 3 X 7 = 21

c. 7 X 3 = 21

Which type of expression has a value of either true or false? a. Relational b. Binary c. Boolean d. if-else

c. Boolean

To prevent another programmer from directly accessing the attributes of an object, you use a. Instantiation b. Composition c. Encapsulation d. annotation

c. Encapsulation

The object-oriented programming concept that allows you to define a new class that's based on an existing class is a. Encapsulation b. Polymorphism c. Inheritance d. instantiation

c. Inheritance

The feature of inheritance that allows an object of a subclass to be treated as if it were an object of the superclass is known as a. Abstraction b. Encapsulation c. Polymorphism d. chaining

c. Polymorphism

In a three-tier architecture, the database tier stores the code that a. controls the user interface b. defines the business objects c. accesses the file or database d. handles all monetary transactions

c. accesses the file or database

The best way to call the main() function of a program is to code a. main() b. an if statement that calls the main() function only if the function exists c. an if statement that calls the main() function only if the current module is the main module d. a while statement that calls the main() function in each loop

c. an if statement that calls the main() function only if the current module is the main module

A frame is a. the same as the root window b. a synonym for a component c. an invisible component that's used to group other components d. the padding around the edge of the root window

c. an invisible component that's used to group other components

Which of the following defines the type of data that an object can store? a. data structures b. methods of a class c. attributes of a class d. instances of a class

c. attributes of a class

To jump to the end of the current loop, you can use the a. end statement b. continue statement c. break statement d. switch statement

c. break statement

For a root window to begin listening for events, you must a. call the title() method to finish setting up the root window b. call the pack() method to enable the listeners c. call the mainloop() method after all the code that sets up the window d. none of these are required to begin listening for events

c. call the mainloop() method after all the code that sets up the window

Refer to the below code example, class Rectangle: --def __init__(self, height, width): ----self.height = height ----self.width = width --def getPerimeter(self): ----perimeter = self.height * 2 + self.width * 2 ----return perimeter --def getArea(self): ----area = self.height * self.width ----return area --def getStr(self): ----return str(self.height) + " by " + str(self.width) Which of the following adds an attribute named color to the class? a. def __init__(self, height, width, color): --self.height = height --self.width = width b. def __init__(self, height, width, color): --self.height = height --self.width = width --color = color c. def __init__(self, height, width): --self.height = height --self.width = width --self.color = "red" d. def __init__(self, height, width): --self.height = height --self.width = width --color = "red"

c. def __init__(self, height, width): --self.height = height --self.width = width --self.color = "red"

Refer to the below code example, class Rectangle: --def __init__(self, height, width): ----self.height = height ----self.width = width --def getPerimeter(self): ----perimeter = self.height * 2 + self.width * 2 ----return perimeter --def getArea(self): ----area = self.height * self.width ----return area --def getStr(self): ----return str(self.height) + " by " + str(self.width) What is another way to code the getArea() method and have it perform the same task? a. ef getArea(self): --area = self.height * self.width --return b. def getArea(self): --self.area = self.height * self.width c. def getArea(self): --return self.height * self.width d. def getArea(): --return height * width

c. def getArea(self): --return self.height * self.width

A UML class diagram a. describes the functions of one or more modules b. shows the state of one of more classes c. describes the attributes and methods of one or more classes d. shows the relationship between the identity of an object and its state

c. describes the attributes and methods of one or more classes

What will be the result of the following code if the user enters 81 at the prompt? score_curve = 7 score = input("Enter your score on the exam: ") score_curve += score print(score_curve) a. 88 will be displayed on the console. b. 81 will be displayed on the console c. error: you cannot use the += operator to add a string variable to an int value d. error: you cannot print a numeric variable

c. error: you cannot use the += operator to add a string variable to an int value

Refer to the below code example, class Rectangle: --def __init__(self, height, width): ----self.height = height ----self.width = width --def getPerimeter(self): ----perimeter = self.height * 2 + self.width * 2 ----return perimeter --def getArea(self): ----area = self.height * self.width ----return area --def getStr(self): ----return str(self.height) + " by " + str(self.width) If the Rectangle class is stored in a module named shapes, which of the following imports the class and creates an object from it? a. import shapes --rectangle = Rectangle(10, 20) b. import Rectangle from shapes --rectangle = Rectangle(10, 20) c. from shapes import Rectangle --rectangle = Rectangle(10, 20) d. import Rectangle from shapes --rectangle = new Rectangle(10, 20)

c. from shapes import Rectangle --rectangle = Rectangle(10, 20)

Python relies on correct __________ to determine the meaning of a statement. a. continuation b. punctuation c. indentation d. comments

c. indentation

What will be displayed after the following code executes? guess = 19 if guess < 19: --print("Too low") elif guess > 19: --print("Too high") a. Too low b. Too high c. nothing will display

c. nothing will display

In a program designed with the three-tier architecture, the developer a. uses tiers instead of objects b. groups all data attributes and methods into tiers c. organizes the code for the program in three tiers d. uses objects in all three tiers

c. organizes the code for the program in three tiers

If a data attribute contains two or more components, you should consider a. combining it with other attributes b. removing it from your list of attributes c. subdividing it into multiple attributes d. storing it in a separate class

c. subdividing it into multiple attributes

What is the argument of the print() function in the following Python statement? print("My student ID is " + str(123456) ) a. 123456 b. str(123456) c. "My student ID is " d. "My student ID is " + str(123456)

d. "My student ID is " + str(123456)

What do you typically use to model the relationships between the classes in an object-oriented program? a. Hierarchy charts b. Pseudocode c. Object models d. UML diagrams

d. UML diagrams

Python comments a. are ignored by the compiler b. can be used to document what a program or portion of code does c. can be used so certain lines of code are not executed during testing d. all of the above

d. all of the above

To create a root window, you can a. call the title() method b. call the geometry() method c. call the mainloop() method d. call the Tk() constructor

d. call the Tk() constructor

Given a variable named cust that refers to a Customer object, which of the following statements calls the getFullName() method of the object? a. Customer.fullName b. cust.fullName c. Customer.getFullName() d. cust.getFullName()

d. cust.getFullName()

Which of the following defines a constructor that initializes one attribute? a. def __init__(name): --this.name = name b. def __init__(this, name): --name = this.name c. def __init__(name): --self.name = name d. def __init__(self, name): --self.name = name

d. def __init__(self, name): --self.name = name

class Product: --def __init__(self, name="", price=0.0): ----self.name = name ----self.price = price --def getDescription(self): ----return self.name class Book(Product): --def __init__(self, name="", price=0.0, author=""): ----Product.__init__(self, name, price) ----self.author = author --def getDescription(self): ----return Product.getDescription(self) + " by " + self.author class Movie(Product): --def __init__(self, name="", price=0.0, year=0): ----Product.__init__(self, name, price) self.year = year --def getDescription(self): ----return Product.getDescription(self) + " (" + str(self.year) + ")" If a class named App inherits the Product class, you can add an attribute named version by coding the constructor for the App class like this: a. def __init__(self, version="1.0"): -- self.version = version b. def __init__(self, name="", price=0.0, version="1.0"): --App.__init__(self, name, price, version) c. def __init__(self, name="", price=0.0, version="1.0"): --App.__init__(self, name, price) --self.version = version d. def __init__(self, name="", price=0.0, version="1.0"): --Product.__init__(self, name, price) --self.version = version

d. def __init__(self, name="", price=0.0, version="1.0"): --Product.__init__(self, name, price) --self.version = version

Which of the following data types would you use to store the number 25.62? a. Str b. Int c. num d. float

d. float

A global variable a. is defined inside the main() function b. cannot be modified inside a function c. cannot be accessed from within a function d. is defined outside of all functions

d. is defined outside of all functions

Given a variable named p that refers to a Product object, which of the following statements accesses the price attribute of the Product object? a. Product.price b. p.price c. Product.price() d. p.price()

d. p.price()

In a three-tier architecture, the presentation tier stores the code that e. controls the user interface f. defines the business objects g. accesses the file or database h. handles all monetary transactions

e. controls the user interface


Conjuntos de estudio relacionados

Chapter 4 - Life Policy Provisions and Options

View Set

Ethnocentrism Vs. Cultural Relativism

View Set

Lesson 1 : Introduction to Web Development

View Set

FIN 310 CHAPTER 10, Fin 310 Chapter 9, Fin 310 Chapter 8, FIN 310 Chapter 6, Fin 310 Chapter 7

View Set

ATI Parenteral (IV) Medications Test

View Set