Final Exam Review CS 1101 Programming Fundamentals

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

Which of the following is an invalid Python assignment statement?

'3' = 3

When defining a Python function that has no parameters, the parentheses that follow the function's name are optional.

'False

What output will the following Python 3 statement produce? >>> print (1,000,000)

1 0 0

Boolean expressions control _________________

???

Consider the following Python program. fin = open('words.txt') for line in fin: word = line.strip() print(word) What is line?

???

What does function subroutine do? def subroutine( n ): while n > 0: print (n,) n -= 1

???

Assume that d is a Python dictionary. What does the following Python code produce? result = dict() for key in d: val = d[key] if val not in result: result[val] = [key] else: result[val].append(key)

?????c. a list of tuples Incorrect

If you use a Python dictionary in a for statement, it traverses the _____ of the dictionary.

????c. keys and values Incorrect

Assume that d is a Python dictionary. What does the following Python code produce? result = dict() for key in d: val = d[key] if val not in inverse: result[val] = [key] else: result[val].append(key)

???a. a histogram Incorrect

What is the output of the following Python 3 statements? x=1 y=2 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)

1 is less than 2

What output will the following Python statements produce? >>> n = 17 >>> print (n)

17

What does the following Python 3 function do? def subroutine(n): while n > 0: print (n,) n -= 1

Counts from n down to 1 and displays each number

: To create a new object that has the same value as an existing object is know as creating an alias.

False

Functions can only return boolean expressions.

False

Handling an exception with a try statement is called throwing an exception.

False

In Python, the '+' operator can be used with numbers and with strings. What is a property that number addition has, but string concatenation does not?

The expression value does not depend on the order of numeric addition operands.

The % or modulus operator returns the remainder from dividing two numbers.

True

Consider the following text from a Python interpreter. >>> print(2 + 2) 4 What is the text "4" called?

a value

What output will the following code produce? n = 10 while n != 1: print (n,end=' ') if n % 2 == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1

a. 10 5 16 8 4 2 Correct

Consider the following text from a Python interpreter. >>> print(2 + 2) 4 What is the text "+" called?

an operator

What do we call the value provided to a function when the function is called (which is assigned to the corresponding parameter in the function)?

argument

Which one of the following Python expressions has the value 64?

b. 8 ** 2 Correct

A development approach that that is intended to avoid a lot of debugging by only adding and testing small amounts of code at a time is called.

b. incremental development

The statements inside of a Python loop are known as the ____ of the loop.

body

Programmers generally choose names for their variables that are meaningful and document what the variable is used for.

'True'.

What is the output of the following Python statements? def recurse(a): if (a == 1): print(a) else: recurse(a) recurse(1)

1

What output will the following python commands produce: x=1 y=2 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)

1 is less than 2

What output will the following Python commands produce? >>> percentage = float ( 60 * 100) / 55 >>> print (percentage)

109.0909090909091

What output will the following Python program produce? def area(l, w): temp = l * w return temp l = 4.0 w = 3.25 x = area(l, w) if ( x ): print (x)

13.0

What output will the following code produce? mylist = [ [2,4,1], [1,2,3], [2,3,5] ] a=0 b=0 total = 0 while a <= 2: while b < 2: total += mylist[a][b] b += 1 a += 1 b = 0 print (total)

14

Which one of the following Python expressions generates a syntax error?

2 += 2

What is the output of the following Python 3 statements? x=2 y=1 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)

2 is greater than 1

What is the output of the following Python program? mylist = [ [2,4,1], [1,2,3], [2,3,5] ] total = 0 for sublist in mylist: total += sum(sublist) print(total)

23

Given the following code, what will the output be? import string index = "Ability is a poor man's wealth".find("w") print(index)

24

What is the output of the following Python program? pi = int(3.14159) print (pi)

3

What is the output of the following statements? pi = int(3.14159) print (pi)

3

What is the value of the variable index after the Python code below is executed? word = 'bAnana' index = word.find('a')

3

What is the output of the following Python program? pi = float(3.14159) print (pi)

3.14159

What is the output of the following statements? pi = float(3.14159) print (pi)

3.14159

What output will the following Python statement produce? >>> print (2 * (3-1))

4

What output will the following Python program produce? n = 10000 count = 0 while n: count = count + 1 n = n / 10 n=int(n) print(count)

5

What output will the following Python 3 program produce? x = 5 if x % 2 == 0: print (x) else: print (x, x%2)

5 1

What output will the following interactive Python statements produce? >>> n = 2 >>> n += 5 >>> n

7

What output will the following Python statement produce? >>> print ((1+1)**(5-2))

8

Which one of the following Python expressions generates a syntax error?

8 += 2

In Python, the expression "a**(b**c)" is the same as "(a**b)**c".

False

Repeated execution of a set of programming statements is called repetitive execution. Select one:

False

The Python 3 program below prints the number 3. s = "Python3" print(s[len(s)])

False

d. RuntimeError: maximum recursion depth exceeded Correct

False Correct

Match concepts with their definition! Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are this kind of languages. Answer 1 Correct Any one of the languages that people speak that evolved naturally. Answer 2 Correct An error that does not occur until the program has started to execute but that prevents the program from continuing. Answer 3 Correct An error in a program that makes it do something other than what the programmer intended. Answer 4 Correct The meaning of a program. Answer 5 Correct The structure of a program. Answer 6 Correct An error in a program that makes it impossible to parse — and therefore impossible to interpret. Answer 7 Correct One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.

The correct answer is: Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are this kind of languages. → formal language, Any one of the languages that people speak that evolved naturally. → natural language, An error that does not occur until the program has started to execute but that prevents the program from continuing. → runtime error, An error in a program that makes it do something other than what the programmer intended. → semantic error, The meaning of a program. → semantics, The structure of a program. → syntax, An error in a program that makes it impossible to parse — and therefore impossible to interpret. → syntax error, One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language. → token

Match the following terms and definitions The order in which statements are executed during a program run. Answer 1 Correct The first part of a compound statement begins with a keyword and ends with a colon ( : ) Answer 2 Correct A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses. Answer 3 Correct A variable defined inside a function that can only be used inside its function. Answer 4 Correct A graphical representation of functions, their variables, and the values to which they refer. Answer 5 Correct

The correct answer is: The order in which statements are executed during a program run. → flow of execution, The first part of a compound statement begins with a keyword and ends with a colon ( : ) → header, A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses. → function call, A variable defined inside a function that can only be used inside its function. → local variable, A graphical representation of functions, their variables, and the values to which they refer. → stack diagram

Match concepts with their definition! To join two operands end-to-end. Answer 1 Correct What the Python interpreter does to an expression to find its value. Answer 2 Correct A combination of variables, operators, and values that represents a single result value. Answer 3 Correct A reserved word that is used by the interpreter to parse programs. Answer 4 Correct A special symbol that represents a simple computation like addition, multiplication, or string concatenation. Answer 5 Correct A unit of code that the Python interpreter can execute. Answer 6 Correct A name that refers to a value.

The correct answer is: To join two operands end-to-end. → concatenate, What the Python interpreter does to an expression to find its value. → evaluate, A combination of variables, operators, and values that represents a single result value. → expression, A reserved word that is used by the interpreter to parse programs. → keyword, A special symbol that represents a simple computation like addition, multiplication, or string concatenation. → operator, A unit of code that the Python interpreter can execute. → statement, A name that refers to a value. → variable

Python functions may or may not take arguments and may or may not return a result.

True

The Python line below causes "5 dollars" to be printed. print('%d %s' % (5, 'dollars'))

True

The operands of the logical operators should be boolean expressions, but Python is not very strict. Any nonzero number is interpreted as True.

True

True or False: A local variable is a variable defined inside a function that can only be used inside its function.

True

When a Python function is called, inside the function, the arguments are assigned to variables called parameters.

True

With built in functions, it is generally acceptable to take a "Leap of Faith".

True

When a Python function is called, inside the function, the arguments are assigned to variables called parameters.

True Correct

Expressions evaluate to either True or False. What will be the output of the following Python 3 program? if "Ni!": print ('We are the Knights who say, "Ni!"') else: print ("Stop it! No more of this!")

We are the Knights who say, "Ni!"

Consider the following Python program. fin = open('words.txt') for line in fin: word = line.strip() print(word) What is fin?

a. A file object

A loop where the terminating condition is never achieved is called an _______

a. infinite loop Correct

What is the output of the Python code below? my_list = [3, 2, 1] print(my_list.sort())

c. None Correct

Assume that d is a Python dictionary. What does the following Python code produce? d.items()

c. a list of tuples Correct

What is the output of the following Python statements? def recurse(a): if (a == 0): print(a) else: recurse(a) recurse(1)

d. RuntimeError: maximum recursion depth exceeded Correct

If you assign the result a void function to a variable in Python, you get:

d. the special value None Correct

What is the output of the following Python program? mylist = ["now", "four", "is", "score", "the", "and seven", "time", "years", "for"] print(" ".join(mylist[1::2]))

four score and seven years

Will the Python code below print something? And will it terminate? def print_n(s, n): if n > 0: print(s) print_n(s, n-1) return n n = 3 while print_n("hi", n): print_n("there!", n) n = 0

yes and yes


Conjuntos de estudio relacionados

Meta-ethics: The language of Ethics

View Set

Exam 1 Chapter 3-4 Test Prep Questions

View Set