Ruby Quiz 25.2.2021 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q
class ABC
  def xyz
   puts "xyz in ABC"
  end
end.     #for this code, which one is true?
1.ABC::new::xyz
2.ABC::new.xyz
3.ABC.new::xyz
4.ABC.new.xyz
A
  1. ABC::new::xyz
  2. ABC::new.xyz
  3. ABC.new::xyz
  4. ABC.new.xyz

all are true

output “xyz in ABC”

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

Is the below Ruby code valid?

-> (a) {p a}[“Hello world”]

If so, what does it do? Explain your answer.

A

The -> operator creates a new Proc, which is one of Ruby’s function types. (The -> is often called the “stabby proc”.)

This particular Proc takes one parameter (namely, a). When the Proc is called, Ruby executes the block

puts a

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

Which one is true?

my_lambda = -> { puts “Lambda called” }

a) my_lambda.call
b) my_lambda.()
c) my_lambda[]
d) my_lambda.===

A

All are true.

a) my_lambda.call
b) my_lambda.()
c) my_lambda[]
d) my_lambda.===

These methods are used to call the lambda

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

Which of the following is true?
defining a regular expression:

a) Regexp.new( ‘string pattern’ [, options ] )
b) /string pattern/
c) %r{string pattern}
d) ->(x){string pattern}

A

First three syntax are true.

a) Regexp.new( ‘string pattern’ [, options ] )
b) /string pattern/
c) %r{string pattern}
d) ->(x){string pattern}

fourth one is lambda syntax

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

What are the meaning of the following character ranges in Regular Expression?
\w
\d
\s

A

\w is equivalent to [0-9a-zA-Z_]

\d is the same as [0-9]

\s matches white space (tabs, regular space, newline)

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

What are the meaning of the following character ranges in Regular Expression?
\W
\D
\S

A

\W anything that’s not in [0-9a-zA-Z_]

\D anything that’s not a number

\S anything that’s not a space

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

What is the use of the method “named_capture” in Regexp?

A

It will return a hash representing information about named captures of rxp.

A key of the hash is a name of the named captures. A value of the hash is an array which is list of indexes of corresponding named captures.

/(?< foo >.)(?< bar >.)/.named_captures
#=> {"foo"=>[1], "bar"=>[2]}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the use of “to_c” method in String?

A

It will return the complex form of the input sting.

‘9’.to_c #=> (9+0i)
‘2.5’.to_c #=> (2.5+0i)

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

a = “ 3”

How you will get this string?(without concatenation)

“Ruby 3”

A

a = “3”
a.prepend(“Ruby “)
puts a

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

What is the use of abs2 method in Complex class?

A

gives square of the result

Complex(5).abs2 =>25

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

What is the alternative way of expressing ‘if not’.?

A
unless
# if !(aDay == 'Saturday' or aDay == 'Sunday')
	unless aDay == 'Saturday' or aDay == 'Sunday'
		daytype = 'weekday'
	else
		daytype = 'weekend'
	end
	return daytype
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How “loop” keywords can be used in Ruby?

A

loop do

end
=============================
i=0
loop {
	puts(arr[i])
	i+=1
	if (i == arr.length) then 
		break 
	end
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between #remove_method and #undef_method?

A
#undef_method( )
removes all methods, including the inherited ones.
#remove_method( )
removesall  the methods, incase of inheritance, it leaves inherited methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is there an equivalent of “continue” in Ruby?

A

The Ruby next statement is used to skip loop’s next iteration. Once the next statement is executed, no further iteration will be performed.

The next statement in Ruby is equivalent to continue statement in other languages.

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

How to display the inbetween values in a range?

print (1..7) #=> [1, 2, 3, 4, 5, 6, 7]

A

entries, to_a

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

What is the difference between last and max methods in Range?

A
  1. .10.last=> “10”

1. ..10.max => “9”

17
Q

How can you resolve name conflict(same name in mixin modules) in modules?

A

alias_method.

module Happy

	def expression
		return "smiling"
	end
	alias happyexpression expression
end
18
Q

What each_pair method will do? Which class it belongs to?

A

take pair from a Hash, Hash class

h = { “a” => 100, “b” => 200 }
h.each_pair {|value| puts value }

19
Q
def sum(array)
  sum = 0
  array.each do |number|
    sum += number
  end
  return sum 
end 
p sum([5, 10, 20])
# => 35

Refactor this code using reduce method

A

def sum(array)
array.reduce(:+)
end
p sum([5, 10, 20])

The ‘reduce’ method can be used to take an array and reduce it to a single value.

20
Q

How can we print the minimum and maximum value from an array using a single method?

A

minmax

Return both minimum and maximum values from an array

puts [2,4,55,0,45].minmax