Skip to content

Instantly share code, notes, and snippets.

@kannans
Last active August 29, 2015 14:25
Show Gist options
  • Save kannans/d7620d2f880f3d804fb5 to your computer and use it in GitHub Desktop.
Save kannans/d7620d2f880f3d804fb5 to your computer and use it in GitHub Desktop.
Ruby questions
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
@madhu7
Copy link

madhu7 commented Jul 22, 2015

  1. What does the following code print?
    p ["tommy", "chuckie"].map(&:upcase)

Ans : ["TOMMIE", "CHUCKIE"]

  1. Write a programe for this.
    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])}

  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

  2. 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]

  1. 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 - secound

  1. Ans : "ARGH"

@madhu7
Copy link

madhu7 commented Jul 22, 2015

ANSWERS UPDATED WITH RESULT:

  1. What does the following code print?
    p ["tommy", "chuckie"].map(&:upcase)
    Result : ["TOMMIE", "CHUCKIE"]

  2. 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]]

  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]

  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"}

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"}

  1. 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"]

  2. Ans : "ARGH"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment