Ruby quiz questions

Ruby interview questions

  • 1.

    Which of the following is best described as a block?

    1)  my_array = [1, 2, 3]
        my_array.each {|i| puts i*i}
    2)  def double(n)
          return n * 2
        end
    3)  def welcome
            puts "Welcome to Ruby!"
        end
    4)  city = ["London", "Paris", "Belfast", "Rome"]
    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 2.

    Which of the following correctly sort the array below in descending order?

    city = ["London", "Paris", "Belfast", "Rome"]
    1. city.sort! do |first, second| second <=> first end

    2. city.sort! { |first, second| first <=> second }

    3. city.sort! { |first second| second <=> first }

    4. city.sort! do |first second| first <=> second end

    Answer
  • 3.

    Which of the the following will be returned from the statement below?

    7 % 3 <=> 2+10/2
    1. 0

    2. 1

    3. -1

    4. =

    Answer
  • 4.

    Which of the following best describes an argument?

    1. The placeholder used when defining a part of method that takes an input

    2. The placeholder used when defining a part of method that takes an output

    3. The user input when calling a method

    4. The user input when creating a method

    Answer
  • 5.

    Which of the following best describes a parameter?

    1. The placeholder used when defining a part of method that takes an input

    2. The placeholder used when defining a part of method that takes an output

    3. The user input when calling a method

    4. THe user input when creating a method

    Answer
  • 6.

    Which of the following best describes a method?

    1. A section of code that can only e used in objects
      
    2. A section of code tha performs a specific task once

    3. A section of code that belogs within a hash

    4. Reusable section of code written to perform a specific task in a program

    Answer
  • 7.

    Which of following correctly iterates over the hash below printing every key and value?

     

    Address_book {
      "Henry" => "14 Ladbroke Grove"
      "Emily" => "243 Baker Street"
      "Jamie" => "43 Jubilee Street"
    
    1. Address_book.each {|x, y| puts "{x}; {y}" }

       

       

    2. Address_book.each {(x, y) puts "{x}; {y}" }

    3. Address_book.each [|x, y| puts "#{x};

    4. Address_book.each {|x, y| puts "#{x};

    Answer
  • 8.

    Which of the following best describes the next keyword?

    1. it can skip over certain iterations in a loop
    2. it can only break a loop once
    3. it skips to the next loop in the code
    4. it incriments a loop by one automatically

    Answer
  • 9.

    Which of the following is the correct syntax for a hash?

    1)   hash_name = {
          key1 = value1,
          key2 = value2
         }
    2)   hash_name = {
          key1 => value1
          key2 => value2
          end
         }
    3)   hash_name = {
          key1 => value1,
          key2 => value2
         }
    4)   hash_name = {
          key1 = value1,
          key2 = value2
          end
         }

     

    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 10.

    Which of the following correctly accesses 10 from the array below?

    array = [12, 10, 12, 14]
    1. array [#2]

    2. array [1]

    Answer

© 2017 QuizBucket.org