Visual Programming 1: - Exam Preparation: With MCQS, Pracs, Questions and Solutions
Visual Programming 1: - Exam Preparation: With MCQS, Pracs, Questions and Solutions
INF1511
Semester 1
MCQs and Pracs
1
MCQ 1
INF
2
6. Consider the following statement: 1 Python will throw an error, as
x=2.5 x is a float variable and %d is
the format code for integer
print(“The value of x is
%d” %x) 2 The value of x is 2.5
11. Which is the correct output for the 1 HiHow are you?
following print statement: 2 Hi, How are you?
print('Hi, \ 3 Hi How are you?
How are you?') 4 Hi
How are you?
3
12. The output of the following statement 1 Syntax error
is: 2 Today
print(‘‘‘Today is a
is a public holiday.
public holiday.’’’)
3 Today is a public holiday.
4 Today is a
public holiday
4
18. Escape sequences in Python begin 1 #
with a ….. character 2 //
3 \
4 None of the above
5
PYTHON PRACTICE
Command-line:
IDLE:
2. Create a Python program that asks the input of students’ marks from the user and displays the
message ‘Sorry, you don’t qualify for a distinction’ for marks below
6
75 and ‘Well done, you have received a distinction!’ for marks higher than
75. Provide the code that you used. (4)
# ifelse.py
m= int(input("Enter marks: "))
if(m<75):
print("Sorry, you don't qualify for a distinction")
else:
print("Well done, you have received a distinction!")
3. Write a program in Python that computes the area of a triangle by accepting the base and
height from the user. Provide the code that you used. (3)
area= b*h/2
4. Write a program that generates a random number between 5 and 10(both included) and
then print a pattern as shown below. If the random number generated is 5, the output will
be: (6)
54321
4321
3 2 1
2 1
A sample run:
The random number generated is 6
654321
54321
4321
3 2 1
2 1
1
7
#pattern
#create a random number between 5 and 10
8
MCQ 2
9
7. What is the output of: 1 6
lst = ['Africa'] 2 5
print(len(lst)) 3 1
4 None of the above
12. Which of the options will output the 1 print('I don\'t know')
string I don’t know? 2 print("I don\'t know")
10
13. The statement 1 9
print(len("Good" + "Night")) 2 10
11
19. The output of print(“abc” * 3) 1 abc*3
is ..… 2 invalid operation
3 abcabcabc
4 None of the above
12
PYTHON PRACTICE P2
1. Write a program that accepts two strings from the user and prints the combination and the
concatenation thereof. Provide the code that you used. (4)
A sample run:
p.53
# stringjoin.py
p= input("Enter a string: ")
q= input("Enter another string: ")
print ("The combination is ", p.join(q))
print ("The concatenation is ", p+q)
2. Create a program that stores the days of the week in a list and prompts the user to enter a
numerical value for the day of the week and then displays the day in text form. Provide the
code that you used. (4)
Example:
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
p.65
# listdays.py
13
days= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday']
n= int(input("Enter a value between 1 and 7: "))
if 1 <= n <= 7:
print ("The day is", days[n-1])
else:
print ("Value is out of the range")
3. Write a recursive function to compute the sum of numbers from 1 to n. Provide the code that
you used. (3)
# recursion.py
def oneToN(n):
if n == 1:
return 1
else:
return n + oneToN(n-1)
4. The following code shows a normal function definition. Rewrite the function as a Lambda
function: (2)
def func(x):
return x ** 4
print(func(7))
g= lambda x: x ** 4
print(g(7))
5. Through the use of modules, write a program that gives you this year’s calendar and the
current date and time as output. Provide the code that you used. (2)
# caltime
import calendar
import time
calendar.prcal(2016)
print(time.ctime())
14
MCQ3
15
5. The ..… method produces the string 1 __init__
representation of a class instance 2 __str__
3 __doc__
4 None of the above
6. Which of the given code segments 1 class Shape:
will print the area of an object r1 of def __init__(self, x,y):
self.__l=x
class Rect as 12 correctly?
self.__b=y
def area(self):
return self.__l *
self.__b
class Rect(Shape):
def __init__(self,x,y):
Shape.__init__(self,x,y)
r1=Rect(3,4)
print("Area:", r1.area())
2 class Shape:
def __init__(self, x,y):
self.__l=x
self.__b=y
def area(self):
return self.__l *
self.__b
class Rect(Shape):
def __init__(self,x,y):
Shape.__init__(self,x,y)
r1=Rect(3,4)
print("Area:", r1._Shape__l *
r1._Shape__b)
3 Both 1 and 2
4 None of these
7. Which of the given code segments 1 class Square:
will print the area of an object s of def __init__(self, a):
class Square as 144 correctly? self.__a=a
s=Square(12)
print(s.__a * s__a)
2 class Square:
def __init__(self, a):
self.__a=a
s=Square(12)
print(s._Square__a *
s._Square__a)
16
3 class Square:
def __init__(self, a):
self.__a=a
s=Square(12)
print(s.Square__a * s.Square__a)
4 None of these
8. The code given demonstrates a 1 class Rect:
simple class Rect and its instance def __init__(self,l,b):
a. Which code snippet will output self.l=l
self.b=b
the area as 15 correctly?
def area():
return self.l * self.b
a=Rect(5,3)
print(a.area())
2 class Rect:
def __init__(self,l,b):
self.l=l
self.b=b
def area(self):
return l * b
a=Rect(5,3)
print(a.area())
3 class Rect:
def __init__(l,b):
self.l=l
self.b=b
def area(self):
return self.l * self.b
a=Rect(5,3)
print(a.area())
17
10. What is the output? 1 Toyota
class Car(object):
2 Honda
make="Toyota"
def __init__(self, 3 Ford
m="Honda"): 4 None of the above
self.make=m
c=Car("Ford")
print(Car.make)
11. What is the output? 1 Toyota
class Car(object):
2 Honda
make="Toyota"
def __init__(self, 3 ToyotaHonda
m="Honda"):
make=m 4 None of the above
c=Car()
print(c.make)
12. What is the output? 1 Spud
class Pet:
2 Bella
def __init__(self,
n="Spud"): 3 self
_name=n
4 None of the above
p=Pet("Bella")
print(p._name)
13. What is the output? 1 Spud
class Pet: Bella
def __init__(n="Spud"): 2
self._name=n 3 self
p=Pet("Bella")
print(p._name) 4 None of the above
18
16. What does the following class 1 A single inheritance where
definition represent? class Shape inherits class
Rect.
class Rect(Shape): 2 A single inheritance where
def __init__(self,x,y): class x inherits class y.
Shape.__init__(self,x,y)
3 There is no inheritance.
4 A single inheritance where
class Rect inherits class
Shape.
17. In the following class definition, 1 An instance attribute
what is total?
19
PYTHON PRAC 3
class IOString():
def __init__(self):
self.str1 = " "
def get_String(self):
self.str1= input("Type a word: ")
def print_String(self):
print(self.str1.upper())
str1 = IOString()
str1.get_String()
str1.print_String()
2. Give the definition of multiple inheritance and explain for what purpose it is used. (4)
Multiple inheritance is when a class is derived from more than one base class. It is used
when using the members of two or more classes, that have no connection, via another
class. You combine the features of all those classes by inheriting them.
3. We have a class defined for cats. Create a new cat called cat1. Set cat1 to be a grey
Burmese cat worth R3000 with the name Whiskers. (5)
20
value = 100.00
def description(self):
desc_str = "%s is a %s %s cat worth R%.2f." % (self.name,
self.color, self.kind, self.value)
return desc_str
# test code
print(cat1.description())
4. You are given a text file with full names of people and their gender.
Store the details in a text file and name it names.txt. (You can copy and paste the
contents below into a text editor like Notepad. Ensure that there is a single space between
firstname and lastname and there should be a space on either side of the hyphen).
Write a program that reads the above file and displays the names of all females. Also
display the count of males and females. (6)
The file looks as follows:
Ashley Morrison - M
Frederick Thompson - M
Affection Molafe - F
Keiran George - M
Isaac Milan - M
Jennifer Low - F
Kerry Milan - F
Marie Livingston - F
Lorna Ben - F
Laura McMillan - F
Hugo Strydon - M
Detlev King - M
A sample run:
Affection Molafe
Jennifer Low
Kerry Milan
Marie Livingston
Lorna Ben
Laura McMillan
No. of females: 6
No. of males: 6
21
#names list - display female names, count each gender
countF = 0
countM = 0
f = open("names.txt", 'r')
for line in f:
line = line.strip() #without this count is
wrong parts = line.split(" - ") name, gender =
parts
if gender == "F":
print(name)
countF += 1
else:
countM += 1
f.close()
print()
print("No. of females:", countF)
print("No. of males:", countM)
22
Create a basic program using PyQt that converts kilograms to pounds and vice versa:
3. Add a picture to your program (it should relate to the program’s functionality). (2)
23
4. Replace the name “John Doe” in the label at the top of your program to show your own
name and surname (2)
24
Create a program in PyQt that is aimed towards benefiting a local community Library. The
program should be able to add, edit and delete book entries to and from the inventory list.
1. Provide a screenshot of your program in runtime, which shows at least two entries that you
added to the list. Your screenshot should include the following features: (6)
i. Two list entries (2)
ii. One list widget (1)
iii. One line edit box (1)
iv. One label (1)
v. Four push buttons (1)
25
2. Provide the code that you used to call the list operations. (9)
i. Add to list (3)
def addlist(self):
self.ui.listWidget.addItem(self.ui.lineEdit.text())
self.ui.lineEdit.setText('')
self.ui.lineEdit.setFocus()
def editlist(self):
row=self.ui.listWidget.currentRow()
newtext, ok=QInputDialog.getText(self, "Enter new text",
"Enter new text")
if ok and (len(newtext) !=0):
self.ui.listWidget.takeItem(self.ui.listWidget.currentRow())
self.ui.listWidget.insertItem(row,QListWidgetItem(newtext))
26
iii. Delete item (1)
def delitem(self):
self.ui.listWidget.takeItem(self.ui.listWidget.currentRow())
def delallitems(self):
self.ui.listWidget.clear()
27