CS Test 2

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

What is "Programming is fun"[4: 6]

ra

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

return number

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

6

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

7

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

3 -1

What will be displayed by the following code? x = 1 def f1(): x = 3 print(x) f1() print(x)

3 1

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

3 3

Whenever possible, you should avoid using __________.

Global variables

____________ is a template, blueprint, or contract that defines objects.

A class

A variable defined outside a function is referred to as __________.

A global variable

A variable defined inside a function is referred to as __________.

A local variable

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

__________ is a simple but incomplete version of a function.

A stub

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

An object

Which of the following statement is most accurate? A. A reference variable is an object. B. A reference variable refers to an object. C. An object may contain other objects. D. An object may contain the references of other objects.

B & D

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

False

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

False

__________ is to implement one function in the structure chart at a time from the top to the bottom.

Top-down approach

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

aaaa

An object is an instance of a _________.

class

The keyword ___________ is required to define a class.

class

What is "Programming is fun"[-1]

n

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

" "

What is min("Programming is fun")?

A blank space character

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

-1

Given a string s = "Welcome", what is s.count('e')?

2

Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What is k after invoking nPrint("A message", k)? k = 2 nPrint("A message", k)

2

Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What is k after invoking nPrint("A message", k)? k = 2 nPrint(n = k, message = "A message")

2

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

3 1

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 will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1()

3 3

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

3 3

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

4

What is len("Good")?

4

_____________ is used to create an object.

A value-returning method

If a class defines the __str__(self) method, for an object obj for the class, you can use ______ to invoke the __str__ method. A. obj.__str__() B. str(obj) C. obj.str() D. __str__(obj)

A, B

To check whether string s1 contains s2, use _________. A. s1.__contains__(s2) B. s1 in s2 C. s1.contains(s2) D. si.in(s2)

A, B

To return the length of string s, use _________. A. s.__len__() B. len(s) C. size(s) D. s.size()

A, B

To retrieve the character at index 3 from string s, use _________. A. s[3] B. s.getitem(3) C. s.__getitem__(3) D. s.getItem(3)

A, C

To concatenate two strings s1 and s2 into s3, use _________. A. s3 = s1 + s2 B. s3 = s1.add(s2) C. s3 = s1.__add(s2) D. s3 = s1.__add__(s2)

A, D

Given the following function header: def f(p1, p2, p3, p4) Which of the following is correct to invoke it (More than 1 answer)? A. f(1, 2, 3, 4) B. f(p1 = 1, 2, 3, 4) C. f(p1 = 1, p2 = 2, p3 = 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)

A, D, and E

Analyze the following code: class MyDate: def __init__(self, year, month, day): self.year = year self.month = month self.day = day class Name: def __init__(self, firstName, mi, lastName, birthDate): self.firstName = firstName self.mi = mi self.lastName = lastName self.birthDate = birthDate birthDate = MyDate(1990, 1, 1) name = Name("John", 'F', "Smith", birthDate) birthDate = MyDate(1991, 1, 1) birthDate.year = 1992 print(name.birthDate.year) A. The program displays 1990. B. The program displays 1991. C. The program displays 1992. D. The program displays no thing.

A. The program displays 1990.

Which of the following should be defined as a None function? A. Write a function that prints integers from 1 to 100. B. Write a function that returns a random integer from 1 to 100. C. Write a function that checks whether a number is from 1 to 100. D. Write a function that converts an uppercase letter to lowercase.

A. Write a function that prints integers from 1 to 100.

Analyze the following code: class Name: def __init__(self, firstName, mi, lastName): self.firstName = firstName self.mi = mi self.lastName = lastName firstName = "John" name = Name(firstName, 'F', "Smith") firstName = "Peter" name.lastName = "Pan" print(name.firstName, name.lastName) A. The program displays Peter Pan. B. The program displays John Pan. C. The program displays Peter Smith. D. The program displays John Smith.

B. The program displays John Pan.

Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() a.__y = 45 print(a.getX()) A. The program has an error because x is private and cannot be access outside of the class. B. The program has an error because y is private and cannot be access outside of the class. C. The program has an error because you cannot name a variable using __y. D. The program runs fine and prints 1. E. The program runs fine and prints 45.

B. The program has an error because y is private and cannot be access outside of the class.

Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() print(a.__y) A. The program has an error because x is private and cannot be access outside of the class. B. The program has an error because y is private and cannot be access outside of the class. C. The program has an error because you cannot name a variable using __y. D. The program runs fine and prints 1. E. The program runs fine and prints 0.

B. The program has an error because y is private and cannot be access outside of the class.

In the following code, def 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? A. __a B. __b C. __c__ D. __d__

B. __b

What is the value of times displayed? def main(): myCount = Count() times = 0 for i in range(0, 100): increment(myCount, times) print("myCount.count =", myCount.count, "times =", times) def increment(c, times): c.count += 1 times += 1 class Count: def __init__(self): self.count = 0 main() A. count is 101 times is 0 B. count is 100 times is 0 C. count is 100 times is 100 D. count is 101 times is 101

B. count is 100 times is 0

Suppose i is 2 and j is 4, i + j is same as _________. A. i.__add(j) B. i.__add__(j) C. i.__Add(j) D. i.__ADD(j)

B. i.__add__(j)

What will be displayed by the following code? class Count: def __init__(self, count = 0): self.__count = count c1 = Count(2) c2 = Count(2) print(id(c1) == id(c2), end = " ") s1 = "Good" s2 = "Good" print(id(s1) == id(s2)) A. True False B. True True C. False True D. False False

C. False True

Which of the following function headers is correct? A. def f(a = 1, b): B. def f(a = 1, b, c = 2): C. def f(a = 1, b = 1, c = 2): D. def f(a = 1, b = 1, c = 2, d):

C. def f(a = 1, b = 1, c = 2):

Given a string s = "Welcome", which of the following code is incorrect? A. print(s[0]) B. print(s.lower()) C. s[1] = 'r' D. print(s.strip())

C. s[1] = "r"

Analyze the following code: class A: def __init__(self, s): self.s = s def print(self): print(s) a = A("Welcome") a.print() A. The program has an error because class A does not have a constructor. B. The program has an error because class A should have a print method with signature print(self, s). C. The program has an error because class A should have a print method with signature print(s). D. The program would run if you change print(s) to print(self.s).

D. The program has an error because class A does not have a constructor.

Analyze the following code: class A: def __init__(self, s): self.s = s def print(self): print(self.s) a = A() a.print() A. The program has an error because class A does not have a constructor. B. The program has an error because s is not defined in print(s). C. The program runs fine and prints nothing. D. The program has an error because the constructor is invoked without an argument.

D. The program has an error because the constructor is invoked without an argument.

Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() print(a.x) A. The program has an error because x is private and cannot be access outside of the class. B. The program has an error because y is private and cannot be access outside of the class. C. The program has an error because you cannot name a variable using __y. D. The program runs fine and prints 1. E. The program runs fine and prints 0.

D. The program runs fine and prints 1.

Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() a.x = 45 print(a.x) A. The program has an error because x is private and cannot be access outside of the class. B. The program has an error because y is private and cannot be access outside of the class. C. The program has an error because you cannot name a variable using __y. D. The program runs fine and prints 1. E. The program runs fine and prints 45.

E. The program runs fine and prints 45

Analyze the following code: class A: def __init__(self, s = "Welcome"): self.s = s def print(self): print(self.s) a = A() a.print() A. The program has an error because class A does not have a constructor. B. The program has an error because s is not defined in print(s). C. The program runs fine and prints nothing. D. The program has an error because the constructor is invoked without an argument. E. The program runs fine and prints Welcome.

E. The program runs fine and prints Welcome

The header of a function consists of ____________.

Function name and parameter list

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

Geed

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

Infinite loop

A function _________.

May have no parameters

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

No

A function with no return statement returns ______.

None

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

None

Arguments to functions always appear within __________.

Parentheses

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

What is "Programming is fun"[:2]?

Pr

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

Programming is fu

The __________ creates an object in the memory and invokes __________.

The __init__ method

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

The program has a runtime error because x is not defined.

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

True

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

True

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

fu

What is max("Programming is fun")?

u


Conjuntos de estudio relacionados

Word Smart and Word Smart II SAT vocabulary(1621~)

View Set

Heart Quiz Review - Path that blood takes through the heart

View Set

Chapter 8 Hispanic/Latino Americans

View Set