-
-
Save kannans/d7620d2f880f3d804fb5 to your computer and use it in GitHub Desktop.
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 |
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"
p ["tommy", "chuckie"].map(&:upcase)
Ans : ["TOMMIE", "CHUCKIE"]
colors = ['red', 'violet', 'blue']
Use the colors array to construct the following array:
[['red', 1], ['violet', 2], ['blue', 3]]
Ans: res = [] ; ['red', 'violet', 'blue'].each_with_index{|a,i| res.push([a,i+1])}
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
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]
first = ["cool", "busta", "odb"]
second = ["puffy", "cool", "busta"]
Ans: Result = first - secound