Comp Sci Exam #1 review

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/58

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:26 PM on 9/28/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

59 Terms

1
New cards

Programming

The act of designing, implementing, and testing computer programs

2
New cards

Hardware

consists of the physical elements in a computer system.

  • ex: monitor, the mouse, external storage, and the keyboard.


3
New cards

what does the central processing unit (CPU) do?

performs program control and data processing

4
New cards

The CPU has what two components?

  • control unit

  • arithmetic logic unit


5
New cards

Control unit

directs the operation of the processor

  • communication and coordination between input/output devices.

  • reads and interprets instructions

  • determines the sequence for processing the data

  • provides timing and control signal


6
New cards

arithmetic logic unit

contains the circuitry to perform calculations and do comparisons

7
New cards

Primary storage

composed of memory chips: electronic circuits that can store data as long as it is provided electric power

8
New cards

Secondary storage

provides a slower, less expensive storage that is persistent

  • the data persists without electric power


9
New cards

Algorithm

describes a sequence of steps that is:

  1. Unambiguous

  • No “assumptions” are required to execute the algorithm

  • The algorithm uses precise instructions

  1. Executable

  • The algorithm can be carried out in practice

  1. Terminating

  • The algorithm will eventually come to an end, or halt


10
New cards

variable

a named storage location in a computer program

example: total = 0

11
New cards

assignment statement

“=” is used to place a new value into a variable

12
New cards

what is an integer (int)?

A whole number (no fractional part)

  • ex: 7


13
New cards

what is a float?

A number with a fraction part

  • ex: 8.88


14
New cards

what is a string (str)?

A sequence of characters

  • ex: "Bob”


15
New cards

The data type is associated with the____, not the ____

value, variable

  • ex: cansPerPack = 6 #int

  • ex: canVolume = 12.0 #float


16
New cards

warning about variables and assigning different values at different places

If you mix stings with integer or floating point values the result is an error

17
New cards

naming variables

names must start with a letter or an underscore

continue with letters (upper or lower case), digits or the underscore

  • You cannot use other symbols (? or %...) and spaces are not permitted


18
New cards

A constant is a variable….

whose value should not be changed after it’s assigned an initial value

19
New cards

naming a constant

use this format: UPPER_CASE

20
New cards

Mixing numeric types

If you mix integer and floating-point values in an arithmetic expression, the result is a floating- point value.

  • ex: 7 + 4.0 # Yields the floating value 11.0


21
New cards

double stars ** are used to…

calculate an exponent

22
New cards

floor division using the // operator

The “//” operator computes the quotient and discards the fractional part

23
New cards

what is % used as

to find a remainder

24
New cards

what is a library?

collection of code, written and compiled by someone else, that is ready for you to use in your program

25
New cards

what is a standard library?

a library that is considered part of the language and must be included

with any Python system

26
New cards

Floating-point to integer conversion

You can use the function int() and float() to convert between integer and floating point values:

  • balance = total + tax # balance: float

  • dollars = int (balance) # dollars: integer


27
New cards

Text, Characters, and strings

Text consists of characters

Characters are letters, numbers, punctuation marks, spaces, ….

A string is a sequence of characters

28
New cards

String Length

The number of characters in a string

  • (For example, the length of "Harry" is 5).

You can compute the length of a string using Python’s len() function:

  • length = len("World!") # length is 6


29
New cards

What will be the values of the variables num1 and num2 after the given set of assignments?

num1 = 20
num2 = 10
num1 = num1 + num2 / 2
num2 = num1


num1 = 25.0, num2 = 25.0

30
New cards

What must be done first before you can use a function from the standard library?

the function must be imported

31
New cards

what is wrong with this?

result = num1 // num2 / num3 num1 = 20 num2 = 10 num3 = 2 print(result)

A variable is used before it is assigned a value.

32
New cards

What is wrong with the following code snippet?

((num1 + num2) * num3 / 2 * (1 - num4)


there is an extra parenthesis

33
New cards

Which statement correctly creates a new variable by combining the two string variables: firstName and lastName?

name = firstName + lastName

34
New cards

What is printed from the following code snippet:

message = "ho.."
print(message * 3)


ho..ho..ho..

35
New cards

What is printed by the following code snippet?

num = int("45") * float("1.5")
print(num)


67.5

36
New cards

What is the index value of the letter 'h' in the string below ?

message = "hello"


0

37
New cards

Assignment vs. Equality Testing

Assignment: makes something true

  • floor = 13

Equality testing: checks if something is true

  • if floor == 13


38
New cards

Which statement finds the last letter of the string variable name?

last = name[len(name)]

last = len(name) - 1

last = len(name)

last = name[len(name) - 1]

last = name[len(name) - 1]

39
New cards

Which username would be accepted as valid?

 

username = input("Enter username: ")

if " " in username or not username.endswith("3"):
    print("Invalid")
else:
    print("Valid")

 

A. truman 1839

 B. truman18393

 C. 1839Truman

 D. Tru18man39

 B. truman18393

40
New cards

What is printed by the following code snippet?

cost = 25.45378
print("%.2f" % cost)

 

A.) 25.45378

B.) %25.45

C.) 25.45

 D.) nothing, there is an error

C.) 25.45

41
New cards

What is the data type of the value returned by the input function?

string

42
New cards

What is the output of the following code snippet if count contains 56?

if count % 2 == 0 :
   print("Count is an even number")
else :
print("Count is an odd number")

A.) Count is an even number

B.) Count is an odd number

C.) Nothing, there is a syntax error

D.) Nothing, the program runs but does not print any output

C.) Nothing, there is a syntax error

43
New cards

What value causes the following logical expression to 'short-circuit'?

if temp >= 32 and temp <= 100


temp = 0

44
New cards

What string method can be used to determine if the string contained in the variable text only consists of numbers?

text.isalnum()

text.isalpha()

text.isdigit()

text.islower()

text.isdigit()

45
New cards

Using De Morgan's law, what is the equivalent to this statement?

if not (state == "PA" or state == "OH")


if state != "PA" and state != "OH"

46
New cards

Boolean Variables

it is often called a flag because it can be either up (true) or down (false)

there are two operators: and, or

  • they are used to combine multiple conditions


47
New cards

Combined Conditions: and

Both sides of the and must be true for the result to be true

ex:

if temp > 0 and temp < 100 :

print("Liquid")

48
New cards

Combined Conditions: or

We use or if only one of two conditions need to be true

ex:

if temp <= 0 or temp >= 100:

print("Not liquid")

49
New cards

The not operator: not

If you need to invert a boolean variable or comparison, precede it with not

ex:

if not attending or grade < 60 :

print("Drop?")

50
New cards

The not operator: inequality !

sed for the not when checking for inequality rather than negation.

Example inequality:

  • The password that the user entered is not equal to the password on file.

  • if userPassword != filePassword :


51
New cards

De Morgan’s law

tells you how to negate and and or conditions

52
New cards

A while loop that is controlled by a ____

counter

ex:

counter = 1 # Initialize the counter

while counter <= 10 : # Check the counter

print(counter)

counter = counter + 1 # Update the loop variable

53
New cards

Infinite Loops

The loop body will execute until the test condition becomes False

54
New cards

sentinel value

denotes the end of a data set, but it is not part of the data

55
New cards

Common Loop Algorithms - Average Example

total = 0.0

count = 0

inputStr = input("Enter value: ")

while inputStr != "" :

value = float(inputStr)

total = total + value

count = count + 1

inputStr = input("Enter value: ")


if count > 0 :

average = total / count

else :

average = 0.0

56
New cards

Common Loop Algorithms - Sum Example

total = 0.0

inputStr = input("Enter value: ")

while inputStr != "" :

value = float(inputStr)

total = total + value

inputStr = input("Enter value: ")

57
New cards

Uses of a for loop:

  • The for loop can be used to iterate over the contents of any container.

  • A container is is an object (Like a string) that contains or stores a collection of elements

  • A string is a container that stores the collection of characters in the string


58
New cards

For Loop - while version

stateName = "Virginia"

i = 0

while i < len(stateName) :

letter = stateName[i]

print(letter)

i = i + 1

59
New cards

For Loop - for version

stateName = "Virginia"

for letter in stateName :

print(letter)