0% found this document useful (0 votes)
391 views4 pages

Midterm 1 Practice: CMPT 120

This document contains a practice midterm exam for a CMPT 120 course, with 4 multiple choice questions about Python syntax and programs, and 3 coding questions - the first asks to modify a given string, the second asks to output two strings based on a user-input integer, and the third provides sample code to construct the requested strings in a single loop.

Uploaded by

AmiurRahman
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)
391 views4 pages

Midterm 1 Practice: CMPT 120

This document contains a practice midterm exam for a CMPT 120 course, with 4 multiple choice questions about Python syntax and programs, and 3 coding questions - the first asks to modify a given string, the second asks to output two strings based on a user-input integer, and the third provides sample code to construct the requested strings in a single loop.

Uploaded by

AmiurRahman
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/ 4

CMPT 120

Midterm 1 Practice Sol


Q1 [4 marks] Select the correct answer. Fill out your answers in the bubble sheet
provided at the end of this exam. (Check the last page)

1. Which of the following has a syntax error?


(a) num = input("enter a number") # Assume user enters 0
print(1 / int(num))
(b) a = 1
if (a = 1):
print("a is equal to 1")
(c) # Profit = 0.25 * total_sales
total_sales = int(input("Enter total sales: "))
profit = 0.25 * total_sales
print(profit)
(d) None of the given option

2. What does this code output?  pri


N=8
if N <= 3:
print("up")
elif N == 3:
print("down")
if N > 2:
print("left")
else:
print("none")

(a) Left
(b) None
(c) Up
(d) Down
(e) Error

3. 15 % 3 == 0
(a) True
(b) False
4. What are two kinds of programs that process high-level languages into low-level
languages?
(a) Interpreters and compilers
(b) interpreters and executors
(c) compliers and executors
(d) interpreters, compilers and executors

Ques 2 [6 marks] What is the output of the following code?


aher
string = "iamahero"
print(string[3:-1])

b=5 buzz
if b==4 and a==2:
print("fizz")
elif b>5:
print("fizzbuzz")
else:
print("buzz")

Ques 3. Coding
(a) [10 marks] Write a python code that receives a string from the user and then
outputs a new string from the original string where all occurrences of its first
character have been changed to '*' except for the first and third characters.
# get input
string = input("Enter a string: ")
# Receive a string
first_char = string[0] # Store the first character in a
variable
new_string = ""

for i in range(len(string)): # Loop over characters in


the given string, for string = hhi,
#i range will be 0, 1,2
if(string[i] == first_char and i not in [0,2]): #
Check if current character is equal
#to the first character and is not first and
third character
new_string = new_string + '*'
else: # Keep other characters
new_string = new_string + string[i]

print("New string: " + new_string)


(b) [10 marks] Write a python code using a function asking the user to enter an integer
number n. Construct, using one single loop, the string “1234…n” and the string
“11223344..nn”. After the loop, print the two strings. Below is the output sample.

def strConstruct():
new_string1 =""
new_string2 =""

#prompt user to enter integer number n


n=int(input("Enter an integer number: "))
# for loop
for num in range (1, n+1): # for n =3, num = 1,2,3
# construct new strings
new_string1 = new_string1 + str(num)
new_string2 = new_string2 + str(num)*2 #
repeating the character
#print the strings
print(new_string1)
print(new_string2)

strConstruct()

You might also like