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

CS101 Quiz1 Sec1 2

This document contains a quiz with 5 questions. The quiz covers topics like variable naming rules in Python, arithmetic operations, conditional statements, loops and functions. Each question provides multiple parts to test different concepts and students are asked to write code or evaluate expressions.

Uploaded by

esmaalanya007
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)
34 views4 pages

CS101 Quiz1 Sec1 2

This document contains a quiz with 5 questions. The quiz covers topics like variable naming rules in Python, arithmetic operations, conditional statements, loops and functions. Each question provides multiple parts to test different concepts and students are asked to write code or evaluate expressions.

Uploaded by

esmaalanya007
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

CS 101 Quiz 1 - Sec.

1 - A 2023 - 2024 Fall


Surname Name:
Grade:
Student No:

1. (10 p.) For each variable name, write "Valid" if it is a valid variable name, and "Invalid" if it is not a valid.
If the variable name is not valid, write the reason. For valid ones, don't write reason.
Variable Name Valid / Invalid Reason if invalid
a-4 invalid '-' minus sign cannot be used in variable names
a bc invalid space character cannot be used in variable names
_4_a_ valid
hello.world invalid '.' dot sign cannot be used in variable names
4a invalid variable name cannot start with digit character

2. (10 p.) Write the output of the following Python statements.


a, b, c, d, e = 7, 2, 3, 6, -4
www = 2 * a - d // c * (e + c ** b) 4
print(www)

3. (20 p) Variables below contain numbers. Write an if-elif-else statement that handles the following cases:
Condition Operations
n1 greater than n2 increase cnt by n2
n1 equal to n2 double the value of cnt (i.e. set cnt to 2 times its original value)
otherwise decrease cnt by n1

if n1 > n2:
cnt += n2 # cnt = cnt + n2
elif n1 == n2:
cnt *= 2 # cnt = cnt * 2
else:
cnt -= n1 # cnt = cnt - n1

4. (30 p) Write a Python program that finds and prints the sum of the integer numbers divisible by 12
between integer variables k1 and k2 (both inclusive). Assume k1 < k2. Hint: use a for loop.

total = 0
for n in range(k1, k2+1): # numbers from k1 to k2
if n % 12 == 0: # if n is divisible by 12,
total += n # then add n to total
print(total)

5. (30 p) Write a function cntSpaces() that gets a string as parameter, returns the number of spaces (' ') in
this string.
def cntSpaces(s):
cnt = 0
for c in s:
if c == " ":
cnt += 1
return cnt
CS 101 Quiz 1 - Sec. 1 - B 2023 - 2024 Fall
Surname Name:
Grade:
Student No:

1. (10 p.) For each variable name, write "Valid" if it is a valid variable name, and "Invalid" if it is not a valid.
If the variable name is not valid, write the reason. For valid ones, don't write reason.
Variable Name Valid / Invalid Reason if invalid
x*2 invalid '*' asterix cannot be used in variable names
2x invalid variable name cannot start with digit character
x x invalid space character cannot be used in variable names
_2_x_ valid
x,x invalid ',' comma cannot be used in variable names

2. (10 p.) Write the output of the following Python statements.


a, b, c, d, e = -2, 2, 1, 3, -4
aa = 4 * a - d % c // (e + c ** b) -8
print(aa)

3. (20 p) Variables below contain numbers. Write an if-elif-else statement that handles the following cases:
Condition Operations
p greater than 3q multiply q by 3 (i.e. set q to 3 times its original value)
p greater than q divide p by 2 (i.e. set p to half of its original value)
otherwise increase q by 100

if p > 3 * q:
q *= 3 # q = q * 3
elif p > q:
p /= 2 # p = p / 2
else:
q += 100 # q = q + 100

4. (30 p) Write a Python program that finds and prints the sum of the numbers that has 6 as last digit
between integer variables zz1 and zz2 (both exclusive). Assume zz1 < zz2. Hint: use a for loop.

total = 0
for n in range(zz1+1, zz2): # between zz1 and zz2
if n % 10 == 6: # if last digit is 6,
total += n # then add to total
print(total)

5. (30 p) Write a function cntTt() that gets a string as parameter, returns the total number of 'T' and 't'
characters in this string.
def cntTt(s):
cnt = 0
for c in s:
if c == "T" or c == "t":
cnt += 1
return cnt
CS 101 Quiz 1 - Sec. 2 - A 2023 - 2024 Fall
Surname Name:
Grade:
Student No:

1. (10 p.) For each variable name, write "Valid" if it is a valid variable name, and "Invalid" if it is not a valid.
If the variable name is not valid, write the reason. For valid ones, don't write reason.
Variable Name Valid / Invalid Reason if invalid
_2 good invalid space character cannot be used in variable names
_2good valid
2_good invalid variable name can not start with a digit character
two+good invalid '+' minus cannot be used in variable names
twox2=four invalid '=' equal cannot be used in variable names

2. (10 p) Write the output of the following Python statements.


a, b, c, d, e = 4, 3, 1, 2, -2
p = a % d - (d - a ** b) * c // e ** 2 16
print(p)

3. (20 p) Variables below contain numbers. Write an if-elif-else statement that handles the following cases:
Condition Operations
x greater than 2y half the value of x (i.e. set x to half of its original value)
x≥y increase y by 10
otherwise multiply x by 1.5 (i.e. set x to 1.5 times its original value)

if x > 2 * y:
x /= 2 # or: x = x / 2
elif x >= y:
y += 10 # or: y = y + 10
else:
x *= 1.5 # or: x = x * 1.5

4. (30 p) Write a Python program that finds and prints the sum of the numbers that has 24 as last 2-
digits between integer variables qa and qb (both inclusive). Assume qa < qb. Hint: use a for loop.

total = 0
for n in range(qa, qb+1): # numbers from qa to qb
if n % 100 == 24: # if last 2-digits are 24,
total += n # then add n to total
print(total)

5. (30 p) Write a function cntCommaDot() that gets a string as parameter, returns the total number of
comma (',') and dot ('.') in this string.
def cntCommaDot(s):
cnt = 0
for c in s:
if c == "," or c == ".":
cnt += 1
return cnt
CS 101 Quiz 1 - Sec. 2 - B 2023 - 2024 Fall
Surname Name:
Grade:
Student No:

1. (10 p.) For each variable name, write "Valid" if it is a valid variable name, and "Invalid" if it is not a valid.
If the variable name is not valid, write the reason. For valid ones, don't write reason.
Variable Name Valid / Invalid Reason if invalid
n1!n2 invalid '!' cannot be used in variable names
_1 nature invalid space character cannot be used in variable names
1_nature invalid variable name can not start with a digit character
_2x valid
_2^3is8 invalid '^' cap sign cannot be used in variable names

2. (10 p) Write the output of the following Python statements.


a, b, c, d, e = 4, 3, 1, 2, -2
a = a * e + (d - a ** d) * c // e ** 2 -12
print(a)

3. (20 p) Variables below contain numbers. Write an if-elif-else statement that handles the following cases:
Condition Operations
aa greater than bb double the value of bb (i.e. set bb to 2 times its original value)
aa is equal to bb increase aa by 4
otherwise decrease bb by 20% (i.e. set bb to 0.8 times its original value)

if aa > bb:
bb *= 2 # or: bb = bb * 2 bb = bb + bb
elif aa == bb:
aa += 4 # or: aa = aa + 4
else:
bb *= 0.8 # or: bb = bb * 0.8

4. (30 p) Write a for loop that finds the sum of the numbers that are divisible by 2 and by 7 between
integer variables vv1 and vv2 (both inclusive). Assume vv1 < vv2. Hint: use a for loop.

total = 0
for n in range(vv1, vv2+1): # from vv1 to vv2
if n % 2 == 0 and n % 7 == 0: # if n % 14 == 0:
total += n # then add to total
print(total)

5. (30 p) Write a function cnt01() that gets a string as parameter, returns the total number of '0' and '1'
characters in this string.
def cnt01(s):
cnt = 0
for c in s:
if c == "0" or c == "1":
cnt += 1
return cnt

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