You are on page 1of 2

LASTNAME, FIRSTNAME: DELA CRUZ, EMILY JOYCE E.

ES 26: Quiz 2 (30 pts.)


April 21, 2018

Objective
1. What are the three programming control structures? (3 pts) Answer: sequential, conditional and iterative

2. Allows the execution of statements as long as the condition resolves to True. Answer: while loop

3. What are the two things that can happen when the condition inside a loop never changes? (2 pts)

Answer: It will continue until the loop reaches the stopping value stated in the condition of the loop, or it can go on until the memory in the
computer runs out, or you exit the program.

4. What happens when the body of the while loop does not change the value of the variable in the loop condition?

Answer: The computer will keep on executing the statements inside the while loop until computer memory runs out, or the user exits the
program.

5. Explain what random.randint(a,b) does. (2 pts)

Answer: What random.randint(a,b) do is that it will make the computer pick a random integer from the range a-b.

6. How do you remove ‘c’ from t = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]? Answer: t.pop(2) or del(t(c)) or t.remove(‘c’)

7. What is the index of the fifth character of a string? Answer: 4

8. How do you remove the element ‘c’ from a list t assuming it exists? Answer: t.remove(‘c’)

9. True or False. A function must always return a value. Answer: False

10. True or False. A list can have elements of any type. Answer: True

11. True or False. A local variable exists only while the function where it is defined is running. Answer: True

12. What does continue do?

Answer: It makes the program do nothing and just continue executing the loop that encloses the ‘continue’ statement.

13. What does return do?

Answer: It returns the value of a variable from where it was called, or returns the value that results from the statements in the function.

14. Enumerate the four advantages of using functions. (4 pts.)

Answer: It makes debugging easier. It also makes a piece of code reusable throughout the program without having to write the entire piece of
code over and over again. It enhances the readability of the code because it reduces the complexity of the program. Lastly, the job can be
divided, and have multiple people working on different functions at the same time.

15. When is it best to use a for loop over a while loop?

Answer: When you want to repeat a code a number of times while relating it to a certain variable, and without having to write a “counter” to
count the times it already repeated the code like in while loop.

Code Analysis
1. OUTPUT HERE: 2. OUTPUT HERE:
def hello(): def foo(blanks):
print(“hello”) >hello for columns in range(1,blanks+1): >**
def area(width, height): >welcome freddie print g(columns) >****
return width * height
>area=20 def g(columns): >******
def print_welcome(name):
print(“welcome”, name) > return "*" * (columns*2) >
hello() > >
foo(3)
print_welcome(“freddie”)
w=4 >
h=5 >
print “area =”,area(w,h)
3. OUTPUT HERE: 4. OUTPUT HERE:
string = "Nagpur" int_str = “-1 3 2 4 5 -11 0 22”
string[0]='K' >Error int_str_list = int_str.split() >111
print(string) > int_list = [] >
string = "Kanpur" for x in int_str_list:
> >
print string[1:] int_list.append(int(x))
> s = str(min(int_list)) >
print s[1:]+str(int_str[12])

You might also like