Ruby Programming Training Quiz #1

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Ary.Alva
A
Ary.Alva
Community Contributor
Quizzes Created: 2 | Total Attempts: 2,178
| Attempts: 468 | Questions: 15
Please wait...
Question 1 / 15
0 %
0/100
Score 0/100
1. What is the first index of an array

Explanation

An array's indices start with 0

Submit
Please wait...
About This Quiz
Ruby Quizzes & Trivia

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

*** Ary Alva ***

Tell us your name to personalize your report, certificate & get on the leaderboard!
2. Ruby array can contain values of which type?

Explanation

Ruby array may contain values of any type

Submit
3. What is the size of Ruby array

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.

Submit
4. 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!"
end
my_pet_goes = zoo(animal_sounds)


What is the value of my_pet_goes variable in the snippet of code below?
 

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

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

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.

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

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.

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

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

Submit
8. What is correct way to pass parameters to a block?

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.

Submit
9. Which statement correctly defines hash h?

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

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

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

Submit
11. What makes a snippet of code 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.

Submit
12. 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?

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'...

Submit
13. What will a standard output of this code snippet be?

def print_parameters(par1, par2, par3)
        puts par1
        puts '#{par2}'
        puts "#{par3}"

end
   

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.

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

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.

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

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.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 19, 2023 +

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
Cancel
  • All
    All (15)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the first index of an array
Ruby array can contain values of which type?
What is the size of Ruby array
Please look at this snippet of code:animal_sounds = {"dogs" =>...
What is the difference between a hash and an array in Ruby?
What is the name of key/value pair in Ruby hash?
Let's create an array:a=[12345, 23456, 34567, 98765]Which...
What is correct way to pass parameters to a block?
Which statement correctly defines hash h?
Let's create an array:a=[1,2,3,4,5]Which statement creates the...
What makes a snippet of code into a block?
Let's define a hash h. h={"fish" => "Catfish", "dog" =>...
What will a standard output of this code snippet be?def...
What will a standard output of this code snippet be?name =...
Let's define a hash h. h={"fish" => "Catfish", "dog" =>...
Alert!

Advertisement