CHECKPOINT AND EXERCISE OF CHAPTER 2
(CHAPTER NO.2)
Checkpoint
2.1 = Who is a programmer’s customer?
Ans. Any
person, group, or organization that is asking you to write a program.
2.2 = What is
a software requirement?
Ans. A single
function that the program must perform in order to satisfy the customer.
2.3 = What is an algorithm?
Ans. An algorithm is a specific procedure
for solving a well-defined computational problem. The development and analysis
of algorithms is fundamental to all aspects of computer science:
artificial intelligence, databases, graphics, networking, operating systems,
security, and so on.
2.4 = What is pseudocode?
Ans. Pseudocode is a way of expressing an
algorithm without conforming to specific syntax rules
2.5 What is a flowchart?
Ans. A flowchart is a type of diagram that
represents a workflow or process. A flowchart can also be defined as
a diagrammatic representation of an algorithm, a step-by-step approach to
solving a task. The flowchart shows the steps as boxes of various
kinds, and their order by connecting the boxes with arrows.
2.6 What do each of the following symbols mean in a
flowchart?
• Oval
• Parallelogram
• Rectangle
Ans. Oval - terminal (start/end), Parallelogram -
I/O, Rectangle - processing symbols
2.7 Write a statement that displays your name
Ans. print("Awais Bscsf2020m50")
2.8 write a statement that displays the following
text: Python's the best!
Ans. print("Python's the best!")
2.9 Write a statement that displays the following the
following text: The cat said "meow."
Ans.print('The cat said "meow."')
2.10 What is a variable?
Ans. a name that represents a value stored in memory
2.17 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.
Ans. Customer_last_name = input("Please enter
the customer's last name: ")
2.18 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.
Ans. Weekly_sales = float(input("Please enter
the weekly sales: "))
2.22 How do you suppress the print function’s ending
newline?
Ans. 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
2.23 How can you change the character that is
automatically displayed between
multiple items that are passed to the print function?
Ans. you can pass the argument sep= to the print
function, specifying the desired character
2.24 What is the '\n' escape character?
Ans. the backslash "\" is a
special character, also called the "escape" character. It is used in
representing certain whitespace characters: "\t" is a tab,
"\n" is a newline, and "\r" is a carriage return.
2.25 What does the + operator do when it is used with
two strings?
Ans. String
concatenation- useful for breaking up a String literal so a lengthy call to the
print function can span multiple lines
2.28 What are three advantages of using named
constants?
Ans. Make programs more self-explanatory, widespread
changes can be made easier, and they help prevent typographical errors common
to writing magic numbers
2.29 Write a Python statement that defines a named
constant for a 10 percent discount.
Ans. DISCOUNT_RATE = .10
2.30 What is the turtle’s default heading when it
first appears?
Ans. The first step in using Python's turtle graphics
system; it is stored in a file known as the "turtle module;" this
statement loads the turtle module into memory so the Python interpreter can use
it
2.31 How do you move the turtle forward?
Ans. This command moves the turtle forward n pixels
2.32 How would you turn the turtle right by 45
degrees?
Ans. RIGHT 90 or RT 90 (press ENTER)
will turn the turtle to the right 90 degrees. LEFT 1
or L T 1 will turn the turtle to the left I degree. RIGHT
45 or RT 45 will turn the turtle to the right 45
degrees. Try changing the number of degrees after the commands, LEFT
and RIGHT.
2.33 How would you move the turtle to a new location
without drawing a line?
Ans. When you move the cursor (turtle), if
the pen is down, the turtle draws a line from the
current position to the new one. If the pen is up,
the position of the turtle changes to
the new one, but no line is drawn. You can draw a
dot or a circle of a given size, too.
2.34 What command would you use to display the
turtle’s current heading?
Ans. turtle.heading()
2.35 What command would you use to draw a circle with
a radius of 100 pixels?
Ans. turtle.circle(100)
2.36 What command would you use to change the
turtle’s pen size to 8 pixels?
Ans. turtle.pensize(8)
2.37 What command would you use to change the
turtle’s drawing color to blue?
Ans. turtle.pencolor(‘blue’)
2.38 What command would you use to change the
background color of the turtle’s
graphics window to black?
Ans. turtle.bgcolor('black')
2.39 What command would you use to set the size of
the turtle’s graphics window to
500 pixels wide by 200 pixels high?
Ans. turtle.setup(500,200)
2.40 What command would you use to move the turtle to
the location (100, 50)?
Ans. turtle.goto(100,50)
2.41 What command would you use to display the
coordinates of the turtle’s current position?
Ans. turtle.pos()
2.42 Which of the following commands will make the
animation speed faster? turtle.
speed(1) or turtle.speed(10)
Ans. turtle.speed(10)
2.43 What command would you use to disable the
turtle’s animation?
Ans. turtle.speed(0)
2.44 Describe how to draw a shape that is filled with
a color.
Ans. turtle.begin_fill() before shape draws
turtle.end_fill() after she is drawn
2.45 How do you display text in the turtle’s graphics
window?
Ans.
turtle.write()
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
·
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) #
·
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.
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 program’s
execution.
a)
named literal
b) named constant
c)
variable signature
d)
key term
True or False:-
1. Programmers must be careful not to
make syntax errors when writing pseudocode programs.
False
2. in a math expression, multiplication and division take place before
addition and subtraction. True
3. Variable names can have spaces in them. False
4. In Python, the first character of a variable name cannot be a number. True
5. If you print a variable that
has not been assigned a value, the number 0 will be displayed. False
Short Answer
1. What
does a professional programmer usually do first to gain an understanding of a
problem?
Ans.
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.
2. How
does pseudocode differ from actual code written in a programming language?
Ans.
Pseudocode is not an actual programming language. So it cannot be
compiled into an executable program. It uses short terms or simple
English language syntaxes to write code for programs before
it is actually converted into a specific programming language
3. Computer
programs typically perform what three steps?
Ans.
Computer programs typically perform three steps: input is received, some
process is performed on the input, and output is produced.
4. What
rules and considerations should influence the names given to variables in a
program?
Ans.
All variable names must begin with a letter of the alphabet or an.
underscore( _ ). ...
After the
first initial letter, variable names can also contain letters and
numbers. ...
Uppercase
characters are distinct from lowercase characters. ...
You
cannot use a C++ keyword (reserved word) as a variable name
5. What
is the difference between floating-point division and integer division?
Ans.
The division operator / means integer division if there is
an integer on both sides of it. If one or two sides has
a floating point number, then it means floating point division.
The result of integer division is always
an integer. Integer division determines how many times
one integer goes into another.
6. What
is a magic number? Why are magic numbers problematic?
Ans.
A magic number is a numeric literal (for example, 8080 , 2048 ) that
is used in the middle of a block of code without explanation. It is
considered bad practice to use magic numbers because:
A number in isolation can be difficult for other programmers to
understand.
great
ReplyDelete