CSIS 150 exam 2

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/74

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

75 Terms

1
New cards

What will the output of the following code be?

x = 7
if x > 10:
    print("Greater than 10")
elif x > 5:
    print("Greater than 5 but less than or equal to 10")
else:
    print("5 or less")

greater than 5 but less then or equal to 10 

2
New cards

What will the following code output?

x = 8
y = 12
if x < 10 and y > 10:
    print("Condition met")
else:
    print("Condition not met")

condition met 

3
New cards

will this evaluate to True or False: 5>3 and not (6<1)

True

4
New cards

What will the following code print?

x = 21
if x % 2 == 0:
    print("x is even")
else:
    if x % 3 == 0:
        print("x is magically odd")
    else:
        print("x is odd")

x is magically odd

5
New cards

(Challenging) What will the following code output?

a = 4
b = 15
if (a % 2 == 0 or b % 2 == 0) and not (a > 10 or b < 5):
    print("Condition 1")
elif a * b > 40 and (a % 3 == 0 or b % 3 == 0):
    print("Condition 2")
else:
    print("Condition 3")

Condition 1 

6
New cards

which list method will add a single element to the end of the list

append()

7
New cards

What will be the output of the following code?

my_string = "Hello"
new_string = ""
for i, char in enumerate(my_string):
    if i % 2 == 0:
        new_string += char.upper()
    else:
        new_string += char.lower()
print(new_string)

HeLlO

8
New cards

What will be the result of running this code?

numbers = [1, 2, 3, 4]
numbers.append([5, 6])
print(numbers)

[1, 2, 3, 4, [5, 6]]

9
New cards

Read this code  and determine the output of running this code?

my_list = [1, 2, 3]
new_list = my_list
new_list[2] = 6
print(my_list)

[1, 2, 6]

10
New cards

(Challenging) Read this code carefully and determine the output of running this code?

my_list = [1, 2, 3]
new_list = my_list
new_list = [4, 5, 6]
print(my_list)

[1, 2, 3]

11
New cards

what is the primary different between a for loop and a while loop

A for loop runs a fixed number of times, while a while loop runs indefinitely until a condition becomes false

12
New cards

which of the following conditions would case a while loop to execute indefinitely

while true with no break statement and while x > 0 where x is never modified 

13
New cards

when does the break statement exit a loop

immediately upon encountering the break statement

14
New cards

What happens when the continue statement is executed in a while loop?

The loop continues with the next iteration, skipping the rest of the current iteration.

15
New cards

What is the output when the following code executes?

x = 0
while x < 5:
    if x == 2:
        break
    x += 1
print(x)

2

16
New cards

what will the following code do?

x = 10
while x > 0:
    x -= 2
    if x % 3 == 0:
        continue
    print(x)

prints all values of x as it decrease by 2, skipping the values where x is divisible by 3

17
New cards

which Path method can be used to check if a Path object exists as a file or directory in the file system

exists()

18
New cards

Given the following code, what does it return?

from pathlib import Path
p = Path('data.txt')
print(p.is_dir())

False if data.txt is a file and true if data.txt is a directory

19
New cards

Given the following code, what will be printed?

from Pathlib import Path

testfile = Path('example.txt')
with open(testfile, 'w') as f:
    f.write('Hello, World!')
with open(testfile, 'r') as f:
    print(f.read())

‘Hello, World!’

20
New cards

which of the following is a correct way to open a file for reading in Python

file = open(‘example.txt’, ‘r’)

21
New cards

which file handle method lets you read all the lines from a file into a list 

readlines()

22
New cards

What does the with context manager do when working with files? (Select all correct answers)

it ensures the file is properly closed after its context block of code finishes and it opens the file 

23
New cards

what is the correct syntax for defining a function that takes parameters in python

def name(parameters)

24
New cards

what does the return statement do in a function

it stops the function and returns a value 

25
New cards

What will the following code output?

def greet():
    return "Hello, World!"


greet()

nothing (it outputs nothing) 

26
New cards

boolean

expressions either evaluate to True or False 

27
New cards

True in boolean

1

28
New cards

Flase in boolean

0

29
New cards

do strings evaluate to true or false in boolean

true

30
New cards

do empty strings and set evaluate to true or false in boolean

False

31
New cards

equality operator 

== - checks if the values are the same (true) or different (false)

32
New cards

inequality operator

! =, checks values not equal

33
New cards

assignment operator

=, makes x equal to a value

34
New cards

short circuit evaluation

or- only looks till a true, and- only looks till there is a false

35
New cards

in and not in operators

case sensitive and returns true or false

36
New cards

are == and ! = before or after not, and, or

before

37
New cards

what order do and, not, or evaluate in

not, and, or

38
New cards

nested conditionals

if statement inside an if statement

39
New cards

chained conditionals

elif statements

40
New cards

nested vs chained conditionals which is better 

based on readability 

41
New cards

mutable

can be changed after it is created, list

42
New cards

immutable

cannot be changed after it is created, strings, tuples

43
New cards

object

something a variable can refer to in memory

44
New cards

reference

when you assign the variable to an object and you create a reference to that object

45
New cards

Id() function 

shows the memory location of the variable 

46
New cards

is operator

returns true if they have the same memory location and returns false if they dont

47
New cards

aliasing

if two variables refer to the same thing, changing one can change both

48
New cards

cloning

making a copy of a list to not mess up the OG list

49
New cards

slice operator

the best way to copy a list [:]- takes the whole list

50
New cards

.copy()

copies to a new object, same id

51
New cards

.append()

adds the argument to the end of the list

52
New cards

.insert()

adds the second argument in the index indicated by the first argument .insert(1,2) adds 2 in the first spot 

53
New cards

.reverse()

reverses the order of the list in place

54
New cards

.sort()

sorts the list items in place (numerical or alphabetical)

55
New cards

.remove()

removes an item at the given index

56
New cards

.pop()

returns and removes the value of the last item in the list 

57
New cards

why is this bad code 

sorted_list = orginal_list.sort()

it destroys the list as the sort function returns nothing 

58
New cards

what is the difference between append and + when talking about lists

append combines elements and + adds the list together [1,2,3[4,5,6]] vs [1,2,3,4,5,6]

59
New cards

.sorted()

creates a copy of the sorted list

60
New cards

why are string methods not mutable

cause strings are not mutable

61
New cards

.split()

returns list of strings based on your parameters

62
New cards

.upper()

all uppercase

63
New cards

.lower()

all lowercase

64
New cards

.strip()

removes the white space (tabs, /n)

65
New cards

.replace()

replaces all occurrences of a substring with a new substring

66
New cards

example of .format()

greeting = 'hello my name is {} and i am a {}'. format(name,job)

print(greetings)

67
New cards

why should you not change the length of a list when iterating over it 

can lead to skipping values or an infinite loop 

68
New cards

definite iteration 

stops at a certain number 

69
New cards

indefinite iteration

keeps going ‘forever’ till it reaches a stop (false)

70
New cards

infinite loops

a while loop that will never become false

71
New cards

listener loop

repeat indefinitely till a particular input is given- make sure use input is valid

72
New cards

sentinel value

special value that signals the end of a loop

73
New cards
74
New cards
75
New cards