Quiz - June Flashcards

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

What does the following code return?

book = ["intelligent investor"]
great_book = ["intelligent investor"]

book.object_id == great_book.object_id

A

false

The book and great_book arrays have the same content, but are different instances of the Array class and therefore have different object_ids. Array equality comparisons in Ruby check if arrays have the same content in the same order, not if the arrays are the same exact object.

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

What is the name of this operator? How it will work?

===

A

subsumption operator.

“If a described a set, would b be a member of that set?”

(1. .5) === 3 # => true
(1. .5) === 6 # => false

Integer === 42 # => true
Integer === ‘fourtytwo’ # => false

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

True or False?

> count ||= 0

count ||= 0 gives count the value 0 if count is nil or false.

A

True

a ||= b
The assignment statement supports a set of shortcuts: a op= b is the same
as a = a op b. This works for most operators:
count += 1 # same as count = count + 1
price *= discount # price = price * discount
count ||= 0 # count = count || 0
So, count ||= 0 gives count the value 0 if count is nil or false.

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

string = “Ruby” + “ 3”

string = “Ruby” “3”

string = “Ruby” &laquo_space;” 3.”

string = “Ruby”.concat(“3”)

puts string

“Ruby 3”

Which one is false?

A

All are true

there are 4 ways to do string concatenation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What would be the output?
class Monster
  private
  def method_missing(name, *args)
    puts "blah monster, attack!"
  end
end

ob = Monster.new

ob. method :output
ob. method :name

ob. name
ob. output

A

ob.name alone will give output. rest of the ob.method will raise errors.

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

What would be the output?

class Mammal
  def self.about
    "we are living creatures"
  end
end

class Dog < Mammal; end

p Dog.about

A

“we are living creatures”

Class methods are also inherited.

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

What are differences in these two class definitions?

#Class Definition #1
class Cat < Object
end
#Class Definition #2
class Cat
end
A

There is no difference between Class Definition #1 and Class Definition #2. Class Definition #1 explicitly inherits from Object, but Class Definition #2 inherits from Object by default (implicitly), so these class definitions are identical.

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

What does the following code print? Explain.

module X; end
module Y; end
class Z
  include Y
  include X
end

p Z.ancestors

A

[Z, X, Y, Object, Kernel, BasicObject]
When Y is included, it is added immediately to the right of Z in the ancestors chain. When X is included, it is added immediately to the right of Z in the ancestors chain, thus moving Y one position to the right.

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

Is this Valid code?

module Batman
  def self.sidekick
    'robin'
  end
end

p Batman::sidekick

A

‘robin’

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
10
Q

Is this valid?

class Dog; end
dog = Dog.new
def dog.bark
  "ruff ruff"
end

p dog.bark

A

“ruff ruff”

dog is an instance of the Dog class and is given a singleton method bark(). The bark() method can be called on the dog instance of the Dog class, but is not available for other instances of the Dog class.

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

What is the difference between to_s and to_str methods?

A

Do you see the difference? In the first case,
we cast an object into a string, so we call the to_s method.
In the second example, we don’t cast object into a string.
We want to object behave like a string.
We want to object allow to use the :+ method the same as strings do

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

What is the difference between Explicitly casting and implicitly coercing types in Ruby?

A

The most common casting helpers are #to_s, #to_i, #to_a and #to_h. These are explicit casting methods. They help us easily transform a value from one type to another.

Ruby also offers implicit coercion methods which only return a value when objects act like the type. This way we can be sure that the value acts like the type we want. These implicit coercion methods are #to_str, #to_int, #to_ary and #to_hash

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

What is the use of Undef Keyword?

A

Ruby provides a special keyword which is known as undef keyword. This keyword used to avoid the current working class from responding to calls to the specified named methods or variable.

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

Which one is not a reserved keyword in Ruby?

a) defined?
b) alias
c) _ FILE _
d) nil

A

All are reserved keywords

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

If we do dynamic modification to a class (means to add new or overwrite existing methods) at runtime that technique will be called as ?

A

In Ruby, a Monkey Patch (MP) is referred to as a dynamic modification to a class and by a dynamic modification to a class means to add new or overwrite existing methods at runtime.

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

What would be the output of this code?

h1 = {a: 1, b: 2}
h2 = h1.merge!({lala: “word up”})
puts h1.object_id == h2.object_id

A

true

The Hash#merge! method combines two hashes and mutates the original hash. Since the haha and bozo variables are assigned to the same object, they have the same object id. If the Hash#merge method was used (notice no !), then the original object would not have been mutated and the object ids would be different

17
Q

what is the user of this operator?

!~

A

if “Ruby Quicktips” !~ /\d+/
puts “The string does not contain any digits.”
end
# The string does not contains any digits.
# => nil

18
Q

How to print this output?

30 29 28 27 26 25

A

30.downto(25){ |i| puts i}

The downto() function in Ruby returns all the numbers less than equal to number and greater than equal to limit.

19
Q

What is the use of partition method in string?

A

partition(sep) → [head, sep, tail]

“hello”.partition(“l”) #=> [“he”, “l”, “lo”]
“hello”.partition(“x”) #=> [“hello”, “”, “”]