ENGR 102

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

What is the output for the following program? [Quotes indicate String Data Type] a = "1" b = "32"print(a+b)

"132"

What is the output of the following program to the console. my_tuple = (1, 2, 3, 4) print(my_tuple[1:3]) (1,3) (2,3) (2,3,4) (1,2,3)

(2,3)

Which of the following has a loop a -> a -> a -> a -> b -> b v b b -> b -> b ^ v-------------- ^ c -> c -^ c -> c -----------------> c

C

Calling a function in python has the form: output_if_any = function_name(arguments_if_any): True False

False

Find output for the following bool(False) True False

False

For list A, A.sort( ) sorts the list from the largest to the smallest. True False

False

The following command adds a carriage return / new line to the file, after the text 'some text' with open('file.txt', 'a') as file: file.write('some text') True False

False

The following python program WILL throw an "Error" as output. def F(): global a a += 3 a = 5 F() print(a) True False

False

The output of the following line of code: int('3.5') WILL produce the value 3. True or False?

False

We will get an error if we go past the beginning or end of a list in python when slicing. True False

False

Is the following statement True or False: The "Arch" style of code development, lets you develop the code incrementally. You can write small pieces of code and test them, and ensure that they are working before adding more code. True False

False The "Pyramid" style of code development, lets you develop the code incrementally. You can write small pieces of code and test them, and ensure that they are working before adding more code.

The output of the following program is an error. my_tuple=[1,2,3,4] my_tuple[2]=5 True False

False (my_tuple is a LIST defined with [] that is mutable)

What will be printed to the console when the following Python code is executed? health = 100 max_health = 100 armor = 40 enemy_attack = 30 enemy_defense = 50 A = health == max_health B = armor > enemy_attack C = enemy_attack > enemy_defense no_bonus1 = not(A and not(B and C)) no_bonus2 = (not A) or (not ((not B) or (not C))) print(no_bonus1, no_bonus2) Options: True False False True False False True True

False False

What is the output printed when the following lines of code are executed? Note that the answer is case sensitive x = str(bool(0)) print(x*2)

FalseFalse

a = 3 # "a" refers to the number of alphabets needed in the code The above is a valid line of code that will assign the integer 3 to the variable 'a'. True False

True

Does the following Python program contain a comment? ' ' ' Howdy!! ' ' ' a = 'Howdy Aggies!! ' b = "Howdy Sea Aggies!! " print(a) print(b) True False

True ' ' ' Howdy!! ' ' ' is a comment as the string is not assigned to any variable and thus has no effect on he program

Edmund Halley in 1694 produced an iterative formula for finding the square root of a number Q as follows: Assuming that you want to program this as a Python loop. Which loop construct would be more suitable if you want to iterate the algorithm until ? WHILE loop construct FOR loop construct

WHILE loop

What is the output for the following program if the user enters 19 as Input: a = float(input("What is your age? ")) print("Input value:", a) print("Subtracting 2:", a-2)

What is your age? 19 Input value: 19.0 Subtracting 2: 17.0

If we have to determine whether variable (A) is not zero, what is the correct Python Syntax: A/= 0 A \= 0 A -= 0 A != 0

A != 0

What will be printed when the following line of code are executed? Note that the answer is case sensitive. x = bool('0') print(x)

True

When sequentially stepping through code, an encounter with a function call, causes the code in the function to be executed next. True False

True

What is the output of the following program? a = 23 b = 43 a *= 12 b += 13*a c = a+b c -= c+5 print(c)

-5

What will be the output of the following python code? X = 32 Y = 2 X /= X X //= Y print(X)

0.0

What is the output of the following code? num_x = 3 num_y = 2 val_num = num_x//num_y print(val_num)

1

if True: print("1") print("8") if False: print("6") print("9") print("2")

1 8 2

Which of the following statements describe a good use of a comment from a programming standpoint? 1) Separate different sections of code from each other 2) Describe sections of code whose function is obvious from the reading code 3) Describe sections of code whose function is NOT obvious from the reading the code 4) Provide a reference to an external source which the reader needs to have knowledge of to understand the code

1) Separate different sections of code from each other 3) Describe sections of code whose function is NOT obvious from the reading the code 4) Provide a reference to an external source which the reader needs to have knowledge of to understand the code

In order to change all the elements of variable 'grades' to the integer 10, we run the following code: grades=[23, 45, 76,88, 90, 12, 10]for i in range (len(grades)): grades[i]=__________________ Fill in the blank with the appropriate number to get the answer, as per the problem requirements. 10 "10" 10.0 '10'

10

What will be printed when the following Python program is executed? a = [10.0, 20.0, 30.0] b = [0, 1, 0, 2] c = 0 for i in b: print(a[b[i]]) 10.0 20.0 10.0 20.0 10.0 20.0 30.0 10.0 10.0 20.0 10.0 10.0 10.0 20.0 10.0 30.0

10.0 20.0 10.0 10.0

What will be the output of the following Python code? print((2 x 5 + (10 // 5) / 10) x 10.0)

102.0

What is printed to the console when the following Python program is executed? a = [12.3, 25.6, 48.6] b = [78.6, 14.6, 3.5] c = b + a print(c[1]) 40.2 Results in an error 25.6 14.6

14.6

What will be printed when the following Python coe is executed? magic = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] diag_sum = 0 vert_sum = 0 horz_sum = 0 for i in range(3): diag_sum += magic[i][i] vert_sum += magic[2][i] horz_sum += magic[i][1] print(diag_sum, vert_sum, horz_sum) 10 15 15 15 15 15 15 10 15 15 15 10

15 15 15

What is the value stored in variable X after all the following python statements are executed? X = 2 Y = 5 Z = X*Y X = X*Z - X

18

Two styles of software construction have been discussed in the class - Pyramid style and the Arch style. Select all the statements which relate to the arch style of software construction. 1) You can write small pieces of code, test them, and ensure that they are working before adding more code 2) Only after all of the code has been written can the program be tested 3) You always have a "stable" piece of software, even when not complete! 4) if any one piece is missing or broken, the whole program falls apart!

2) Only after all of the code has been written can the program be tested 4) If any one piece is missing or broken, the whole program falls apart!

Consider the following piece of code: x = multiplication typedInput = input('Enter any number: ') print(typedInput x 2 + str(float(typedInput) x 5)) When executed, the user entered the integer 2 as shown below. Enter any number: 2 What will be printed to the console?

2210.0

What would the following python code print? a=[23, 43, 63, 83, 103, 123, 143] print(a[-7]) 123 143 23 43

23

Find the output of the following program. my_tuple = (1, 2, 3) a, b, c = my_tuple print(c) 3 2 1 error

3

What is the output of the following program? a = 10 b = 2**4 b -= a c = b//2 print(c)

3

What is the output of the following program? num1 = 33 num2 = 55 num3 = float(num1) print(num3)

33.0

What will be printed to the console when the following program is executed? HINT: Write out step by step what the values of 'i' and 'x' will be for each iteration. n = 4 x = 1 for i in range(n): x += 1 if i%2 == 0: continue elif i == 3: break else:x -= 1 print(x) The program is stuck in an infinite loop 3 None of the above 4

4

What is the output of the following program? p = round(4.576) print(p) 4 5 4.5 4.6

5

What will be the two numbers printed to the console when the following Python code is executed? def func(x): while x > 0: x -= 1 return x x = 5 y = func(x) print(x,y) 5 0 0 5 0 0 5 5

5 0

What is the output of the following program? import math q=abs(math.sqrt(25)) print(q) 5 5.0 25 25.0

5.0

What is the output of the following program? length_x = 3.2 length_y = 2 area_rect = length_x*length_y print(area_rect)

6.4

Where can functions be defined? Library All of the mentioned Module Another function

All of the mentioned

Which of the following has a conditional construct? a -> a -> a -> a -> b -> b v b -> b ^v b -> b -> b ^ v----------------- c -> c -> c -> c -^

B

Consider the following python code. n = int(input('Enter a whole number:')) x = 1 for i in range(n): x += x if i == 2: break print(x) What will be printed to the console when n = 2 and n = 4? HINT: Write out step by step what the values of 'i' and 'x' will be for each iteration. For n = 2, the program will print 4 For n = 4, the program will print 8 For n = 2, the program will print 8 For n = 4, the program will print 8 The program will not print anything as it is stuck in an infinite loop For n = 2, the program will print 4 For n = 4, the program will print 16

For n = 2, the program will print 4 For n = 4, the program will print 8

What will the following Python code print to the console when executed? x = 50 if not(x >= 10 and x <= 100): print("Hello from the if block") elif (x < 10 and x > 100): print('Hello from elif block') else: print('Hello from the else block')

Hello from the else block

Which lines will be printed when the following Python code is executed? [Negative points for wrong answers] x = 20 y = 10 z = 30 if x == 10: print("Howdy 10") if y == 20:print("Howdy 20") else:print("Howdy 30") elif y == 20: print("Howdy 40") else: print("Howdy 50")if z == 30: print("Howdy 60") else: print("Howdy 70")

Howdy 50 Howdy 60

Assume the text file temp.txt in the current directory contains only the following two lines: Howdy! Welcome to Aggieland What will be printed to the console when the following commands are executed? myfile = open('temp.txt') w = myfile.read() print(w) Howdy! Welcome to Howdy! Welco Howdy! Welcome to Aggieland Howdy! Howdy! Howdy!

Howdy! Welcome to Aggieland

What is the output of the print statement a = 10 b = 20 c = 40 d = "Howdy" print (2*(d) + (d))

HowdyHowdyHowdy

The "continue" statement is a keyword that you can put in the middle of your loop. What does it do? Does not do anything Immediately stops that specific iteration in the loop End the program Throws an error because there's no such keyword

Immediately stops that specific iteration in the loop

Which of the following lines of code will give an error? int('3') # Line 1 float('3.14') # Line 2 float('2') # Line 3 int('2.5') # Line 4 Line 3 Line 4 Line 1 Line 2

Line 4

If one has to access all the elements of an array of known length which of the following constructs is best for this purpose? None of these Conditional: if...else Loop: for Loop: while

Loop: for

Does the following code snippet carry out all of the basic file operations to process the file before returning control? myfile = open("data.dat",'r+') #Do stuff with myfile - read/write Yes , it is complete No, it's missing the file close command No, 'r+' is not a valid designator No, it's missing the file id

No, it's missing the file close command

Find the output of the following python program: how_many = 10 if how_many == 1 : print("Single") elif how_many == 2 : print("Couple") elif how_many < 5 : print("Few") elif how_many < 10 : print("Several") elif how_many > 10 : print("Lots") else: print("None") OPTIONS: Few None Lots Several

None

A global variable 'G' can be accessed from inside a function even though it is not passed in as an argument to that function. For example -- myfunction(Parameter1, Parameter2) can access G inside myfunction True False

True

Defining a function in Python has the form: def function_name (arguments_if_any): # Things function does True False

True

Find output for the following bool('False') True False

True

Slicing in python has the form:[b:a] where: a indicates the element to stop before in the list b indicates the element to start with in the list True False

True

The following code block WILL CLOSE the file when done executing with open("data.dat",r+) as myfile: #Do stuff with myfile - read/write True False

True

The following is an example of top down design Digital clock - clock - hour - minute - second - date - month - day - counter/stop clock - seconds - ms True False

True

The following program gives an error. my_tuple = (1, 2, 3, 4) my_tuple[2] = 5 True False

True

The following python program will throw an "Error" as output. def warn(): print("********** WARNING!!! **********") print("You are about to do something dangerous!") def doublewarn(): warn() warn() triplewarn() True False

True

The lists use [ ] and tuples can use the elements just separated with commas without any parentheses. True False

True

There are functions that might NOT return anything back True False

True

To test if two variables are equal and positive below are two methods: Method 1: ((variable1 == variable2) and (vairable1 > 0)) Method 2: not ((variable1 != variable2) or (variable1 <= 0)) Qn: Are Method 1 and Method 2 equivalent? True False

True

What would you have to enter as an input for the following code to exit the loop? answer = "N" while answer != "Y": print("are we there yet?") answer = input("Please respond Y or N) print("Yay! We are there!")

Y

Assume the text file temp.txt contains only the following line: 02/23/2019 What will be printed to the console when the following commands are executed? myfile = open('temp.txt') w = myfile.readline() print(w.split('/')) None of the above The program will throw an error ['2019', '23', '', '02'] ['02', '23', '2019']

['02', '23', '2019']

Assume the text file temp.txt contains only the following two lines: Howdy! Welcome to Aggieland What will be printed to the console when the following commands are executed? myfile = open('temp.txt') w = myfile.readlines() print(w) Howdy ['Howdy!\n'] Howdy!\n ['Howdy!\n', 'Welcome to Aggieland']

['Howdy!\n', 'Welcome to Aggieland']

What is the output of the following program? language = ['P', 'y', 't', 'h', 'o', 'n'] print(language[:-4]) ['P','y','t','h','o'] 'Py' [-4] ['P' , 'y']

['P' , 'y']

What will be printed to the console when the following python code is executed? x = [] for i in range(2): x.append(i) x += x print(x) [1, 1, 2, 1, 1, 2] [0, 0, 1, 0, 0, 1] [0, 2] [0, 1]

[0, 0, 1, 0, 0, 1]

The output of the following python program is def dosomething(a): a[0] = 10 x = [1, 2, 3] dosomething(x) print(x) [1,10,3] [10,20,30] [1, 2, 3] [10, 2, 3]

[10, 2, 3]

Select all the test cases which correspond to an edge cases for the following program: a = round(float(input('Enter an integer value for variable a: '))) b = round(float(input('Enter an integer value for variable b: '))) c = a+b if (a + b <= 10): print('Sum = ',a+b) elif c - 5 >= 10: print('Sum is', c) else: print('Sum is between 10 and 15') a = 7, b = 3 a = 5, b = 10 a = 20, b = -5 a = 12, b = -3

a = 7, b = 3 a = 5, b = 10 a 20, b = -5

What is printed when the following Python code is executed? def test(a, b=5, c=10): print('a is', a, 'and b is', b, 'and c is', c) test(3, 7) a is 7 and b is 3 and c is 10 a is 3 and b is 7 and c is 10 Nothing is displayed Gives an error

a is 3 and b is 7 and c is 10

What does the designator 'a' in the file open command do? Does not exist reads data from the existing file appends data to the existing file writes data to a new file

appends data to the existing file

Which of the following command can be used to open a file 'temp.txt' in the current directory in read only mode? f = open('temp.txt','r+') f = open('temp.txt','w') f = open('temp.txt','r') f = open('temp.txt','a')

f = open('temp.txt','r')

If more than one operator of equal precedence has to be evaluated, how does the evaluation occur? right to left does not matter left to right no clue :)

left to right

How is repitition handled in program? Data structures lists loops conditional statements

loops

Pick the python statement to assign your age (an integer value 19) to the variable "my_age"?

my_age = 19

Which of the following functions is a built-in function in python? print() seed() sqrt() factorial()

print()

What is the default mode when a file is opened and no mode is specified? appending reading writing there is no default - the code would show an error

reading

When executed, what output does the following Python code produce? if "cat" == "dog": print("prrrr") else: print("ruff")

ruff

What is the output of the following program? num_x = 6 num_y = 2 val_num = num_x + num_y print('val_num')

val_num


Conjuntos de estudio relacionados

practice questions for nursing exam 4

View Set

CPR, AED, & Basic First Aid Knowledge Checks

View Set

Personal Finance Chapter 4 Part 2

View Set

utc bio 2 mindtap chapter 31 test

View Set

AP1 LAB 2- INTEGUMENTARY SYSTEM AND SYNOVIAL JOINTS

View Set