Ruby Quiz December 2021 Flashcards

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

Express this code using normal array method.

p [“tommy”, “chuckie”].map(&:upcase)

A

[“tommy”, “chuckie”].map { |name| name.upcase }

[“TOMMY”, “CHUCKIE”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
module Sample
  def self.sidekick
    'robin'
  end
end

p Sample.sidekick

Another syntax to call this method?

A
module Sample
  def self.sidekick
    'robin'
  end
end

p Sample::sidekick

Singleton methods of a module can either be called with dot notation (Batman.sidekick) or :: notation (Batman::sidekick).

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

How to convert “to_boolean” of any value?

A

s = !! “hello” - double bang

s.class =>TrueClass

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

Which one is correct?

def is_odd(x)
  x % 2 == 0 ? false : true
end

def is_odd(x)
x % 2 != 0
end

A

Both are correct

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

What are called First-class citizens in the Ruby programming language?

A

procs can be stored in data structures, passed around as arguments, and returned by methods, thus meeting the definition of a “first-class” language component.

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

What does the following code print?

class Array
  def say_hi
    "HEY!"
  end
end

p [1, “bob”].say_hi

A
"HEY!"
The Array class was reopened and the say_hi() method was added for all instances of the Array class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the following code print? Explain the result.

class Computer
  def initialize
    @sound = "beep beep"
  end

def self.about
“Sometimes I go #{@sound}”
end
end

p Computer.about

A

“Sometimes I go “

The about() method is a class method and does not have access to the @sound instance variable. @sound is only available to the instance methods, not the class methods. Undefined instance variables have a default value of nil.

What does the following code print?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
class Xyz
  def paper
    unassigned_local_variable
  end
end

What would be the output?
xyz = Xyz.new
p xyz.paper

A

This raises an exception and provides the following message: undefined local variable or method `unassigned_local_variable’.

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

What would be the output of the following?

true. class
false. class
nil. class

A

true. class # TrueClass
false. class # FalseClass
nil. class # NilClass

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

Do you know fixed object_id in Ruby? if so what would be the output of this?

false. object_id
true. object_id
nil. object_id

A

false. object_id # 0
true. object_id # 20
nil. object_id # 8

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

How you will get the current call stack in Ruby?

A
def foo
  bar
end
def bar
  puts caller
end
foo
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What this line will do?

ruby -n -e ‘p eval($_)’

A

This works because the -n flag does this:

-n assume ‘while gets(); … end’ loop around your script
And $_ is a global variable. Which contains the following:

The last input line of string by gets or readline.

It will act as mini IRB

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. object_id => 3
  2. object_id=>5
  3. object_id=>7

How these ids are generated?

A

Integer ids use this formula: (number * 2) + 1.

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

queue = []
%w{hello x world}.each do |word|
queue &laquo_space;word and puts “Added to queue” unless word.length < 2
end

puts queue.inspect

is this valid? If so, what is the output?

A
# Output:
#   Added to queue
#   Added to queue
#   ["hello", "world"]

we can use use ‘and’ and ‘or’ to group operations for single liners

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

How to see the whole Traceback of an exception in Ruby?

A
def do_division_by_zero; 5 / 0; end
begin
  do_division_by_zero
rescue => exception
  puts exception.backtrace
end

backtrace method

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