2022-2023 Spring - Ceng240 20230516 Midterm Exam Answers
2022-2023 Spring - Ceng240 20230516 Midterm Exam Answers
QUESTION 1
Page 2 of 52
QUESTION 2
QUESTION 3
Page 3 of 52
QUESTION 4
Page 4 of 52
QUESTION 5
Page 5 of 52
QUESTION 6
Page 6 of 52
QUESTION 7
Page 7 of 52
QUESTION 8
Page 8 of 52
QUESTION 9
QUESTION 10
Page 9 of 52
QUESTION 11
Page 10 of 52
QUESTION 12
Page 11 of 52
QUESTION 13
Page 12 of 52
QUESTION 14
Page 13 of 52
QUESTION 15
Page 14 of 52
QUESTION 16
Page 15 of 52
QUESTION 17
Page 16 of 52
QUESTION 18
Page 17 of 52
QUESTION 19
QUESTION 20
Page 18 of 52
SOLUTIONS
Page 19 of 52
QUESTION 1
QUESTION 2
Explanation:
The formula for the area of a circle is πr^2, where r is the radius. Since the diameter (d) is
provided as the argument, we need to divide it by 2 to obtain the radius (r). Therefore, (d/2)
represents the radius, and (d/2)**2 calculates the square of the radius. Multiplying this by π
(pi) gives us the area of the circle.
Final answer
The correct answer is option 2 the answer is B def f(d): (d/2)**2 * pi.
Page 21 of 52
QUESTION 3
II. Inheritance: It is another key concept in OOP that allows classes to inherit properties and
behavior from other classes. Inheritance promotes code reusability and establishes hierarchical
relationships between classes, with derived classes inheriting characteristics from their parent
classes.
III. Responsiveness: Responsiveness is not specifically a property of object-oriented
programming. It refers to the ability of a system or program to respond to user input or
external events in a timely manner. While OOP can facilitate the design and implementation of
responsive systems, it is not a direct property of the programming paradigm itself.
Explanation:
Therefore, the correct properties of object-oriented programming from the given options are I
(Encapsulation), II (Inheritance), and V (Polymorphism). The correct answer is I-II-V.
Final answer
The correct answer is option 2 the answer is B I-II-V.
QUESTION 4
The Memory Manager is responsible for allocating and managing the memory resources of a
computer system. It handles the organization and tracking of available memory, as well as
fulfilling memory requests from processes or programs.
In the given statement, it describes a scenario where a running process requires additional
memory space. This can happen when a process, such as a photo editing program, needs to open a
Page 23 of 52
new image file that requires memory for storage. In such cases, the process makes a demand to
the Memory Manager, requesting additional memory space.
The Memory Manager then evaluates the request and determines whether it can grant the
requested memory or deny it based on the available memory resources. If there is enough free
memory, the Memory Manager allocates the required space to the process, allowing it to
proceed with its operation. However, if the available memory is insufficient or the system has
reached its memory limits, the Memory Manager may deny the request, and the process may
experience issues or errors.
In summary, the Memory Manager plays a crucial role in managing the allocation and utilization
of memory resources in an operating system. It handles requests for additional memory space
from processes and decides whether to grant or deny those requests based on memory
availability.
Final answer
The answer is option 4 the answer is D. Memory manager.
Page 24 of 52
QUESTION 5
Explanation:
Page 25 of 52
I. coco[“miguel”][1][0]
In this line, we access the dictionary with the key "miguel" and retrieve the value associated
with it. The value is a tuple: (0, (0, [0]), 0, 0). Next, we access the element at index 1 of the
tuple (0, [0]).
Finally, we access the element at index 0 of that tuple, 0. Therefore, this line returns 0.
II. coco[“hector”][1][1][0]
In this line, we access the dictionary with the key "hector" and retrieve the value associated
with it. The value is a list: [[0], [[0]], [(0, 0)], 0]. Next, we access the element at index 1 of the
list, [[0]]. Within this (specific) sub-list, the item located at position 1 is equivalent to the index
[0].
Finally, we access the element at index 0 of that sublist, which is 0. Therefore, this line returns
0.
III. coco[“hector”][2][0][0]
In this line, we again access the dictionary with the key "hector" and retrieve the value
associated with it. The value is the same list as in option II: [[0], [[0]], [(0, 0)], 0]. Next, we
access the element at index 2 of the list, which is [(0, 0)]. Within that sublist, we access the
element at index 0, which is (0, 0).
Finally, we access the element at index 0 of that tuple, which is 0. Therefore, this line also
returns 0.
IV. coco["ernesto"][1][0]
In this line, we access the dictionary with the key "ernesto" and retrieve the value associated
with it. The value is a list: [(0, 0), [0], (0,)]. Next, we access the element at index 1 of the list,
which is [0].
Finally, we access the element at index 0 of that sublist, which is 0. Therefore, this line returns
0.
Final answer
The answer is option 3 the answer is C. I, III, and IV.
Page 26 of 52
QUESTION 6
Explanation:
Which are correct for the given function definitions?
1. "g(x) returns the string representation of the integer x."
This statement is incorrect. The function g(x) does not return the string representation
of the integer x. Instead, it returns the digit sum of x. The function f(x) converts x into
a list of its individual digits using list comprehension, and then g(x) calculates the sum of
those digits.
Final answer
According to the explanation given for the second question, the answer is option 4 the correct
answer is D. ii and iii.
Page 28 of 52
QUESTION 7
In optıon 2, str(5) + "5" -> "10" because str(5) is strong and we are concatenating (joining two
string values} it with string "5," so this would result in 55 and not 10.
Here it is two string values getting concatenated and not two integer values.
Explanation:
Here, we will go through all the other options
1. list(" ") == [] -> True: The expression list(" ") creates a list with a single element, which is
the string " ". On the other hand, [] represents an empty list. Since list(" ") and [] are not the
same, the comparison list(" ") == [] evaluates to False, not True. Therefore, this claim is false.
2. str(5) + "5" -> "10": This claim is false. str(5) is strong, and we are concatenating (joining
two string values} it with string "5", so this would result in 55 and not 10.
3. True != False -> True: This claim is true. The inequality operator != checks if the two
operands are not equal; in this case, True and False are not equal, so the expression True !=
False evaluates to True.
4. len([]) -> 0: This claim is true. The len() function is used to determine the length of a
sequence, such as a list. In this case, len([]) returns 0 because the empty list [] has no elements.
5. str(5) == "5" -> True: This claim is true. The expression str(5) converts the integer 5 to
the string "5". When comparing "5" with "5", they are equal, so the expression str(5) == "5"
evaluates to True.
Final answer
The correct answer to this question is option 1 and option 2.
Page 29 of 52
QUESTION 8
Option I:
1 i=0
2 while i < len(my_str):
3 if my_str[i] == " ":
4 break
5 print(ch)
6 i += 1
Page 30 of 52
This code has several issues. It refers to ch instead of my_str[i] inside the loop, which will
result in a NameError. Additionally, the code does not correctly print the characters of my_str
that are not spaces. Therefore, this option does not give the same output as the original code.
Option II:
1 print ("H")
2 print ("e")
3 print("l/nl")
4 print("o")
This code prints the specified strings "H," "e," "l," "l," and "o." It does not iterate over the
characters of my_str or consider spaces. Therefore, this option does not give the same output
as the original code.
Option III:
1 for i in enum(len(my_str)):
2 if my_str[i] != " ":
3 print(my_str[i])
4 else:
5 break
This code attempts to use enum instead of "enumerate" to iterate over the indices of my_str.
The correct syntax is "enumerate". Additionally, the code does not correctly print the
characters of my_str that are not spaces. Therefore, this option does not give the same output
as the original code.
Explanation:
In conclusion, none of the provided options give the same output as the original code segment.
Option 1:
Page 31 of 52
Option 2:
Option 3:
Final answer
None of the provided options give the same output as the original code segment.
Here are the outputs of the given options:
The answer is option 4 the correct answer is D. "None of the above."
Page 32 of 52
QUESTION 9
QUESTION 10
Page 33 of 52
QUESTION 11
Page 34 of 52
QUESTION 12
• The output (3, 1, 2) represents the updated values of a, b, and c after the swapping
operation.
Therefore, the correct interpretation is that the code swaps the values of a, b, and c using the
"metu" technique, resulting in option E) "Swaps the values of a←c, b←a, c←b, and prints if
a, b, c are distinct."
Explanation:
The remaining options are not correct:
A) Swaps the values of a←b, b←c, c←a, and prints if a, b, c are not distinct. This option
is incorrect because the code does not perform the swapping operation this way. Instead, it
uses the "metu" technique, which involves a specific sequence of operations to swap the values.
B) Swaps the values of a←b, b←c, c←a, and prints if a, b, c are distinct. This option is
incorrect because the code does not check for a, b, and c distinctness after the swapping
operation. It simply performs the swapping and returns the updated values.
C) Swaps the values of a←c, b←a, c←b, and prints if a, b, c are not distinct. This option
is incorrect because it describes the correct swapping operations but does not mention the
distinctness condition. The code does not check for a, b, and c distinctness.
D) None of the other choices. This option is incorrect because option e) provides the correct
interpretation of the code, which involves swapping the values using the "metu" technique and
printing the result.
Therefore, option E) "Swaps the values of a←c, b←a, c←b and prints if a, b, c are distinct," is
the correct interpretation of the code, while the remaining options are incorrect.
Summary:
The given code utilizes the "metu" technique to swap the values of the variables a, b, and c. It
performs the swapping using a specific sequence of arithmetic operations. After the swapping,
it returns the updated values of a, b, and c as a tuple.
Code:
1 def metu(a,b,c):
2 a=a+b+c
3 b=a-b-c
4 c=a-b-c
5 a=a-b-c
6 return(a,b,c)
7 a=int(input())
Page 36 of 52
8 b=int(input())
9 c=int(input())
10
11 if(a!=b and b!=c and a!=c):
12 print(metu(a,b,c))
Output:
In output, you can clearly see that "a value" is replaced with "c value," "b value" is replaced with
"a value, " and "c value" is replaced with "b value. "
So, option e is correct.
Final answer
The answer is option 5 the correct answer is E. "Swaps the values of a←c, b←a, c←b, and
prints if a, b, c are distinct."
QUESTION 13
Page 37 of 52
Explanation:
• The return statement should be outside the while loop, at the same level of indentation
as the while statement. This ensures that the loop iterates through all characters before
returning the count.
• Line 9 should be indented inside the while loop. It increments the value of i and allows the
loop to continue.
• Lines 10, 11, and 12 are not necessary and should be removed.
So, the correct option is B) Remove Line 10.
Final answer
Hence, the answer is option 2 the correct answer is B. Remove Line 10.
Page 38 of 52
QUESTION 14
1 def get_year(self):
2 return self.year
3
Page 39 of 52
1 class watch:
2 name = "The Godfather"
3 number_awards = 2
4 year = 1972
5 directed_by = "Francis Ford Coppola"
6 summary = "About the aging patriarch of an organized crime dynasty"
7 stars = ["Marlon Brando", "Al Pacino", "James Caan"]
8
9 def get_year(self):
10 return self.year
11
12 Gfather = watch()
13 print(Gfather.get_year())
14
When you run this code, it creates an instance of the watch class called Gfather. Then, it calls
the get_year() method on the Gfather object, which returns the value of the year attribute of
the watch class. In this case, the output will be:
"1972"
Explanation:
1. A class called watch is defined. It represents a movie and contains several attributes, such
as name, number_awards, year, directed_by, summary, and stars. These attributes hold
information about the movie "The Godfather"
2. Inside the watch class, a method named get_year is defined. This method takes the self
parameter, which represents the instance of the class. It returns the value of the year
attribute of the class.
3. An instance of the watch class is created and assigned to the variable Gfather. This
instance represents the movie "The Godfather".
4. The get_year method is called on the Gfather object using dot notation
(Gfather.get_year()). This invokes the get_year method defined within the watch class for
the Gfather instance.
Page 40 of 52
5. The get_year method retrieves the value of the year attribute, which is 1972.
6. The print statement outputs the value returned by Gfather.get_year(), which is 1972.
The get_year method allows you to retrieve the value of the year attribute from the watch
class instance, enabling you to obtain the desired output of "1972".
Final answer
Hence, the answer is option 2 the correct answer is B. def get_year(self):
return self.year
Page 41 of 52
QUESTION 15
Page 42 of 52
1 def email_checker(email):
2 a = email.find('@') + 1
3 if a == 0:
4 return 0
5 elif email[:a] == '':
6 return 0
7 elif email[:a].find('.') != -1:
8 return 0
9 elif email[a:] == '':
10 return 0
11 b = email[a:].find('.')
12 if b == -1:
13 return 0
14 else:
15 b += a + 1
16 if email[b:] == '':
17 return 0
18 c = email[b:].find('.')
19 if c == -1:
20 return 1
21 else:
22 c += b + 1
23 if email[c:] == '':
24 return 0
25 c = email[b:].find('.')
26 if c == -1:
27 return 1
28 else:
29 c += b + 1
30 if email[c:] == '':
31 return 0
32 d = email[c:].find('.')
33 if d == -1:
34 return 1
Page 43 of 52
35 else:
36 d += c + 1
37 if email[d:] == '':
38 return 1
39 elif email[d:].find('.') == -1:
40 return 0
41 return 1
42
43
44 print(email_checker('[email protected]'))
45 print(email_checker('[email protected]'))
46 print(email_checker('[email protected]'))
47 print(email_checker('ceng240@40ceng'))
Output:
0
1
1
0
Note: The other options can also be checked by putting the values in the above code one by one.
1. email.find('@') + 1 searches for the position of '@' in the email address. Adding 1 is
necessary because the specification requires the user name to be written before '@'. If
the '@' symbol is not found, it means the email is invalid, so 0 is returned.
2. email[:a] == '' checks if the user name (substring from the start to position 'a') is
empty. If it is empty, the email is invalid as the user name should not be blank, so 0 is
returned.
4. email[a:] == '' checks if the domain name (substring after the '@' symbol) is empty. If
it is empty, the email is invalid as the domain name should not be blank, so 0 is returned.
Page 44 of 52
5. email[a:].find('.') == -1 checks if there is at least one dot in the domain name. The
specification requires the domain name to have at least one dot. If no dot is found, it
means the email is invalid, so 0 is returned.
6. email[b:] == '' checks if there is any content after the first dot in the domain name. If
it is empty, the email is invalid, so 0 is returned.
8. email[c:] == '' checks if there is any content after the second dot in the domain name.
If it is empty, the email is invalid, so 0 is returned.
10. email[d:] == '' checks if there is any content after the third dot in the domain name. If
it is empty, the email is valid, so 1 is returned.
11. email[d:].find('.') == -1 checks if there is any dot after the third dot in the domain
name. If another dot is found, it means the email is invalid, so 0 is returned.
Explanation:
1. (a) email, 0, 0, ==: This option is suitable because we need to find the position of '@' in
the email address, and comparing it with 0 using '==' checks if the '@' symbol exists or
not.
2. (b) str, 0, -1, ==: This option is not suitable because we need to check for an empty
string ('') as the condition, not -1.
3. (c) email, 1, 1, !=: This option is suitable because we are checking if there is any dot
('.') in the user name using the condition '!= -1'.
4. (d) str, 1, 0, ==: Using option (d) with the data type 'str' is not suitable because it
causes errors when accessing and manipulating the domain name portion of the email
address in the code. The correct option is (a) with the data type 'email' to ensure proper
functionality.
Page 45 of 52
5. (e) email, 0, -1, !=: This option is not suitable because we are searching for a dot ('.') in
the domain name, not comparing it with -1.
Final answer
Therefore, options (A) and (C) are suitable in the code, but when option (e) is placed, then it
will show output, but it won't provide the correct output.
Function one:
def one(a):
return a**2
This function squares the input value, which matches the expected output.
Function "one(a)" returns the square of the input value, so it correctly returns [-1, 1, 4, 9] for
the input [-1, 1, 2, 3].
Explanation:
1. Function: one(a)
o This function returns the square of the input value.
o Example: one(-1) = (-1)**2 = 1
Page 48 of 52
Function five:
def five(a):
return a**a
This function raises the input value to the power of itself, which also matches the expected
output.
Function "five(a)" returns the value raised to the power of itself, so it correctly returns [-1, 1,
4, 27] for the input [-1, 1, 2, 3].
Explanation:
1. Function: five(a)
o This function returns the value raised to the power of itself.
o Example: five(3) = 3**3 = 27
Function six:
def six(a):
return int(a**a)
This function raises the input value to the power of itself and returns the integer value, which
aligns with the expected output.
Function "six(a)" returns the integer value of the value raised to the power of itself, so it
correctly returns [-1, 1, 4, 27] for the input [-1, 1, 2, 3].
Explanation:
1. Function: six(a)
o This function returns the integer value of the input raised to the power of itself.
o Example: six(3) = int(3**3) = 27
The function that provides the expected output of [-1, 1, 4, 27] for the given input [-1, 1, 2, 3]
is:
✓ Function one(a): return a**2
Final answer
The answer is option 5; the correct answer is E. one, five, six.
Page 49 of 52
QUESTION 17
Explanation:
Let's evaluate the expression step by step:
Final answer
The answer is option 1; the correct answer is A. 40.
Page 50 of 52
QUESTION 18
Page 51 of 52
QUESTION 19
Explanation:
1. `1st[4:9]`: This would return `[1, 2, 5, 3, 4]`. It includes elements from index 4 to index
8 (excluding index 9).
3. `1st[4:11]`: This expression goes beyond the length of the list. It would return `[1, 2, 5,
3, 4, 10, 0, 8]`. It includes elements from index 4 to index 10 (excluding index 11).
4. `1st[3:10]`: This would return `[11, 1, 2, 5, 3, 4, 10]`. It includes elements from index
3 to index 9 (excluding index 10).
5. `1st[5:11]`: This would return `[2, 5, 3, 4, 10, 0]`. It includes elements from index 5 to
index 10 (excluding index 11).
Final answer
The answer is option 2; the correct answer is B. 1st[4:10]
Page 52 of 52
QUESTION 20
list_name[start_index:end_index] = new_value
The start_index and end_index parameters specify the range of elements in the list that will
be replaced with the new value. The start_index is inclusive, and the end_index is exclusive.
In the problem statement, the list lst is assigned the value of the list right, starting at index 3
and ending at index 5. This means that the elements at indexes 3, 4, and 5 in the list lst will be
replaced with the elements in the list right. The other options for the list assignment operator
will not change the value of the list lst to the desired value.
Explanation:
lst = [10, 0, 4, 11, 2, 5, 9, 3, 1, 6, 7, 8]
right = [100]
lst[3:5] = right
print(lst)
[10, 0, 4, 100, 5, 9, 3, 1, 6, 7, 8]
As you can see, the value of the list lst has been changed to the desired value.
Final Answer
The answer is option 5; the correct answer is E. 1st[3:5] = right.
[10, 0, 4, 100, 5, 9, 3, 1, 6, 7, 8]
1st[ 3:5] = right