Com Sci Exam 2

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/66

flashcard set

Earn XP

Description and Tags

Exam 2 will be on Monday October 28th. 30 Multiple Choice Questions IPO Charts - 2 Questions Binary Numbers - 7 Questions Greenfoot Chapter Notes - 14 questions Python Chapter Notes - 7 questions

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

67 Terms

1
New cards

What are objects in Greenfoot?

Many objects can be created from a class

2
New cards

What are methods in Greenfoot?

Objects have methods, invoking performs an action

3
New cards

What is an int in Greenfoot?

Integer, whole numbers (numbers without decimal points, can be positive or negative

4
New cards

What is boolean in Greenfoot?

True or False

5
New cards

What is the return type void() in Greenfoot?

Void means that no information is returned when it is invoked, they just carry out their action and then stop.

Methods with void return types are like commands, invoke turn left, and the wombat will turn left. ACTIONS

Methods without return types are usually just commands to the objects that make it do something

<p>Void means that no information is returned when it is invoked, they just carry out their action and then stop. </p><p>Methods with void return types are like commands, invoke turn left, and the wombat will turn left. ACTIONS </p><p>Methods without return types are usually just commands to the objects that make it do something</p>
6
New cards

What are non- void return types in Greenfoot?

Like questions- boolean canMove() is like asking the wombat “Can you move"?” and because it is boolean, it answers true or false (yes or no)

int getLeavesEaten() using this we get the information of how many leaves the wombat has eaten.

Methods with return types usually just tell us something about the object, but do not change the object

7
New cards

What are parameters in Greenfoot?

int getLeavesEaten() or void setDirection (int direction)

The parentheses after the method name hold the parameters list. This tells us whether the method requires additional information to run.

If there is nothing in the parentheses then it has an empty parameter list, which means it will just run, and does not expect a parameter.

If there is something in the parentheses then it expects parameters; additional information we need to provide. Every parameter is defined by two words, first the type then the name of it- void setDirection (int direction)

Shows us that it expects one parameter of type int which, specifies direction. At the top of the dialogue box, assuming commenting is done correctly it specifies more information. If we enter something that is not expected we get an error

8
New cards

What is a method signature in Greenfoot?

The description of return type, method name and parameter list

<p>The description of return type, method name and parameter list</p>
9
New cards

What are world classes in Greenfoot?

Always there by default. Every scenario will have at least one below it, in this case Crab World. Crab World is-a World. Also referred to by saying Crab World is a subclass of World

Unless we start without a scenario, Greenfoot automatically creates an object of the world subclass.

<p>Always there by default. Every scenario will have at least one below it, in this case Crab World. Crab World is-a World. Also referred to by saying Crab World is a subclass of World</p><p>Unless we start without a scenario, Greenfoot automatically creates an object of the world subclass. </p>
10
New cards

What are Actor Classes in Greenfoot?

Objects that can be placed into the world, for example animal, crab, lobsters, worms and pelicans

<p>Objects that can be placed into the world, for example animal, crab, lobsters, worms and pelicans</p>
11
New cards

What are subclasses and Superclasses in Greenfoot?

Subclass is a class that represents a specialization of another. In Greenfoot, this is shown with an arrow in the class diagram

Superclass is conversely to subclasses. We would say that Actor is a superclass to Animal.

Actor clas and animal have certain functions and methods that the Crab, lobster, worm and pelican inherit from them. So since they are below animal, they will inherit and methods or actions that are in the animal class.

12
New cards

What is a source code in Greenfoot?

Every class is defined by a source code. This code defines what objects of this class can do. We can look at the source code by opening the class’s editor.

13
New cards

What is compiling and compilation in Greenfoot?

Compiling- Make the change with an error, make the change correctly.

Compilation- Computers do not understand source code. It needs to be translated to machine code before it can be executed.

14
New cards

What is a method call in Greenfoot?

An instruction that tells an object to perform an action. The action is defined by a method of the object.

15
New cards

What is a parameter in Greenfoot?

Additional information can be passed to some methods within the parentheses, the value passed is called a parameter

16
New cards

What does it mean for something to be in sequence in Greenfoot?

Multiple instructions are executed in sequence, one after the other, in order in which they are written

17
New cards

What is an error message in Greenfoot?

When a class is compiled, the compiler checks to see whether there are any errors. If an error is found, an error message is displayed

18
New cards

True or False: A subclass inherits all the methods from its superclass. That means that is has, and can use, all the methods that its superclass defines?

True

19
New cards

What is an if-statement?

Can be used to write instructions that are executed only when a certain condition is true.

<p>Can be used to write instructions that are executed only when a certain condition is true. </p>
20
New cards

Which statement is NOT a valid Crab Class statement?

A. move(10);

B. turn(-10);

C. move(10.5);

D. turn(5×2);

E. move(-2×5);

move(10.5);

When you input move(10.5); an error comes up saying “incompatible types: possible lossy conversion from double to int” You cannot input decimal numbers since it will lose the decimal portion of the number. Since you can't hold the decimal in the integer variable, because it only allows whole numbers. It requires a whole number integer, not a decimal.

21
New cards

How can you make Crabs move randomly in Greenfoot?

getRandomNumber

<p>getRandomNumber</p>
22
New cards

What is dot notation in Greenfoot?

When methods are called that were defined in our own class (crab) or inherited (animal), just typing the method name and parameter list was enough. However, because it is a method that is from another class, not inherited from animal, we need to use a notation called dot notation. Since the getRandomNumber method is not in the Crab or Animal class, but in a class called Greenfoot, we write Greenfoot in front of the method call.

Greenfoot.getRandomNumber(20)

23
New cards

What does the parameters in Greenfoot.getRandomNumber(20) mean?

This will give us a random number between 0 and 20. the limit (20) is EXCLUDED, so the number is actually in the range of 0-19.

24
New cards

What are static methods in Greenfoot?

Methods may belong to objects or classes. When methods belong to a class we write:

class-name.method-name(parameters); to call the method

When a method belongs to an object, we write:

object.method-name(parameters); to call it

Both kinds of methods are defined in a class. The method signature tells us whether the method belongs to object of that class, or to the class itself.

Methods that belong to the class itself are marked with the keywords static at the beginning of the method signature. Ex. static in getRandomNumber(int limit)

25
New cards

What do these symbols mean in Greenfoot?

< > <= >= == !=

< less than

> greater than

<= less than or equal

>= greater than or equal

== equal

! = not equal

26
New cards

How do you make a new method in Greenfoot?

/**

*Check whether we have stumbled upon a worm #method description

*If we have, eat it. If not, do nothing #not read by computer, for humans only

/*

public void lookForWorm() #this is the new method

{

. if (canSee(Worm.class))

. {

. eat(Worm.class)

. }

}

The first four lines are a comment. A comment is ignored by the computer, but it is important to human readers. When we define this method, the code does not immediately get executed. We are just defining a new possible action (“looking for a worm”) that can be carried out later. It will only be carried out when this method is called.

<p>/**</p><p>*Check whether we have stumbled upon a worm #method description</p><p>*If we have, eat it. If not, do nothing #not read by computer, for humans only</p><p>/*</p><p>public void lookForWorm() #this is the new method</p><p>{</p><p>.         if (canSee(Worm.class))</p><p>.     {</p><p>.          eat(Worm.class)</p><p>.     } </p><p>}  </p><p>The first four lines are a comment. A comment is ignored by the computer, but it is important to human readers. When we define this method, the code does not immediately get executed. We are just defining a new possible action (“looking for a worm”) that can be carried out later. It will only be carried out when this method is called. </p>
27
New cards

How do you call a method in Greenfoot?

Calling it inside the act method

Ex. lookForWorm();

<p>Calling it inside the act method</p><p>Ex. lookForWorm(); </p>
28
New cards

What percentage of the time will the code after this if statement is executed?

if (Greenfoot.getRandomNumber (100) < 7)

A. 700%

B. 70%

C. 7%

D. None of the above

In the original code that I input it was if (Greenfoot.getRandomNumber (100) < 10). And the percentage of the time the crab would turn is 10%. So if it was 100 < 7 it would be 7% of the time. It's almost like a fraction 10 divided by 100 ,multiplied by 100 (for percent) is 10%, so 7 divided by 100 multiplied by 100 would be 7%.

29
New cards

What is API Documentation in Greenfoot?

Lists all classes and methods available in Greenfoot. Often need to look up methods here

30
New cards

What is a string in Greenfoot?

Piece of text (word/sentence), written in double quotes ('““)

“This is a string”

31
New cards

Why should you take a method with a lot of lines and break it up into smaller methods?

You should take a method with a lot of lines and break it up into smaller methods because it is easier to read and understand. Having a method with a lot of lines can become confusing and you may mess up your code. But breaking it up organizes it better and allows you to see each specific method piece in its own area.  

32
New cards

What is a constructor in Greenfoot?

Looks similar to a method but has some difference:

-Constructor has no return type specified between the keyword “public” and the name

-the name of a constructor is always the same as the name of the class

It is always automatically executed whenever an instance of this class is created

<p>Looks similar to a method but has some difference: </p><p>-Constructor has no return type specified between the keyword “public” and the name</p><p>-the name of a constructor is always the same as the name of the class</p><p>It is always automatically executed whenever an instance of this class is created</p>
33
New cards

How do you create new objects in Greenfoot?

Java objects can be created programmatically (from within your code) by using the new keyword Ex new GreenfootImage(“crab2.png”);

34
New cards

How do actors maintain their image in Greenfoot?

By holding an object of type GreenfootIMage. These are stored in an instance variable inherited from class Actor

35
New cards

What are instance variables in Greenfoot?

Also called fields, can be used to store information (objects, or values). Bit of memory that belongs to the object ( the instance of the class) Anything stored on it, will be remembered as long as the object exists.

Is declared in a class by writing the keyword private followed by the type of the variable and the variable name. private variable-type variable-name;

The type of the variable defines what we want to store on it. Always write instance variables declarations at the top of our class, BEFORE constructors and methods

<p>Also called fields, can be used to store information (objects, or values). Bit of memory that belongs to the object ( the instance of the class) Anything stored on it, will be remembered as long as the object exists. </p><p>Is declared in a class by writing the keyword private followed by the type of the variable and the variable name. private variable-type variable-name;</p><p>The type of the variable defines what we want to store on it. Always write instance variables declarations at the top of our class, BEFORE constructors and methods</p>
36
New cards

What is an assignment statement in Greenfoot?

Assigns an object or a value to a variable. Enables us to store something into a variable. Written with an equal symbol, variable = expression;

On the left is the name of the variable, on the right is what we want to store. “=” is the assignment symbol. Read it as “becomes”. Variable becomes expression.

image1 = new GreenfootImage(“crab.png”);

37
New cards

What is an if/else statement in Greenfoot?

Executes a segment of code when a given condition is true, and a different segment of code when it is false.

38
New cards

What is binary?

The binary numeral system, or base-2 number system, represents numeric values using two symbols: 0 and 1. More specifically, the usually base-2 system is positional notation with a radix of 2. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers.

39
New cards

How do you count in binary?

0000

0001, (rightmost digit starts over, and next digit is incremented)

0010, 0011, (rightmost two digits start over, and next digit is incremented)

0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit is incremented)

1000, 1001, 1011, 1100, ...

40
New cards

How do you convert binary to decimal?

Take the sum of the products of the binary digits and the powers of 2 which they represent. Ex 15 in binary is 1111

(1×2³) + (1×2²) + (1×2^1) + 1×2^0) = 8 +4+2+1 = 15

or 37 in binary is 100101

(1x2^5) + (0x2^4) + (0x2^3) + (1x2^2) + (0x2^1) + (1x2^0) =

(1x32) +(0x16)+(0x8)+(1x4)+(0x2)+(1x1)=

32+0+0+4+0+1=37

OR ANOTHER METHOD

1 0 0 1 1 0 1 1 to decimal

128, 64, 32, 16, 8, 4, 2, 1 (increments of 2 for each digit)

128 0 0 16 8 0 2 1 (write the number if its a 1 above, if its a zero write 0)

Add together for 155

<p>Take the sum of the products of the binary digits and the powers of 2 which they represent. Ex 15 in binary is 1111</p><p>(1×2³) + (1×2²) + (1×2^1) + 1×2^0) = 8 +4+2+1 = 15</p><p>or 37 in binary is 100101</p><p>(1x2^5) + (0x2^4) + (0x2^3) + (1x2^2) + (0x2^1) + (1x2^0) =</p><p>(1x32) +(0x16)+(0x8)+(1x4)+(0x2)+(1x1)=</p><p>32+0+0+4+0+1=37</p><p>OR ANOTHER METHOD</p><p>1      0    0    1  1  0  1 1 to decimal</p><p>128, 64, 32, 16, 8, 4, 2, 1 (increments of 2 for each digit) </p><p>128  0   0    16  8  0  2  1 (write the number if its a 1 above, if its a zero write 0)</p><p>Add together for 155</p>
41
New cards

How do you convert decimal to binary?

if its even it ends in 0, if its odd it ends in 1

For example 15 in binary is 1111

15/2 = 7 remainder of 1 —— rightmost digit

7/2 = 3 remainder 1 —— second rightmost digit

1/2= 0.5 gives us a 1 for the leftmost digit

Our quotient got to <1 so we stop, 1111

Or 16 in binary is 10000

16/2=8 remainder 0 -- rightmost digit

8/2= 4 remainder 0 -- second rightmost digit

4/2=2 remainder 0 -- third rightmost digit

2/2=1 remainder 0 -- fourth rightmost digit

1/2=0.5 gives us a 1 for our leftmost digit

Our quotient got to <1 so we stop. Result 10000

<p>if its even it ends in 0, if its odd it ends in 1</p><p>For example 15 in binary is 1111</p><p>15/2 = 7 remainder of 1 —— rightmost digit</p><p>7/2 = 3 remainder 1 —— second rightmost digit</p><p>1/2= 0.5 gives us a 1 for the leftmost digit</p><p>Our quotient got to &lt;1 so we stop, 1111</p><p>Or 16 in binary is 10000</p><p>16/2=8 remainder 0 -- rightmost digit</p><p>8/2= 4 remainder 0 -- second rightmost digit</p><p>4/2=2 remainder 0 -- third rightmost digit</p><p>2/2=1 remainder 0 -- fourth rightmost digit</p><p>1/2=0.5 gives us a 1 for our leftmost digit</p><p>Our quotient got to &lt;1 so we stop. Result 10000</p>
42
New cards

What is 11010011 as a decimal?

knowt flashcard image
43
New cards

What is 121 as a binary?

<p></p>
44
New cards

What are variables in Python?

Python uses dynamic variables, meaning the type of the variable can be assigned and changed at will. This also means that the type isnt checked until runtime, resulting in not needing to pre-define the type.

45
New cards

What are the data types in Python?

int: signed whole integer

float: floating point number (decimal)

string: text

booleans: True or False

46
New cards

How can you change data types in Python?

Putting the type you want to convert the variable you want to convert.

str()- changes item to a string

int() - changes item to an integer

float() - changes item to a floating point

Ex. num = 12

num = str(num)

num = float(num)

<p>Putting the type you want to convert the variable you want to convert. </p><p>str()- changes item to a string</p><p>int() - changes item to an integer</p><p>float() - changes item to a floating point</p><p>Ex. num = 12</p><p>num = str(num) </p><p>num = float(num) </p>
47
New cards

What are the logical operators in Python?

== equal to statement (same as Java)

!= not equal to statement (same as Java)

or statement (Like | | in Java)

and statement (Like && in Java)

48
New cards

How do you get inputs in Python?

The primary method of user input is “input([prompt])”

myVariable = input(“Please enter your answer: “)

Whatever you enter as input, the input() function converts it into a string. If you enter an integer value, it will still convert it into a string. If you want to accept a number input from a user, you need to perform type conversion on the input value.

<p>The primary method of user input is “input([prompt])”</p><p>myVariable = input(“Please enter your answer: “) </p><p>Whatever you enter as input, the input() function converts it into a string. If you enter an integer value, it will still convert it into a string. If you want to accept a number input from a user, you need to perform type conversion on the input value. </p>
49
New cards

How do you output in Python?

The primary method of output in Python is the “print()” function. this is very similar to “System.out.printIn()” in Java, the drawString() method in Greenfoot, and the other various speech tiles in Scratch.

print(“This is a string”) or put it into a variable

myVariable = “This is a string variable”

print(myVariable)

<p>The primary method of output in Python is the “print()” function. this is very similar to “System.out.printIn()” in Java, the drawString() method in Greenfoot, and the other various speech tiles in Scratch. </p><p>print(“This is a string”) or put it into a variable</p><p>myVariable = “This is a string variable” </p><p>print(myVariable) </p>
50
New cards

What does \n and \t and + do in Python?

\n creates a new line within the string

\t creates an indent in the string

+ concatenate two strings

51
New cards

How do you end a program?

Once it reaches the bottom of your code, the program will stop/terminate. However, sometimes within a loop, or condition, we want to stop the program sooner. We ca use the exit() function to do so. this operates like the stop(all) tiles in Scratch.

52
New cards

What is an IPO chart?

Identifies a program’s inputs, its output, and the processing steps required to transform the input into the outputs.

I: Input- The information, ideas, and resources used in creating a program

P: Processing- Actions taken upon/using input or stored material

O: Output- Results of the processing that then exit the system

S: Storage- Location(s) where materials inside the system is/are placed for possible use at a later point in time.

Things to consider:

- The text only version of the IPO chart should be made in such a way that it is

general enough that it could work with any user inputs.

- The input section cannot have any math done in it, it is just the raw data inputted

- Any math that needs to be done, should be done and shown in the process section of the chart. Because that is “processing” the data

- If you have multiple calculations as part of your process,

- The output section is reserved for just the one output of the processed information, no further calculations are done here.

<p>Identifies a program’s inputs, its output, and the processing steps required to transform the input into the outputs. </p><p>I: Input- The information, ideas, and resources used in creating a program</p><p>P: Processing- Actions taken upon/using input or stored material</p><p>O: Output- Results of the processing that then exit the system</p><p>S: Storage- Location(s) where materials inside the system is/are placed for possible use at a later point in time. </p><p>Things to consider: </p><p>- The text only version of the IPO chart should be made in such a way that it is</p><p>general enough that it could work with any user inputs.</p><p>- The input section cannot have any math done in it, it is just the raw data inputted</p><p>- Any math that needs to be done, should be done and shown in the process section of the chart. Because that is “processing” the data</p><p>- If you have multiple calculations as part of your process,</p><p>- The output section is reserved for just the one output of the processed information, no further calculations are done here.</p>
53
New cards

What are Python Functions?

Can be used to break down a program into many different, useful parts. if we intend on using the same algorithm in multiple places, a function is likely the best option. However, unlike Java, Python functions do not require a privacy or type reference.

54
New cards

How to do write a Python Function?

Initialize a function using the “def” term

def helloWorld():

. print(“hello world!”)

Can also give a defined function a return value

def myFunction():

. num = 12

. return num

print(myFunction())

Can also set a variable using a return from a function

def question():

. a = input(“what is your lucky number? “)

. return a

luckyNumber = question()

print(luckyNumber + “ is a great lucky number!”

MUST be defined prior to being called (defined higher up in the sequential order of the code)

55
New cards

What are parameters in Python?

Like in Java, we can also pass parameters into a function. To do this, we must define the parameter names in the function definition

def concatenate(strin1, string2):

. newString = string1 + string2

. print(newString)

concatenate(“arch”, “bishop”) so we get archbishop

56
New cards

What does random.randint() do in Python?

Produces a random number between a and b (inclusive of a and b)

<p>Produces a random number between a and b (inclusive of a and b) </p>
57
New cards

What does random.random() do in Python?

Produces a random floating point number between 0 and 1 (exclusive)

<p>Produces a random floating point number between 0 and 1 (exclusive) </p>
58
New cards

What does random.uniform(a, b) do in Python?

Produces a floating point between a and b (inclusive of a and b)

<p>Produces a floating point between a and b (inclusive of a and b) </p>
59
New cards

What are while loops in Python?

Formatted similarly to the While loops in Java. they still require the same 3 essential components (counter, condition, increment). The differences are similar to those of using if statements in Python vs Java. We need a : after our condition and indentation of the body instead of curly brackets.

While(condition) repeats until condition is FALSE

<p>Formatted similarly to the While loops in Java. they still require the same 3 essential components (counter, condition, increment). The differences are similar to those of using if statements in Python vs Java. We need a : after our condition and indentation of the body instead of curly brackets. </p><p>While(condition) repeats until condition is FALSE </p>
60
New cards

What is elif in Python?

A way of saying “if the previous conditions were not meant, then try this condition” The else is not required here, however it is not a bad idea to include one as a “catch all” if none of our conditions are met. There can be multiple elif statements included as part of 1 block of code. Covering several specific conditions

<p>A way of saying “if the previous conditions were not meant, then try this condition” The else is not required here, however it is not a bad idea to include one as a “catch all” if none of our conditions are met. There can be multiple elif statements included as part of 1 block of code. Covering several specific conditions</p>
61
New cards

Can you write an elif statement any other way or can you write it differently?

Yes you can write it another way. Instead of having multiple elif statements in one block of code, you can divide it into multiple if statements.

<p>Yes you can write it another way. Instead of having multiple elif statements in one block of code, you can divide it into multiple if statements. </p>
62
New cards

What is possible downside of writing elif statements in seperate if statements in Python?

All three if statements will run(each if statement considered its own block of code) which requires more computing power. In the previous example, as soon as 1 condition is met, the program does not continue checking the other conditions. Furthermore, if we want to make sure that only 1 possible case is executed, keeping it in the if/elif/else format (which is considered one block of code) is a better practice.

63
New cards

What are global variables in Python?

Variables that are created outside of a function are known as global variables. Can be used by everyone, both inside and outside of functions.

<p>Variables that are created outside of a function are known as global variables. Can be used by everyone, both inside and outside of functions. </p>
64
New cards

What happens if you create a variable with the same name inside a function in Python?

This variable will be local and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.

<p>This variable will be local and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value. </p>
65
New cards

What is the global keyword in Python?

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword. Also use the global keyword if you want to change a global variable inside a function. This is similar to using “this” keyword in Java.

<p>Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword. Also use the global keyword if you want to change a global variable inside a function. This is similar to using “this” keyword in Java. </p>
66
New cards

How do you change the value of a global variable inside a function?

Refer to the variable using the global keyword.

<p>Refer to the variable using the global keyword. </p>
67
New cards

REVIEW YOUR GREENFOOT AND PYTHON PROJECTS TO KNOW HOW EVERYTHING PIECES TOGETHER

You got this!!