CSC111 modules 2-6

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is printed by the following code if the user input is 0, 1, 1, 9, 12 for each iteration? count = 0 total = 0 while count < 5: num = int(input("Enter a number: ")) if num % 2 == 0: total += num count +=1 print(total) a. 13 b. 12 c. 10 d. 14

12

What value does the following expression evaluate to? 2 + 9 ** ((3 ** 12) - 8) / 10 a. 28 b. 27 c. 27.2 d. 30.8

27.2

When creating flowcharts we represent a decision with a:

Diamond

A variable created inside a function definition can be used outside the function as long as the function has been called first. Select one: True False

False

A while loop continues to execute until its condition becomes True. Select one: True False

False

A while loop executes at least one iteration. Select one: True False

False

All if statements must contain either an else or elif header. Select one: True False

False

Floor division, represented by //, rounds up to the closest integer. Select one: True False

False

In Python, you cannot write functions that accept multiple arguments. Select one: True False

False

Indentation levels do not matter with nested conditional statements. Select one: True False

False

Syntax errors do not appear until after the program has started running. Select one: True False

False

The = operator checks mathematical equivalence and the == assigns a value. Select one: True False

False

The body of a while loop does not have to be indented under the header. Select one: True False

False

The break statement does not completely halt a loop. Select one: True False

False

The print function collects and stores user input. Select one: True False

False

class is a valid variable name. Select one: True False

False

You should answer this by running the code snippet by hand without using the Python interpreter. What is the printed output after running the following lines of code: def a(num): i=0 result = 0 while(num>i): result = 1/num num -= 1 print(a(100))

None

What is the result of the following code? word = "Python" print(word*3)

PythonPythonPython

Improperly updating the condition variable can create an infinite loop. Select one: True False

True

In Python, a function must be defined before it can be called/used. Select one: True False

True

The continue statement skips the current iteration of the loop and continues with the next iteration. Select one: True False

True

The following two chunks of code are interchangeable/equivalent. if 0<x: if x<10: print ('x is a positive single-digit number.') if 0<x and x<10: print('x is a positive single-digit number.') Select one: True False

True

The modulus operator, represented by %, divides two numbers and returns the remainder. Select one: True False

True

What is the result of the following expression where a = 3, b=5.5, c=3.0, and f = True not((b < a) or ((a == c) and not f)) Select one: True False

True

When a function is called, the arguments are assigned to parameters in the order they are listed in the call. Select one: True False

True

You should always test your algorithm multiple times on different inputs. Select one: True False

True

[LO 6.4] if statements evaluate a condition once, whereas while loops repeat until its condition executes to False. Select one: True False

True

Which of the following are valid string literals in Python? a. "Hello" b. "'" c. 'hello' d. 'Hello there' e. "Hello'

a. "Hello" c. 'hello' d. 'Hello there'

What is the value of variable num after the following is executed? num = 10 num = num + 5 num == 20 num = num + 1 a. 16 b. 10 c. 20 d. 15

a. 16

What is an algorithm? a. A clear set of steps to be followed in calculations or other problem-solving operations b. The technique of generalizing a solution to solve similar problems c. A programming technique that tries to create the smallest solution to a given problem d. Using a computer to solve hard and complex problems

a. A clear set of steps to be followed in calculations or other problem-solving operations

How would you fix the program to get rid of the error? ((x+y)***4) + 76 + (8**x-10)) a. Add another open parentheses to the beginning of the equation b. Remove the parentheses around x+y c. Add an equal sign to the front of the equation d. Replace the double asterisk with a single one

a. Add another open parentheses to the beginning of the equation

A(n) _____ is a special variable that receives a piece of data when a function is called. a. packet b. parameter c. header d. argument

b. parameter

When documenting a function, which of the following information should be included in the docstring? a. data type of each parameter b. list of parameters c. what the function returns d. data type of the return value e. brief description of what the function does

(all of them) a. data type of each parameter b. list of parameters c. what the function returns d. data type of the return value e. brief description of what the function does

What operators can you execute on strings? a. + b. - c. / d. *

+ *

What does the following code output? answer = "" y = -10 while y < 0: y = y + 2 answer = answer + str(y) + " " print(answer) a. -8 -6 -4 -2 b. -8 -6 -4 -2 0 2 c. -8 -6 -4 -2 0 d. 8 6 4 2 0

-8 -6 -4 -2 0

Match the following terms to the corresponding loop: 1. Infinite 2. Definite 3. Indefinite a. user_input = input ("Enter your password: ") password = "Python" while user_input != password: print("Incorrect password") user_input = input ("Enter your password: ") b. count = 0 while count < 10: print("iteration:", count) c. count = 0 while count < 10: print("iteration: ", count) count += 1

1. Infinite → b. count = 0 while count < 10: print("iteration:", count) 2. Definite → c. count = 0 while count < 10: print("iteration: ", count) count += 1 3. Indefinite → a. user_input = input ("Enter your password: ") password = "Python" while user_input != password: print("Incorrect password") user_input = input ("Enter your password: ")

Match the correct definition to the key technique. 1. Pattern Recognition 2. Algorithm Design 3. Abstraction 4. Decomposition a. Breaking down a complex problem into smaller parts that are easier to understand and solve. b. Filter out unnecessary information and generalize the necessary information so it can be reused to solve similar problems. c. Use the characteristics of a good algorithm to solve a problem. d. Finding the similarities or patterns among small, decomposed problems that can help us solve more complex problems more efficiently.

1. Pattern Recognition → d. Finding the similarities or patterns among small, decomposed problems that can help us solve more complex problems more efficiently. 2. Algorithm Design → c. Use the characteristics of a good algorithm to solve a problem. 3. Abstraction → b. Filter out unnecessary information and generalize the necessary information so it can be reused to solve similar problems. 4. Decomposition → a. Breaking down a complex problem into smaller parts that are easier to understand and solve.

What is the output after executing the following code snippet if the user enters 16? import math value = input("Please enter a positive number") value = float(value) value = math.sqrt(value print(value) a. 2 b. 16.0 c. 4.0 d. 256.0

4.0

What will be the output of the following code? i = 5 line = "" while True: if i % 9 == 0: break line += str(i) + " " i += 1 print(line)

5 6 7 8

What is the following type of string manipulation called? First = "throat" Second = "warbler" First + Second

Concatenation

What is really being outputted here? a = 3 b = 5.5 c = 3.0 if (a>b) and (a == c): print("This is true.") else: print("This is false.") a. False b. Nothing c. This is true. d. True e. This is false

This is false.

Boolean values can be stored into variables. Select one: True False

True

An infinite loop is an iterative control structure that, a. Loops forever and must be forced to terminate b. Loops until the program terminates with a system error c. Loops a predictable amount of time d. Both a and b are correct

a. Loops forever and must be forced to terminate b. Loops until the program terminates with a system error

The elif header allows for, a. Multi-way selection as a single if statement b. The use of a "catch-all" case in multi-way selection c. Multi-way selection that cannot be accomplished otherwise

a. Multi-way selection as a single if statement

How many conditions must execute to False for the "x is greater than y" message to be displayed? if x == y: print('x and y are equal') else: if x < y: print('x is less than y') else: print('x is greater than y') a. Two b. One c. Three d. Zero

a. Two

After the following code executes, which of the statements is true? def convert (degrees): degrees += 273.15 return degrees degrees = 10 print(convert(degrees)) a. Two variables called degrees have been created. b. The value of degrees in the main namespace is 283.15. c. The value of degrees in the function namespace is 10. d. This code generates an error because you cannot use the same variable name twice

a. Two variables called degrees have been created.

A(n) _____ is a piece of data that is sent into a function. a. argument b. packet c. parameter d. header

a. argument

You _____ a function to execute it. a. call b. export c. define d. import

a. call

A group of statements that exist within a program for the purpose of performing a specific task is a(n) _____. a. function b. parameter c. expression d. block

a. function

The first line of a function definition is known as the _____. a. header b. body c. initialization d. introduction

a. header

Which one of the following Boolean expressions is not logically equivalent to the other two? a. num > 0 and num < 10 b. not(num < 0 or num > 10) c. num >= 0 and num <= 10

a. num > 0 and num < 10

Which of the following correctly asks for user input, converts the input to an integer and then prints out the type of the user input to confirm that it has been correctly converted? a. print(type(int(input("Enter a number: ")))) b. print(type(int(input("Enter a number: "))) c. print(type(int("Enter a number: "))) d. type(print(int(input("Enter a number: "))))

a. print(type(int(input("Enter a number: "))))

When will the else block get executed in the following program? if x > 0 : result = x**2 else: result = 3 a. when x is negative b. The else block never gets executed c. The else block always gets executed d. when x is negative or zero

a. when x is negative or zero

The function pow(x,y,z) is evaluated as: a. (x***y)***z b. (x**y) % z c. (x***y)**z d. (x**y) / z

b. (x**y) % z

How many times will the following print statement execute? loop_controller = True while (loop_controller == True): print('Hello') loop_controller = False a. 0 b. 1 c. 2 d. It's an infinite loop

b. 1

Given the following code snippet, how many times will the print statement occur? i = 1 while i <= 20: if i % 4 == 0: print(i) i += 3 a. 0 b. 2 c. 3 d. 1

b. 2

At least how many testing cases are needed to fully test the program below? Hint: Each branch should have at least one testing case. if (num/3) < 10: num = num * 2 elif (num/3) < 15: num = num + 1 elif(num/3) < 25: num = num/2 else: print(num) a. 5 b. 4 c. 6 d. 3

b. 4

What will happen if the following chunk of code is executed? var = 13 print(var a. A runtime error message will appear b A syntax error message will appear c. 13 will be printed d. Nothing will be printed

b. A syntax error message will appear

What is always the first step of designing a good algorithm? a. Decompose the tasks into smaller problems b. Identify the inputs and outputs of the problem c. Generalize the process to similar problems d. Identify patterns among the problems

b. Identify the inputs and outputs of the problem

If a function doesn't have a return statement, which of the following does the function return? a. int b. None c. null d. Function without the return statement cannot be executed.

b. None

Which is not a characteristic of a good algorithm? a. Correctness b. Rigidness c. Input d. Finiteness

b. Rigidness

The terms definite loop and indefinite loop are used to indicate whether, a. A given loop executes at least once b. The number of times that a loop is executed can be determined before the loop is executed c. Both of the above

b. The number of times that a loop is executed can be determined before the loop is executed

Given two variables x and y that both refer to floating point values, choose the best way to test the values for equality in Python? a. x equals y b. abs(x - y) < 0.0001 c. x = y d. x >= y and x <= y e. x == y

b. abs(x - y) < 0.0001

Which of the following refers to the imported function in the math module? a. sum b. degrees c. sqrt d. rhombus

b. degrees c. sqrt

According to the documentation for the math module, there is a function which can find the greatest common divisor of two integers: math.gcd(a, b) Return the greatest common divisor of the integers a and b. If either a or b is nonzero, then the value of gcd(a, b) is the largest positive integer that divides both a and b. gcd(0, 0) returns 0. You want to write a program that makes use of this function, the last three lines of the program are shown below. What should the first line be? Select the correct answer below. first = int(input('First number: ')) second = int(input('Second number: ') print(math.gcd(first, second)) a. import gcd b. import math c. from math import gcd d. None of these

b. import math

If I want to rewrite the line 2 to line 5 of the following code snippet in one single line using nested function calls, which of the following syntax is correct? import math value = input("Please enter a positive number") value = float(value) value = mth.sqrt(value) print(value) a. print(input(float(math.sqrt("Please enter a positive number")))) b. print(math.sqrt(float(input("Please enter a positive number")))) c. input("Please enter a positive number").float.math.sqrt.print() d. print(float(math.sqrt(input("Please enter a positive number"))))

b. print(math.sqrt(float(input("Please enter a positive number"))))

What value does the variable n have after executing the following code: n = 13 n + 12 n/5 a. 5 b. 25 c. 13 d. 5.0

c. 13

What is the output of the following code? i = 0 while i <= 20: i += 1 if i % 2 == 1: continue print(i) a. The numbers 1 through 20 get printed b. All odd numbers between 1 and 20 get printed c. All even numbers between 1 and 20 get printed d. The numbers 0 through 20 get printed

c. All even numbers between 1 and 20 get printed

Runtime errors occur when a. The program runs fine but produces incorrect results b. There are typos in the code that cause the program not to compile c. There is an improperly written program that crashes

c. There is an improperly written program that crashes

What is a variable defined inside a function referred to as? a. a volatile variable b. a static variable c. a local variable d. a global variable

c. a local variable

Which of the following is the correct output for the code snippet below? myvar = "insurmountable" print(myvar[11:7:-1]) a. batnu b. lbat c. batn d. ntab

c. batn

Which one of the following is correct for reading and storing an integer value from the User? a. n = int(input('Enter': )) b. n = int(input(Enter: )) c. n = int(input('Enter: ')) d. n = int_input('Enter: '))

c. n = int(input('Enter: '))

All of the following are valid variable names except a. _average_76 b. average_76 c. average76 d. 76average

d. 76average

What happens when more than one conditional is True? a. All of the conditionals that evaluate to True run b. Python will crash c. Only the last conditional that is True will run d. Only the first conditional that is True will run

d. Only the first conditional that is True will run

What is a variable defined outside a function referred to as? a. a volatile variable b. a static variable c. a local variable d. a global variable

d. a global variable

Indicate the data type that will be returned by function c. def c(x,y): #where x and y can both be ints or floats return x>y a. float b. None c. int d. bool e. int or float

d. bool

All of the following are true except: a. There is no limit on the number of elifs you can have b. There does not have to be an else clause c. Each conditional is checked and you only move on to the next one if it evaluates to False d. elif is an abbreviation of "either if"

d. elif is an abbreviation of "either if"

Which of the following expressions evaluate to True? a. 10 != 8 b. 10 == 8 c. 8 <= 10 d. '8' < '10' e. 10 >= 8

e. 10 >= 8 c. 8 <= 10 a. 10 != 8

Consider the following Python code: n = -1 num = int(input('Enter a number: ')) s = num while num != n: if num > s: s = num num = int(input('Enter the next number: ') If the user enters the following sequence of inputs, what value will be stored in the variable s when the program terminates? 5 15 3 -113 46 -1 a. -1 b. 5 c. 3 d. -113 e. 46

e. 46

What data type is the name variable? name = input("Enter your name: ") a. str b. float c. list d. int

str


Set pelajaran terkait

MGT 3013 Chapter 13, 14, 15, & 16 Multiple Choice Questions

View Set

Chapter 23: Pharmacotherapy of Muscle Spasms and Spasticity, Musculoskeletal

View Set

Combo with "Peter the Great" and 1 other

View Set

Personality, Ch. 10 (from Launchpad)

View Set