Variables Flashcards

1
Q

What is a variable?

A

A way to store information to be referenced and manipulated in a computer program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do variables do?

A

Provide a way of labelling data with a descriptive name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the sole purpose of a variable?

A

To label and store data in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When naming a variable, what is considered good practice?

A

Making sure the name assigned to the variable is accurately descriptive and understandable to the reader

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would we get rid of the newline at the end of a string?

A

Add .chomp to the end of that string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does a newline character represent?

A

Hitting the enter key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does a variable’s scope determine?

A

Where in a program a variable is available for use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is a variable’s scope defined?

A

A variable’s scope is defined by where the variable is initialized or created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How is a variable’s scope defined in Ruby?

A

By a method definition or a block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a method?

A

A piece of reusable code that your program can execute many times during its run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does ‘methods have self contained scope’ mean?

A

Means only variables initialized within the method’s body can be referenced or modified within the method’s body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Are variables initialized within a method’s body available outside of it?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a block?

A

A piece of code that follows a method’s invocation, delimited by either curly braces or do/end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is one rule we need to emphasize with blocks?

A

Inner scope can access variables initialized in an outer space, but not vice versa

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we know if curly braces or do/end delimiting code is a block?

A

By seeing if they immediately follow a method invocation. If they do, it is a block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 5 types of variables?

A
  1. Constants
  2. Global variables
  3. Class variables
  4. Instance variables
  5. Local variables
17
Q

How are constants declared?

A

By capitalizing every letter in the variable’s name

18
Q

What are constants used for?

A

For storing data that never needs to change

19
Q

Can you change the value of a constant?

A

In most coding languages - no. In Ruby, yes.

20
Q

Can constants be declared in method definitions?

A

No. They are available throughout your application’s scopes

21
Q

How are global variables declared?

A

By starting the variable name with a $ (dollar sign)

22
Q

Where are global variables available?

A

Throughout your entire app, overriding all scope boundaries

23
Q

How do you declare a class variable?

A

By starting the variable name with @@

24
Q

Where are class variables accessible?

A

They are available by instances of your class, as well as the class itself

25
Q

When would you use a class variable?

A

When you need to declare a variable that is related to a class, but each instance of that class doesn’t need its own value for this variable

26
Q

Where do you initialize a class variable?

A

Must be initialized at the class level, outside of any method definitions

27
Q

How can you alter a class variable?

A

Using class or instance definitions

28
Q

How do you declare a instance variable?

A

By starting the variable name with @

29
Q

Can instance variables cross scope boundaries?

A

Some, but not all of them

30
Q

How do you declare a local variable?

A

Variable = data

31
Q

Which boundaries do local variables obey?

A

They obey all scope boundaries