CGS 3465 Final Exam

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

__________ variables are declared outside of all functions

"global" variables

________ variables are declared within functions

"local" variables

What is a string in Python

1 or more characters placed in a sequence is considered to be an "ascii text" string, or just "string" for short.

what is 12 decimal written as binary? what is 7 decimal written as binary?

1100 and 111

what is binary 1101 in decimal what is binary 101 in decimal?

13 and 5

According to lectures, when we change the value of a variable, 1) does the interpreter accomplish this by changing the value at the place pointed to by the variable, or 2) does the interpreter accomplish this by moving the pointer over to point to another place in memory that has the new desired value.

2nd way is correct

What is the output of the following code? (I suggest you do it by drawing arrows b/c you won't have idle when taking the final.) X = 7 Y = 4 X = Y Y = 10 print(X)

4 is printed out

If there are 12 months in the year, then how many bits of memory would be required to represent what "month" a student is born in?

4 bits b/c: 4 is 2 to the fourth is 16 (enough) and 2 to the third is only 8 (not enough)

What is a variable in Python?

A NAME OF A POINTER TO A VALUE A pointer to a value

What is a pointer?

A pointer, in Python, is a memory location which holds an address/name of some other memory location. This "other" location is assumed to hold something we are interested in.

What is a computer program, and what is a computer programming language

A program is a list of commands to a computer/microwave/car, etc. A computer programming language is a set of rules and conventions for making such programs.

What makes a programming language "higher" than another

A programming language is "higher" than another if the language allows you to be more human like (English/Spanish) , and allows you to do more in one step or command.

what is an operating system

A software which allows the computer to interact with the user, so that the user can do such things as delete files, start up Python, etc.

Given the code AA = [] write code with the append function to make AA become the list with 2 elements, both equal to 2

AA = [] AA.append(2) AA.append(2)

what is an IDE.

An IDE is short for interactive development environment and is a tool to create and run programs.

when you call a function, the variables or values sent to the function are called __________.

Arguments

What is one thing the split command is great for doing?

Extracting/parsing data from a string that uses delimiters for separating data items

What is the "closing" of a file?

Getting any remaining writes done and breaking the link between memory/program and the file on disk.

Why would a variable show up as __variable__?

If you do not input the variable name, Python interpreter will name it and put the double underscore ( __ ) before and after it to say "don't mess with this"

What does it mean that the print function is polymorphic?

It changes according to the types of things sent it.

What is the most defining feature of "assembly" code as compared to higher level languages

It is a one to one association between a statement of machine code and a statement in assembly.

According to lectures, what happens when a variable (all of it, not a part of it) is assigned a value?

It means the new value is put into some place of memory, and then the pointer value of the variable is changed to now point to this new place.

In python, what other programming languages call "libraries" refer to the __________ of python

Modules

is it normal to execute pseudo code, yes or no

No.

On a "read only" file, can you read, write, or append the file?

ONLY read from it, cannot write to it.

what is "volatile" memory or "random access" memory or "RAM"?

Places in the computer to hold information likely to get reset/lost when the program that uses it stops, and certainly will when computer is turned off.

True/ False When we call a function, we can send function names AS WELL AS variable names in our call

TRUE.

Given the code TheList = [2,3,4] what would the statemet print(TheList[2],len(TheList)) output?

The answer is 4 3

What is one advantage of Pythons indention policy over C's use of brackets to show what code belongs within an if, etc,.

The big advantage is that then both the interpreter/computer decides what code goes with what based on the same way as a human reading the program. (in other words, humans and computer see indention in python, not just the humans as in C)

What is the difference between a compiled and interpreted style of language processing?

The compiled style is to go through the whole program, converting the program to an equivalent program in another language (usually machine code). And the interpreted style is to make no copy of the original program into another language just goes though the steps doing the actions since it can.

what exactly does the CPU do?

The cpu runs programs, analogous to our trains of thought, which determine what equipment does what, much as our brain runs trains of thought which cause our arms and legs to move. By running programs, the cpu bosses around the rest of the computer

What is a machine language

The only language a cpu understands, it is a language created specifically for that cpu, that takes into account the register type, what the cpu is like, etc. The instructions are encoded as binary numbers so that it works well with the circuitry.

True or False: a boolean variable takes on values "True or False" and a string takes on values like "wwwww"

True

True or False: According to lectures, is it possible for roundoff errors to cause problems with equality with real numbers

True

True or False: When doing expressions like (5 + 5.0) the Python interpreter first turns the integer 5 into a float

True

True or False?: If I turned the following Psuedo Code into a flow chart read in a language if language equals Spanish print "Badbunny says "buen día" " else if language equals English print "Paul McCarntney says "good day" " else if language equals French print "Alizee says "bonne journee" " would I use a diamonds for the various ifs and then a slanted parallelogram for the "read in a language"

True

True/False Two common word sizes are 32 and 64 bits, and of course the larger word size allows greater precision and greater range for values stored.

True

True/false A variable can be thought of as a name which points to a value

True

True/False using rstrip, it returns the string minus blanks/invisible chars from the end (right side) of the string

True Useful for taking out "end of liners" or random spaces

True/False using lstrip, it returns the string minus blanks/invisible chars before the non-blank character.

True Using lstrip takes only the characters after any blanks on the left/starting side to use in the string

True/False "Opening" a file is to make a certain file on disk available to the program by means of creating a "file object" in memory that holds info necessary for reading/writing to that file. When the program is done with the file, it updates and "closes" the file with the file object going to garbage collection.

True. note the difference between the file and file object

Describe the difference between using = and == when creating if statements.

Use == for equals within the "condition" part of an if statement; do not use = which means "gets" within the "condition" part of an if statement (sets a value to call upon the value of the named variable)

When does the bubble sort stop its process of sorting and swapping list items?

When it goes through the list and there are no swaps to be made, it is done.

With an "append mode" on a file, can you read, write, or append the end of the file?

With append mode, you can only add to the end of the file.

Can we change a variable's type at runtime?

Yes

Does Python's "a variable is a pointer to a value" idea make it easier for variables to change size?

Yes

Yes or No. Does a file object get matched to a file on disk when a file is opened? AND does a file object for a file go away when its corresponding file is closed?

Yes to both, a file object is created while working on a file, then the file is updated at closing and the file object goes to garbage.

Is it true that a variable is a pointer to a value?

Yes, True.

When using the split command can you tell Python what you want the delimiter between the strings to be?

Yes, by saying string1 = string.split(",") you can split the given string based on the commas within it

Is __333 a legal variable name?

Yes, it is.

Are integer, float, string, and boolean all data types in Python?

Yes.

On a "write only" file, can you read, write, or append the file?

You can write to it, but cannot read from it.

Which of the following are legal variable names?: __wwww aaa_aaa_aaa aaa_ee9 9ee

__wwww - yes aaa_aaa_aaa - yes aaa_ee9 - yes 9ee - no

What is a drive?

a "secondary" storage device which can be inside or outside the computer, that holds data in a non-volatile way (sticks around even when computer is off).

What size of memory is the least amount of memory that has an address/name that machine code can refer to?

a "word" of memory

given that that string "123.4" can be thought of as a floating point number, what do we call the process of converting that float in string form to a binary floating point number used by the CPU to do math.

a cast or casting of the string from a string to float.

A _______________ _______ is information we have stored on the computer, generally a disk / flash drive, or CD, etc.

a computer file

Which term (hardware or software) do the following belong to: a printer the computer case Mac OS IDLE a cable

a printer - hardware the computer case h hardware Mac OS - software IDLE - software a cable - hardware

Suppose there are 100 students in a class, and a Python program used by the professor has a variable NumStudents, which is stored at memory location 5, and in this 5th word of memory is value 15, and at word 15 is the value 100. Then we could say NumStudents is a pointer to a value, the value 100, which is the number of students in the class. Is this what I mean when I say a variable in Python is a pointer to a value?

a) Yes is correct

Consider the code X = 3 Y = X X = 7 which is the correct arrow diagram for the above code a) X -> 3 and then X -> 3 <- Y and then X ->7 Y -> 3 b) X - > 3 and then Y -> X -> 3 and then X -> Y -> 3

a) is correct

Which of the following is a better representation for the following two statements of code: X = 2 X = "aaa" a) X --> 2 goes to X 2 | V "aaa" b) X --> 2 goes to X --> "aaa"

a) is correct because the pointer to the value changes, not the originial value

which of a) or b) represents the following two statements of code: X = 2 Y = X a) X --> 2 goes to X --> 2 <-- Y b) X --> 2 goes to Y --> X --> 2

a) is the correct answer

If you were editing a Python program and accidentally deleted a ":", would that cause a syntax or a logic/semantic error?

a) syntax error is right

True or False. In a computer program, a constant is usually something like PI, NUM_DAYS_IN_WEEK, NUM_INCHES_IN_YARD, etc., values that make the program more English like and are unlikely to change within the program.

a) true is correct

What size of memory does a computer typically use to store an integer for floating point value?

again a word of memory even though it is not uncommon to use two words for greater accuracy on floats, and greater range for ints.

without using the append function, use an assignment statement to create a variable aaa which is eaual to an empty list.

answer aaa=[]

Given code astring = " James , Lamm : 12 , 19, 2021 " write some lines of code using two "split" commands together with the "strip", "lower" commands together with "int" casts which assign variables FN LN Month Day Year MonthPlusDay to james lamm 12 19 2021 31 respectively. Note that you need to convert the Month and Day to ints in order to add them up and put the result in variable MonthPlusDay. Also note that lower should be used to take out the caps.

answer astring = " James , Lamm : 12 , 19, 2021 " name, date = astring.split(":") FN, LN = name.split(",") Month, Day, Year = date.split(",") FN = FN.strip() LN = LN.strip() FN = FN.lower() LN = LN.lower() Day=int(Day) Month=int(Month) Year=int(Year) MonthPlusDay=Day+Month print(FN) print(LN) print(Day) print(Month) print(Year) print(MonthPlusDay)

what is the output of print("ddddddd\nnddd")

answer ddddddd nddd

Building on what you did for 19) Now add a second for loop that prints out Names[i],Quiz1Grades[i],Quiz2Grades[i],AveGrades[i] all on one line, for the 2 indexes In other words, the total code should result in Dan 70 60 65.0 Sally 56 62 59.0 getting printed out

answer for i in range(len(Quiz1Grades)): print(Names[i],Quiz1Grades[i],Quiz2Grades[i],AveGrades[i])

Now add to aaa of question 17) the elements 1-500 using a while loop and the append function. This is to say that index 0 of aaa should be 1, index 1 index should be 2, etc., up to the 499 index which should be 500 . Then, after this first while loop, write a second while loop that prints what is in aaa, one element per line, so that you get the following output: aaa 1 aaa 2 aaa 3 . . . aaa 500

answer aaa=[] i=1 while i< 501: aaa.append(i) i = i + 1 i=1 while i < 501: print("aaa",aaa[i-1]) i= i+ 1

what is the output of print(1,2,3)

answer is 1 2 3

what is the output of print(1,2,3,sep="")\

answer is 123

What is the output of b=int(5.8) print(b)

answer is 5

what it the output of print("aaaaa",end="");print("bbb",end="");print("c");print("d")

answer is: aaaaabbbc d

Given the starting code Names =["Dan","Sally"] Quiz1Grades=[70,56] Quiz2Grades=[60,62] AveGrades=[] write two lines of code which use a for loop and the append function to set AveGrades to a list where for each indes, i, AveGrades[i] is (Quiz1Grades[i] + Quiz2Grades[i])/2.0 again, use the append function to do this

answer: for i in range(len(Quiz1Grades)): AveGrades.append( (Quiz1Grades[i]+Quiz2Grades[i])/2)

What is a popular convention set that associates 8 bit binary bit patterns to different characters?

ascii

Would you expect = or == to be used in the expression of an if statement? a) = b) ==

b) == is correct

Now how would the memory X points at change if the program executed the statement X=int(X) a) X ---> Bad Bunny b) X ---> 00000100 c) X ---> 00110100

b) X ---> 00000100 is right

Consider the code X = 4 X = 5 which is the correct arrow diagram for the above code a) X -> 4 and then X -> 5 (where the 5 replaces the 4) b) X -> 4 and then X-> 5 (where the 4 is still in memory, at least till possibily garbage collected)

b) is correct

Consider the code def fff(a): a = 7 b = 8 fff(b) print("aa") which is the correct arrow diagram for the above code a) b from main -> 8 and then parameter a of function fff -> 8 <- b from main and then b from main --> 7 <-- parameter a of function fff and then parameter a of function fff is gone since function fff ended aa gets printed out b) b from main -> 8 and then parameter a of function fff -> 8 <- b from main and then b from main --> 8 and parameter a of function fff --> 7 and then parameter a of function fff is gone since function fff ended aa gets printed out

b) is correct

How does a bubble sort work in sorting a list? Does it a) go through all of the items in a list at once to reorganize them or b) go individually by adjacent items and compare them?

b) is correct

If a program asks the user for an integer, and the user responds with "dfdfdfdf" and this causes the program to bomb, would this be a syntax or logic/semantic error?

b) logic/semantic error is right

Fill in the blank A molecule of water is to water what a ______ of memory is to memory.

bit

Multiple choice. Given that the 8 bit pattern 00110100 is the ascii representation for the character "4", and assuming we have a computer with only 8 bit words, what would the memory X points at look like if we executed the statement? X = input("type in an int ") and the user entered a 4 and then pressed enter? a) X ---> Bad Bunny b) X ---> 00000100 c) X ---> 00110100

c) X ---> 00110100 is right

a ______ ___________ is a python type of data. It is designed by the language designed and interpreter writer to contain all data needed to open, close, change, etc a particular file on a disk or drive.

file OBJECT

for int variable TEMP write code using some or all of these statements "if","elif", "and", and "else" which prints "too low" if TEMP less than 96 prints "fine" if TEMP is greater than or equal to 96 and less than 100 prints "fever" if TEMP is greater than or equal to 100 and less than 103 prints "call an ambulance" if TEMP is greater than or equal to 103

if (TEMP < 96): print("too low") elif (TEMP >= 96) and (TEMP <100): print("fine") elif (TEMP >= 100) and (TEMP <103): print("fever") else: print("call an ambulance") OR this would also do: if (TEMP < 96): print("too low") elif (TEMP <100): print("fine") elif (TEMP <103): print("fever") else: print("call an ambulance")

What happens or can happen to a value in memory if things change and there is no variable pointing to it anymore?

it gets garbage collected

a declared variable is nonlocal when a function afunc contains another function afuncinafunc. Is the variable nonlocal from the afuncinafunc's perspective or from the afunc's perspective?

it is nonlocal from the afuncinafunc's perspective

what is a path variable

lists of (folders/locations) which the computer uses to search for stuff.

What was the first computer, the eniac, developed to do ?

make war (be used for military warfare)

is XXX the same variable as xxx

no. (Python is case sensitive for variables)

Why does the following code bomb (assuming I enter a 6 at the prompt) a=input("enter an int") print(a+1)

official output is TypeError: can only concatenate str (not "int") to str or you could say forgot to turn the string into an int before adding to it

How many ways are there to declare a variable in Python as expressed in class lectures

only one

What is the output of the following code. (try to do with arrows at first, then prove with idle) def addsTen(T): T = T + 10 X=5 addsTen(X) print(X)

the correct answer is 5

What defines an immutable variable?

the values of these variables are hard to change because they have no subpointers; their arrow pointers don't change the value pointed at, only what the argument passes as the variable

Using the code string.lower() does what to the output of the string that is wanted to be printed

using lower() outputs the characters all in lowercase

What is the output of print("v\\n\\n\\n\\nrr\nj")

v\n\n\n\nrr nj

Is it reasonable to store an ascii character value in a byte?

yes, done all the time

Yes or No. According to lectures are Python file objects and files on disk different things?

yes, they are different.

According to lectures, are constants and variables declared the same way in Python?

yes. There is only one way to define a variable in python


Ensembles d'études connexes

Unit 1 - Chapter 2 AP Computer Science

View Set

Module 2: Introduction to vSphere and the Software-Defined Data Center

View Set

CH. 6. S. 3. How Is Intelligence Measured

View Set

CH. 4- Growth and Development of the Toddler

View Set