Ruby Programming Training Quiz #2

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,162
Questions: 15 | Attempts: 1,705

SettingsSettingsSettings
Ruby Programming Training Quiz #2 - Quiz

Test your proficiency in Ruby. Take other quizzes in Ruby series.
. . . I'd love to hear from you. Please leave your comments. . .
Best of luck!
*** Ary Alva ***


Questions and Answers
  • 1. 

    What is the correct way of defining a method with variable number of parameters?

    • A.

      Def my_method(param_all*)

    • B.

      Def my_method(*param_all)

    • C.

      Def my_method(first_param, *param_all)

    • D.

      Def my_method(*first_param, *param_all)

    • E.

      Def my_method(*first_param, param_all*)

    Correct Answer(s)
    B. Def my_method(*param_all)
    C. Def my_method(first_param, *param_all)
    Explanation
    The correct answers are B and C - The variable number of parameters in a method is denoted by prefixing the last parameter with an asterisk.

    Rate this question:

  • 2. 

    Which statement below contains a symbol?

    • A.

      H={:bird=>'fly', :horse=>'gallop', :snake=>'snake'}

    • B.

      :snake.to-s

    • C.

      H={bird:=>'fly', horse:=>'gallop', snake:=>'snake'}

    • D.

      Snake:.to_s

    • E.

      None of the above

    Correct Answer(s)
    A. H={:bird=>'fly', :horse=>'gallop', :snake=>'snake'}
    B. :snake.to-s
    Explanation
    The correct answers are A and B

    Rate this question:

  • 3. 

    Which statement uses an instance variable?

    • A.

      Sound="roar"

    • B.

      @@sound="roar"

    • C.

      @sound="roar"

    • D.

      Puts #{sound}

    • E.

      None of the above

    Correct Answer
    C. @sound="roar"
    Explanation
    The correct answer is C - The instance variable starts with a single at-sign

    Rate this question:

  • 4. 

    Which statement prints the objec animal?

    • A.

      Animal.to_s

    • B.

      P animal

    • C.

      Puts animal.inspect

    • D.

      None of the above

    Correct Answer(s)
    B. P animal
    C. Puts animal.inspect
    Explanation
    B and C are both correct answers. The statement p animal is equivalent of the statement puts animal.inspect

    Rate this question:

  • 5. 

    Which statement allows to modify instance variable @sound?

    • A.

      Attr @sound, TRUE

    • B.

      Attr :sound, TRUE

    • C.

      Attr_reader :sound

    • D.

      Attr_accessor :sound

    • E.

      Attr_writer :sound

    Correct Answer(s)
    B. Attr :sound, TRUE
    D. Attr_accessor :sound
    E. Attr_writer :sound
    Explanation
    The correct answers are B, D and E. The accessor which allows to modify an instance variable @sound can be any of these three statements:
    attr :sound, TRUE
    attr_accessor :sound
    attr_writer :sound

    Rate this question:

  • 6. 

    Which statement prints the Ruby interpreter load path, which is used to search for files listed with the "require" directive?

    • A.

      Puts ::

    • B.

      Puts $$

    • C.

      Puts $:

    • D.

      Puts @@

    • E.

      None of the above

    Correct Answer
    C. Puts $:
    Explanation
    The correct answer is C - the statement puts $: prints out Ruby interpreter load path.

    Rate this question:

  • 7. 

    Which of these statements is false?

    • A.

      Enumeratable class doesn't have iterator methods

    • B.

      You can not write your own iterator method

    • C.

      Array class doesn't have iterators methods

    • D.

      Hash class doesn't have iterators methods

    • E.

      All of the above

    Correct Answer
    E. All of the above
    Explanation
    The correct answer is E - All of the above statements are false.

    Rate this question:

  • 8. 

    What is the right way to continue Ruby statement on a second line?

    • A.

      Use a semicolon at the end of the first line

    • B.

      Use a backslash at the end of the first line

    • C.

      Break line at an operator like + or - or break after semicolon between method parameters

    • D.

      None of the above

    Correct Answer(s)
    B. Use a backslash at the end of the first line
    C. Break line at an operator like + or - or break after semicolon between method parameters
    Explanation
    The coreect answers are B and C

    Rate this question:

  • 9. 

    What is the right way to create subclass Bird based on the calss Animal?

    • A.

      Class Bird > Animal

    • B.

      Class Bird < Animal

    • C.

      Class Bird extends Animal

    • D.

      Calss Bird

    • E.

      None of the above

    Correct Answer
    B. Class Bird < Animal
    Explanation
    The correct answer is B - calss Bird < Animal

    Rate this question:

  • 10. 

    Which statement is false?

    • A.

      Super is Ruby keyword

    • B.

      Super invokes the method with the same name in the superclass

    • C.

      Super invokes the method with the same name in the Object class

    • D.

      Super can be used outside of the class

    • E.

      None of the above

    Correct Answer(s)
    A. Super is Ruby keyword
    C. Super invokes the method with the same name in the Object class
    D. Super can be used outside of the class
    E. None of the above
    Explanation
    Statements A, C, D, and E are false.

    Rate this question:

  • 11. 

    Class Animal is defined in the module Zoo which is stored in the file zoo_file.rbWhich statement is true?

    • A.

      To use the code from module Zoo you have to include the statement require 'zoo_file'

    • B.

      To use the code from module Zoo you have to start with the statement "include Zoo"

    • C.

      To use methods defined in the class Animal outside of the file zoo_file all methods from Animal class should be preceeded with Animal::

    • D.

      To avoid prefixing Animal methods with "Zoo::", the statement "include Zoo" should be located anywhere in the code after the statement "require zoo_file" and before methods from Animal class are used

    • E.

      Ruby code can be grouped into modules

    Correct Answer(s)
    A. To use the code from module Zoo you have to include the statement require 'zoo_file'
    D. To avoid prefixing Animal methods with "Zoo::", the statement "include Zoo" should be located anywhere in the code after the statement "require zoo_file" and before methods from Animal class are used
    E. Ruby code can be grouped into modules
    Explanation
    The statements A, D and E are correct. Ruby code can be grouped in the module. To use classes defined in the module, the statement require 'module_file_name' has to be present. To allow using the class methods without ModuleName::, the statement "include ModuleName" has to be included in the code after the statement "require 'module_file'" and before any methods are used.

    Rate this question:

  • 12. 

    Which identifyer listed below can be used as a name of a local variable?

    • A.

      Alias

    • B.

      Alias

    • C.

      #alias

    • D.

      %alias

    • E.

      None of the above

    Correct Answer
    B. Alias
    Explanation
    The correct answer is B. Ruby is a case sensitive language, so Alias is not the same a a key word alias. #alias is a comment and %alias is illegal, since alias is a keyword in Ruby and can not be used as a delimiter which is expected after %.

    Rate this question:

  • 13. 

    What is the right way to define "Here document" string in Ruby?

    • A.

      My_string=

    • B.

      My_string=

    • C.

      My_string=

    • D.

      Size="small" my_string=

    • E.

      None of the above

    Correct Answer(s)
    A. My_string=
    D. Size="small" my_string=
    Explanation
    The correct answers are A and D. The here document is interpolated unles singlo quotes around the delimiter are used.

    Rate this question:

  • 14. 

    A=5b=7Which of the statements below is illegal in Ruby?

    • A.

      A+b if TRUE

    • B.

      A+b if false

    • C.

      True=a+b

    • D.

      A + b if a==true

    • E.

      None of the above

    Correct Answer
    C. True=a+b
    Explanation
    The correct answer is C. The true is a spacial constant in Ruby and we can not assign any value to constants like self, true, false, nil, __FILE__, __LINE__

    Rate this question:

  • 15. 

    What is the output of this statementa= 3_1_4_1_5_9_2_6; puts a

    • A.

      3_1_4_1_5_9_2_6

    • B.

      3 1 4 1 5 9 2 6

    • C.

      31415926

    • D.

      None of the above

    Correct Answer
    C. 31415926
    Explanation
    The correct answer is C. Underscore characters are ignored in the digit string.

    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 21, 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.