0% found this document useful (0 votes)
121 views

Visual Programming 1: - Exam Preparation: With MCQS, Pracs, Questions and Solutions

The document provides information about an exam preparation for Visual Programming 1. It contains 2 sections - MCQs and Python Practice. The MCQ section provides 20 multiple choice questions related to Python concepts like data types, operators, functions etc. The Python Practice section contains 4 programming questions where students are asked to write Python code to solve problems related to if-else conditional statements, area of a triangle calculation, random number generation and pattern printing. Students need to provide screenshots of Python installed in command line and IDLE mode for the first question.

Uploaded by

Chiko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Visual Programming 1: - Exam Preparation: With MCQS, Pracs, Questions and Solutions

The document provides information about an exam preparation for Visual Programming 1. It contains 2 sections - MCQs and Python Practice. The MCQ section provides 20 multiple choice questions related to Python concepts like data types, operators, functions etc. The Python Practice section contains 4 programming questions where students are asked to write Python code to solve problems related to if-else conditional statements, area of a triangle calculation, random number generation and pattern printing. Students need to provide screenshots of Python installed in command line and IDLE mode for the first question.

Uploaded by

Chiko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Visual Programming 1

INF1511
Semester 1
MCQs and Pracs

--EXAM PREPARATION: with MCQs, Pracs, Questions and Solutions--

1
MCQ 1
INF

Question Option Answer


1. Python is a/an ….. language 1 compiled
2 interpreted
3 assembly
4 None of the above
2. Comments in Python begin with 1 *
a ….. 2 //
3 #
4 None of the above
3. Which is the proper assignment 1 Word = “hello”
for a string variable? 2 Word = ‘hello’
3 Word = hello
4 All of the above
5 Both 1 and 2
4. The format code for an integer 1 %f
variable is ….. 2 %e
3 %d
4 %c
5 %x
5. The function used to print 1 printf()
messages and the results of 2 print()
computations to the console is
3 printline()
..…
4 println()

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

The output is: 3 The value of x is 2


4 The value of x is %d %x

7. What is the output of the following 1 Hello


statements? Peter
print(“Hello”, 2 Hello Peter
end= ‘ ’) Hello = Peter
3
print(“Peter”)
4 Hello end Peter

8. What is the output of the following 1 Hello,How are you?


statement?
2 Hello How are you?
print(“Hello”,“How are 3 HelloHow are you?
you?”)
4 None of the above

9. What is the output of the following 1 GoodMorning


statement? 2 Good Morning
print(“Good” + “Morning”) 3 Good
Morning

4 None of the above

10. The correct statement to print the 1 print(y)


value of variable y=100 is ….. 2 print(“The value of y is”,y)
3 print(‘The value of y: %d’ %y)
4 All of the above

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

13. The output of the following code is: 1 10


i=1; 2 4 7
while i < 10:
i = i + 3 3 1 4 7
print (i, end=' ')
4 1 4 7 10
5 4 7 10
14. The output of print(3.5//2) is ….. 1 1.0
2 1.75
3 3.5//2
4 None of the above

15. x=5%2. The value of x is ….. 1 2


2 1
3 2.5
4 None of the above

16. Which of the operations will give a 1 x = pow(3,2)


value 9 to x? 2 x = 3**2
3 x = pow(2,3)
4 All of the above
5 Only 1 and 2

17. The function which returns the data 1 datatype()


type of an object is ….. 2 data()
3 object()
4 type()

4
18. Escape sequences in Python begin 1 #
with a ….. character 2 //
3 \
4 None of the above

19. print(“10/5”) will output: 1 10/5


2 2
3 0
4 None of the above

20. print("Mac said: \"I like 1 Mac said: I like programming


programming\"") will output: 2 “Mac said: I like programming”
3 Mac said: “I like programming”
4 None of the above

5
PYTHON PRACTICE

1. Install Python and present screenshots of the program running in:


i) Command-line mode and (1)
ii) IDLE (1) (2)

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)

The formula is Base x Height ÷ 2


# Question3.py
b = int(input("Input the base: "))
h = int(input("Input the height: "))

area= b*h/2

print("area= ", area)

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

from random import choice


r = choice(range(5,11))
print("The random number generated is",r)
for i in range(r,0,-1):
j = 1
k = i
while(j <= k):
print(i, end = ' ')
i -= 1
j += 1;
print()

8
MCQ 2

Question Option Answer


1. An example of an immutable 1 lists
sequence in Python is ..… 2 strings
3 integers
4 None of the above
2. a=(‘tiger’, ‘lion’, ‘fox’). 1 string
Here, a is an example of a ….. 2 tuple
variable 3 list
4 None of the above
3. b=[2,6,8]. Here, b is an example 1 string
of a ….. variable 2 tuple
3 list
4 None of the above
4. …… is an example of a mutable 1 String
sequence in Python 2 Tuple
3 List
4 Boolean
5 None of the above
5. The index value of the first element 1 user defined
in a sequence is ….. 2 always zero
3 always one
4 None of the above
6. The output of: 1 hurr
word=”hurry” 2 hurry
print(word[4]) is: 3 IndexError: string index
out of range
4 y

9
7. What is the output of: 1 6

lst = ['Africa'] 2 5
print(len(lst)) 3 1
4 None of the above

8. What is the output? 1 MONICA

name = “MoNiCa” 2 mOnIcA


name = name.upper()
name = name.swapcase() 3 monica
print(name) 4 None of the above
9. Which is a true statement? 1 A sequence is an unordered
group of elements
2 The length of a sequence will
be greater than the position
number of its last element
3 All elements in a tuple must
be of the same type
4 None of the above

10. The value of x in 1 3

x = 5 in [3, 'yes', 'no', 2 4


5] is ….. 3 true
4 None of the above

11. Which is a true statement? 1 Tuple is a mutable sequence


2 Indexing of sequences begins
at zero
3 List is an immutable sequence

4 All 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")

3 print("I don't know")


4 All of the above
5 Only 1 and 2

10
13. The statement 1 9
print(len("Good" + "Night")) 2 10

will output ….. 3 Good Night


4 GoodNight
5 None of the above

14. What is the output of the following 1 orange


code: 2 apple
fruit=('apple', 3 banana
'mango','banana','orange')
4 mango
print(fruit[len(fruit)])
5 None of the above
15. Which of the options will print all the 1 for item in fruit:
elements in the tuple print(fruit)
fruit=('apple', 2 for item in fruit:
'mango','banana','orange')
print(item)
one by one?
3 for fruit in item:
print(item)
4 for fruit in item:
print(fruit)
5 None of the above

16. What is the output of the following 1 Sight


code? 2 Night
word = “Night” 3 S
word[0]=”S”
print(word) 4 SNight
5 None of the above

17. What is the output of the following 1 (‘Night’)


code? 2 (‘Sight’)
word = (“Night”) 3 (‘SightNight’)
word[0]=”Sight”
print(word) 4 None of the above

18. What is the output of the following 1 [‘Night’]


code? 2 [‘Sight’]
word = [“Night”] 3 [‘SightNight’]
word[0]=”Sight”
print(word) 4 None of the above

11
19. The output of print(“abc” * 3) 1 abc*3
is ..… 2 invalid operation

3 abcabcabc
4 None of the above

20. The output of 1 -A-f-r-i-c-a-


print(“-”.join(“Africa”)) 2 A-f-r-i-c-a
is …..
3 -Africa
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:

Enter a string: Good


Enter another string: day
The combination is dGoodaGoody
The concatenation is Goodday

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

Question Option Answer


1. The ..… statement in Python creates 1 class
a class object. 2 def
3 init
4 None of the above
2. Which is a true statement? 1 A class in Python should always define
the __init__ method

2 The __init__ method is executed


before the creation of an instance.

3 The __init__ method can have zero


or more arguments
4 None of the above
3. A/an ….. variable is shared by all 1 object
instances of a class 2 data
3 class
4 instance
4. Which is a valid built-in class 1 __bases__
attribute in Python? 2 __doc__
3 __name__
4 All of the above
5 Only 1 and 2

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())

4 None of the above


9. 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(c.make)

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

14. What is the output? 1 Spud


class Pet:
def 2 Bella
__init__(self,n="Spud"): 3 self
self._name=n
p=Pet("Bella") 4 None of the above
print(p._name)

15. When one class is derived from 1 Simple inheritance


another single class it is called …..
2 Single inheritance
3 Multi-level inheritance
4 Multiple inheritance
5 None of these

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?

class Travel(object): 2 A class attribute


"""Bon voyage"""
total = 0 3 An instance method
def display(self):
4 A class method
print("Total number of
bookings:", Travel.total)

18. Which is not a type of inheritance? 1 Double


2 Single
3 Multiple
4 Multilevel
19. In the following class definition, 1 An instance attribute
what is method display?

class Travel(object): A class attribute


2
"""Bon voyage"""
total = 0
def display(self): 3 An instance method
print("Total number of
bookings:", Travel.total) 4 A class method

20. What is the output of the following 1 5 6


code?
class Shape: 2 0 0
def __init__(self,
x=3,y=4): 3 3 4
self.x=x
self.y=y 4 None of the above
s1=Shape(5,6)
s2=Shape()
s1=s2
print(s1.x,s1.y)

19
PYTHON PRAC 3

1. Write a Python class that contains two methods: (5)


a. get_String: accepts a string from the user (The program should prompt the user
to provide a word) and
b. print_String: prints the string in upper case

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)

# define the Cat


class class Cat:
name = ""
kind = "cat"
color = ""

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

# your code goes here


cat1 = Cat() cat1.name
= "Whiskers"
cat1.color = "grey"
cat1.kind = "Burmese"
cat1.value = 3000.00

# 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

#for line in open("names.txt"):


#open file for reading

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:

1 kilogram = 2.2 pounds

1. Provide a screenshot of your program (2)


The screenshot should show a similar program with similar features

2. Your program should include the following features:


i. Three labels
ii. Two line edit boxes
iii. Two push buttons (3)
One mark per feature type

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)

5. Provide the code of the two buttons in your program. (6)

The kilogram to pound button:


def btn_KtoP_clicked(self):
kilo = float(self.edt_Kilo.text())
pound = (kilo * 2.2)
self.edt_Pounds.setText(str(pound))

The pound to kilogram button:


def btn_PtoK_clicked(self):
pound = float(self.edt_Pounds.text())
kilo = (pound/2.2)
self.edt_Kilo.setText(str(kilo))

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()

ii. Edit list (4)

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())

iv. Delete all items (1)

def delallitems(self):
self.ui.listWidget.clear()

27

You might also like