csc studyguide1

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

15 _____ 3 = 0 a. % b. / c. // d. *

a. %

Which input value causes "Goodbye" to be output next? x = int(input()) while x >= 0: # Do something x = int(input()) print('Goodbye') a. -1 b. 0 c. 1 d. No such value

a. -1

What is the ending value of a when b is assigned with the value 5? a = 7 a = b + 5 if b > 5 else 0 a. 0 b. 5 c. 7 d. 10

a. 0

What is the value of x after the following code is executed? x = 17 if x * 2 <= 34: x = 0 else: x = x + 1 x = x + 1 a. 1 b. 18 c. 19 d. 35

a. 1

What is output? class Student: def __init__(self): self.age = 0 self.height = 0 self.weight = 0 student1 = Student() student1.age = 10 student2 = Student() student2.height = 155 print(student1.age, student1.weight, student1.height) print(student2.age, student2.weight, student2.height) a. 10 0 0 0 0 155 b. 155 0 0 0 0 10 c. 10 10 10 155 155 155 d. 10 0 155 10 0 155

a. 10 0 0 0 0 155

If a list has 20 elements, how many outer loop iterations are executed for selection sort? a. 19 b. 20 c. 21 d. 18

a. 19

30) How many times will the body of the loop execute? my_list = [6, 2, 8, -1, 12, 15, -7] x = Get first my_list value While x is not negative: put "Positive number!" to output x = Get next my_list value Put "Done" to output a. 3 b. 4 c. 5 d. 7

a. 3

In [2, 3, 6, 10, 20, 25, 100], how many elements will be checked to find the element20 using binary search? a. 3 b. 4 c. 1 d. 2

a. 3

How many times will the print statement execute?for i in range(1, 3): for j in range(8, 12, 2): print(f'{i}. {j}') a. 4 b. 6 c. 9 d. 36

a. 4

ABC.txt: Hello Hi Greetings What is output? file = open('ABC.txt') lines = file.readlines()[2] print(lines) a. Greetings b. Hello Hi c. Hello Hi Greetings d. Hi Greetings

a. Greetings

Which of the following statements about my_list is false? my_list = ['JFK', 'LAX', 'MIA'] a. The element at index 1 is 'JFK' b. The list has a length of 3 c. The list elements are all strings d. The index of the last item in the list is 2

a. The element at index 1 is 'JFK'

Which is an essential feature of a while loop having the following form? while loop_expression: loop_body a. The loop_expression should be affected by the loop_body b. The loop_expression should not be affected by the loop_body c. The loop_body should get user input d. The loop_body should update at least two variables

a. The loop_expression should be affected by the loop_body

What happens when an object's reference count is zero? a. The object is no longer referenced. b. The object is ready for memory allocation. c. The object is being referenced. d. The object will be deallocated from memory immediately.

a. The object is no longer referenced.

Which of the following is a good candidate for using recursive functions? a. To solve the greatest common divisor (GCD) problem b. To solve problems that have a true and false solution c. To solve problems that require excessive memory allocation d. To solve logarithmic problems

a. To solve the greatest common divisor (GCD) problem

What is output? my_string = 'What is your name?' print(my_string.split('?')) a. ['What is your name', ''] b. ['What is your name', '?'] c. ['What is your name?', ''] d. ['What', 'is', 'your', 'name', '?']

a. ['What is your name','']

What is output? b1 = [7, 5, 9, 6] b1 = sorted(b1) b2 = b1 b2.append(2) print(b1, b2) a. [5, 6, 7, 9, 2] [5, 6, 7, 9, 2] b. [5, 6, 7, 9] [2, 5, 6, 7, 9] c. [2, 5, 6, 7, 9] [2, 5, 6, 7, 9] d. [5, 6, 7, 9] [5, 6, 7, 9, 2]

a. [5, 6, 7, 9, 2] [5, 6, 7, 9, 2]

A derived class may define a method having the same name as the base class means that: a. a derived class method overrides the method of the base class. b. a base class method overrides the method of the derived class. c. the functionality of the method is the same in both derived and base class. d. a derived class method overloads the method of the base class

a. a derived class method overrides the method of the base class.

A python code only runs the except block when _____. a. all the statements in the try block are executed b. the programmer manually calls the except block c. an error occurs anywhere in the code d. an error occurs in the preceding try block

a. all the statements in the try block are executed.

A sequence of instructions that solves a problem is called _____. a. an algorithm b. a process c. an allegory d. turtle graphics

a. an algorithm

A base case is a _____. a. case that returns a value without performing a recursive call b. function method that calls itself c. function execution instance that calls another execution instance of the same function d. case that calls another function method

a. case that returns a value without performing a recursive call

What conditions have to be true to make the following code display "B"? if color == 'red': if style < 3: print('A') elif style < 5: print('B') else: print('C') elif color == 'blue': print('D') a. color is 'red' and style is 4 b. color is 'red' and style is 5 c. color is 'red' and style is 6 d. color is 'blue' and style is 3

a. color is 'red' and style is 4

Which XXX generates the following output? Division by zero 100 50 33 25 my_list = [0, 1, 2, 3, 4] for i in my_list: try: if i == 0: raise ZeroDivisionError('Division by zero') else: num = 100 // i print(num) XXX a. except ZeroDivisionError as e:print(e) b. except e:print(e) c. except ZeroDivisionError:print(except) d. except ZeroDivisionError: print(ZeroDivisionError)

a. except ZeroDivisionError as e:print(e)

Which of the following is the correct syntax for a multiple exception handler? a. except (NameError, AttributeError): b. except [NameError, AttributeError]: c. except NameError AttributeError: d. except NameError, AttributeError:

a. except(NameError, AttributeError)

Which statement opens a file for appending? a. file = open('ABC.txt', 'a') b. file = open('ABC.txt', 'r') c. file = open('ABC.txt', 'rw') d. file = open('ABC.txt', 'w')

a. file = open('ABC.txt', 'a')

Identify the correct syntax used to write to a text file. a. file = open('My_data.txt', 'w') file.write('This file contains data') b. file = open('My_data.txt', 'w')file.write(100) c. file = open('My_data.txt', 'w') file.write([100, 200, 300]) d. file = open('My_data.txt', 'a')file.write(['hello', 'hi', 'hey'])

a. file = open('My_data.txt', 'w') file.write('This file contains data')

Identify the code that generates 'The value of pi is: 3.14' as output. a. from math import pi print(f'The value of pi is: {pi:.2f}') b. import pi from math print(f'The value of pi is: {pi:.2f}') c. import math print(f'The value of pi is: {pi:.2f}') d. import piprint(f'The value of pi is: {math.pi:.2f}')

a. from math import pi print('The value of pi is: {pi:.2f}')

A key aspect of an abstract data type is to facilitate _________. a. information hiding b. interface hiding c. information manipulation d. attribute manipulation

a. information hiding

A(n) _________ represents a single instance of a class. a. instance object b. instance attribute c. class object d. class attribute

a. instance object

19) Which method call returns the number of elements in my_list? a. len(my_list) b. size(my_list) c. my_list.count() d. my_list.size()

a. len(my_list)

Which type of error does not cause the program to crash? a. logic error b. value error c. indentation error d. assignment error

a. logic error

The reload() function returns the _____. a. namespace of the module with updated definitions b. pre-existing namespace c. attributes corresponding to a module d. variables defined in the module

a. namespace of the module with updated definitions

Select the code that gives ['cat in a hat', 'fluffy dogs', 1989, 1990] as the output. a. new_list = ['1000', '2000', '3000', 'cat in a hat', 'fluffy dogs']print(new_list[-2:] + [1989, 1990]) b. new_list = ['cat in a hat', 'fluffy dogs','1000', '2000','3000']print(new_list[-2:] + [1989, 1990]) c. new_list = ['1000', '2000', '3000', 'cat in a hat', 'fluffy dogs']print(new_list[-1:] + [1989, 1990]) d. new_list = ['1000', '2000', '3000', 1989, 1990]print(new_list[-2:] + ['cat in a hat', 'fluffy dogs'])

a. new_list = ['1000, '2000', '3000', 'cat in a hat', 'fluffy dogs']print(new_list[-2:] + [1989,1990])

Dividing by zero is an example of which type of error? a. runtime b. syntax c. logic d. infinity

a. runtime

What sequence is generated by range(4)? a. 4 b. 0 1 2 3 c. 1 2 3 4 d. 0 1 2 3 4

b. 0 1 2 3

What is x's finalvalue? x = 10 y = 20 if y <= 2 * x: x = x + 5 else: x = x * 2 a. 10 b. 15 c. 20 d. 25

b. 15

Consider the following program: t = 15, t = t * 2, t = t + 1, t = t - 4, put t What does the program produce as output? a. 11 b. 27 c. 12 d. 15

b. 27

Which statement is true about inheritance? a. A derived class cannot serve as a base class for another class. b. A class can serve as a base class for multiple derived classes. c. A class can be derived from only one class. d. A class can serve as a base class for only one class.

b. A class can serve as a base class for multiple derived classes.

Which type of program converts a high-level language program into machine instructions? a. App b. Compiler c. Processor d. Assembler

b. Compiler

Which of the following loops is best implemented with a for loop? a. Asking a user to enter names until the user enters 'Quit'. b. Counting the number of negative values in a list of integers. c. Starting from a user-entered integer, increment the value until the value is a prime number. d. Reading values from a temperature sensor until it gives a value greater than 100 degrees.

b. Counting the number of negative values in a list of integers.

32) _____ is a concept where the derived class acquires the attributes of its base class. a. Encapsulation b. Inheritance c. Abstraction d. Polymorphism

b. Inheritance

What is the worst-case runtime for a merge sort? a. 𝑂(log𝑁) b. 𝑂(𝑁log𝑁) c. 𝑂(𝑁^2) d. 𝑂(𝑁)

b. O(NlogN)

How does the given function improve the code versus if no function was present? def fahrenheit_to_celsius(fahrenheit): return (fahrenheit - 32.0) * 5.0 / 9.0 fahrenheit = float(input()) c1 = fahrenheit_to_celsius(fahrenheit); c2 = fahrenheit_to_celsius(32.0); c3 = fahrenheit_to_celsius(72.0); a. The use of the function makes the program run faster b. The use of the function decreases redundant code c. The use of the function reduces the number of variables in the code d. The function does not improve the code

b. The use of the function decreases redundant code

Which expressions for YYY and ZZZ will output "Young" when user_age is less than 20 and "Young but not too young" when user_age is between 10 and 20?age_type = '' if YYY: age_type = age_type + "Young" if ZZZ: age_type = age_type + " but not too young" print(age_type) a. YYY: user_age < 20 ZZZ: user_age < 10 b. YYY: user_age < 20 ZZZ: user_age > 10 c. YYY: user_age > 20 ZZZ: user_age < 10 d. YYY: user_age > 20 ZZZ: user_age > 10

b. YYY: user_age < 20 ZZZ: user_age > 10

Given [0, 2, 4, 7, 92], i = 0, k = 4, what are the contents of the right partition? a. [0, 2, 4] b. [7, 92] c. [0, 2, 4, 7] d. [4, 7, 92]

b. [7, 92]

Which of the following statements has a syntax error? Assume age and years are variables that have already been defined. a. age = years - 2 b. age + 2 = years c. age = 17 - 2 d. age = -15

b. age + 2 = years

In the following code, the variable val is the function call's _____. def calc_square_area(size): area = size * size return area val = float(input('Enter size of square: ')) square_area = calc_square_area(val) print(f'A square of size {val} has area {square_area}') a. parameter b. argument c. property d. value

b. argument

Which of the following options can sort the list in descending order? i.list_name.sort(reverse=True) ii.sorted(list_name, reverse=True) iii.sorted(list_name)[::-1] iv.sorted(list_name) a. i, ii, iv b. i, ii, iii c. i, ii d. i, ii, iii, iv

b. i, ii, iii

The built-in Python function that gives an object's identity is: a. memory() b. id() c. type() d. identity()

b. id()

While adding output statements to debug recursive functions, _____ the print statements to show the current depth of recursion. a. left align b. indent c. right align d. center align

b. indent

Which statement removes the last element of my_list? a. my_list.pop(len(my_list) b. my_list.pop(len(my_list)-1) c. my_list.remove(len(my_list)) d. my_list.remove(len(my_list)-1)

b. my_list.pop(len(my_list)-1)

Assigning a value to a floating point variable that is too large for the computer to represent is a condition called _____ . a. bit error b. overflow c. overcapacity d. system error

b. overflow

A(n) _____ is a directory that gives access to all of the modules stored in a directory when imported .a. function b. package c. init d. group

b. package

Which print statement displays the value of a variable called argv in a module called sys? a. print(argv in sys) b. print(sys.argv) c. print(sys_argv) d. print(module sys var argv

b. print(sys.argv)

Which of the following statements causes a ValueError to occur? a. try ValueError b. raise ValueError c. except ValueError d. except

b. raise ValueError

Which code opens a csv file with semicolon as a delimiter? a. with open('my_file.csv', 'r') as file: csv_data = csv.reader('my_file.csv', delimiter = ';') b. with open('my_file.csv', 'r') as file: csv_data = csv.reader(file, delimiter = ';') c. with open('my_file.csv', 'r') as file: csv_data = csv.reader('file', delimiter = 'semicolon') d. with open('my_file.csv', 'r') as file: csv_data = csv.reader(file)

b. with open('my_file.csv', 'r') as file: csv_data = csv.reader(file, delimiter = ';')

Which symbol represents the multiplication operation in programming? a. () b. . c. * d. x

c. *

What is output? def sub(i,j): if(i==0): return j else: return sub(i-1,j-i) print(sub(4,10)) a. 20 b. -3 c. 0 d. Infinite loop

c. 0

What is the output? for j in range(2): for k in range(4): if (k == 2): break print(f'{j}{k}', end=' ') a. 00 01 02 b. 00 01 02 03 c. 00 01 10 11 d. 00 01 02 10 11 12

c. 00 01 10 11

What is the ending value of z? x = 0.3 z = math.pow(math.ceil(x), 2) a. 0.0 b. 0.09 c. 1.0 d. 1.09

c. 1.0

What is the output? my_list = [2, 8, 3, 1, 18, 5]print(my_list[3] + my_list[1] * 2) a. 7 b. 10 c. 17 d. 18

c. 17

What is the value of x after the following code is executed? x = 7 If x < 7 x = x + 1 x = x + 2 a. 7 b. 8 c. 9 d. 10

c. 9

Which of the following statements is true for built-in exceptions? a. Even if the code can raise an exception, the code must always include an 'except' clause. b. A built-in exception cannot be explicitly called in a code. c. A built-in exception can be called in a code using a 'raise' statement. d. A built-in exception needs to be always caught in a try-except block.

c. A built-in exception can be called in a code using a 'raise' statement

Which is the best way to debug recursive functions? a. Adding print statement of what that line of code does. b. Adding output statements by keeping all the statements left aligned. c. Adding output statements with an indent to print statements at every iteration. d. Adding output statements by keeping all the statements equally indented.

c. Adding output statements with an indent to print statements at every iteration.

For what values of x will "Medium" be output? If x > 40: Output "Large" Else If x > 20: Output "Medium" Else If x > 10: Output "Small" a. Any x larger than 20 b. Any x smaller than 40 c. Any x from 21 to 40 d. Any x from 10 to 40

c. Any x from 21 to 40

Which of the following loops is best implemented with a while loop? a. Checking to see if a list of integers contains the value 12. b. Counting how many keys in a dictionary start with the letter 'A'. c. Asking the user to enter positive integers, exiting by entering -1. d. Looping through the characters in a string, and displaying 'yes' if it contains a vowel

c. Asking the user to enter positive integers, exiting by entering -1.

What is output? def divide_by_two(count):if count == 1: print('Terminated..!') else: print(count) divide_by_two(count/2) divide_by_two(9) a. 9 4.5 2.25 1.125 Terminated..! b. 9 4.5 2.25 Terminated..! c. Infinite loop d. 9 4.5 2.25 1.125 0.5625 Terminated..!

c. Infinite loop

What is the output? def print_water_temp_for_coffee(temp): if temp < 195: print('Too cold.') elif (temp >= 195) and (temp <= 205): print('Perfect temperature.') elif (temp > 205): print('Too hot.') print_water_temp_for_coffee(205) print_water_temp_for_coffee(190) a. Too cold. b. Perfect temperature. c. Perfect temperature. Too cold. d. Perfect temperature.Too cold.

c. Perfect temperature. Too cold.

RAM is an abbreviation that stands for: a. Real Analog Memory b. Ranged Access Memory c. Random Access Memory d. Random Abbreviated Memory

c. Random Access Memory

What happens when an unhandled exception occurs? a. The finally clause executes, and then the exception is re-raised. b. The finally clause executes, and then runs the try block again. c. The finally clause executes, and the code finishes. d. The finally clause executes, and the code returns -1

c. The finally clause executes, and the code finishes.

The _____ keyword binds a name to the exception being handled. a. raise b. try c. as d. except

c. as

A(n) _________ acts as a factory that creates instance objects. a. instance object b. instance attribute c. class object d. class attribute

c. class object

Python uses _________ constructs to handle errors during execution. a. control b. class method c. exception-handling d. conditional

c. exception-handling

Which statement makes the code in the math module available? a. use math b. allow math c. import math d. include math

c. import math

A function defined within a class is known as a(n) _________. a. class attribute b. method object c. instance method d. class object

c. instance method

Which of the following functions returns a Boolean value depending on whether a given variable matches a given type? a. isvalue() b. isnan() c. isinstance() d. isdtype()

c. isinstance()

Which is the correct syntax for opening a file in Python? a. my_file = readlines('Wikipedia_data.txt') b. my_file = read('Wikipedia_data.txt') c. my_file = open('Wikipedia_data.txt') d. my_file = open_text('Wikipedia_data.txt')

c. my_file = open('Wikipedia_data.txt')

It is recommended to use a 'with' statement when opening files so that ____. a. the file opens using the correct path b. opening of the file will not fail c. the file is closed when no longer needed d. the file is always opened in the read-write mode

c. the file is closed when no longer needed

Which of the following methods will change the string 'Python Programming' into 'PYTHON PROGRAMMING'? a. title() b. isupper() c. upper() d. capitalize()

c. upper()

Which statement correctly creates a new tuple west_cities with elements 'Vancouver', 'Portland', 'Eugene' in that order? a. west_cities = ['Vancouver', 'Portland', 'Eugene'] b. west_cities = ('Portland', 'Vancouver', 'Eugene') c. west_cities = ('Vancouver', 'Portland', 'Eugene') d. west_cities = ['Portland', 'Vancouver', 'Eugene']

c. west_cities = ('Vancouver', 'Portland', 'Eugene')

Which symbol is used in Python to create a comment? a. * b. C c. // d. #

d. #

Which expression is equivalent to: not x and y == a and b? a. (not (x and y)) == (a and b) b. ((not x) and y) and (a and b) c. not ((x and (y == a)) and b) d. ((not x) and (y == a)) and b

d. ((not x) and (y==a)) and b

What is the output? id_list = [13, 984, 231, 140] for id in id_list: if id != 231: print(id, end=' ') else: print('Done') a. Done b. 13 984 Done c. 13 984 231 140 Done d. 13 984 140 Done

d. 13 984 140 Done

What will be the date type for type(list_name.sort()) and type(sorted(list_name))? a. < class 'NoneType'>, < class 'NoneType'> b. < class 'list'>, < class 'NoneType'> c. < class 'list'>, < class 'list'> d. < class 'NoneType'>, < class 'list'>

d. < class 'NoneType'>, < class 'list'>

What is output? def get_name(name): if len(name) <= 2: raise ValueError('You need to enter your complete name.') return name def get_age(age): if age > 100: raise ValueError('Please enter some value') return age try: name = get_name('Mike') school_name = get_age('3') print(f'{name} is {school_name} years old.') except ValueError as excpt: print(excpt) except: print('Error!') a. You need to enter your complete name. b. Please enter some value c. Mike is 3 years old. d. Error!

d. Error!

Which of the following requests memory from the operating system? a. Reference counter b. Python runtime c. Python compiler d. Memory allocation device

d. Memory allocation device

_____ is a sorting algorithm that divides a list into two halves, recursively sorts eachhalf, and then merges the sorted halves to produce a sorted list. a. Quicksort b. Insertion sort c. Selection sort d. Merge sort

d. Merge sort

_____ can be set to specify optional directories where modules are located. a. sys.path b. python.path c. SYSPATH d. PYTHONPATH

d. PYTHONPATH

33) What is the output? count = 0 while count < 3: print('loop') count = count + 1 print(f'Final value of count: {count}') a. Prints 'loop' once, then 'final value of count: 1' b. Prints 'loop' three times, then 'final value of count: 3' c. Prints 'loop' three times, then 'final value of count: 4' d. Prints 'loop' forever (infinite loop)

d. Prints 'loop' forever (infinite loop)

What happens when a raise statement is encountered in a function? a. The code executes the next line in the function. b. The raise exception is printed immediately. c. An exception is immediately executed by the function returning -1. d. The function is exited with no return

d. The function is exited with no return

Which of the following is not a reason to use functions? a. To avoid writing redundant code b. To improve code readability c. To support modular development d. To make the code run faster

d. To make the code run faster

Which statement is not true? a. Inheritance tree describes the hierarchy between base and derived classes. b. A derived class can access the attributes of all of its base classes via normal attributereference operations. c. A derived class inherits the attributes of the base class, and then adds additionalattributes. d. When adding a new derived class, a programmer has to change the base class as well.

d. When adding a new derived class, a programmer has to change the base class as well.

Which of the following symbols can be used as part of an identifier? a. @ b. $ c. & d. _ (underscore)

d. _ (underscore)

Which correctly calls the add() function? def add(a, b, c): return a + b + c a. add(2 4 6) b. add(2 + 4 + 6) c. add(2; 4; 6) d. add(2, 4, 6)

d. add(2, 4, 6)

An indent variable _____ number of spaces on each iteration. a. adds unequal b. removes unequal c. removes equal d. adds equal

d. adds equal

A ________ consists of methods to interact with an instance. a. class object b. class attribute c. class instantiation d. class interface

d. class interface

Linear search is a search algorithm that searches the list _____. a. in descending order and then searches the list until the search key is found b. in ascending order and then searches the list until the search key is found c. by splitting the list into two halves and searching each half until the search key is found d. from the start of a list and checks each element until the search key is found

d. from the start of a list and checks each element until the search key is found.

Which built-in object is mutable? a. bool b. int c. str d. list

d. list

If the base condition is not defined in the recursive function, _____. a. the program runs only once b. the program runs as many times as the number passed as its argument c. the program terminates giving an error d. the program gets into an infinite loop

d. the program gets into an infinite loop


Ensembles d'études connexes

Unit 3- Clinical decision making

View Set

U12Ch46 어(魚/漁): fish, to fish (KBC)

View Set

Latin American Relations Midterm

View Set

Business policy Exam 2 chapter 5

View Set

Chapter 21 - Child, Partner, and Elder Violence

View Set