Ruby Programming Training Quiz #1

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Ary.Alva
A
Ary.Alva
Community Contributor
Quizzes Created: 2 | Total Attempts: 2,165
Questions: 15 | Attempts: 460

SettingsSettingsSettings
Programming Quizzes & Trivia

Test your proficiency in Ruby.
. . . Please leave your comments. . .
Best of luck!

*** Ary Alva ***


Questions and Answers
  • 1. 

    Ruby array can contain values of which type?

    • A.

      Integers

    • B.

      Floats

    • C.

      Characters

    • D.

      Any type

    • E.

      Strings

    Correct Answer
    D. Any type
    Explanation
    Ruby array may contain values of any type

    Rate this question:

  • 2. 

    What is the size of Ruby array

    • A.

      10

    • B.

      A Ruby array has no fixed size

    • C.

      Count

    • D.

      None of the above

    Correct Answer
    B. A Ruby array has no fixed size
    Explanation
    A Ruby array has no fixed size. It is automatically incremented as elements are added at the run time.
    The count is an itterator used with for loop.

    Rate this question:

  • 3. 

    What is the first index of an array

    • A.

      1

    • B.

      0

    • C.

      -1

    • D.

      None of the above

    Correct Answer
    B. 0
    Explanation
    An array's indices start with 0

    Rate this question:

  • 4. 

    Let's create an array:a=[1,2,3,4,5]Which statement creates the following output:[1, 2, 3, 4, 5]

    • A.

      Puts a[0..5]

    • B.

      A=[0,1,2,3,5] puts a[0..5]

    • C.

      Print a[1..5]

    • D.

      For count in a print a[count] end

    • E.

      None of the above

    Correct Answer
    D. For count in a print a[count] end
    Explanation
    A. Is the wrong answer because:
    puts outputs one string at a time, so the elemets of an array will be separated by new line.
    B. Is the same as A.
    C. will print 2345
    D. Is a correct answer

    Rate this question:

  • 5. 

    Let's create an array:a=[12345, 23456, 34567, 98765]Which statement will print an index of the third element of this array.

    • A.

      A[2]

    • B.

      Puts a[3]

    • C.

      For count in a print count if count ==2 end

    • D.

      For count in a print a[2] end

    Correct Answer
    C. For count in a print count if count ==2 end
    Explanation
    A. Is wrong because it will not print anything.
    B. Is wrong because it will print 98765
    C. Is the correct answer
    D. Is wrong because it will output the third element with each itteration of for loop

    Rate this question:

  • 6. 

    What is the difference between a hash and an array in Ruby?

    • A.

      Hash indices can be of any type

    • B.

      There is no difference

    • C.

      The size of a hash is not fixed

    • D.

      The hash size is incremented dynamically

    • E.

      None of the above

    Correct Answer
    A. Hash indices can be of any type
    Explanation
    A. This is correct answer.
    B. Is the wrong answer because array indices can only be of integer type.
    C. This answer is wrong because the size of both array and hash are not fixed and incremented dynamically
    D. This answer is wrong. The same reason as C.

    Rate this question:

  • 7. 

    What is the name of key/value pair in Ruby hash?

    • A.

      Index

    • B.

      Value

    • C.

      Entry

    • D.

      Parameter

    • E.

      None of the above

    Correct Answer
    C. Entry
    Explanation
    In Ruby hash, a key/value pair is referred to as an "entry". This term is commonly used to describe the combination of a key and its corresponding value in a hash data structure. The entry allows for efficient retrieval and manipulation of data stored in the hash, where each key is unique and associated with a specific value.

    Rate this question:

  • 8. 

    Which statement correctly defines hash h?

    • A.

      H=[fish => Catfish, dog => Terrier, cat => Siamese]

    • B.

      H={fish => Catfish, dog => Terrier, cat => Siamese}

    • C.

      H=[fish - Catfish, dog - Terrier, cat - Siamese]

    • D.

      H=[fish => Catfish dog => Terrier cat => Siamese]

    • E.

      None of the above

    Correct Answer
    E. None of the above
    Explanation
    The correct answer is E - "None of the above"
    The correct answer to define such hash is h={"fish" => "Catfish", "dog" => "Terrier", "cat" => "Siamese"}

    Rate this question:

  • 9. 

    Let's define a hash h. h={"fish" => "Catfish", "dog" => "Terrier", "cat" => "Siamese"} What is the right way to refer to the second element of this hash?

    • A.

      H[1]

    • B.

      H[2]

    • C.

      H[dog]

    • D.

      H['dog']

    • E.

      H["dog"]

    Correct Answer(s)
    D. H['dog']
    E. H["dog"]
    Explanation
    Answers D and E are correct.
    Value of h[1] and h[2] is nil and value of h[dog] prodices an error ...undefined local variable or method 'dog'...

    Rate this question:

  • 10. 

    Let's define a hash h. h={"fish" => "Catfish", "dog" => "Terrier", "cat" => "Siamese"} Which for loop produces an error?

    • A.

      For animal in h puts animal end puts h['cat']

    • B.

      For animal, type in h puts "I love " + type + " " +animal +"s" end puts h['cat']

    • C.

      For count in h puts "I love " + type + " " +animal +"s" end puts h['cat']

    • D.

      For h in h puts h end

    • E.

      For h in h puts h end puts h['cat']

    Correct Answer
    E. For h in h puts h end puts h['cat']
    Explanation
    Correct answer is E, it produces an error "can't convert String into Integer". This happens because after for loop is executed, variable h is redefined as two dimensional array. So notation h['cat'] is not legal, since indices of an array can only be numerical.

    Rate this question:

  • 11. 

    What will a standard output of this code snippet be?name = "Alva"print name if name = 'Ary ' ; print "Alva" ;

    • A.

      AryAlva

    • B.

      Ary Alva

    • C.

      Nothing

    • D.

      Ary Alva

    • E.

      None of the above

    Correct Answer
    C. Nothing
    Explanation
    The correct answer is C - there will be no output in a standard out stream. This is because the last line of the question ends with a semicolon. This semicolon signifies that there will be a valid statement after it.

    Rate this question:

  • 12. 

    What will a standard output of this code snippet be?def print_parameters(par1, par2, par3)        puts par1        puts '#{par2}'        puts "#{par3}"end   

    • A.

      Par1 par3

    • B.

      Par1 #{par2} par3

    • C.

      Par1 par2 par3

    • D.

      No output

    • E.

      None of the above

    Correct Answer
    D. No output
    Explanation
    The correct answer is D. There will be no output, since the method print_parameters was not called yet. The statements inside the method are executed only when the method is called.

    Rate this question:

  • 13. 

    Please look at this snippet of code:animal_sounds = {"dogs" => "bark", "elefant" => "trumpet", "lion" =>"roar", "cat" => "meow"}def zoo(param)        for each_animal, each_sound in param              puts each_sound        end        i_go ="Enough already!"endmy_pet_goes = zoo(animal_sounds)What is the value of my_pet_goes variable in the snippet of code below? 

    • A.

      Enough already!

    • B.

      Roar

    • C.

      Bark

    • D.

      Trumpet

    • E.

      None of the above

    Correct Answer
    A. Enough already!
    Explanation
    The correct answer is A - "Enough already!".
    The value of my_pet_goes variable is set by return value of zoo method. Even though there is no explicit return statement, by default, the method returns the last evaluated expression, which is i_go ="Enough already!"

    Rate this question:

  • 14. 

    What makes a snippet of code into a block?

    • A.

      Putting square braces before the first statement and after the last statement of the snippet of code turns it into a block.

    • B.

      Making a method out of snippet of code turns it into a block.

    • C.

      Putting the word "do" at the beginning of the first statement and the word "end" after the last statement of the snippet of code turns it into a block.

    • D.

      Putting curly braces before the first statement and after the last statement of the snippet of code turns it into a block.

    • E.

      None of the above

    Correct Answer(s)
    C. Putting the word "do" at the beginning of the first statement and the word "end" after the last statement of the snippet of code turns it into a block.
    D. Putting curly braces before the first statement and after the last statement of the snippet of code turns it into a block.
    Explanation
    Answers C and D are correct. The block can be created by surrounding set of statements with curly brackets or with "do" and "end" words.

    Rate this question:

  • 15. 

    What is correct way to pass parameters to a block?

    • A.

      Block parameters are located at the beginning of the block, enclosed in a pipe symbols (||) and are separated by a colon.

    • B.

      Block parameters are located at the beginning of the block, enclosed in a pipe symbols (||) and are separated by space.

    • C.

      Block parameters are located at the beginning of the block, enclosed in square brackets and are separated by semicolon.

    • D.

      Block parameters are located at the beginning of the block and separated by semicolon.

    • E.

      None of the above

    Correct Answer
    A. Block parameters are located at the beginning of the block, enclosed in a pipe symbols (||) and are separated by a colon.
    Explanation
    The correct answer is A - Block parameters are located at the beginning of the block, enclosed in a pipe symbols (||) and are separated by a colon.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 19, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 19, 2010
    Quiz Created by
    Ary.Alva
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.