Ruby · ruby excercise

Ruby — Array related Excercise

What is the output of the following programs

Note : Please find the answers manually

——————————————————–

1. Question 1

@a=[34,45,56,2,13,54]

@a.sort!

@a.reverse

puts @a[4] gives,

a) 13 b)54 c) 45 d) 56

——————————————————–

2. Question 2

@a=[34,45,56,2,13,54]

@a=@a.length.to_a.join

puts @a.class gives,

a) fixNum b)Array c) String d) ERROR ——————————————————–

3. Question 3

@a=[34,45,56,2,13,54]

@a=@a[5,4]

puts @a gives,

a) 13 b)54 c) 45 d) 56

——————————————————–

4. Question 4

@a=[34,45,56,2,13,54]

@a= @a.flatten

puts @a gives,

a) The reverse order b) NIL c) NULL d) Same Order

——————————————————–

5. Question 5

@a=[34,45,56,2,13,54]

@b= @a.min + @a.max + @a.first + @a.last

puts @b gives,

a) 92 b) 144 c) 146 d) 112

——————————————————–

6. Question 6

@a=[34,45,56,2,13,54]

@b= @a[2].value+@a[3].value

puts @b gives,

a) Argument Error b) 58 c) NomethodError d) 0

——————————————————–

7. Question 7

@a=[34,45,56,2,13,54]

@b= @a[2].display.to_i + @a[3].display.to_i

puts @b gives,

a) Error b) 58 c) 5620 d) 562

——————————————————–

8. Question 8

@a=[34,45,56,2,13,54]

@b= @a.rindex(13) + @a.values_at(4)[0]

puts @b gives,

a) Error b) 17 c) 47 d) 7

——————————————————–

9. Question 9

@a=[34,45,56,2,13,54]

@a.insert(6)

@a.insert(6,7)

@a << [137,89]

@b=@a.length

puts @b gives,

a) Error b) 10 c) 9 d) 8

——————————————————–

10. Question 10

@a=[34,45,56,2,13,54]

@b= @a.__id__.class

puts @b gives,

a) Error b) Array c) Nil class d) FixNum

Send your answers as a Comments of this article upto Oct 31 Result Date: Nov 1 (List of the names who scored 100%)

Advertisements

3 thoughts on “Ruby — Array related Excercise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s