Java
Everything is attached in a file
SECTION A [30 Marks]
This section is in two parts. Part 1 will test your knowledge on python language structure and Part 2 will be test of your general knowledge in python.
PART 1: Multiple Choice Question. [20 marks]
You are required to choose from the list of alternatives provided, an answer that you think is most accurate.
[Each question carries 1 mark]
1. An identifier identifies a variable. Is Python case sensitive when dealing with identifiers?
a. yes
b. no
c. machine dependent
d. none of the mentioned
2. What is the maximum possible length of an identifier?
a. 31 characters
b. 63 characters
c. 79 characters
d. none of the mentioned
3. Which of the following is invalid?
a. _a = 1
b. __a = 1
c. __str__ = 1
d. none of the mentioned
4. Which of the following is an invalid variable?
a. my_string_1
b. 1st_string
c. foo
d. _
5. Which one of these is a floor division?
a. /
b. //
c. %
d. None of the mentioned
.
6. What is answer to this expression, 22 % 3 is?
a. 7
b. 1
c. 0
d. 5
7. Which of the following results in a SyntaxError?
a. ‘”Once upon a time…”, she said.’
b. “He said, ‘Yes!’”
c. ‘3\’
d. “’That’s okay”’
8. Select all options that print: hello-how-are-you
a. print(‘hello’, ‘how’, ‘are’, ‘you’)
b. print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)
c. print(‘hello-‘ + ‘how-are-you’)
d. print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)
9. The output of the code shown below is:
s='{0}, {1}, and {2}’
s.format(‘hello’, ‘good’, ‘morning’)
a. ‘hello good and morning’
b. ‘hello, good, morning’
c. ‘hello, good, and morning’
d. Error
10. What is the output when the following code is executed ?
>>>str1=”helloworld”
>>>str1[::-1]
a. dlrowolleh
b. hello
c. world
d. helloworld
11. What is the output of this: print(‘ab\ncd\nef’.splitlines())
a. [‘ab’, ‘cd’, ‘ef’].
b. [‘ab\n’, ‘cd\n’, ‘ef\n’].
c. [‘ab\n’, ‘cd\n’, ‘ef’].
d. [‘ab’, ‘cd’, ‘ef\n’].
12. What is the output of this: print(‘Ab!2’.swapcase())
a. AB!@
b. ab12
c. aB!2
d. aB1@
13. What is the output of this: print(‘ab cd ef’.title())
a. Ab cd ef
b. Ab cd eF
c. Ab Cd Ef
d. None of the mentioned
14. Which of the following commands will create a list?
a. list1 = list()
b. list1 = []
c. list1 = list([1, 2, 3])
d. all of the mentioned
15. What is the output when we execute list(“hello”)?
a. [‘h’, ‘e’, ‘l’, ‘l’, ‘o’].
b. [‘hello’].
c. [‘llo’].
d. [‘olleh’].
16. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a. 5
b. 4
c. None
d. Error
17. Suppose list1 is [1, 5, 9], what is sum(list1) ?
a. 1
b. 9
c. 15
d. Error
18. What is the output of the following code?
>>> a = (1, 2, 3, 4)
>>> del( a[2] )
a. Now, a = 1, 2, 4)
b. Now, a = (1, 3, 4)
c. Now, a = (3, 4)
d. Error as tuple is immutable
19. What is the data type of (1)?
a. Tuple
b. Integer
c. List
d. Both tuple and integer
20. If a = (1, 2, 3, 4), a[1:-1] is
a. Error, tuple slicing doesn’t exist
b. [2, 3]
c. (2, 3, 4)
d. (2, 3)
PART 2: General Knowledge on Python. [10 marks]
Provide answers to the questions below in your answer booklet. You are advised to write clearly and concisely (briefly).
1. Mention 2 key features of python.
2. When will you need to use a tuple data structure rather than a list data structure?
3. Design a data structure to store the class score and exam score of students (using their index numbers) in each course. The students read only 3 courses; English (ENGL 101), Maths (MATH 101) and Science (SCI 101).
[Hint: Use a combination of dictionaries and, either list or tuples—whichever one you decide].
A programmer has been wrestling with the sample codes below. Something should be definitely wrong with them. Find the errors and correct them.
4. Checking for palindrome[footnoteRef:1] words: Returns true if palindrome, else not palindrome. [1: Palindrome word: A word that is spelt the same way from either side. Example; madam and racecar.]
word = ‘madam’
rword = reversed(word)
print(word == rword)
Output: False
5. Decisioning:
score = 49
if (score < 50);
print (‘Student has trailed’)
For the questions below, state whether the logical expressions will evaluate to
True
or
False.
6. statement = ‘’
bool(statement)
_____________
7. x= -3
x > 0 and x < 1
_____________
SECTION B [30 Marks]
In this section, you are required to write a program that solves the problems stated. [Answer only one question]
You are reminded that, marks will be awarded for good programming ethics, and nice organization of code. A well-organized thinking style should be an advantage to you.
Q1)
Energy is essential. It is expensive to convert it into a useful form. Yet, we too easily waste it. Take a stroll one night around the university campus, and you will note quite a lot; Many bulbs and sockets are switched on, when no one is using them. This is an obvious energy waste.
Well, there is a way of solving this problem. We can employ someone to go around the whole campus and turn off idle sockets and bulbs in classes and offices where there is no one. Better still, we can print out stickers to remind a tired worker, storming out of his/her office to turn off idle gadgets because they are not in use. Or in our days of advanced technology, use a computer to do that.
Now, imagine that, you are a seasoned programmer, and well established. The University has therefore, contracted your service, to write a python program that will run on this computer to automatically turn off lights left on in both
offices
and
lecture halls
.
[NOTE: Assume that, there are 10 lecture halls (Lt1 – Lt10) in the school. And 15 offices (Of1 – Of15)]
a) Develop an algorithm that solves the problem.
b) Write a python script that implements your algorithm.
Q2)
In industrial fabrication of plastics, waste is produced. This waste, contain poisonous compounds. Before a safe disposal, into the environment, it must first be treated, and the poisonous compounds, removed from the waste. The poisonous compound has been found to naturally decompose after some days, after which, the waste can be safely disposed into the environment. A computer has been dedicated to automatically discharge the waste to the environment, through an outlet valve when it has found the waste to be harmless after some days.
After some years, the computer developed an irreparable fault, that needed to be replaced. As a seasoned Environmental Engineer, you’ve been employed to develop a program that can run on this new computer to do just that.
a) Develop an algorithm that solves the problem.
b) Implement your algorithm in python.
Q3)
You have been employed as a control system engineer at the Ghana Water Company Limited (GWCL). A reservoir of known volume stores the treated water pumped from a clear well before distribution. The reservoir uses a liquid level regulator to control the volume of water to a maximum level to prevent tank overflow.
After several years, the liquid level regulator developed an irreparable that needed to be replaced. As a seasoned Civil Engineer, you’ve been instructed to apply your knowledge in programming to fix this problem.
a) Develop an algorithm that solves the problem.
b) Implement your algorithm in python.
Q4)
As Chief Technical Officer (CTO) of an oil company, you have developed centralised monitoring software which in connection with a smart sensor will allow the pressure, flow and level of oil to be reported remotely from the field without the need for on-site crew and reduce risk. Upon receiving this information, rig crews can monitor and adjust settings as needed.
Apply your knowledge in python programming to plot the pressure drop vs flow rate of the oil in a straight pipe.
NB: Assumptions may be applied in choosing values or better still, variables can be used to represent any range of values.