Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
133 views

Ch2 Python

The document contains 40 multiple choice questions about Python programming concepts such as variables, data types, operators, string formatting, and turtle graphics. It tests knowledge of basic syntax, built-in functions like print and input, and how to define variables, do math operations, concatenate strings, and control turtle movement. The questions cover a range of foundational topics to assess understanding of core Python programming principles.

Uploaded by

Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Ch2 Python

The document contains 40 multiple choice questions about Python programming concepts such as variables, data types, operators, string formatting, and turtle graphics. It tests knowledge of basic syntax, built-in functions like print and input, and how to define variables, do math operations, concatenate strings, and control turtle movement. The questions cover a range of foundational topics to assess understanding of core Python programming principles.

Uploaded by

Mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

SIMAD UNIVERSITY ‫جــامــعـــة ســـيــمــد‬

JAAMACADDA SIMAD

October 10, 2021


Name:

Faculty:

Program:

Class:

ID No. :

Phone No. :

Subject : Pearson Starting Out with Python 5rd Tony Gaddis (2021)

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 1
Q1) Who is a programmer’s customer?
October 10, 2021
A1) The person, group, or organization that is asking you to write a program.

Q2) What is a software requirement?

A2) A single function that the program must perform in order to satisfy the customer.

Q3) What is an algorithm?

A3) A set of well-defined logical steps that must be taken to perform a task.

Q4) What is pseudocode?

A4) Fake code that you translate into high-level language. OR

An informal language that has no syntax rules and is not meant to be compiled or executed. Instead,
programmers use pseudocode to create models, or “mock-ups,” of programs.

Q5) What is a flowchart?

A5) A diagram that graphically depicts the steps that take place in a program.

Q6) What do each of the following symbols mean in a flowchart?

• Oval

• Parallelogram

• Rectangle

A6) Oval - terminal (start/stop) Parallelogram - input/output Rectangle - processing

Q7) Write a statement that displays your name.

A7) print("Mohamed")

Q8) Write a statement that displays the following text: Python's the best!

A8) print("Python's the best!")

Q9) Write a statement that displays the following text: The cat said "meow."

A9) print('The cat said "meow."')

Q10) What is a variable?

A10) A name that represents a value stored in the computer's memory.

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 2
Q11) Which of the following are illegal variable names in Python, and why?

99bottles

july2009

theSalesFigureForFiscalYear
October 10, 2021
r&d

grade_repor

A11) 99bottles is illegal because it begins with a number. r&d is illegal because the & character is not
allowed.

Q12) Is the variable name Sales the same as sales? Why or why not?

A12) No because Python variable names are case sensitive

Q13) Is the following assignment statement valid or invalid? If it is invalid, why? 72 = amount

A13) Invalid, because var names cannot start with a number OR

It is invalid because the variable that is receiving the assignment (in this case
amount ) must appear on the left side of the = operator.

Q14) What will the following code display?

val = 99

print('The value is', 'val')

A14) The value is val.

Q15) Look at the following assignment statements:

value1 = 99

value2 = 45.9

value3 = 7.0

value4 = 7

value5 = 'abc'

After these statements execute, what is the Python data type of the values referenced by each
variable

A15) value1 will reference an int . value2 will reference a float .

value3 will reference a float . value4 will reference an int . value5 will reference an str (string).

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 3
Q16) What will be displayed by the following program?

my_value = 99 my_value = 0 print(my_value)

A16) 0

Q17) You need the user of a program to enter a customer’s last name. Write a statement that prompts
the user to enter this data and assigns the input to a variable.
October 10, 2021
A17) last_name = input("Enter the customer's last name: ")

Q18) You need the user of a program to enter the amount of sales for the week. Write a statement
that prompts the user to enter this data and assigns the input to a variable.

A18) sales = float(input('Enter the sales for the week: '))

Q19) Complete the following table by writing the value of each expression in the Value column:

Expression Value

6+3*5 ______

12 / 2 – 4 ______

9 + 14 * 2 − 6 ______

(6 + 2) * 3 ______

14 / (11 − 4) ______

9 + 12 * (8 − 3) ______

A19)

Q20) What value will be assigned to result after the following statement executes? result = 9 // 2

A20) 4

Q21) What value will be assigned to result after the following statement executes? result = 9 % 2

A21) 1

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 4
Q22) What is string concatenation?

A22) String concatenation is the appending of one string to the end of another.

Q23) After the follow statement executes, what value will be assigned to the result variable?

result = '1' + '2

A23) '12'
October 10, 2021
Q24) After the follow statement executes, what value will be assigned to the result variable?

result = 'h' 'e' 'l' 'l' 'o'

A24) 'hello'

Q25) How do you suppress the print function’s ending newline?

A25) If you do not want the print function to start a new line of output when it finishes displaying its
output, you can pass the special argument end = ' ' to the function.

Q26) How can you change the character that is automatically displayed between multiple items that
are passed to the print function?

A26) You can pass the argument sep= to the print function, specifying the desired character.

Q27) What is the '\n' escape character?

A27) It is the newline escape character.

Q28) What will the following code display? name = 'Karlie' print('Hello {name}')

A28) Hello {name}

Q29) What will the following code display? name = 'Karlie' print(f'Hello {name}')

A29) Hello Karlie

Q30) What will the following code display? value = 99 print(f'The value is {value + 1}')

A30) The value is 100

Q31) What will the following code display ?value = 65.4321 print(f'The value is {value:.2f}')

A31) The value is 65.43

Q32) What will the following code display? value = 987654.129 print(f'The value is {value:,.2f}')

A32) The value is 987,654.13

Q33) What will the following code display? value = 9876543210 print(f'The value is {value:,d}')

A33) The value is 9,876,543,210

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 5
Q34) In the following statement, what is the purpose of the number 10 in the format specifier?

print(f'{name:10}')

A34) It is a field width designator. It indicates that the value should be displayed in a field that is a
minimum of 10 spaces wide.

Q35) In the following statement, what is the purpose of the number 15 in the format specifier
October 10, 2021
print(f'{number:15,d}')

A35) It is a field width designator. It indicates that the value should be displayed in a field that is a
minimum of 15 spaces wide

Q36) In the following statement, what is the purpose of the number 8 in the format specifier?

print(f'{number:8,.2f}')

A36) It is a field width designator. It indicates that the value should be displayed in a field that is a
minimum of 8 spaces wide.

Q37) In the following statement, what is the purpose of the < character in the format specifier?

print(f'{number:<12d}')

A37) It is an alignment specifier. It specifies that the value should be left aligned.

Q38) In the following statement, what is the purpose of the > character in the format specifier?

print(f'{number:>12d}')

A38) It is an alignment specifier. It specifies that the value should be right aligned.

Q39) In the following statement, what is the purpose of the ^ character in the format specifier?

print(f'{number:^12d}')

A39) It is an alignment specifier. It specifies that the value should be center aligned

Q40) What are three advantages of using named constants?

A40) (1) Named constants make programs more self-explanatory, (2) widespread changes can easily be
made to the program, and (3) they help to prevent the typographical errors that are common when
using magic numbers.

Q41) Write a Python statement that defines a named constant for a 10 percent discount.

A41) DISCOUNT_PERCENTAGE = 0.1

Q42) What is the turtle’s default heading when it first appears?

A42) 0 degrees

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 6
Q43) How do you move the turtle forward?

A43) With the turtle.forward command.

Q44) How would you turn the turtle right by 45 degrees?

A44) With the command turtle.right(45)

Q45) How would you move the turtle to a new location without drawing a line?
October 10, 2021
A45) You would first use the turtle.penup() command to raise the turtle’s pen.

Q46) What command would you use to display the turtle’s current heading?

A46) turtle.heading()

Q47) What command would you use to draw a circle with a radius of 100 pixels?

A47) turtle.circle(100)

Q48) What command would you use to change the turtle’s pen size to 8 pixels?

A48) turtle.pensize(8)

Q49) What command would you use to change the turtle’s drawing color to blue?

A49) turtle.pencolor('blue')

Q50) What command would you use to change the background color of the turtle’s graphics window
to black?

A50) turtle.bgcolor('black')

Q51) What command would you use to set the size of the turtle’s graphics window to 500 pixels wide
by 200 pixels high?

A51) turtle.setup(500, 200)

Q52) What command would you use to move the turtle to the location (100, 50)?

A52) turtle.goto(100, 50)

Q53) What command would you use to display the coordinates of the turtle’s current position?

A53) turtle.pos()

Q54) Which of the following commands will make the animation speed faster? turtle. speed(1) or
turtle.speed(10)

A54) turtle.speed(10)

Q55) What command would you use to disable the turtle’s animation?

A55) turtle.speed(0)

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 7
Q56) Describe how to draw a shape that is filled with a color.

A56) To fill a shape with a color, you use the turtle.begin_fill() command before drawing the shape, then
you use the turtle.end_fill() command after the shape is drawn. When the turtle.end_fill() command
executes, the shape will be filled with the current fill color

Q57) How do you display text in the turtle’s graphics window?

A57) With the turtle.write()commandOctober 10, 2021

Q58) Write a turtle graphics statement that displays a dialog box that gets a number from the user.
The text Enter a Value should appear in the dialog box’s title bar. The dialog box should display the
prompt What is the radius of the circle?. The value that the user enters into the dialog box should be
assigned to a variable named radius.

A58) radius = turtle.numinput('Enter a Value',

'What is the radius of the circle?')

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 8
Review Questions
Multiple Choice

1. A __________ error does not prevent the program from running, but causes it to produce incorrect
results.

a. syntax b. hardware c. logic d. fatal


October 10, 2021
2. A __________ is a single function that the program must perform in order to satisfy the customer.

a. task b. software requirement c. prerequisite d. predicate

3. A(n) __________ is a set of well-defined logical steps that must be taken to perform a task.

a. logarithm b. plan of action c. logic schedule d. algorithm

4. An informal language that has no syntax rules and is not meant to be compiled or executed is called
__________.

a. faux code b. pseudocode c. Python d. a flowchart

5. A __________ is a diagram that graphically depicts the steps that take place in a program.

a. flowchart b. step chart c. code graph d. program graph

6. A __________ is a sequence of characters.

a. char sequence b. character collection c. string d. text block

7. A __________ is a name that references a value in the computer’s memory.

a. variable b. register c. RAM slot d. byte

8. A __________ is any hypothetical person using a program and providing input for it.

a. designer b. user c. guinea pig d. test subject

9. A string literal in Python must be enclosed in __________.

a. parentheses. b. single-quotes c. double-quotes. d. either single-quotes or double-quotes.

10. Short notes placed in different parts of a program explaining how those parts of the program work
are called __________.

a. comments b. reference manuals c. tutorials d. external documentation

11. A(n) __________ makes a variable reference a value in the computer’s memory.

a. variable declaration b. assignment statement c. math expression d. string literal

12. This symbol marks the beginning of a comment in Python.

a. & b. * c. ** d. #

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 9
13. Which of the following statements will cause an error?

a. x = 17 b. 17 = x c. x = 99999 d. x = '17'

14. In the expression 12 + 7, the values on the right and left of the + symbol are called __________.

a. operands b. operators c. arguments d. math expressions

15. This operator performs integer division.


October 10, 2021
a. // b. % c. ** d. /

16. This is an operator that raises a number to a power.

a. % b. * c. ** d. /

17. This operator performs division, but instead of returning the quotient it returns the remainder.

a. % b. * c. ** d. /

18. Suppose the following statement is in a program: price = 99.0. After this statement executes, the
price variable will reference a value of which data type?

a. int b. float c. currency d. str

19. Which built-in function can be used to read input that has been typed on the keyboard?

a. input() b. get_input() c. read_input() d. keyboard()

20. Which built-in function can be used to convert an int value to a float?

a. int_to_float() b. float() c. convert() d. int()

21. A magic number is ________________.

a. a number that is mathematically undefined

b. an unexplained value that appears in a program’s code

c. a number that cannot be divided by 1

d. a number that causes computers to crash

22. A __________ is a name that represents a value that does not change during the pro- gram’s
execution.

a. named literal b. named constant c. variable signature d. key term

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 10
True or False

False 1. Programmers must be careful not to make syntax errors when writing pseudocode programs.

True 2. In a math expression, multiplication and division take place before addition and subtraction.

False 3. Variable names can have spaces in them.

True 4. In Python, the first character of a variable name cannot be a number.

False 5. If you print a variable that has not been assigned a value, the number 0 will be displayed.

Short Answer

Q1) What does a professional programmer usually do first to gain an understanding of a problem?

A1) Programmers will conduct an initial interview with their customer to understand what exactly the
customer wants out of the program and how it should function. The programmer can also conduct
follow-up interviews if they come up with additional questions.

Q2) What is pseudocode?

A2) Pseudocode is an informal language that has no syntax rules, and is not meant to be compiled or
executed.

Q3) Computer programs typically perform what three steps?

A3) 1. Receive Input 2. Perform process on input 3. Produce output

Q4) If a math expression adds a float to an int, what will the data type of the result be?

A4) If a math expression adds a float to and int, the data type result will be a float.

Q5) What is the difference between floating-point division and integer division

A5) The difference in floating-point division and integer division is that in floating-point division, the /
operator gives the result as a floating-point value, and in integer division, the // operator gives the result
as an integer.

Q6) What is a magic number? Why are magic numbers problematic?

A6) A magic number is an unexplained value that appears in a program's code. Magic numbers can be

problematic, for a number of reasons. First, it can be difficult for someone reading the code to

determine the purpose of the number. Second, if the magic number is used in multiple places in the

program, it can take painstaking effort to change the number in each location, should the need arise.

Third, you take the risk of making a typographical mistake each time you type the magic number in

the program's code

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 11
Q7) Assume a program uses the named constant PI to represent the value 3.14159. The program uses
the named constant in several statements. What is the advantage of using the named constant
instead of the actual value 3.14159 in each statement?

A7) The named constant makes the program more self-explanatory. In a math statement, it is evident

that PI represents the value of pi. Another advantage to using the named constant is that widespread

changes can easily be made to the program. Let's say the value of pi appears in several different

statements throughout the program. If you need to change the number of decimal places of precision

used with the number, the initialization value in the declaration of the named constant is the only

value that needs to be modified. For example, to use only two decimal places of precision, the

declaration can be changed to: PI = 3.14 The new value of 3.14 will then be used in each statement

that includes the PI constant. Another advantage to using the named constant is that it helps to

prevent the typographical errors that are common when using magic numbers. For example, if you

accidentally type 31.4159 instead of 3.14159 in a math statement, the program will calculate the

wrong value. However, if you misspell PI, the Python interpreter will display a message indicating that

the name is not defined.

October 10, 2021


Prepared by: mohammedkhaliffarah@gmail.com 12

You might also like