Variables & Methods Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do you define a variable?

A

variable_name = value

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

How do I reassign the value of:

variable_name = value

A

variable_name = other_value

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

What does snake case look like?

A

this_is_snake_case

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

What does camel case look like?

A

thisIsCamelCase

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

True or False: Variables can be written in both snake case and in camel case.

A

True

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

True or False: Variable names should start with a lowercase letter.

A

True

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

True or False: This is an acceptable name for a variable: 1st_place

A

False

Variable names should never start with a number.

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

True or False: This is an acceptable name for a variable: end

A

False

end is a reserved word in Ruby, meaning that it is used for other purposes and cannot be used as a variable name.

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

True or False: This is an acceptable name for a variable: danny’s_age

A

False

Apostrophes are not allowed in variable names.

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

name=”Richard”
How do I output the following using string interpolation?
“Hello, my name is Richard!”

A

puts “Hello, my name is #{name}!”

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

What is the difference between puts and print?

A

puts will output your code and will enter a new line. print will output your code but will not enter a new line.

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

How would you define a method named greeting?

A
def greeting
  #your code here
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you define a method named greeting with the argument of name?

A
def greeting(name)
  #your code here
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you invoke a method?

A

greeting(“Richard”)

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

True or False: A method can have multiple arguments.

A

True
Ex:
def greeting(name, date)
end

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

How do you write an optional argument in a method?

A
def greeting(name="Ruby programmer")
  #your code here
end
17
Q

What does an optional argument do?

A
If a method is called and no argument is given, it will default to the default value given. This is good when you want to avoid errors.
Ex:
def greeting(name="Ruby programmer")
  puts "Hello, #{name}!"
end
greeting("Richard")
#Will output "Hello, Richard!"
greeting()
#Will output "Hello, Ruby programmer!"
18
Q

True or False: You can use both optional and required arguments for the same method.

A

True
Ex:
def greeting(name, age=”a secret”)
end

19
Q

True or False: It is okay to define optional arguments before required arguments in a method.

A

False
Ex:
def greeting (name=”Ruby programmer”, age)
puts “Hello, my name is #{name} and my age is #{age}!”
end
#greeting(24)
#This will error out since the method will think name is 24 and age is not defined

def greeting(age, name="Ruby programmer")
  puts "Hello, my name is #{name} and my age is #{age}!"
end
#greeting(24)
#Will come out as "Hello, my name is Ruby programmer and my age is 24!"
#Should be written like this
20
Q

How is return different from puts/print?

A

puts/print can show you the value but return will actually give you the value.
Ex:
#puts/print
def greeting
puts “Hello there!”
end
#Will output “Hello there!” but value will be nil

#return
def greeting
  "Hello there!"
end
#Value will be "Hello there!"