Cs Midterm 2021
Cs Midterm 2021
Choose most correct and general answer. اختر أصح اإلجابات و أعمهاCopy answers to bubble sheet.
_______________________________________________________________________________________
For questions 1 to 5, assume we have this bit pattern in memory: 1111-1110-1111-1011
1- In hexadecimal, this number is ….= هذا الرقم61 بنظام الa. FEFA b. 177373 c. BF7F d. F7FB e. FEFB
2- In signed two's complement integer format, this number is ….بنظام مكمل االثنين هذه الرقم هو
a. -261 b. -260 c. 65275 d. -32507 e. 261
3- Assume the four possible notations for representing signed integers (+ve and –ve), the above bit
pattern would represent a +ve number in: فإن هذا الرقم يعتبر رقما موجبا وفق، باعتبار طرق تمثيل األرقام السالبة
a. Sign & magnitude b. 1's complement c. 2's complement d. excess notation e. It can't be +ve
4- This pattern represents letter الin Arabic in …… يمثل هذا الرقم الثنائى حرف ال العربى بـ
a. UTF-8 b. UTF-16 big endian c. 2's Complement d. ASCII e. UTF-16 little endian
5- If this pattern is the Unicode for an Arabic letter, how many bytes does it need to be represented in
UTF-8? ؟8 – كم بايت نحتاجها لتمثيل الحرف المقابل لهذا الرقم الثنائى لو افترضنا أنه يمثل يونيكود بصيغة يو تى اف
a. 1 byte b. 2 bytes c. 3 bytes d. 4 bytes e. 5 bytes
_______________________________________________________________________________________
6- Given this bit pattern in IEEE 754 representation: 1-10000001-11110000000000000000000, what is
the corresponding decimal number? بت؟23 فى457 ما الرقم العشرى المقابل لهذا الرقم الممثل بصيغة اى تريبل ايي
a. -15 b. -1.875 c. -3.75 d. -7.75 e. +7.75
7- One Zettabyte is equivalent to about ……….. Petabyte. . بيتا بايت....... الزيتا بايت تعادل حوالى
a. 1,000,000,000 b. 1,000,000 c. 1,000 d. 100 e. 20
8- What is the maximum +ve integer that can be stored in 16 bits using two's complement format?
a. +32,767 b. +32,768 c. -32,768 d. +65,536 e. +4294967295
9- Which of the following numbers causes an overflow error if stored in 8 bits in 2's complement format?
a. -100 b. -128 c. -127 d. +127 e. None
10- What problem happens if we try to save -17.4 in IEEE754 format بصورة64.7- ما المشكلة التى تحدث لو خزنا
a. Overflow error b. Syntax error c. Logical error d. Truncation error e. Runtime error
11- If we store -1.125 in the bits floating representation of the book (1 bit for sign, 3 bits for exponent in
excess 4 and 4 bits for mantissa), what is the resulting bit pattern? بت؟8 لو خزناه فى6.635- ما شكل الكسر
a. 1-101-1001 b. 1-001-1001 c. 0-100-0010 d. 1-100-0010 e.1-111-0001
12- How is 4 – 6 executed in 2's complement format in 4 bits? بت؟7 بصيغة مكمل االثنين فى1-7 كيف يتم حساب
a. 0100 + 1001 = 1101 b. 0100 – 0110 = -0010 c. 0100 + 1010 = 1110 d. 0100 + 0110 = 0010
1
Question 2 (0.5 x 12 questions)
13. Which of the following is the fastest correct algorithm for finding all the factors of a given number?
One is included in the factors but the number itself is not included.
#(a) #(c)
n = int(input("Enter +ve int: ")) n = int(nput("Enter +ve int: "))
factors = [] factors = []
for i in range(1, n): for i in range(1, n // 2 + 1):
if n % i == 0: if n % i == 0:
factors.append(i) factors.append(i)
print(factors) print(factors)
#(b) #(d)
from math import sqrt n = int(input("Enter +ve int: "))
n = int(input("Enter +ve int: ")) factors = []
factors = [] for i in range(1, n, 2):
for i in range(1, int(sqrt(n))): if n % i == 0:
if n % i == 0: factors.append(i)
factors.append(i) print(factors)
print(factors)
from random import randint
14. What is the output of this program? def fun (numbers):
(a) [1, 2, 3] for i in range (4, randint(1, 10)):
(b) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] numbers.append(i)
(c) [1, 2, 3, 4]
(d) I do not know until I run it numbers = [1,2,3]
fun(numbers)
print (numbers)
15. Which of these is a worng call for
function add_ID? def add_ID (n, nums = []):
nums.append(n)
(a) add_ID ()
return nums
(b) add_ID (20210435)
(c) add_ID (20210089, []) IDs = [20210123]
(d) add_ID (nums = IDs, n = 121) num = input("Enter +ve int: ") #1
if num.isdigit(): #2
16. Which of the following is the correct tracing sum = 0 #3
of this algorithm if the input is 789? for digit in num: #4
sum += int(digit) #5
(a) (b) print(sum) #6
# num sum digit # num sum digit
1 '789' - - 1 '789' 0 0 (c) (d)
2 '789' - - 2 '789' 0 0 # num sum digit # num sum digit
3 '789' 0 - 3 '789' 0 0 1 789 - - 1 789 - -
4 '789' 0 '7' 4 '789' 0 '9' 2 7 - - 2 True - -
5 '789' 7 '7' 5 '789' 9 '9' 3 7 0 - 3 789 0 -
4 '789' 7 '8' 4 '789' 9 '8' 4 7 0 '7' 4 789 0 -
5 '789' 15 '8' 5 '789' 17 '8' 5 7 7 '7' 5 789 24 -
4 '789' 15 '9' 4 '789' 17 '7' 6 7 7 '7' 6 789 24 7-
5 '789' 24 '9' 5 '789' 24 '7' Output: 24 Output: 24
6 '789' 24 - 6 '789' 24 '7
Output: 24 Output: 24
2
17. Which of these flowcharts will produce this output if input is 6? Output: 720
(a) (b) (c) (d)
18. What will this program print? Note: insert(index, value) is a list funtion that inserts a value
in a list at specific inex. pop(index) is a list funtion that deletes the value at index from the list.
(a) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list = [1,2,3,4,5,6,7,8,9,10]
(b) [2, 1, 4, 3, 6, 5, 8, 7, 10, 9] for i in range (0, len(list) - 1, 2):
(c) [1, 1, 3, 3, 5, 5, 7, 7, 9, 9] list.insert(i, list[i + 1])
list.pop (i + 2)
(d) [2, 2, 4, 4, 6, 6, 8, 8, 10, 10] print(list)
(e) [1, 3, 5, 7, 9]
19. Your friend Kirolos Usama Adeeb is trying to write a function that takes a list and returns another list
with the unque items in the original list. He wrot this code but could not complete it. His friend
Adham Wael Mansour wrote these possible codes to complete the function. Which one is correct?
Example: If you send to the function [3, 4, 4, 3, 5, 3, 2, 5] it returns [3, 4, 5, 2].
3
20. Liu Hui [刘徽] was a Chinese mathematician who invented this formula for calculating PI as shown
below. Ahmed Murad Abdelfatah, our colleague in CS111, wrote some algorithms in pseudocode to
implement Liu Hui formula. Unfortunately not all of them work. Find, which version is NOT correct
or ambiguous. (All other versions are correct)
(a) (d)
initial = 1 initial = 3
for i = 1 to 8 : for i = 1 to 7 :
initial = 2 + sqrt (initial) initial = 2 + sqrt (initial)
PI = 768 * sqrt (2 - sqrt(initial)) PI = 768 * sqrt (2 - sqrt(initial))
print PI print PI
(b)#This style is not recommended
PI=768 * sqrt(2 - sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+sqrt(2+1)))))))))
print PI
(c)
initial = 1
first_part = calculate sqrt(2 + initial) 7 times
PI = 768 * sqrt (2 - first_part)
print PI
21. Which of the of the following loops in pseudocode is not equivalent to the other ones.
(a) (c)
i = 0 i = 0
sum = 0 sum =
while (i <= 100) do
sum += i sum += i
i += 1 i += 1
print sum while (i <= 100)
print sum
(b) (d)
sum = 0 sum = 0
for i = 1 to 100 for i = 1 to 100
sum += i sum += i
print sum i += 1
print sum
22. Which of the following pieces of code follow Python PEP-8 style guide correctly?
(a) (c)
total_salary = int(input('Enter Salary:')) total_salary = int(input('Enter Salary:'))
tax = total_salary * 0.2 + 15 tax = total_salary*0.2+15
net_salary = total_salary - tax net_salary = total_salary-tax
(b) (d)
totalsalary = int(input('Enter Salary:')) TOTAL_salary=int(input('Enter Salary:'))
TAX = totalsalary * 0.2 + 15 tax=TOTAL_salary * 0.2 + 15
netsalary = totalsalary – TAX net_salary=TOTAL_salary - tax
4
23. Your colleague Mona Ashraf Mohamed drew a
flowchart to read n items in an array & sort them
using bubble sort. She asked you to trace them
using n = 6 and data = [5, 6, 3, 2, 8, 1] to see if it
works fine. Which of these is a correct trace of the
algorithm, assuming that we show data array only
after each iteraiton of the outer loop?
(a) Original: 5 6 3 2 8 1
After 1st iteration: 1 5 6 3 2 8
After 2nd iteration: 1 2 5 6 3 8
After 3rd iteration: 1 2 3 5 6 8
After 4th iteration: 1 2 3 5 6 8
After 5th iteration: 1 2 3 5 6 8
(b) Original: 5 6 3 2 8 1
After 1st iteration: 5 3 2 6 1 8
After 2nd iteration: 3 2 5 1 6 8
After 3rd iteration: 2 3 1 5 6 8
After 4th iteration: 2 1 3 5 6 8
After 5th iteration: 1 2 3 5 6 8
(c) Original: 5 6 3 2 8 1
After 1st iteration: 5 6 3 2 1 8
After 2nd iteration: 5 3 2 1 6 8
After 3rd iteration: 3 2 1 5 6 8
After 4th iteration: 2 1 3 5 6 8
After 5th iteration: 1 2 3 5 6 8
_______________________________________________________________________________________
إيمان حسنى. دمحم الرملى و د. د، مع أطيب أمنياتنا و خالص دعواتنا بالتوفيق
5