Last active
August 29, 2015 14:25
-
-
Save kannans/d7620d2f880f3d804fb5 to your computer and use it in GitHub Desktop.
Ruby questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) What does the following code print? | |
p ["tommy", "chuckie"].map(&:upcase) | |
---------------------------------------------------------------------------------------------- | |
2) Write a programe for this. | |
colors = ['red', 'violet', 'blue'] | |
Use the colors array to construct the following array: | |
[['red', 1], ['violet', 2], ['blue', 3]] | |
----------------------------------------------------------------------------------------------- | |
3) Create an array of all the numbers between 1 and 100 that are divisible by both 3 and 5. | |
4) Write a programe for this. | |
music = [["blind melon", "no rain"], ["sublime", "40 oz to freedom"], ["damian marley", "jr gong"]] | |
Output: | |
Use the music array to construct the following hash: | |
{"blind melon" => "no rain", "sublime" => "40 oz to freedom", "damian marley" => "jr gong"} | |
--------------------------------------------------------------------------------------------- | |
5) Create an array of all the elements that are in first, but not in second. | |
first = ["cool", "busta", "odb"] | |
second = ["puffy", "cool", "busta"] | |
----------------------------------------------------------------------------------------------- | |
6) What does the following code print? | |
class Parent | |
def status | |
"I am happy" | |
end | |
end | |
class Child < Parent | |
def status | |
"ARGH" | |
end | |
end | |
p Child.new.status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ANSWERS UPDATED WITH RESULT:
What does the following code print?
p ["tommy", "chuckie"].map(&:upcase)
Result : ["TOMMIE", "CHUCKIE"]
Write a programe for this.
colors = ['red', 'violet', 'blue']
Use the colors array to construct the following array:
Ans: res = [] ; ['red', 'violet', 'blue'].each_with_index{|a,i| res.push([a,i+1])}
Result : [["red", 1], ["violet", 2], ["blue", 3]]
Create an array of all the numbers between 1 and 100 that are divisible by both 3 and 5.
Ans:
arr = []
100.times do|i|
arr.push(i) if((i% 3 == 0) && (i%5 == 0))
end
Result : [0, 15, 30, 45, 60, 75, 90]
Write a programe for this.
music = [["blind melon", "no rain"], ["sublime", "40 oz to freedom"], ["damian marley", "jr gong"]]
Output:
Use the music array to construct the following hash:
{"blind melon" => "no rain", "sublime" => "40 oz to freedom", "damian marley" => "jr gong"}
Ans: a = [["blind melon", "no rain"], ["sublime", "40 oz to freedom"], ["damian marley", "jr gong"]].flatten
h = Hash[*a]
Result => {"blind melon"=>"no rain", "sublime"=>"40 oz to freedom", "damian marley"=>"jr gong"}
Create an array of all the elements that are in first, but not in second.
first = ["cool", "busta", "odb"]second = ["puffy", "cool", "busta"]
Ans : result = first - second
Result => ["odb"]
Ans : "ARGH"