0% found this document useful (0 votes)
50 views13 pages

CSC1015F 2023 Test1

The document is a Theory Test for Computer Science 1015F at the University of Cape Town, consisting of 25 questions covering algorithms, programming concepts, and Python syntax. Each question is worth 1 point, and the test has a total duration of 50 minutes. Students are instructed to answer all questions using pen and may use a calculator.

Uploaded by

veronicanaidoo56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views13 pages

CSC1015F 2023 Test1

The document is a Theory Test for Computer Science 1015F at the University of Cape Town, consisting of 25 questions covering algorithms, programming concepts, and Python syntax. Each question is worth 1 point, and the test has a total duration of 50 minutes. Students are instructed to answer all questions using pen and may use a calculator.

Uploaded by

veronicanaidoo56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

University of Cape Town ~ Department of Computer

Science Computer Science 1015F ~ 2023

Theory Test 1

Marks : 25
Time : 50 minutes

Instructions:
a) Answer all questions.
b) Write your answers in PEN in the spaces provided.
c) You may use a calculator .
Question 1 (1 point)
Which of the following is NOT a property of an algorithm?
a) It can only be used once
b) There exists an algorithm for most well-defined problems
c) It may not run forever
d) It can be executed by any machine

^
____________________________________________

Question 2 (1 point)
Which of the following is an example of a program?
a) The setup manual for your new Laptop.
b) The algorithm to drive a car.
c) Wing 101
d) Direction to your house.

C
____________________________________________

Question 3 (1 point)

Question 3 options:
A is a set of instructions given to a computer, written in
a precise language.

______________________________
program
Question 4 (1 point)

Match the following elements of an algorithm with it's


description.
Question 4 options:
a) Each step is followed by another
step 1. Sequence
b) A set of steps may be repeated 2. Selection
c) A choice may be made among 3. Iteration
alternatives

a) ______
1
b) ______
3
2
c) _______

Question 5 (1 point)

The following algorithm will output the (insert a single


word) of three numbers.

Step 1: Start the Program


Step 2: Input 3 numbers, let's say S1, S2, S3
Step 3: Calculate What = S1+S2+S3)
Step 4: Calculate Now = (7 /21)* What
Step 5: Print the value of Now
Step 6: End of Solution

___________________________________
average
Basics of Programming

Question 6 (1 point)

Question 6 options:
What is the output of the following Python program:

a = "front"
b = 'back'
print(b, 2, a, sep='-', end='!')

_______________________________
back-2- front!

Question 7 (1 point)

The following Python program contains:

ans = 42
print("The answer to everything is:", ans)
a) expression, function, assignment
b) function, assignment, comment
c) parameters, floating point number, variable
d) string literal, comment, parameters
e) variable, numeric literal, string literal

____________________________________________
Question 8 (1 point)

What is the value of the following Python expression:


(Be exact, capitalization and spaces matter)

3 ** 2 // 3 % 4

____________________________
3

Question 9 (1 point)
What line of code is missing from the following Python
program:
num = 9
print("The square root of", num, "is", math.sqrt(num))

import math
_______________________________

Question 10 (1 point)

Which of these operators has the highest precedence in Python?


Question 10 options:
a) **
b) or
c) %
d) ==
e) not

A
____________________________________________
Selection

Question 11 (1 point)

x != y is an example of which of the following?


a) Relational expression
b) Boolean expression
c) Arithmetic expression
d) String literal
e) A division operator

A
____________________________________________

Question 12 (1 point)

Consider the following Python program:

ans = input("Do you like broccoli (y/n)? ");


if ans == 'Yes' or 'yes':
print("You like broccoli")
else:
print("You don't like broccoli")

If the user types n from the keyboard, the program will:


a) print You like broccoli because it has a logic error
b) print You like broccoli because it has a syntax error
c) print nothing
d) print You don't like broccoli because it has no errors
e) terminate when it encounters a logic error

A
____________________________________________

Question 13 (1 point)
The following program sorts three numbers in descending
order. What is the MISSING LINE?
if a > b:
if c > a:
print("c > a > b")
else:
# MISSING LINE
print("a > b > c")
else:
print("a > c > b")
else:
if c > b:
print("c > b > a")
else:
if a > c:
print("b > a > c")
else:
print ("b > c > a")

a) if a > b:
b) if b > a:
c) if b > c:
d) if a > c:
e) if c > b:

___________________________
C

Question 14 (1 point)
The ________ clause is an optional part of the if statement that
is used to decide what to do if the condition is not met.

_________________________
else

Question 15 (1 point)
What is the value of the following boolean expression?
T f F F
(5<10 or 2<1) and (10>20 or not 1>2)
a) False
b) True
c) and
d) or
e) None

________________________
B
Iteration

Question 16 (1 point)
What is the output of the following Python code?
string = "Python is awesome"
count = 0
for letter in string:
if letter == "o":
count += 1
elif letter == "a":
continue
else:
print(letter)
print(count)
a) P, y, t, h, n, i, s, w, e, s, o, m, e, 2
b) P, y, t, h, n, i, s, a, w, e, s, m, e, 2
c) P, y, t, h, n, i, s, w, e, s, m, e, 2
d) P, y, t, h, n, i, s, w, e, s, m, e, 4
e) P, y, t, h, n, i, s, w, e, s, o, m, e, 4

c
____________________________________________

Question 17 (1 point)
Which of the following statements about the range() function in Python
is false?
a) The range() function can only generate sequences in ascending order.
b) The range() function can be used to generate an empty sequence.
c) The range() function can be used to generate a sequence of numbers
between a start and end value.
d) It is used to iterate over a sequence of elements in reverse order.
e) It returns a tuple containing both the index and the value of each element in an iterable.

A
____________________________________________

Question 18 (1 point)
What is the difference between a for loop and a while loop in
Python?
a) A for loop is used for iterating over a sequence of elements, while a while loop is used for
executing code repeatedly as long as a specified condition is true.
b) A for loop is used for iterating over a sequence of elements and also supports the use of the
range() function, while a while loop is used for executing code repeatedly as long as a
specified condition is true.
c) A while loop is used for iterating over a sequence of elements and also supports the use of
the range() function, while a for loop is used for executing code repeatedly as long as a
specified condition is true.
d) A for loop and a while loop are exactly the same and can be used interchangeably.
e) A for loop is used for executing code repeatedly as long as a specified condition is true,
while a while loop is used for iterating over a sequence of elements.

A
____________________________________________

Question 19 (1 point)
What is the difference between the following for loop and while
loop code snippets in Python?
# for loop
for i in range(5):
print(i)
# while loop
i=0
while i < 5:
print(i)
i += 1
a) The for loop and the while loop have the same speed.
b) The for loop executes the code block once for each element in a sequence, while the while
loop executes the code block repeatedly as long as a specified condition is true.
c) The while loop executes the code block once for each element in a sequence, while the for
loop executes the code block repeatedly as long as a specified condition is true.
d) The for loop is faster than the while loop.
e) The while loop is faster than the for loop.

B
____________________________________________

Question 20 (1 point)
The following code prints "Yes" at the end of the execution
when, and only when:

num = eval(input("Enter a number: "))


result = True
i=2

while i <= num // 2:


if num % i == 0:
result = False
break
i += 1

if result:
print("Yes")
else:
print("No")
a) num is an even number
b) num is a decimal number
c) num is a negative number
d) num is an odd number
e) num is a prime number

E
____________________________________________
Strings

Question 21 (1 point)

Question 21 options:
What is the function you would use to remove all capital letters
from a string. Give just the function, not the invocation, as
follows: len()

lower ()
____________________________________________

Question 22 (1 point)

Write a line of Python code to extract the word "Cruel" from the
string below using reverse indexing:

str = "Hello Cruel World"

Str [7:-123
____________________________________________

Question 23 (1 point)
What is the output of the following code?

print("{1} {3} {2} {0}". format("one", "two", "three", "four"))


a) "one two three four"
b) "two four three one"
c) "one three two zero"
d) "{1} {3} {2} {0}"
e) "{0} {1} {2} {3}"

B
____________________________________________
Question 24 (1 point)
What is the output of the following code?

s1="CSC"
s2="1015F"
s3="2023"

print((s1+s2)*2, s3, sep="\n")


a) CSC
1015F
CSC
1015F
2023
b) Nothing – a syntax error
c) CSC1015FCSC1015F
2023
d) CSCCSC1015F1015F2023
e) CSC1015FCSC1015F2023

____________________________________________

Question 25 (1 point)

Which of the following Python command(s) will extract the


substring "like" from str="unlikely"?
....
a) str[-6:-3] and str[::3]
b) str[2:6]
c) str[::3]
,
d) str[2:6] and str[-6:-3]
e) str[-6:-3]

____________________________________________

You might also like