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

Midterm 1 Practice: CMPT 120

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy