Docsity

Conditional Statements and Loops in Python, Exams of Nursing

Toggle sidebar
bg1
COSC 1436 FINAL EXAM QUESTIONS
AND ANSWERS
[SHORT ANSWER] Assume the string variables firstName and userID are assigned
with values. use string slicing and concatenation to create a username based on
the first two letters of the firstName, concatenated with the last four digits of the
userID as shown in the example below.
For Example: Suppose firstName = "Mary" and userID = "W12345678" then, the
username should be:
Ma5678 - CORRECT ANSWER-username = firstName[:2] + userID[-4:]
SHORT ANSWER Assume variable gpa has already been assigned a value. Write
python code that displays invalid GPA if the value associated with variable gpa is
not in the range [0,4] - CORRECT ANSWER-if gpa < 0 or gpa > 4:
print("Invalid GPA")
Which type of error prevents the program from running?
human
syntax
grammatical
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30

Partial preview of the text

Download Conditional Statements and Loops in Python and more Exams Nursing in PDF only on Docsity!

COSC 1436 FINAL EXAM QUESTIONS

AND ANSWERS

[SHORT ANSWER] Assume the string variables firstName and userID are assigned with values. use string slicing and concatenation to create a username based on the first two letters of the firstName, concatenated with the last four digits of the userID as shown in the example below. For Example: Suppose firstName = "Mary" and userID = "W12345678" then, the username should be: Ma5678 - CORRECT ANSWER -username = firstName[:2] + userID[-4:] SHORT ANSWER Assume variable gpa has already been assigned a value. Write python code that displays invalid GPA if the value associated with variable gpa is not in the range [0,4] - CORRECT ANSWER -if gpa < 0 or gpa > 4: print("Invalid GPA") Which type of error prevents the program from running? human syntax grammatical

logical - CORRECT ANSWER -syntax The smallest storage location in a computer's memory is known as a switch byte ketter bit - CORRECT ANSWER -bit A Python line comment begins with ________. a. /* b. $$ c. // d. # - CORRECT ANSWER -d. # Which of the following are valid camel case identifier naming styles (Choose all that apply)? (a) dollar_amount (b) dollarAmount (c) dollar$Amount (d) amtDollar (e) dollarAmt$

Match the relational operators with its meaning: a. > b. < c. >= d. <= e. == f. != - CORRECT ANSWER -a. Greater than b. less than c. Greater than or equal to d. Less than or equal to e. Equal to (equality operator) f. Not equal to Which of the following will assign a random integer in the range of 1 through 50 to the variable number? (a) number = random(range(1, 50)) (b) random(1, 50) = number c) number = random.randint(1, 50) (d) randint(1, 50) = number - CORRECT ANSWER -c) number = random.randint(1,

x < y or z > x What is the result of the following Boolean expression, given that x = 5, y = 3, and z= 8? not (x < y or z > x) and y < z (a) 5 (b) False (c) 8 (d) True - CORRECT ANSWER -(b) False What is the result of the following Boolean expression, given that x = 5, y = 3, and z = 8? x < y or z > x (a) False (b) 8 (c) True (d) 5 - CORRECT ANSWER -(c) True Consider the following statements. If the input is 85, the output of the following code will be:

A - CORRECT ANSWER -D

What is the output of the following code? x = 10 if x != 10: print("This is true!") else: print("This is false!") print("This is all folks!") a. This is false! b. This is true! This is all folks! c. This is true! d. This is false! This is all folks! - CORRECT ANSWER -d. This is false! This is all folks! Select the if statement that assigns .2 to comissionRate if the monthlySales is greater than 10000. a. if monthlySales > 10000:

commissionRate =. b. if monthlySales > 10000 commissionRate =. c. if monthlySales > 10000: commissionRate ==. d. if monthlySales > 10000: .2 = commissionRate - CORRECT ANSWER -a. if monthlySales > 10000: commissionRate =. Consider the following statements. If the input is 85, the output of the following code will be: score = 0 score = int(input("Enter a score: ")) if score >= 90: grade = "A" if score >= 80: grade = "B" if score >= 70:

(a) Your score is: - (b) Test was not taken (c) Score is not valid (d) ERROR (e) Score is valid - CORRECT ANSWER -(c) Score is not valid (T or F) The \n escape character (when used within quotes), causes Python to print a capital N to the output. - CORRECT ANSWER -False The following statements will both display the same result which is to generate a random ASCII character between a-z: print(chr (random.randint(0, 127))) print(chr (random.randint (ord ('a'), ord ('Z')))) - CORRECT ANSWER -False Suppose s1 = "Welcome to Hcc", What will be displayed after the following statements are executed? r1 = "HCC" in s r2 = "come" not in s print(r1, r2)

(a) True True (b) False False (c) False True (d) True False - CORRECT ANSWER -(b) False False What is the output of the following statements: print('One', end=' ') print('Two') print('Three') (a) OneTwoThree (b) OneTwo Three (c) One Two Three (d) One TwoThree - CORRECT ANSWER -(b) OneTwo Three Suppose s1 = "smith" and s2 = "Mary", what is the output of the following code?

Welcome to Hcc (b) 2 * s s1 + s (c) Hcc * Hcc Welcome to Hcc (d) Hcc Hcc Welcome to Hcc (e) 2 * Hcc Welcome to + Hcc - CORRECT ANSWER -(a) HccHcc Welcome to Hcc What is the output of the following two statements: print(3.246) print(format(3.246,'.2f')) (a) 3.

(b) 3.

(c) 3.

(d) 3.

3.2 - CORRECT ANSWER -(c) 3.

4.5.c) Given three variables a, b,c of type float, that have already been assigned with appropriate values, which of the following statements displays each of the value formatted into a string whose width is 10, including a decimal point and two digits after the point. print(format(a, "10.2f"), format(b, "10.2f"), format(c, "10.2f")) print(format(a, 2.10+), format(b) 2.10f), format(c, 2.10f)) print(a, b, c, format("10.2f")) print(a, b, c, fornat(.2f")) print(format(a, b, c, "10.2f") - CORRECT ANSWER -print(format(a, "10.2f"), format(b, "10.2f"), format(c, "10.2f")) what is the output of the following code? s1="Houston" s2="Dallas" if s1 ==s 2: print(s1[0], s2[0]) elif s1 > s2: print(s1[1], s2[1]) else:

what is the output of the following code? str = "#Sp23" count = 0 if ((str[1].isupper())): count = count + 1 if ((str[2]).islower()): count = count + 1 if ((str[3]). is alpha()): count = count + if ((str[4]).isdigit()): count = count + 1 print(count) 4 3 0

1 - CORRECT ANSWER -

What is the value of result when x = 5, y = 12,z = 8? result = "True" if x > y or (x < z and y > z) or 0 else "False" True None of the answer choices are correct False Run-Time error - CORRECT ANSWER -True Given two already defined variables, x and y. Write a python statement that swaps their associated values. - CORRECT ANSWER -x,y = y,x A program is loaded into -----while it is being processed on the computer and when the program is not running it is stored in ----- a. memory, secondary storage b. memory, the CPU c. the monitor, memory d. secondary storage, the IDE - CORRECT ANSWER -a. memory, secondary storage

result = "123.456" - string result = '123' - string result = 123 - int result = 123.0 - float In the expression 12+7: (a) The 12 and 7 are the: (b) The + is the: operand(s) operator(s) - CORRECT ANSWER -(a) operand(s (b) operator(s) An if statement that displays 'Speed is normal' if the speed variable is between the range of 55-65 is: if speed >= 55 or speed <= 65: print('Speed is normal') - CORRECT ANSWER -False Assume the variables: a = 2, b = 4, c = 6

The result of the following expression is [True/False]? a == 4 or b > 2 - CORRECT ANSWER -True What is the value of z after the following code is executed? x = 10 y = 40 z = 0 if x >= y z = x + y else: z = y % x 4 0 400 50 - CORRECT ANSWER - Suppose repeat = 'y' , both the following if statements will evaluate to True? if repeat == 'y' or repeat == 'Y':