CompSci Final Exam

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

Assume you are writing a program, and you have a boolean variable called b, defined like so: b = True Pick the correct if statement to follow the code above. The if statement should be correct Python, and the body of the if statement should only run if b is True. a) if b: print "b is True!" b) if b: print "b is True!" c) if True: print b d) if True: print b

a

In the following for loop, how may spaces has Tracy moved when it is complete? for i in range(5): forward(10) a) 50 spaces b) 60 spaces c) 10 spaces d) 5 spaces

a

In the following line of code: my_boolean = True and (10 / 0 == 0) Will the 10 / 0 == 0 operation be evaluated? a) Yes b) No

a

In which of the following situations would it be best to make a function? a) You want Tracy to draw a blue line, and your program requires lots of blue lines. b) You need Tracy to move forward by 100. c) You need Tracy to turn right. d) You need to change Tracy's color

a

Suppose you have a variable defined a = "4". What is the variable type of a? a) str b) int c) float d) number

a

Suppose you run the following Python program: x = input("Enter some text: ") print x While the program is running, after the prompt has been printed, the user types the following and presses Enter: Delaware ratified the U.S. Constitution in 1787. What does the program print? a) Delaware ratified the U.S. Constitution in 1787. b) Delaware ratified the U.S. Constitution in c) Delaware d) (The program does not print anything.)

a

Suppose you want to make Tracy draw a mountain range, like the one shown below, starting from the left side. Which of the following functions would be the most useful function to write in order to solve this problem? a) # Has Tracy draw a single triangle def make_triangle(): b) # Change's Tracy's color to red def change_to_red(): c) # Moves Tracy forward by 100 def move_100(): d) # Draws a square def make_square():

a

What does the following Python code display? print "Hello" print "World" a) Hello World b) HelloWorld c) Hello World d) Hello World

a

What does the following expression evaluate to? True and not False a) True b) False

a

What does the following program do? color("red") forward(50) left(90) color("blue") forward(50) a) It draws a red line and then a blue line. b) It draws a line that is both red and blue. c) It draws two red lines and then a blue line. d) It draws a red line and then two blue lines.

a

What does the following program print? a = "hi" b = 4 c = a * b print type(c) a) <type 'str'> b) <type 'int'> c) <type 'float'> d) The program crashes and doesn't print anything.

a

What does the following program print? print "a" in "banana" a) True b) False c) 1 d) "aaa"

a

What does this function do? def mystery_function(): for i in range(10): color("green") forward(100) a) Draws a green line that is longer than 100 units long. b) Draws a green line that is less than 100 units long. c) Draws a green line that is 100 units long d) Draws a black line that is longer than 100 units long.

a

What function should be used in the blank to capitalize the first letter of the word stored in word? first_char = word[0] word = first_char.______() + word[1:] a)upper b)lower c)swapcase d)find

a

What is the difference between defining and calling a function? a) defining a function means you are creating the definition of the function. Calling a function means you are using the function in your program. b) There is no difference. Calling a function means you are creating the definition of the function. ' c) Defining a function means you are using the function in your program. d) Defining a function must be done each time you want to use the function. Calling a function can only happen once in your code.

a

What is the type of the variable x in the following Python program? x = input("Enter something: ") a) string b) integer c) float d) The type cannot be determined.

a

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("W") a) 7 b) 8 c) W d) -1

a

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("z") a) -1 b) 20 c) z d) 0

a

When you define a function, where should it go in your program? a) You should define the function before you use it. b) You should define the function after you use it.

a

Which of the following choices draws a straight multicolor line? a) color("red") forward(40) color("blue") forward(40) color("green") forward(40) b) color("red") forward(40) color("red") forward(40) color("red") forward(40) c) color("red") forward(40) backward(40) forward(40) d) color("red") right(40) color("blue") forward(40) color("green") right(40) forward(40)

a

Which of the following choices is a properly formed Python variable name, meaning it is both legal in the Python language and considered good style? a) user_age b) uSeRaGe c) user!age! d) 1user_age

a

Which of the following commands changes Tracy's color to green? a) color("green") b) color(green) c) green() d) color "green"

a

Which of the following functions is declared correctly? a) def draw_edge(): forward(100) left(90) b) def draw_edge(): forward(100) left(90) c) def draw_edge forward(100) left(90) d) def draw_edge(){ forward(100) left(90) }

a

Which of the following is NOT a valid type of comment in Python? a) %% This is a comment b) # This is a comment c) """ This is a comment """

a

Which of the following programs DOES NOT PRINT the first five letters of the alphabet, each on its own line? a) my_string = "abcde" for letter in range(len(my_string)): print(letter) b) my_string = "abcde" for letter in my_string: print(letter) c) my_string = "abcde" for i in range(len(my_string)): print(my_string[i])

a

Which of the following statements are true about for loops? I. By default, the range function starts at 0 II. Using for i in range(4) will result in i taking the values 0, 1, 2, 3, 4 III. For loops let you repeat something any number of times IV. Statements in a for loop do not need to be indented a) I, III b) II, III c) III only d) I, II, III, IV

a

Which of the following string operation is illegal? Assume that word = "music". a) word[0] = "M" b) word = word[2] + word[-1] c) word = "musical" d) word = word + "al"

a

Which operator allows you to create a string that is the result of putting two different strings together, side by side? a) + b) - c) * d) /

a

# sentence: a string containing a sentence # char: a letter def mystery(sentence, char): res = "" for letter in sentence: if letter == " ": res = res + char else: res = res + letter return res a) Returns the same sentence, but adds the letter char after every space b) Returns the same sentence, but replaces every space with the letter char c) Returns the same sentence, but adds the letter char after every letter in the sentence, except for spaces d) Returns the same sentence, but adds the letter char after every letter in the sentence

b

Adding two strings together to form one string is also called: a) slicing b) concatenating c) gluing d) looping

b

Choose the option that correctly fills in the blanks A and B for the following piece of code. When completed, the for loop should draw a zig zag by repeating 5 times. Inside of the loop, Tracy needs to move forward, turn left, move forward, and turn right. # Makes Tracy draw a zig zag left(15) for i in __A___ : forward(50) left(135) __B___(50) right(135) a) A) range(6) B) forward b) A) range(5) B) forward c) A) 0 ... 5 B) backward d) A) range(5) B) backward

b

How many possible values are there for a boolean variable? a) 1 b) 2 c) 3 d) There is an infinite number of possibilities.

b

In the following line of code: my_boolean = True or (10 / 0 == 0) Will the 10 / 0 == 0 operation be evaluated? a) Yes b) No

b

On which line of code will Python error? weight = input("How much do you weigh? ") oz_water = weight / 2 print "You should drink " + str(oz_water) print "ounces of water every day" a) Line 1 b) Line 2 c) Line 3 d) Line 4

b

Suppose we want Tracy the Turtle to move forward 100. What is wrong with the following statement: forward = 100 a) There shouldn't be a equal sign. b) The 100 should be in parentheses, and there shouldn't be a equal sign. c) forward is not a valid command. d) There is nothing wrong with the statement.

b

What does "immutable" mean with respect to Python strings? a) The variable can be assigned a new value, and the string's value can be modified. b) The variable can be assigned a new value, but the string's value cannot be modified. c) A string variable can never be changed. The variable cannot be assigned a new value, and the string's value cannot be modified. d) The variable cannot be assigned a new value, but the string's value can be modified.

b

What does the following Python program print? x = 9 + 6 / 3 * 2 - 1 print x a) 9 b) 12 c) 15 d) 21

b

What does the following code print? x = 3.4 y = 1 print int(x) print x + y a) 3.4 4.4 b) 3 4.4 c) 3 4 d) The code causes an error

b

What does the following program print? first_string = "hello" second_string = first_string.upper() print first_string print second_string a) hello hello b) hello HELLO c) HELLO hello d) HELLO HELLO

b

What does the following program print? x = "ab" y = "banana" print x in y a) True b) False c) 0 d) "ba"

b

What does the function "type" tell you? a) How many times a variable has been used b) The type of the variable c) What version of Python you are using d) What kind of text encoding a string uses

b

What does this Python expression evaluate to? 100 != 100 a) True b) False c) "True" d) "False"

b

What is printed out by this program? first = "abcde" second = "zyxwv" res = "" for i in range(len(first)): res = res + first[i] + second[i] print res a) abcdezyxwv b) azbycxdwev c) az by cx dw ev d) The program will throw an error

b

What is the final result of the expression 2**3? a) 6 b) 8 c) 2 d) That is not a valid Python expression.

b

What is the value of pos after this function call? name = "Edsger Wybe Dijkstra" pos = name.find("Edsger") a) -1 b) 0 c) E d) 5

b

What keyword means that you are defining a function? a) return b) def c) for d) function

b

What type is the following variable? x = "Hi there" a) display Hello, world! b) print "Hello, world!" c) print Hello, world! d) "Hello, world!"

b

Which Python code segment will display "Hello, world!" on the screen? a) display Hello, world! b) print "Hello, world!" c) print Hello, world! d) "Hello, world!"

b

Which of the following Python programs will not print anything? a) x = True if x: print "hi" b) if False: print "hi" c) x = False if x: print "hi" else: print "hello"

b

Which of the following Python programs will not run? a) x = 4 y = 5 print x + y b) x = 4 y = "hi" print x + y c) x = 4 y = 5.5 print x + y d) x = 4 print x + 5

b

Which of the following best describes the main purpose of comments? a) Comments create better spacing in programs. b) Comments describe your program so other people can read it more easily. c) Comments warn the people running the program about potential problems with the program.

b

Which of the following code samples correctly defines a function called move_and_turn that moves Tracy forward by 30 and then turns Tracy left by 90 degrees? a) def forward(30) left(90) move_and_turn() b) def move_and_turn(): forward(30) left(90) c) def move_and_turn: forward(30) left(90) d) def move_and_turn(): forward(30) left(90)

b

Which of the following expressions will get the last character in a string? Assume that word is a string variable. I. word[0] II. word[-1] III. word[len(word)] IV. word[len(word)-1] a) I, II, III b) II, IV c) II, III, IV d) I, IV

b

Which of the following expressions will result in "brown"? Let sentence = "The brown lazy dog" a) print sentence[4:8] b) print sentence[4:9] c) print sentence[3:8] d) print sentence[3:9]

b

Which of the following if statements checks if the string variable sentence contains the word "the"? a) if "t" or "h" or "e" in sentence: print "Contains 'the'" b) if "the" in sentence: print "Contains 'the'" c) if "the" == sentence: print "Contains 'the'" d) if sentence in "the": print "Contains 'the'"

b

Which of the following is NOT a command you can give to Tracy? a) color b)turn c)backward d)left

b

Which of the following prints CDE? a) my_string = "ABCDE" print(my_string[3:]) b) my_string = "ABCDE" print(my_string[2:]) c) my_string = "ABCDE" print(my_string[3:5]) d) my_string = "ABCDE" print(my_string[2:4])

b

Which one of the statements below will cause an error? a) ans = "hi" * 8 b) ans = "hi" + 9 c) ans = "hi" + "hi" + "hi" d) ans = ("a" * 4) + "b"

b

Which word applies to strings in Python? a) mutable b) immutable

b

which of the following commands moves tracy forward by 50? a) forward 50 b)forward(50) c) 50(forward) d) move forward 50

b

Choose the correct declaration of a float variable with the value 3.14. a) pi = "3.14" b) pi = int(3.14) c) pi = 3.14 d) float pi = 3.14

c

In what order should these statements be executed in order to get input from the user and print out the result? A) response = input("Do you like cheese? ") B) print "You have chosen " + confirm C) print "You responded " + response D) confirm = input("Are you sure? ") a) A, B, C, D b) B, C, A, D c) A, C, D, B d) C, B, A, D

c

What does the following Python program print? x = "I am" y = 6 z = "feet tall" print x print y print z a) I am 6 feet tall b) I am6feet tall C) I am 6 feet tall d) x y z

c

What does this piece of code do? backward(100) right(90) backward(100) right(90) backward(100) right(90) backward(100) right(90) a) Draws a star b) Moves and turns, but doesn't make any shape c) Draws a square moving backwards d) Draws a square moving forwards

c

What does your answer in question 1 mean? a) Strings in Python can be modified and replaced. b) Strings in Python can be modified, but not replaced. c) Strings in Python can be replaced, but not modified. d) Strings in Python can be neither replaced nor modified.

c

What is printed out by this program? word = "killer whale" print word[0:100] a) killer whal b) "killer whale " c) killer whale d) The program will throw an error

c

What is the difference between a binary operator and a unary operator? a) A computer can use binary operators, but it cannot use unary operators. b) A unary operator needs two things, while a binary operator only needs one. c) A binary operator needs two things, while a unary operator only needs one. d) Binary operators are used for arithmetic expressions, while unary operators are for strings.

c

What is the final result of the expression 7 / 3 + 6? a) 0 b) 8.33333333333 c) 8 d) 0.777777777778

c

What is the output of the following program? Assume the user enters "Florence", then "Fernandez". first_name = input("What is your first name? ") last_name = input("What is your last name? ") whole_name = first_name + last_name print whole_name a) Fernandez Florence b) Florence Fernandez c) FlorenceFernandez d) Florence Fernandez

c

What kind of data does a float variable contain? a) Whole numbers b) Words c) Numbers that can have decimal components d) A float is not a Python variable type

c

Which of the following choices will print AeCl? Assume the following variables have been defined. first_name = "Alice" last_name = "Carmichael" a) print first_name[0] + last_name[0] b) print first_name[0] + last_name[0] + first_name[-1] + last_name[-1] c) print first_name[0] + first_name[-1] + last_name[0] + last_name[-1] d) print first_name[1:] + last_name[1:]

c

Which of the following expressions will print "L"? Let word = "PINEAPPLE" a) print word[-4:] b) print word[-1] c) print word[-2] d) print word[-3]

c

Which of the following functions would return the word "CAT" when given "iCjnyAyT"? a) def find_secret_word(message): hidden_word = "" for letter in message: if letter.upper(): hidden_word = hidden_word + letter return hidden_word b) def find_secret_word(message): hidden_word = "" for letter in message: if "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in message: hidden_word = hidden_word + letter return hidden_word c) def find_secret_word(message): hidden_word = "" for letter in message: if letter != letter.lower(): hidden_word = hidden_word + letter return hidden_word d) def find_secret_word(message): hidden_word = "" for letter in message: if letter in message: hidden_word = hidden_word + letter return hidden_word

c

Which of the following is NOT a purpose of using functions? a) Functions let Tracy do new things. b)Functions help group statements together to make your code more readable. c) Functions let you execute code a fixed number of times. d) Functions allow the programmer to reuse code.

c

Which of the following is not a comparison operator? a) <= b) != c) ? d) >

c

Which of the following programs correctly moves Tracy forward by a total of 80? a) for i in range(4): forward(20) b) for i in range(4): forward(80) c) for i in range(4): forward(20) d) for i in range(4): forward(80)

c

Which of the following statements correctly changes Tracy's color to blue? a) color("red") b) color = "blue" c) color("blue") d) color(blue)

c

Which of the following statements is true about print statements? I. In order to print a string literal, the string must be enclosed in quotes. II. Each print statement will be printed on its own line. III. Print statements will not let you print strings and numbers in the same statement. IV. Print statements are how you display text on the screen. a) I, IV b) II, III c) I, II, IV d) I, III, IV

c

Which of the statements below is true about indentation in Python? a) Indentation only matters in functions and for loops. Then, everything must be indented one level. b) Indentation never matters in Python. You can align your code any way you like, but indentation makes your code easier to read. c) Indentation always matters in Python. Every statement must be aligned correctly. d) Indentation only matters in functions. Then, everything must be indented one level.

c

# address: string containing an email address def mystery(address): if not ("@" in address): return "" address = address.strip() return address a) Returns a version of the email address without any symbols in it. b) Returns the address without any leading or trailing whitespace. c) Returns an empty string d) Checks to make sure the email address is valid by checking for '@' in the address. Returns the address without any leading or trailing whitespace.

d

Choose the option that correctly prints out the variable(s). a) x = "codehs" print int(x) b) num = 8 print "num" c) name = "Alyx" age = 32 print name + "is " + age d) language = "Python" print "I'm learning " + language

d

Choose the print statement below that will cause an error. Assume that num has the value 6, and name has the value Isabella. a) print name + ":" print num b) print name + "wants " + "num " + "candies" c) print name + ": " + str(num) d) print name + ": " + num

d

On which line does this program throw an error? word = "killer whale" pos = word.find(" ") word = word + "!" word[pos] = "-" a) Line 1 b) Line 2 c) Line 3 d) Line 4

d

Suppose you write a function. How many times can you call the function in your code? a)Once b) Not more than 15 times c) It depends on the function d) As many times as you want

d

What does len("hello") evaluate to? a) "hello" b) "h" c) "—-" d) 5

d

What does the following command do: right(45)? a) it moves tracy to the right by 45 b) it moves tracy to 45 pixels from the upper left corner c) it turns tracy around 45 times d) it rotates tracy to the right by 45 degrees

d

What does the number in the parenthesis in a forward or backward command represent? a) How many degrees Tracy is supposed to turn b) How fast Tracy is supposed to move c) How many times Tracy should repeat the command d) How far Tracy is supposed to move

d

What is the final result of the expression 4 + 5 * 3? a) 27 b) 12 c) 21 d) 19

d

Which of the following choices is NOT a Python variable type? a) int b) str c) float d) number

d

Which of the following choices is a correct way to write a for loop in Python? a) for i in range(10 # loop body code b) for(var i = 0; i < 10; i++) # loop body code c) for i = 1 ... 10: # loop body code d) for i in range(10): # loop body code

d

Which of the following expressions will print "dog"? Let sentence = "lazy dog" a) print sentence[6:] b) print sentence[:] c) print sentence[:5] d) print sentence[5:]

d

Which of the following functions prints 3, and nothing else? a) s = "hello" x = s.find("l") print x b) s = "banana" x = s.find("a") print x c) s = "hello" for i in range(len(s)): print i d) s = "apple" x = s.find("l") print x

d

Which of the following is not a logical operator in Python? a) and b) or c) not d) because

d

Which of the following logical statements is equivalent to: not (A and B) (Assume A and B are boolean values.) a) not A and B b) not A and not B c) not A or B d) not A or not B

d

Which of the following options is the best way to get a number from the user that you plan to use in a mathematical equation? a) num = str(input("Enter a number: ")) b) num = number(input("Enter a number: ")) c) num = input("Enter a number: ") d) num = int(input("Enter a number: "))

d

Which of the following pieces of code will make Tracy do the following actions three times: go forward, change colors, and then turn around. a) for i in range(4): forward(30) color("blue") left(180) b) for i in range(3): forward(30) color("blue") left(180) color("red") c) for i in range(3): backward(30) color("blue") left(180) d) forward(30) color("blue") left(180) forward(30) color("green") left(180) forward(30) color("orange") left(180)

d

Which of the following prints the letter A? a) my_string = "ABCDE" print(my_string[1]) b) my_string = "ABCDE" print(my_string(0)) c) my_string = "ABCDE" print(my_string(1)) d) my_string = "ABCDE" print(my_string[0])

d

Which of the following questions could be best answered by a boolean value? a) How tall are you? b) How many people are in your class? c) What is your name? d) Do you have any pets?

d

Which of the functions below will return a string that is in alternating caps? For example, alt_case("sheep") should return "sHeEp". a) def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res + word[i].upper() else: res + word[i].lower() return res b) def alt_case(word): res = "" return word.swapcase() c) def alt_case(word): res = "" for i in range(word): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res d) def alt_case(word): res = "" for i in range(len(word)): if i % 2 == 1: res = res + word[i].upper() else: res = res + word[i].lower() return res

d


Ensembles d'études connexes

Assessment & Care of Patients with Fluid & Electrolyte Imbalances

View Set

13th- Documentary Vocabulary Assignment.

View Set

Lesson 4: Real Estate Brokerage and Law of Agency/Ethics

View Set

Quiz 2 Primary Versus Secondary, databases

View Set

Introduction to Psychology: Chapter 7 (Intelligence)

View Set

Chapter 22: Upper res. tract disorders

View Set