Docsity

MA1008 introduction to computational thinking, Quizzes of Computer Communication Systems

Toggle sidebar
bg1
1
MA1008 Introduction to Computational Thinking Quiz 1
Answer all the nine questions in the spaces provided
AY 2023/2024, Semester 2, Week 5
Your Name: _________________________________ Group: _____
1. i. Explain why a microprocessor only uses the digits 0 and 1. (3 marks)
Because transistor switches in a microprocesses have only two states on/off,
or in terms of voltage high/low.
ii. Define the terms bit, byte and word in data storage. (3 marks)
Bit: a single binary digit, either 0 or 1.
Byte: A collection of 8 bits.
Word: A collection of 2 bytes or 16 bits, or 4 bytes or 32 bits. Can
also be 8 bytes or 64 bits.
iii. What is the largest value that can be represented by a 16-bit integer? (3 marks)
The value is largest when all the binary digits are 1, giving 216-1 = 65535.
2. State the result and its data type of each of the following operations. (3 marks each)
Award the result 2 marks and the data type 1 mark.
i. "False" and ("True" or False) True, Boolean
ii. math.cos(90) == math.sin(0) (assume that the math library has been imported)
False, Boolean
iii. 2**3**2 / -2**2**3 -2.0, float
iv. -24 % 7 // 4 1 [-24%7=( -24-7*(-24//7)=(-24-7*(-4)) = 4)], integer
3. The variable F carries a floating-point value. Write Python statements to print the sum of the first
two digits after the decimal point. For example, if F = 12.345, it prints 7 (= 3 + 4). (10 marks)
I = int(F)
Dec = F – I # Get decimal part of F
D1 = int(Dec*10) # Get first digit of the decimal part
Dec = Dec*10 – D1 # Get the decimal after the first digit
D2 = int(Dec*10) # Get the second decimal digit
print(D1 + D2) # print the sum of the two digits
pf3

Partial preview of the text

Download MA1008 introduction to computational thinking and more Quizzes Computer Communication Systems in PDF only on Docsity!

MA1008 Introduction to Computational Thinking Quiz 1

Answer all the nine questions in the spaces provided

AY 202 3 /202 4 , Semester 2 , Week 5

Your Name: _________________________________ Group: _____

  1. i. Explain why a microprocessor only uses the digits 0 and 1. (3 marks) Because transistor switches in a microprocesses have only two states on/off, or in terms of voltage high/low. ii. Define the terms bit, byte and word in data storage. (3 marks) Bit: a single binary digit, either 0 or 1. Byte: A collection of 8 bits. Word: A collection of 2 bytes or 16 bits, or 4 bytes or 32 bits. Can also be 8 bytes or 64 bits. iii. What is the largest value that can be represented by a 16-bit integer? (3 marks) The value is largest when all the binary digits are 1, giving 216 - 1 = 65535.
  2. State the result and its data type of each of the following operations. ( 3 marks each) Award the result 2 marks and the data type 1 mark. i. "False" and ("True" or False) True, Boolean ii. math.cos( 9 0) == math.sin(0) (assume that the math library has been imported) False, Boolean iii. 232 / - 223 - 2.0, float iv. - 24 % 7 // 4 1 [-24%7=( - 24 - 7(-24//7)=(- 24 - 7(-4)) = 4)], integer
  3. The variable F carries a floating-point value. Write Python statements to print the sum of the first two digits after the decimal point. For example, if F = 12.345, it prints 7 (= 3 + 4). (10 marks) I = int(F) Dec = F – I # Get decimal part of F D1 = int(Dec1 0 ) # Get first digit of the decimal part Dec = Dec10 – D1 # Get the decimal after the first digit D2 = int(Dec*10) # Get the second decimal digit print(D1 + D2) # print the sum of the two digits
  1. The following program repeatedly reads in two numbers and prints their sum. The program continues when the sum is greater than zero and ends otherwise. But it is not well written and contains errors. Rewrite the program with improvements and without errors. ( 12 marks) for i in range(1, 10000 00 ) while True: n1 = intput("1st no.:") n1 = int(input("1st no.: ")) n2 = intput("2nd no.:") n2 = int(input("2nd no.: ")) sum = n1 + n2 sum = n1 + n print("n1 + n2 =", sum) print(n1, "+", n2, "=", sum) if sum > 0 if sum <= 0: continue break else end

  2. Given a = "a", b = "bcd", p = 1 and q = 0, write down the values of the following expressions. If you think that a statement is erroneous, provide the correct statement first, followed by the value. ( 3 marks each) i. a > b and p False ii. a < b < "yz" True iii. a <= b and p <= q False

iv. a = b or p = q and not q a == b or p == q and not q, False

  1. In the imperial units of length, 1 mile = 1760 yards. Also 1 mile = 1.60934 kilometers (km). If the variable KM carries a length in km, write Python statements to convert it to miles and yards. For example, 12.3 0 km = 7 miles and 1131.4 8 yards. (10 marks) Miles = KM / 1. int_Miles = int(Miles) yards = (Miles – int_Miles) * 1760 print(KM, "km =", int_Miles, "miles and", yards, "yards. ")
  2. i. A robot’s position at (x, y) in the x-y plane is computed according to the direction and distance by which it moves from a given position. The robot starts its movements from ( 0 .0, 0.0), its home position, and travels to numerous other positions (hence (x, y) goes through numerous computations), and finally returns to its home position. Write a single Boolean expression that returns True if the robot’s position (x, y) is at the home position, and False otherwise. Bear in mind that coordinates are floating point values, and you may assume that floating point representation is exact only up to 8 decimal places. ( 6 marks) Define eps = 1e- 8 abs(x) < eps and abs(y) < eps Can also be (-eps < x < eps) and (-eps < y < eps) ii. Write one single Boolean expression that returns True if the integer carried by variable A is contained in the integer carried by variable B, and False otherwise. For example, if A is 12 and B is 112 3 , it should return True. If B is 1 323 , it should return False. ( 6 marks) str(A) in str(B)