Programming Final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

__________ represents an entity in the real world that can be distinctly identified.

An object

Which of the following statement is most accurate?

An object is a reference type variable An object may contain the references of other objects.

Which classes are exception classes in Python?

ArithmeticError RuntimeError SyntaxError

Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What will be displayed by the call nPrint(4, 'a')?

Invalid call

Which of the following is the most accurate statement?

It is best to create a customized exception class that is a sub class of an existing exception class.

Which of the following should be defined as a function that returns a value?

Write a function that converts gallons to liters Write a function that checks if a number is odd or even Write a function that picks a random character from 'A' to 'Z'

Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

a stack

Suppose d = {"john":40, "peter":45}, to delete the entry for "john":40, use ________.

del d["john"]

To open a file c:\scores.txt for reading, use __________.

infile = open("c:\\scores.txt", "r")

Inheritance relationship is also known as the __________.

is-a relationship

To check whether an object o is an instance of class A, use _________.

isinstance(o, A)

Choose all the valid identifiers below:

mile1 MILE Y_5

To shuffle list1, use _______.

random.shuffle(list1)

Which of the following function(s) is(are) incorrect? range(0, 3.5) range(10, 4, -1) range(1, 3, 1) Correct!

range(0, 3.5) range(2.5, 4.5)

Consider the following incomplete code: def f(number): # Missing function body print(f(5)) The missing function body should be ________.

return number

What is "Programming is fun"[1:3]?

ro

Which of the following functions return 4.

round(3.9)

The time.time() returns ________________ .

the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).

Given two sets s1 and s2, s1 < s2 is _________.

true if s1 is a proper subset of s2

What statement can be used to handle some of the run-time errors in a program?

try/except statement

What is max("Programming is fun")?

u

Assume x = 4 and y = 5, Which of the following is true?

x < 5 or y < 5

Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 & s2?

{1, 4}

________ is used to create an object.

A constructor

Choose all the correct statements.

A function may return multiple values A function may have 0 or more parameters

Check all the correct statement using print function. A) print("Hello") B) print(135) C) print("135") D) print("Hello"); E) print(Hello)

A) print("Hello") B) print(135) C) print("135")

Class design should include ________ fields constructor accessor and mutator methods other useful methods

All of the Above

Which of the following expression results in a value 1?

37%6

What is x after the following statements? x = 2 y = 1 x *= y + 1

4

What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1()

3 3

A Python line comment begins with ________. A) // B) # C) /* D) $

B) #

Which of the following is the correct expression of character 4?

"4" '4'

Suppose d = {"john":40, "peter":45}, the keys are __________

"john" and "peter"

Suppose t = (1, 2, 4, 3), t[1 : 3] is _________.

(2, 4)

What is y after the following statement is executed? x = 0 y = 5 if x > 0 else -5

-5

The expression "Test " + 1 + 2 + 3 evaluates to ________.

Ilegal Expression

Does the function call in the following function cause syntax errors? import math def main(): math.sin(math.pi) main()

No

If a function does not return a value, by default, it returns ___________.

None

Which of the following statements is true on overriding methods?

Overriding means re-defining a method with same name as a method in the super class.

What gives a program the ability to call the correct method depending on the type of object that is used to call it?

Polymorphism

Which of the following is not a superclass/subclass relationship?

Savings Account/Checking Account

Analyze the following code and choose the more accurate statement. class A: def __init__(self, s): self.s = s def print(self): print(s) a = A("Welcome") a.print()

The program would run if you change print(s) to print(self.s).

Suppose s = {1, 2, 4, 3}, what happens when invoking s.add(4)?

This method is executed fine and 4 is not added to the set since 4 is already in the set.

What is the value of the following expression? True or True and False

True

Suppose s is "Welcome", what is s.upper()?

WELCOME

What will be displayed by the following code? ? (note ? represents a blank space) print(format("Welcome", "10s"), end = '#') print(format(111, "4d"), end = '#') print(format(924.656, "3.2f"))

Welcome???#?111#924.66

Which of the following should be defined as a None function?

Write a function that prints integers from 1 to 20

"Welcome to Python".split() is ________

["Welcome", "to", "Python"]

What is list("abcd")?

['a', 'b', 'c', 'd']

Suppose list1 = [2 * x for x in range(0, 4)], list1 is ________

[0, 2, 4, 6]

Suppose t = (1, 2), 2 * t is _________.

[1, 2, 1, 2]

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.sort()?

[1, 3, 3, 4, 5, 5, 20, 25]

You can place the line continuation symbol __ at the end of a line to tell the interpreter that the statement is continued on the next line.

\

In the following code, class A: def __init__(self): __a = 1 self.__b = 1 self.c = 1 __d__ = 1 # Other methods omitted Which of the following is a private data field?

__b

When creating an object, the system allocates memory and invokes the ______ method.

__init__ method

Which special method returns a string representation of the object?

__str__ method

The readlines() method returns a ____________.

a list of lines

The format function returns _______.

a str

Given the following function def nPrint(message, n): while n > 0: print(message, end ="") n -= 1 What will be displayed by the call nPrint('a', 4)?

aaaa

An object is an instance of a __________.

class

Analyze the following code and choose all the correct statements. count = 0 while count < 100: # Point A print("Welcome to Python!") count += 1 # Point B # Point C

count < 100 is always True at Point A count < 100 is always False at Point C

Which of the following statements create a dictionary?

d = {} d = {"john":40, "peter":45} d = {40:"john", 45:"peter"}

What function do you use to read a number?

eval(input("Enter a number"))

What does a subclass inherit from a superclass? (Choose all that apply)

fields methods

Given the following function header: def fn(p1, p2, p3) Which of the following is correct to invoke it?

fn(1, 2, 3) fn(p1 = 1, p2 = 2, p3 = 3)

To format a number x to 3 digits after the decimal point, use _______.

format(x, "5.3f")

Suppose isValid is a boolean variable, which of the following is the correct and best statement for testing if isValid is true.

if isValid :

To read the entire remaining contents of the file as a string from a file object infile, use _________.

infile.read( )

To read two characters from a file object infile, use _________.

infile.read(2)

Suppose d = {"john":40, "peter":45}, to obtain the number of entries in dictionary, use ________.

len(d)

__________ creates a list.

list1 = list() list1 = [ ] list1 = list([12, 4, 4]) list1 = [12, 4, 4]

Whenever possible, you should avoid using __________.

local variables has the same name as a global variable

What is the number of iterations in the following loop: for i in range(1, n): # iteration code

n-1

Choose all the appropriate ways, both syntax wise and logic wise, to invoke the following function? def f1(x = 1, y = 2): return x**2, y**2

n1, n2 = f1(5,10) n1, n2 =f1() n2,n1 = f1(1,2)

Which of the following is equivalent to x != y?

not (x == y) x > y or x < y

Which statement is true regarding object class

object class is the root of class hierarchy Every class inherits directly or indirectly from object class

The ______ function can be used to check if a file f exists.

os.path.isfile(f)

To open a file c:\scores.txt for writing, use __________.

outfile = open("c:\\scores.txt", "w")

When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

pass by value

Suppose s = {1, 2, 4, 3}, which of the following is(are) incorrect?

print(s[3]) s[3] = 45

Which statement(s) are true regarding private class variables?

private variables can be accessible inside the class.

To generate a random integer between 1 and 10, use ________________.

random.randint(1, 10) random.randrange(1, 11)

To check whether string s1 contains s2, use _________.

s2 in s1

You can use ___________ to create an empty set.

set( )

Which of the following statements produces {'a', 'b', 'c'}?

set("abac")

To add number to sum, you write

sum = sum + number

Suppose A is a subclass of B, to invoke the __init__ method in B from A, you write _________.

super().__init__( )

Suppose t = (1, 2, 4, 3), which of the following is incorrect?

t[3]=45

To draw a circle of diameter 20 with red color, use _________.

turtle.dot(20,"red")

To set the pen size to 5 pixels, use _________.

turtle.pensize(5)

Suppose s1 = {1, 2, 4, 3} and s2 = {1, 5, 4, 13}, what is s1 ^ s2?

{2, 3, 5, 13}

If x = 0.428, what is the result of print(format(x,' .2%'))

43%

Given a string s = "Programming is fun", what is s.find('m')?

6

What is ord("B") ?

66

Which of the following expressions return True.

6>=6 5 < 6 True

What is max(3, 5, 1, 7, 4)?

7

What is the value of i printed? j = i = 1 i += j + j * 5 print("What is i?", i)

7

The equal comparison operator is __________.

==

What is chr(ord('A'))?

A

Choose the correct statements: A. It is best to use for loops for counter controlled loops. B. It is best to use while loops for sentinel controlled loops. C. Sometimes, an infinite loop is needed D. It is best to use while loop for counter controlled loops

A B C

Suppose list1 is [1, 3, 2, 4, 5, 2, 1, 0], Which of the following is correct? print(list1[0]) print(list1[:2]) print(list1[:-2]) print(list1[4:9])

A B C

What will be displayed by the following code? print("A", end = ' ') print("B", end = ' ') print("C", end = ' ') print("D", end = ' ')

A B C D

Python syntax is case-sensitive. A) True B) False

A) True

Why do computers use zeros and ones? A) because digital devices have two stable states and it is natural to use one state for 0 and the other for 1. B) because combinations of zeros and ones can represent any numbers and characters. C) because binary numbers are the bases upon which all other number systems are built. D) because binary numbers are simplest.

A) because digital devices have two stable states and it is natural to use one state for 0 and the other for 1.

Suppose x is 1. What is x after x -= 1?

0

random.randint(0, 1) returns ____________.

0 or 1

The function range(5) return a sequence ______________.

0, 1, 2, 3, 4

To following code reads two number. Which of the following is the correct input for the code? x, y = eval(input("Enter two numbers: ")

1, 2

What is the result of 45 // 4?

11

2 * 3 ** 2 evaluates to __________.

18

Assume m = [[1, 2], [3, 4], [5, 6]], what is len(m[0])

2

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.index(5)?

2

What will be displayed by the following code? x, y = 1, 2 x, y = y, x print(x, y)

2 1

Choose all of the following expressions that result in 8 or 8.0

2**3 math.pow(2,3) 2*2*2

Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is max(list1)?

25

What will be displayed by print(ord('z') - ord('a'))?

25

Assume m = [[1, 2], [3, 4], [5, 6]], what is len(m)

3

What will be displayed by the following code? x = 1 x = 2 * x + 1 print(x)

3

What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1(2, 1)

3 2

What statements are true regarding super class and sub class? Super class is bigger than sub class Super class is sometimes called parent class Super class is more general; Sub class is more specific A super class may have more than one sub class.

B C D

___________ translates high-level language program into machine language program. A) An assembler B) A compiler C) CPU D) The operating system.

B) A compiler

________ is interpreted. A) Java B) Python C) C D) The operating system.

B) Python

Choose all possible ways to run a Python program test.py: A) Use the command: run python test.py B) Use the command: python test.py C) Use "Run Module" from IDLE editor that has test.py loaded D) After typing up test.py in an editor, it will run automatically

B) Use the command: python test.py C) Use "Run Module" from IDLE editor that has test.py loaded

Which of the following code is correct? A) print("Programming is fun") print("Python is fun") B) print("Programming is fun") print("Python is fun") C) print("Programming is fun)print("Python is fun") D) print("Programming is fun)print("Python is fun")

B) print("Programming is fun") print("Python is fun")

Analyze the following code: Code 1: number = eval(input("Enter number: ")) if number % 2 == 0: even = True else: even = False Code 2: number = eval(input("Enter number: ")) even = number % 2 == 0

Both Code 1 and Code 2 are correct

One gigabyte is approximately ________ bytes. A) 1 million B) 10 million C) 1 billion D) 1 trillion

C) 1 billion

To show the current location and direction of the turtle object, use ___________. A) turtle.showLocation() B) turtle.showDirection() C) turtle.showturtle() D) turtle.showTurtle()

C) turtle.showturtle()

In Python, a syntax error is detected by the ________ at _________. A) compiler/at compile time B) compiler/at runtime C) interpreter/at compile time D) interpreter/at runtime

D) interpreter/at runtime

To move the turtle to a point at (4, 5), use ___________. A) turtle.move(4, 5) B) turtle.moveto(4, 5) C) turtle.go(4, 5) D) turtle.goto(4, 5)

D) turtle.goto(4, 5)

Which statement is true?

Every object of a subclass is also an object of that class's superclass

list1 = [11, 2, 23] and list2 = [2, 11, 23], list1 == list2 is ________

False

What is "Good".replace("o", "e")?

Geed

In Python exception handling structure, when executing code in the try block, which statements are true?

If no exception occurs, the except clause is skipped. If an exception occurs during execution of the try clause, the rest of the try clause is skipped.


Set pelajaran terkait

Style/Syntax Terms Anadiplosis-Zeumga

View Set

New Jersey Laws, Rules and Regulations Common to All Lines

View Set

CH 12 SOME LESSONS FROM CAPITAL MARKET HISTORY

View Set

3.1.2 Social Engineering Overview Facts

View Set

Homeostasis and the Internal Environment

View Set

BIO 2700 (Evolution) Final Study Guide

View Set