0% found this document useful (0 votes)
8 views4 pages

Exercise 1

Uploaded by

thdt fhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Exercise 1

Uploaded by

thdt fhdh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1 Introduction to Computer Science and Engineering

GV:Trần Thị Mỹ Tiên

CHAPTER 4: PROGRAMMING
EXERCISE 1: ELEMENTARY PROGRAMMING

1. Suppose that a and b are integers, a = 1, b = 0. What does


the following sequence of statements do? What are values
of variables a, b, t and explain outcome.
t=a
b=t
a=b
2. Which of the following variable names is valid in Python?
a. two_apple
b. 2_apple
c. for
d. ab’c
3. What are the results of the following expressions?
a. 42 / 5
b. 42 // 5
c. 42 % 5
d. 45 + 4 * 4 - 2
e. 4 + 6 / 3 * 2 - 2 + 7 % 3
f. 5 ** 2
g. (b > 15) and (c < 0 or a > 0), where a = 5, b = 10, c = -
6
4. Add parentheses to the following expression so that it
evaluates to 0.
8–3*2–1+1
2 Introduction to Computer Science and Engineering

GV:Trần Thị Mỹ Tiên


5. What are the results of the following program?
1. a = 5
2. b = 8
3. a += b
4. b = a - b
5. a -= b
6. print(a)
7. print(b)

6. What are the results of the following program?

1. n = 1703
2. a = n%10
3. b = (n//10)%10
4. c = (n//100)%10
5. d = (n//1000)%10
6. s = a + b + c + d
7. print(s)
3 Introduction to Computer Science and Engineering

GV:Trần Thị Mỹ Tiên

7. What are the results of the following program?

1. n = 294
2. a = n % 10
3. b = (n // 10) % 10
4. c = (n // 100) % 10
5. m = a * 100 + b * 10 + c
6. print(m)

8. What will be printed after each of the following code


segments?
a. x = "computer science"
print("sci" not in x)
b. x = [ 2.3, "dog", 5] #list
print(5 in x)
c. a = 8 == 8.1
b = 2 >= 5
print( a or b)
d. a = 8 > 6
b=0<5
print( a and b)
9. What value does each of the following expressions evaluate
to?
a. 'Computer' + ' Science'
b. 'Darwin\'s'
c. 'H2O' * 3
d. 'CO2' * 0
4 Introduction to Computer Science and Engineering

GV:Trần Thị Mỹ Tiên


10. Which of the following expressions results in Syntax
Errors?
a. 8 = people
b. ((((4 ** 3))))
c. 6 * -----8
d. 4 += 7 / 2
11. What is wrong in the following code? How do you fix
it?
title = "Chapter "+ 1

You might also like