CSCE 110 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

15

>>> a = 3.141592 >>> camel = 42 >>> fout.write('{0} and {1}'.format(camel, a))

{1, 2, 3, 4, 'apple'}

>>> a = [1, 2, 3, 4, 1, 3, 'apple'] >>> b = set(a) >>> b

{2, 3, 4}

>>> b = {2, 3, 4, 'apple'} >>> b.remove('apple') >>> b

True

>>> d1 = {'a': 1, 'b':2} >>> d2 = {'b': 2, 'a': 1} >>> d1 == d2

['apples', 'pears', 'grapes']

>>> fruit = ['apple', 'pear', 'grape'] >>> [i + 's' for i in fruit]

0 -1

>>> fruit = ['apple', 'pear', 'grape'] >>> fruit.index('apple') if 'apple' in fruit else -1 >>> fruit.index('peach') if 'peach' in fruit else -1

6

>>> fruit = ['apple', 'pear', 'grape'] >>> fruit.index('grape') + 4

ValueError

>>> fruit = ['apple', 'pear', 'grape'] >>> fruit.index('peach')

['appleapple', 'grapegrape', 'pearpear']

>>> fruits = ['apple', 'grape', 'orange', 'pear', 'melon'] >>> blist = [x * 2 for x in fruits if 'p' in x] >>> blist

[x for x in fruits if 'p' in x]

>>> fruits = ['apple', 'grape', 'orange', 'pear', 'melon'] >>> alist = CODE HERE >>> alist What code needs to go in line 2 in order to get the following output? ['apple', 'grape', 'pear']

16 (The return value is the number of characters that were written)

>>> line1 = 'This is the end\n' >>> fout.write(line1)

fout.write(line2)

>>> line2 = "Let's start again.\n" What code do you have to add next to get an output of 19? >>> CODE HERE

2

>>> x = 52 >>> fout.write(str(x))

ValueError (100 is not in list)

>>> x = [10, 20, 30, 20] >>> x.index(100)

1

>>> x = [10, 20, 30, 20] >>> x.index(20)

path

A string like 'C:\\Python39' that identifies a file or directory is called what?

class, instance

An instance/class attribute is a Python variable that belongs to a class rather than a particular object. It is shared between all the objects of this class. An instance/class attribute is a Python variable belonging to one, and only one, object. This variable is only accessible in the scope of this object.

Program will print 'Hello' if we change print(d) to print(self.d).

Analyze the code below: class Demo: def __init__(self,d): self.d=d def print(self): print(d) a = Demo(''Hello'') a.print() You cannot use print(self) as a function name. Program has an error because class A does not have a constructor. Program will print 'Hello' if we change print(d) to print(self.d). Syntax Error.

instance

Before you use some objects, like a Turtle object, you must create them. This is formally called creating an __________ instance of the object's class.

File1.readline ()

Consider the file object: File1. How would you read the first line of text? File1.readline () File1.readline () File1.readline () File1.read ()

This is line 1

Consider the following text file: Example1.txt: This is line 1 This is line 2 This is line 3 What is the output of the following lines of code? with open("Example1.txt","r") as File1: file_stuff=File1.readline() print(file_stuff) This is line 1 This is line 1 This is line 2 This is line 3 This is line 1 This is line 2

num*fact(num-1) (Suppose n=5 then, 5*4*3*2*1 is returned which is the factorial of 5.)

Fill in the line of the following Python code for calculating the factorial of a number. def fact(num): if num == 0: return 1 else: return _____________________ num*fact(num-1) (num-1)*(num-2) num*(num-1) fact(num)*fact(num-1)

Open('dog_breeds.txt', 'r')

Given the file dog_breeds.txt, which of the following is the correct way to open the file for reading as a text file? Open('dog_breeds.txt', 'w') Open('dog_breeds.txt', 'rb') Open('dog_breeds.txt', 'r') Open('dog_breeds.txt', 'wb')

False (this is true for Local)

Global variables accessible only inside the function where the variable was initialized.

global

Global/local variables are accessible throughout the program and inside every function.

C

How can we swap two numbers a = 10, b = 20 in python without using third variable? A - a = b b = a B - a,b = b,a C - both a & b D - b = a a = b

+

If you define a method named __add__ for a class, you can use this operator on the class objects.

a string

In Python, whatever you enter as input, the input() function converts it into a string an integer a list a number a set

len([char for char in string if char == " "])

List comprehension: Count the number of spaces in a string answer = ?

[num for num in nums if num % 8 == 0]

List comprehension: Find all of the numbers from 1-1000 that are divisible by 8 answer = ?

[num for num in nums if "6" in str(num)]

List comprehension: Find all of the numbers from 1-1000 that have a 6 in them answer = ?

local, global

Local/global variables are defined inside a function, while local/global variables are not defined inside any function.

inside

Methods are different from functions because they are defined inside/outside a class definition.

def increment(self, seconds): seconds += self.time_to_int() return int_to_time(seconds)

Rewrite the following increment function as a method. def increment(time, seconds): time.second += seconds if time.second >= 60: time.second -= 60 time.minute += 1 if time.minute >= 60: time.minute -= 60 time.hour += 1

It is an illegal command (+ Operator cannot be operated on the sets.)

Suppose we are given with two sets(s1&s2) then what is the output of the code S1 + S2 Adds the elements of the both the sets. Removes the repeating elements and adds both the sets. It is an illegal command. Output will be stored in S1.

(0,0)

The coordinates for turtle's initial position, known as the origin.

read(), readline()

The file function read()/readlines() reads the entire contents of the file and returns a string, while read()/readline() reads and returns the next single line from the file.

is, ==

The is/== operator checks whether both the operands refer to the same object or not, while the is/== operator compares the values of both the operands and checks for value equality.

self

The name used to refer to the current instance of a class within the class definition is: this other self None that

pixel

The smallest rectangular display area on a computer screen.

os.path.exists()

This function checks whether a file or directory exists.

os.path.isdir()

This function checks whether it is a directory.

os.path.isfile()

This function checks whether it is a file.

os.listdir()

This function returns a list of the files (and other directories) in the given directory.

__str__

This method returns a string representation of an object.

Infile.read(2)

To read two characters from a file object infile, we use ____________ Infile.read(2) Infile.read() Infile.readline() Infile.readlines()

'w'

To write a file, you have to open it with what mode as a second parameter? >>> fout = open('output.txt', ???)

False

True/False >>> a1 = [1, 2, 3, 4] >>> a2 = [4, 3, 2, 1] >>> print(a1 == a2)

True

True/False Class methods are defined within a class and perform some action helpful to that specific type of object.

False

True/False The syntax for invoking a class method is the same as the syntax for calling a function.

False

True/False dict1 = {'a': 10, 'b': 20, 'c': 30} dict2 = {'c': 30, 'a': 10, 'b': 20} print(dict1 != dict2)

True

True/False A function can be called from anywhere in a program's code, including code within other functions.

True

True/False A local variable cannot be accessed anywhere outside the function where it is initialized.

False

True/False A recursive function may or may not have a recursive case.

True

True/False Classes can contain functions, called methods available only to objects of that type.

True

True/False Every data value in Python is an object.

True

True/False Every object is an instance of some class.

True

True/False Most of the computation is expressed in terms of operations on objects.

False

True/False Only problems that are recursively defined can be solved using recursion.

False

True/False Strictly speaking, functions are necessary.

True

True/False When a function is called, any expressions supplied as arguments are first evaluated.

False

True/False: The larger the pixel, the smoother the lines drawn with them will be.

Append the file "Example.txt"

What do the following lines of code do? with open("Example.txt","a") as writefile: writefile.write("This is line A\n") writefile.write("This is line B\n") Write to the file "Example.txt" Append the file "Example.txt" Read the file "Example.txt"

error (There is no indent.)

What do the following lines of code do? with open("Example3.txt","w") as file1: file1.write("This is line C\n") Append the file "Example3.txt". error Read the file "Example3.txt".

A list of lines

What does the <readlines()> method returns? Str A list of lines List of single characters List of integers

Program gets into an infinite loop

What happens if the base condition isn't defined in recursive programs? Program gets into an infinite loop Program runs once Program runs n number of times where n is the argument given to the function An exception is thrown

True

What is output of 33 == 33.0 ? False True 33 None of the above

input_file = open('ex1.txt', 'r') input_file.close()

What is the correct way to open, read, and close a file called ex1?

a list of Nones (print() returns None.)

What is the output of print(k) in the following Python code snippet? k = [print(i) for i in my_string if i not in "aeiou"] print(k) all characters of my_string that aren't vowels a list of Nones list of Trues list of Falses

D (Because decrementing condition of 'n' is not present in the while loop.)

What is the output of the following code? def nprint(message, n): while(n > 0): print(message) n-=1 nprint('z', 5) A - zzzz B - zzzzz C - Syntax Error D - Infinite Loop

7

What is the output of the following code? a_list = ['a', 10, 1.23, 'Tom'] b_tuple = (1, 2, 3, 4, 5, 6) print(a_list.index('Tom') + b_tuple.index(5)) 5 6 7 8 Error

[0, 100, -10, 200, -20, 300]

What is the output of the following program? a_list = [-10, 10, -20, 20, -30, 30] b_list = [i*10 if i > 0 else i + 10 for i in a_list] print(b_list) [-100, 100, -200, 200, -300, 300] [-20, 20, -30, 30, -40, 40] [0, 20, -10, 30, -20, 40] [0, 100, -10, 200, -20, 300] Error

NameError

What is the output of the following program? def f(): s = 100 print(s) f() print(s)

[20, 40, 60, 80, 100]

What is the output? numbers = [10, 20, 30, 40, 50] result = [] for number in numbers: result += [number * 2] print(result)

catching an exception

What is this called? try: fin = open('bad_file') except: print('Something went wrong.'

prints all characters of my_string that aren't vowels

What will be the output of the following Python code snippet? k = [print(i) for i in my_string if i not in "aeiou"] prints all the vowels in my_string prints all the consonants in my_string prints all characters of my_string that aren't vowels prints only on executing print(k)

[('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1), (' ', 1), ('W', 1), ('O', 1), ('R', 1), ('L', 1), ('D', 1)]

What will be the output of the following Python code snippet? my_string = "hello world" k = [(i.upper(), len(i)) for i in my_string] print(k) [('HELLO', 5), ('WORLD', 5)] [('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1), (' ', 1), ('W', 1), ('O', 1), ('R', 1), ('L', 1), ('D', 1)] [('HELLO WORLD', 11)] none of the mentioned

[['ad', 'bd', 'cd'], ['ae', 'be', 'ce'], ['af', 'bf', 'cf']]

What will be the output of the following Python code snippet? print([[i+j for i in "abc"] for j in "def"]) ['da', 'ea', 'fa', 'db', 'eb', 'fb', 'dc', 'ec', 'fc'] [['ad', 'bd', 'cd'], ['ae', 'be', 'ce'], ['af', 'bf', 'cf']] [['da', 'db', 'dc'], ['ea', 'eb', 'ec'], ['fa', 'fb', 'fc']] ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']

['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']

What will be the output of the following Python code snippet? print([i+j for i in "abc" for j in "def"]) ['da', 'ea', 'fa', 'db', 'eb', 'fb', 'dc', 'ec', 'fc'] [['ad', 'bd', 'cd'], ['ae', 'be', 'ce'], ['af', 'bf', 'cf']] [['da', 'db', 'dc'], ['ea', 'eb', 'ec'], ['fa', 'fb', 'fc']] ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']

['h', 'e', 'l', 'l', 'o'] (We are iterating over each letter in the string.)

What will be the output of the following Python code snippet? print([i.lower() for i in "HELLO"]) ['h', 'e', 'l', 'l', 'o'] 'hello' ['hello' hello

Syntax error

What will be the output of the following Python code snippet? print([if i%2==0: i; else: i+1; for i in range(4)]) [0, 2, 2, 4] [1, 1, 3, 3] Syntax error none of the mentioned

[0, 1, 2]

What will be the output of the following Python code snippet? x = [i**+1 for i in range(3)]; print(x); [0, 1, 2] [1, 2, 5] error, **+ is not a valid operator error, ';' is not allowed

100 (The fun(fun(n+11)) part of the code keeps executing until the value of n becomes greater than 100, after which n-5 is returned and printed.)

What will be the output of the following Python code? def fun(n): if (n > 100): return n - 5 return fun(fun(n+11)); print(fun(45)) 50 100 74 Infinite loop

17

What will be the output of the following Python code? def test(i,j): if(i==0): return j else: return test(i-1,i+j) print(test(4,7)) 13 7 17 infinite loop

C. Error: I/O operation on closed file.

What will be the output of the following code snippet? colors = ['red\n', 'yellow\n', 'blue\n'] f = open('colors.txt', 'w') f.writelines(colors) f.close() f.seek(0,0) for line in f: print (line) A. redyellowblueOption 1 B. ['red\n', 'yellow\n', 'blue\n'] C. Error: I/O operation on closed file. D. Compilation errorOption 3

.read()

When reading a file using the file object, what method is best for reading the entire file into a single string? .readline() .read() .read_file_to_str() .readlines()

The file is created

When you try to open a file with the mode 'a' and if the file does not exist, what happens? The file is overwritten. The file is created. An error occurs. The file is open for reading. The file is closed.

The file is open for reading

When you try to open a file with the mode 'r' and if the file already exists, what happens? The file is overwritten. The file is created. An error occurs. The file is open for reading. The file is closed.

An error occurs

When you try to open a file with the mode 'r' and if the file does not exist, what happens? The file is overwritten. The file is created. An error occurs. The file is open for reading. The file is closed.

The file is overwritten

When you try to open a file with the mode 'w' and if the file already exists, what happens? The file is overwritten. The file is created. An error occurs. The file is open for reading. The file is closed.

The file is created

When you try to open a file with the mode 'w' and if the file does not exist, what happens? The file is overwritten. The file is created. An error occurs. The file is open for reading. The file is closed.

Os.path.isfile(logo)

Which of the following functions can be used to check if a file "logo" exists? Os.path.isFile(logo) Os.path.exists(logo) Os.path.isfile(logo) Os.isFile(logo)

All of the above

Which of the following statements are true? When you open a file for reading, if the file does not exist, an error occurs When you open a file for writing, if the file does not exist, a new file is created When you open a file for writing, if the file exists, the existing file is overwritten with the new file All of the above

Every recursive function must have a return value

Which of the following statements is false about recursion? Every recursive function must have a base case Infinite recursion can occur if the base case isn't properly mentioned A recursive function makes the code easier to understand Every recursive function must have a return value

5 apple 6 14.7 7 12 8 pear

a_list = ['apple', 14.7, 12, 'pear'] for index, item in enumerate(a_list, 5): print(index, item)

False

b = {1, 2, 3, 4, 'apple'} c = {3, 4, 'apple', 'banana'} b.issubset(c)

False

b = {1, 2, 3, 4, 'apple'} c = {3, 4, 'apple', 'banana'} b.issuperset(c)

True

b = {1, 2, 3, 4, 'apple'} d = set([1, 2]) d.issubset(b)

instance

class Person: def __init__(self, name): self.name = name Is self.name an instance or class variable?

result = [number * 2 for number in numbers]

numbers = [10, 20, 30, 40, 50] CODE HERE print(result) What code needs to go in line 2 in order to get the following output? [20, 40, 60, 80, 100]


Ensembles d'études connexes

Debt: Conceptual Issues, Long term/short term etc

View Set

D073 Best Practices in Management: Projects, Staffing, Scheduling, and Budgeting - D073

View Set

German tenses with 'essen', 'trinken' & 'kochen' (Present, Past, Future + "can" with 'ich')

View Set

Fundamentals Nursing Prep U Chapter 6 Values, Ethics, and Advocacy

View Set

Social Psychology Ch. 11 Prosocial Behavior

View Set

TestOut Security Pro, Lab Practice

View Set

Prepu: Chapter 6: Growth and Development of the School-age Child

View Set