Docsity

PYTHON CODING UPDATED ACTUAL QUESTIONS WITH CORRECT ANSWERS, Exams of Programming Paradigms

Toggle sidebar
bg1
PYTHON CODING UPDATED ACTUAL QUESTIONS WITH
CORRECT ANSWERS
Question 1.
What Python statement would you like me to run?
ANSWER
(Chapter 1) When Python is running in the interactive mode and displaying the chevron prompt
(>>>) - what question is Python asking you?
Question 2.
20
ANSWER
(Chapter 1) What will the following program print out:
>>> x = 15
>>> x = x + 5
>>> print x
Question 3.
.py
ANSWER
(Chapter 1) Python scripts (files) have names that end with:
Question 4.
for
ANSWER
(Chapter 1) Which of these words is a reserved word in Python ?
Question 5.
quit()
ANSWER
(Chapter 1) What is the proper way to say "good-bye" to Python?
1 / 27
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download PYTHON CODING UPDATED ACTUAL QUESTIONS WITH CORRECT ANSWERS and more Exams Programming Paradigms in PDF only on Docsity!

PYTHON CODING UPDATED ACTUAL QUESTIONS WITH

CORRECT ANSWERS

Question 1.

What Python statement would you like me to run?

ANSWER (Chapter 1) When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?

Question 2.

ANSWER (Chapter 1) What will the following program print out: >>> x = 15 >>> x = x + 5 >>> print x

Question 3.

.py

ANSWER (Chapter 1) Python scripts (files) have names that end with:

Question 4.

for

ANSWER (Chapter 1) Which of these words is a reserved word in Python?

Question 5.

quit()

ANSWER (Chapter 1) What is the proper way to say "good-bye" to Python?

Central Processing Unit

ANSWER (Chapter 1) Which of the parts of a computer actually execute the program instructions?

Question 7.

A sequence of instructions in a programming language

ANSWER (Chapter 1) What is "code" in the context of this course?

Question 8.

Secondary Memory

ANSWER (Chapter 1) A USB memory stick is an example of which of the following components of computer architecture?

Question 9.

The computer did not understand the statement that you entered

ANSWER (Chapter 1) What is the best way to think about a "Syntax Error" while programming?

Question 10.

Random steps

ANSWER (Chapter 1) Which of the following is not one of the programming patterns covered in Chapter 1?

Question 11.

This is a test

ANSWER (Chapter 2) Which of the following is a comment in Python?

ANSWER (Chapter 2) What is the value in x after the following statement executes: x = 1 + 2 * 3 - 8 / 4

Question 19.

ANSWER (Chapter 2) What value be in the variable x when the following statement is executed? x = int(98.6)

Question 20.

Pause the program and read data from the user

ANSWER (Chapter 2) What does the Python raw_input() function do?

Question 21.

Indent the line below the if statement

ANSWER (Chapter 3) What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?

Question 22.

ANSWER (Chapter 3) Which of these operators is not a comparison / logical operator?

Depending on the value of x, either all three of the print statements will execute or none of the statements will execute

ANSWER (Chapter 3)What is true about the following code segment: if x == 5 : print 'Is 5' print 'Is Still 5' print 'Third 5'

Question 24.

You de-indent the next line past the if block to the same level of indent as the original if statement

ANSWER (Chapter 3) When you have multiple lines in an if block, how do you indicate the end of the if block?

Question 25.

You have most likely mixed tabs and spaces in the file

ANSWER (Chapter 3) You look at the following text: if x == 6 : print 'Is 6' print 'Is Still 6' print 'Third 6' It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason?

Question 26.

except

ANSWER (Chapter 3) What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?

Question 27.

Small

i. ii. iii. iv.

ANSWER (Chapter 3) For the following code: astr = 'Hello Bob' istr = 0 try: istr = int(astr) except: istr = - What will the value for istr after this code executes?

Question 31.

A built-in function

ANSWER (Chapter 4) In Python what is the raw_input() feature best described as?

Question 32.

def

ANSWER (Chapter 4) Which Python keyword indicates the start of a function definition?

Question 33.

ANSWER (Chapter 4) What will the following Python code print out? def func(x) : print x func(10) func(20)

Question 34.

Bonjour Michael

i. ii. iii. iv. v.

ANSWER (Chapter 4) What will the following Python program print out? def greet(lang): if lang == 'es': return 'Hola' elif lang == 'fr': return 'Bonjour' else: return 'Hello' print greet('fr'),'Michael'

Question 35.

Avoiding writing the same non-trivial code more than once in your program

ANSWER (Chapter 4) What is the most important benefit of writing your own functions?

Question 36.

x

ANSWER (Chapter 4) In the following Python code, which of the following is an "argument" to a function? x = 'banana' y = max(x) print y print x

i. ii. iii. iv. v. vi. vii. viii. ix. x.

i.

ii. iii. iv. v.

ANSWER (Chapter 4)What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully). def addtwo(a, b): added = a + b return a x = addtwo(2, 7) print x

Question 41.

Hellothere

ANSWER (Chapter 6) What does the following Python Program print out? str1 = "Hello" str2 = 'there' bob = str1 + str print bob

Question 42.

ANSWER (Chapter 6) What does the following Python program print out? x = '40' y = int(x) + 2 print y

i. ii. iii. iv. v. vi.

i. ii. iii. iv.

printx[8]

ANSWER (Chapter 6) How would you use the index operator [] to print out the letter q from the following string? x = 'From [email protected]'

Question 44.

print x[14:17]

ANSWER (Chapter 6) How would you use string slicing [:] to print out 'uct' from the following string? x = 'From [email protected]'

Question 45.

letter

ANSWER (Chapter 6) What is the iteration variable in the following Python code? for letter in 'banana' : print letter

Question 46.

ANSWER (Chapter 6) What does the following Python code print out? print len('banana')*

i. ii. iii.

i. ii. iii. iv.

i. ii. iii.

print len(data)

ANSWER (Chapter 8) Which of the following Python statements would print out the length of a list stored in the variable data?

Question 53.

A list of integers

ANSWER (Chapter 8) What type of data is produced when you call the range() function? x = range(5)

Question 54.

append()

ANSWER (Chapter 8) What list method adds a new item to the end of an existing list?

Question 55.

for / in

ANSWER (Chapter 8) What are the Python keywords used to construct a loop to iterate through a list?

Question 56.

print friends[2]

ANSWER (Chapter 8) For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally' ]

i. ii.

i. ii.

ANSWER (Chapter 8) What does the following Python code print out? a = [1, 2, 3] b = [4, 5, 6] c = a + b print len(c)

Question 58.

Glenn

ANSWER (Chapter 8) What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ] friends.sort() print friends[0]

Question 59.

t[2:4]

ANSWER (Chapter 8) Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15]

Question 60.

Nothing would print - the program fails with a traceback

ANSWER (Chapter 8) What would the following Python code print out? fruit = 'Banana' fruit[0] = 'b' print fruit

i. ii. iii. iv. v.

i. ii. iii. iv. v.

i. ii.

i. ii. iii. iv.

print jane()

Question 64.

h e l l o

ANSWER (Midterm) What will the following Python code print out? i = 10 list1 = ['h','e','l','l','o'] for i in list1: print i

Question 65.

The Main memory

ANSWER (Midterm) Where in the computer is a variable such as "x" stored? x = 123

Question 66.

The program would show an error and a trace-back on the second line

ANSWER (Midterm) What would happen if the following Python code were executed? st = "abc" ix = int(st)

vii.

i. ii. iii. iv. v. vi.

i. ii. iii.

i. ii. iii.

Parenthesis

ANSWER (Midterm) If all of the following were used in a Python expression, which would have the highest precedence (i.e. which would be evaluated first)?

Question 68.

11 Hello

ANSWER (Midterm) What will the following Python program print out? (This is a bit tricky so look carefully). def hello(): print "Hello" print "There" x = 10 x = x + 1 print x, "Hello"

Question 69.

ANSWER (Midterm) You develop the following program in Python: f = int(raw_input("Enter:")) c = ( f - 32 ) * ( 5 / 9 ) print "Celsius",c And when you run it three times you get the following output: Enter: Celsius 0 Enter: Celsius 0 Enter: Celsius 0 What part of the program is causing the output to always be zero?

i. ii. iii. iv. v. vi. vii. viii. ix. x. xi.

print x

Question 73.

Bonjour

ANSWER (Midterm) What is the output of the following program, if the user input is: fr?

define function 'greet'

def greet(lang): if lang == 'es': # if lang is Spanish print 'Hola' return 'adios' elif lang == 'fr': # French print 'Bonjour' return 'au revoir' else: print 'Hello' return 'bye' xx = raw_input('enter language: ') #----main program begins here--- yy= greet(xx)

Question 74.

hello yo Done!

ANSWER (Midterm) What is the output of the following Python code, given the following inputs? hello

hi

yo done

Python code begins here

vi.

i. ii. iii. iv. v. vi. vii. viii. ix. x. xi. xii. xiii. xiv. xv. xvi.

i. ii. iii. iv. v. vi.

while True: line = raw_input('>') if line[0] == '#' : continue if line == 'done': break print line print 'Done!'

Question 75.

The program will run indefinitely

ANSWER (Midterm) What will happen if this code is run? n = 5 while True: print n n = n - 1 print 'Done!'

Question 76.

Done!

ANSWER (Midterm) What is the output of this code? n = 5 while True: if n < 3: break print n n = n - 1 print 'Done!'

vii. viii. ix. x. xi. xii. xiii. xiv.

i. ii. iii. iv. v. vi.

i. ii. iii. iv. v. vi. vii.