Professional Documents
Culture Documents
RB109 Written Assessment Study Problems
RB109 Written Assessment Study Problems
● What does the code output? What are the return values?
● Answer the why behind the output/return:
○ Focus only on the lines of code that deliver the output and return values.
● Summarize what the problem demonstrates and why: This problem demonstrates
local variable scope/etc…
○ This can be at the beginning or end of your answer - personal preference.
Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = 'Hello
b = a
a = 'Goodbye'
Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = 4
loop do
a = 5
b = 3
reak
b
end
puts a
puts b
Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = 4
b = 2
loop do
c = 3
a = c
break
end
puts a
puts b
Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def example(str)
i = 3
loop do
puts str
i -= 1
break if i == 0
end
end
example('hello')
Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def greetings(str)
puts str
puts "Goodbye"
end
word = "Hello"
greetings(word)
Problem link
Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4]
counter = 0
sum = 0
loop do
sum += arr[counter]
counter += 1
break if counter == arr.size
end
Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = 'Bob'
Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
animal = "dog"
puts animal
puts var
Variable Shadowing
Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = 4
b = 2
puts a
puts b
Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
n = 10
puts n
Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
animal = "dog"
puts animal
Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = "hi there"
b = a
a = "not here"
What are a and b?
Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = "hi there"
b = a
a << ", Bob"
What are a and b?
Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = [1, 2, 3, 3]
b = a
c = a.uniq
What are a, b, and c? What if the last line was `c = a.uniq!`?
Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def test(b)
b.map {|letter| "I like the letter: #{letter}"}
end
Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = 5.2
b = 7 .3
a = b
b += 1.1
What is `a` and `b`? Why?
Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def test(str)
str += '!'
str.downcase!
end
puts test_str
Link to explanation of examples below
Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
lus(x, y)
def p
x = x + y
end
a = 3
b = plus(a, 2)
puts a
puts b
Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
ncrement(x)
def i
x << 'b'
end
y = 'a'
increment(y)
puts y
Example 9
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def change_name(name)
name = 'bob' # does this reassignment change the object outside the
method?
end
name = 'jim'
change_name(name)
puts name
Example 10
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def cap(str)
str.capitalize! # does this affect the object outside the method?
end
name = "jim"
cap(name)
puts name
Example 11
What is `arr`? Why? What concept does it demonstrate?
a = [1, 3]
b = [2]
arr = [a, b]
arr
a[1] = 5
arr
Example 12
Link to example
arr1 = ["a", "b", "c"]
arr2 = arr1.dup
arr2.map! do |char|
char.upcase
end
puts arr1
puts arr2
s = 'hello'
t = fix(s)
What values do `s` and `t` have? Why?
Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def fix(value)
value = value.upcase
value.concat('!')
end
s = 'hello'
t = fix(s)
What values do `s` and `t` have? Why?
Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def fix(value)
value << 'xyz'
value = value.upcase
value.concat('!')
end
s = 'hello'
t = fix(s)
What values do `s` and `t` have? Why?
Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def fix(value)
value = value.upcase!
value.concat('!')
end
s = 'hello'
t = fix(s)
What values do `s` and `t` have? Why?
Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def fix(value)
value[1] = 'x'
value
end
s = 'abc'
t = fix(s)
What values do `s` and `t` have? Why?
Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def a_method(string)
string << ' world'
end
a = 'hello'
a_method(a)
p a
Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
num = 3
Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = %w(a b c)
a[1] = '-'
p a
Example 9
Link to page with #9 & #10
def add_name(arr, name)
arr = arr + [name]
end
kim']
names = ['bob', '
add_name(names, ' jim')
puts names
Example 10
kim']
names = ['bob', '
add_name(names, ' jim')
puts names
Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
array = [1, 2, 3, 4, 5]
array.select do |num|
puts num if num.odd?
end
Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 3
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 4
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 5
What does the following code return? What does it output? Why? What concept does it
demonstrate?
words = %w(jump trip laugh run talk)
Example 6
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 7
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 8
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 9
What does the following code return? What does it output? Why? What concept does it
demonstrate?
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 10
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = "hello"
Example 11
What does the following code return? What does it output? Why? What concept does it
demonstrate?
[1, 2, 3
].each do |num|
puts num
end
Example 1
Example 2
Example 3
Example 5
[1, 2, 3
].each_with_index do |num, index|
puts " The index of #{num} is #{index}."
end
Example 6
{ a: "ant", b: "bear", c: "cat" }.each_with_object([]) do |pair, array|
array << pair.last
end
Example 7
{ a: "ant", b: "bear", c: "cat" }.each_with_object({}) do |(key, value),
hash|
hash[value] = key
end
Example 8
p odd
p even
Truthiness
Example 1
What does the following code return? What does it output? Why? What concept does it
demonstrate?
a = "Hello"
if a
Hello is truthy"
puts "
else
puts " Hello is falsey"
end
Example 2
What does the following code return? What does it output? Why? What concept does it
demonstrate?
def test
puts "written assessment"
end
var = test
if var
written assessment"
puts "
else
puts " interview"
end
Shaun’s work below
a = 'Hello
b = a
a = 'Goodbye'
a = 4
loop do
a = 5
b = 3
break
end
puts a
puts b
On `line 2` we are calling the `loop` method and passing in the `do..end`
block as an argument.
On `line 3` we are reassigning the local variable `a` to the integer object
5.
Because the local variable `b` was initialized in the inner scope of
the ‘do..end’ block that we passed to the `loop method` as an
argument, it is not available in the other scope.
a = "hello"
This will output nothing and return an array of string objects: [‘hello’, ‘hello’, ‘hello’]
On `line 1` we are initializing the local variable `a` and assigning it the string object `hello`.
On `line 3` we are calling the `map` method on the array object [1, 2, 3] and passing in the `{}`
block as an argument.
The `map` method iterates through the array object, passing each element to the block and then
runs the block. It then takes the return value of the block and moves it to a new array. And when
it finishes every iteration, it returns that new array that now contains the elements that were
passed into it, which is this case was [‘hello’, ‘hello’, ‘hello’]
Because `a` in the block evaluates to `hello` each time `map` runs the block, an array containing
the string object ‘hello’ is returned.
def greetings(str)
puts str
puts "Goodbye"
end
word = "Hello"
greetings(word)
This will output the string objects “Hello” and “Goodbye” and return `nil` (because puts always
returns `nil)
On `line 1-4` we are defining a method called `greetings` that takes one parameter `str`
on `line 2` we are calling the `puts` method and passing in `str` as an argument.
on `line 3` we are calling the `puts` method on the string object `Goodbye`
on `line 6` we are initializing the local variable `word` and assigning it the string object `Hello`
on `line 8` we are calling the `greetings` method and passing in the local variable `word` as an
argument. `Str` now points to the same String object that `word` points to `”Hello”`
On `line 2` `str` is now pointing to the same string object that `word` is pointing to. And so this
is what is output when we call the `puts` method on `str` on `line 2` when we invoke the
`greetings` method on `line 8` (this does not feel good enough, but I don’t know what else needs
to be explained here)
Version 2
# this will output the string objects `Hello` and `Goodbye` and returns `nil`
#On `line 6` we are initialzing the local variable `word` and assigning it to the string object
`Hello`
#On `lines 1-4` we are defining the `greetings` method that takes one parameter `str`
#On `line 8` we are invoking the `greetings` method and passing in the local variable `word` as
an argument. `word` and `str` are now pointing to the same object.
#On `line 2` we are calling the `puts` method and passing in `str` as an argument.
#On `line 3` we are calling the `puts` method and passing in the string object `Goodbye` as an
argument.
#This problem demonstrates the concept of local variable scope. A local variable that is
initialized outside of a method is accessible inside a method defined with a parameter, if the local
variable is passed into the method as an argument. The method can now access the string
`"Hello"` since it is passed in as an argument when the method is invoked.
This will output the integer objects 1, 2, 3 and return the array object that it was called on,
`[1, 2, 3]’
On `line 1` we are calling the `each` method on the array object with values 1, 2, 3 and passing in
the `do..end` block as an argument, with one parameter, `num`
On `line 2` we are calling the `puts` method and passing it `num` as an argument.
As the `each` method iterates over each of the elements in the array, it sends the element to the
block parameter `num` which is then passed to the `puts` method call as an argument and output
to the screen.
This problem demonstrates that the `each` method returns the original collection it was called on.
This will output nothing and return the array object `[1, 3]`
On `line 1` we are calling the `select` method on the array object with
integers 1, 2, 3, 4.
We are passing the `select` method the `{}` block as an argument, with one
parameter, `num`
We are then calling the `odd?` method on `num`.
As the `select` method iterates over each of the elements in the array, it
sends the element to the block parameter `num`, which we then pass to the
`odd?` method. Each iteration will return a boolean value.
Whenever the `odd` method evaluates to `true`, the element will be selected
and passed into a new array. One the `select` method has finished iterating
over the collection, it will return the new array, which, in this case, will
be `[1, 3]`
This problem demonstrates that the `select` method returns a new Array object
based on the truthiness of the block’s return value. `select` uses the return
value of the block to perform selection on each element.
This code outputs the integer objects 6 and 7 and returns the array object
[5, 6, 7]
On `line 1` we call the `each` method on the array object with integers 5, 6,
7. We pass in the `do..end` block as an argument, with one parameter, `num`
As the `each` method iterates over each of the elements in the array, it passes the element to the
block parameter `num`. which is then passed to the `>` method on `line 2`.
Inside the conditional statement, we invoke the `#>` method on the local variable `num` and pass
in the integer `5` as an argument. If `num` is greater than `5` then the code on `line 3` will be
executed. In this case, the integers `6` and `7` are output to the screen.
If the element is greater than the argument passed to the `>` method, which is the integer object
5, that element is then passed to the `puts` method on `line 3` and output to the screen.
This problem demonstrates that the `each` method returns the original collection it was called on.
a = 'Bob'
p a
This outputs the string object “Bill” and returns the string object “Bill”
On `line 1` we are initializing the local variable `a` and assigning it the
string object ‘Bob’
On `line 3` we are calling the `times` method on the integer object 5 and
passing in the `do..end` block as an argument with one parameter `x`
On `line 4` we are reassigning the local variable `a` to the string object
`Bill`
On `line 7` we are outputting the object that the local variable `a` is
pointing to, which is `Bill`
animal = "dog"
puts animal
puts var
`line 9` will output the string object `cat` and return `nil`
`line 10` will produce an error, undefined local variable or method `var` for
main Object.
On `line 1` we initialize the local variable `animal` and assign it the
string object `dog`
On `line 2` we call the `loop` method and pass it the `do..end` block as an
argument.
On `line 4` we are initializing the local variable `var` and assigning it the
string object `ball`
On `line 8` we are calling teh `puts` method and passing it the local
variable `animal` as an argument. Because `animal` was reassigned within the
block, it now points to the string object `cat`
`line 9` produces an error because the local variable `var` was initialized
inside of the `do..end` block and thus is not available outside of the block.
This is an example of variable scope; specifically, that variables initialized in the outer scope are
available in an inner scope.
This is also an example of variable scope; specifically, that variables initialized within a block
are NOT accessible outside of that block, in the outer scope.
n = 10
puts n
This will output the integer object 10 and return the integer object 1. It
returns 1 because the `times` method returns the integer object that it was
called on.
This is an example of variable shadowing. Variable shadowing happens when the block
parameter name is the same as a local variable which was initialized outside of the block. The
consequence is that it prevents access to variables of the same name initialized outside of the
block. It also prevents us from making changes to the outer-scoped variable.
animal = "dog"
puts animal
This code outputs the string object `dog` and returns `nil`.
On `line 1` we are initializing the local variable `animal` and assigning it the string object `dog`
On `line 3` we are calling the `loop` method and passing in the `do..end` block as an argument
with one parameter `animal`
Because the block parameter and the local variable initialized on `line 1` have the same name,
the local variable will NOT be avaliable within the block.
So when we call the `puts` method on `line 8` and pass in the local variable `animal` as an
argument, it will output the string object `dog` and return `nil`. However, if the block parameter
was called anything else, `animal` would have been reassigned to the string object `cat`.
This is an example of variable shadowing. Variable shadowing happens when the block
parameter name is the same as a local variable which was initialized outside of the block. The
consequence is that it prevents access to variables of the same name initialized outside of the
block. It also prevents us from making changes to the outer-scoped variable.
a = "hi there"
b = a
a = "not here"
On `line 1` we are initializing the local variable `a` and assigning it the string object `hi there`
On `line 2` we are initializing the local variable `b` and assigning it to the same object that `a` is
pointing to. `a` and `b` are now pointing to the same string object `hi there`
This is an example of variable reassignment, where we reassign the local variable `a` to point to
a new string object. The local variable `b` is still pointing to the original string object that it was
assigned to, `hi there`
a = "hi there"
b = a
a << ", Bob"
On `line 1` we are initializing the local variable `a` and assigning it the string object `hi there`.
On `line 2` we are initializing the local variable `b` and assigning it to the same object that `a` is
pointing to. `a` and `b` are now pointing to the same string object `hi there`
On `line 3` we are calling the `<<` method on the string object that `a` is pointing to, and passing
in the string object `, Bob` as an argument.
The line of code `a << ", Bob"` mutated the caller and modified the existing string, which is also
pointed to by the variable b. This explains why in this code, b reflects the changes to a - they're
both pointing to the same thing.
def fix(value)
value << 'xyz'
value = value.upcase
value.concat('!')
end
s = 'hello'
t = fix(s)
p s
p t
# `line 10` outputs the string object `helloxyz` and returns the same string object, `helloxyz'
# `line 11` outputs the string object `HELLOXYZ!` and returns the same string object,
`HELLOXYZ!'
#on `line 8` we are initializing a local variable `t` and assigning it to the return value of calling
the `fix` method and passing it the local variable `s` as an arugment.
#on `line 1` we define a method called `fix` that takes one parameter `value`
#on `line 2` we are calling the `<<` method on `value` and passing in the string object `xyz` as an
argument. Because the `<<` method mutates the caller, the local variable `s` and `value` are now
pointing to the same string object 'helloxyz'
#on `line 3` we are reassigning `value` to the return value of calling the `upcase` method on the
object that `value` is pointing to. `value` is now pointing to the string object `HELLOXYZ`
while the local variable `s` is still pointing to the string object `helloxyz`
#on `line 4` we are calling the `concat` method on `value` and passing in the string ojbect `!` as
an argument. `value` now points to the string ojbect `HELLOXYZ!`
On `line 4` we are calling the `concat` method on `value` and passing in the string object `!` as
an argument. `value`is still pointing to the same object, but it has been mutated and is now
`HELLOXYZ!`
#since eht return value of the `fix` method call is the string object `HELLOXYZ!' that is what `t`
is pointing to.
# the local variable `s` is still pointing to the string ojbect `helloxyz`
# this is an example of both reassignment, where we are reassigning `value` to a new string
object on `line 3` as well as an example of mutating the caller, which we do on `line 2` and `line
4`
This will output the array object `[ "d", "a", "e", "c", "b" ]` and return the same array object, `[
"d", "a", "e", "c", "b" ]`
On `line 1` we are initializing the local variable `arr` and assigning it the array object `[ "d", "a",
"e", "c", "b" ]’
On `line 2` we are calling the `sort` method on the object that `arr` is pointing to. Because the
`sort` method is non-mutative, the object that `arr` was assigned to on `line 1` has not changed.
And so when we call the `p` method and pass in `arr` as an argument on `line 3`, the original
array object that `arr` was assigned to on `line 1` is output and returned (because the `p` method
outputs and returns the object it was passed as an argument)
This is an example of...I’m not sure what to say here. This is an example of how a method call
can reassign an object, but because we did not assign the return of the method call to a variable,
no reassignment takes place?
Or
def test
puts "written assessment"
end
var = test
if var
written assessment"
puts "
else
puts " interview"
end
This code outputs the string object `interview` and returns `nil`
On `line 5` we are initializing the local variable `var` and assigning it the
return value of calling the `test` method.
The return value of calling the `test` method is `nil` because `puts` always
returns `nil`
But because the return value of calling the `test` method is `nil`, the first
branch of the conditional statement evaluates to `false`, so the code on
`line 10` is executed.
This is why the string object `interview` is output and why `nil` is
returned.
This code demonstrates that `puts` always returns `nil`. It also demonstrates
that when you initialize a variable and point it to a method call, you are
assigning the return value of that method call to the variable, and not the
output of that method.
a = "Hello"
if a
Hello is truthy"
puts "
else
puts " Hello is falsey"
end
This code will output the string object `Hello is truthy` and return `nil`.
On `line 1` we are initializing the local variable `a` and assigning it the string object `Hello`.
On `line 2-6` we are initializing a conditional `if/else` statement. If the first branch of the
conditional statement is `truthy`, the code on `line 3` will be executed.
Because every value in Ruby evaluates to `true`, except for `false` and `nil`, this means that the
local variable `a` evaluates to `true`, and so the code on `line 3` is executed.
This code demonstrates that every value in Ruby evaluates to `true`, except for `false` and `nil`.
Another, perhaps better way to answer this question would simply be:
The local variable `a` evaluates to `true` in the conditional statement and
so the string object 'Hello is truthy' is output.
On `line 1` we are calling the `any?` method on the hash object with key/value pairs `{ a:
"ant", b: "bear", c: "cat" }`
Because none of the values in the hash have a `size` greater than `4`,
`false` is returned.
This code demonstrates how the `any?` method passes each element of the
collection to the given block and returns `true` if the block ever returns a
value other than `false` or `nil`
[1, 2, 3].all? do |num|
num > 2
end
On `line 1` we are calling the `all?` method on the array object with values `1, 2, 3` and passing
in the `do..end` block as an argument with one parameter `num`
On `line 2` we are calling the `>` method on the local variable `num` and passing in the integer
`2` as an argument.
The method `all` requires that all objects in a collection meet the criteria; in this case, that they
have a value greater than `2`. Since all objects do not have a value greater than `2`, `false` is
returned.
This code demonstrates how the `all` method works. Specifically, that the method passes each
element of the collection to the given block and returns `true` if the block never returns `false` or
`nil`.
{ a: "ant", b: "bear", c: "cat" }.each_with_object([]) do |pair, array|
array << pair.last
end
This code outputs nothing and returns the array object `[“ant”, “bear”, “cat”]
: "ant", b:
On `line 1` we are calling the `each_with_object` method on the hash object `{ a
"bear", c: "cat" }` and passing in the `do..end` block as an argument with
two parameters `pair` and `array`
On `line 2 we are calling the `<<` method on the local variable `array` and
passing in `pair.last` as an argument.
This will insert the `value` of each `key/value` pair into the new array.
This code demonstrates how the `each_with_object` method works. Specifically,
that the method iterates the given block for each element with an arbitrary
object given, and returns the initially given object.
{ a: "ant", b: "bear", c: "cat" }.each_with_object({}) do |(key, value),
hash|
hash[value] = key
end
This code will output nothing and return the hash object `{ “ant” => :a, “bear” => :b, “cat” => :c }`
: "ant", b:
On `line 1` we are calling the `each_with_object` method on the hash object `{ a
"bear", c: "cat" }` and passing in the `do..end` block as an argument with
three parameters `key`, `value` and `hash`.
This code demonstrates that I don’t know how to explain this concept.
p odd
p even
`line 5` will output the array object `[1, 3]` and return the same
object (because the method call `p` always outputs and returns the
object it was called on)
`line 6` will output the array object `[2]` and return the same
object.
On `line 1` we are initializing the local variables `odd` and even` and assigning them to the
return value of calling the `partition` method on the array object `[1, 2, 3]` and passing it the
`do..end` block as an argument with one parameter `num`
On `line 2` we are calling the `odd?` method on the local variable `num`
This is an example of how the `partition` method call returns two arrays, the first containing the
elements for which the block evaluates to true, the second containing the rest.
So the local variable `odd` will contain the elements for which the block evaluates to `true`, and
the local variable `even` will contain the rest.
aO=