Ruby Programming Quiz

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 S_sushant
S
S_sushant
Community Contributor
Quizzes Created: 2 | Total Attempts: 22,050
Questions: 26 | Attempts: 643

SettingsSettingsSettings
Programming Quizzes & Trivia

Developed in 1995, Ruby is an object-oriented programming language that is dynamic, reflective and general-purpose. What do you know about the ins and outs of the language? Let’s take a look in this programming-themed quiz!


Questions and Answers
  • 1. 

    In Ruby, the difference between using double quotes and single quotes to make a string, is

    • A.

      Single quotes will ignore any variables it finds with #{}, while double quotes will replace the variables it finds with #{}

    • B.

      Double quotes will ignore any variables it finds with #{}, while single quotes will replace the variables it finds with #{}

    • C.

      Single quotes will ignore any variables it finds with #{''}, while double quotes will replace the variables it finds with #{''}

    • D.

      Double quotes will ignore any variables it finds with #{''}, while Single quotes will replace the variables it finds with #{''}

    Correct Answer
    A. Single quotes will ignore any variables it finds with #{}, while double quotes will replace the variables it finds with #{}
    Explanation
    In Ruby, when using single quotes to create a string, any variables enclosed within #{ } will be treated as literal characters and not be evaluated. On the other hand, when using double quotes, the variables enclosed within #{ } will be replaced with their corresponding values.

    Rate this question:

  • 2. 

    In Ruby, the order of operations for numeric variables follows the acronym ..... where A is Addition,D is Division,E is Exponent, M is Multiplication,P is Parenthesis, S is Subtraction

    • A.

      Left to Right, irrespective of what the operation is

    • B.

      Parenthesis(P) first and then Left to Right, irrespective of what the operation is

    • C.

      PEDMAS

    • D.

      PEMDAS

    Correct Answer
    D. PEMDAS
    Explanation
    The correct answer is PEMDAS. In Ruby, the order of operations for numeric variables follows the acronym PEMDAS, where P stands for Parenthesis, E stands for Exponent, M stands for Multiplication, D stands for Division, A stands for Addition, and S stands for Subtraction. This means that calculations inside parentheses are performed first, followed by exponentiation, then multiplication and division (from left to right), and finally addition and subtraction (from left to right).

    Rate this question:

  • 3. 

    The string method in ruby that helps remove trailing newline character from a string str is

    • A.

      Str.chop

    • B.

      Str.chomp

    • C.

      Str.each_line

    • D.

      Str.slice

    Correct Answer
    B. Str.chomp
    Explanation
    The correct answer is `str.chomp`. This method removes the trailing newline character from a string. It is commonly used when reading input from a file or user input, as it allows for cleaner and more reliable processing of the string. The `chop` method removes the last character of the string, but it does not specifically target newline characters. The `each_line` method is used to iterate over each line of a string, and the `slice` method is used to extract a portion of a string.

    Rate this question:

  • 4. 

    The right way to copy an array a to b is

    • A.

      Clone

    • B.

      B = a.collect

    • C.

      B = a.copy

    • D.

      Use the each method with a do block to copy each element

    Correct Answer
    A. Clone
    Explanation
    The correct answer is "clone" because the clone method creates a shallow copy of the array, meaning that it creates a new array object with the same elements as the original array. This ensures that any changes made to the new array do not affect the original array, and vice versa.

    Rate this question:

  • 5. 

    ______________________  Returns the operating system’s temporary file path.

    • A.

      Dir()

    • B.

      Tmpdir()

    • C.

      Tempfile()

    • D.

      Tempdir()

    Correct Answer
    B. Tmpdir()
    Explanation
    The tmpdir() function returns the operating system's temporary file path. This path is commonly used to store temporary files that are created and used during the execution of a program. The tmpdir() function is useful when a program needs to create temporary files or directories and wants to ensure that they are stored in the appropriate location for the operating system being used.

    Rate this question:

  • 6. 

    Instance variables need not be declared

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In object-oriented programming, instance variables are variables that are declared within a class but outside any method. They hold data that is unique to each instance of the class. In some programming languages, such as Python, instance variables do not need to be explicitly declared before they are used. They are automatically created when an object is instantiated. Therefore, the statement "Instance variables need not be declared" is true.

    Rate this question:

  • 7. 

    Which of the following statements is correct pertaining to instance variables in ruby

    • A.

      Scope of the instance variable is restricted to the object self refers to

    • B.

      Scope of instance variables is limited to the class in which the variable is instantiated

    • C.

      Instance variables are always public variables

    • D.

      Instance variables can be used as constants in a program

    Correct Answer
    A. Scope of the instance variable is restricted to the object self refers to
    Explanation
    Instance variables in Ruby have a scope that is limited to the object that they belong to. This means that each object has its own set of instance variables, and these variables can only be accessed by methods within that object. The scope of an instance variable is not limited to the class in which it is instantiated, as it can be accessed by any method within the object. Instance variables are not always public variables, as their visibility can be controlled using access modifiers. Instance variables cannot be used as constants in a program, as their values can be changed.

    Rate this question:

  • 8. 

    Instance variables in ruby are prefixed by _____ symbol

    • A.

      @

    • B.

      #

    • C.

      ~

    • D.

      ^

    Correct Answer
    A. @
    Explanation
    In Ruby, instance variables are prefixed by the "@" symbol. This symbol is used to declare and access instance variables within a class. Instance variables are unique to each instance of a class and can be accessed and modified by methods within that class. By using the "@" symbol, Ruby distinguishes instance variables from local variables and allows them to be used throughout the class.

    Rate this question:

  • 9. 

    The ________________ method removes all duplicates and retains all distinct elements in an array.

    • A.

      Uniq

    • B.

      Unique

    • C.

      Distinct

    • D.

      One

    Correct Answer
    A. Uniq
    Explanation
    The "uniq" method is used to remove all duplicates from an array and only retain the distinct elements.

    Rate this question:

  • 10. 

    In Ruby, the stream that reads from a file passed via command line is called

    • A.

      ARGF

    • B.

      ARGV

    • C.

      STDIN

    • D.

      -n flag

    Correct Answer
    A. ARGF
    Explanation
    ARGF is the correct answer because in Ruby, ARGF is a stream that reads from a file passed via the command line. It allows the program to read input from multiple files or from standard input (STDIN) depending on the arguments passed. ARGV is an array that stores the command line arguments, STDIN is a constant that represents the standard input stream, and the -n flag is a command line option used for line-by-line processing of input files.

    Rate this question:

  • 11. 

    The ruby method to trim all leading trailing whitespaces is

    • A.

      Strip()

    • B.

      Trim()

    • C.

      Chomp()

    • D.

      Chop()

    Correct Answer
    A. Strip()
    Explanation
    The correct answer is strip(). The strip() method in Ruby is used to remove all leading and trailing whitespaces from a string. It does not modify the original string, but instead returns a new string with the whitespaces removed. This method is commonly used to clean up user input or to remove unnecessary spaces in a string before further processing.

    Rate this question:

  • 12. 

    In ruby, By convention, the names of procedures that store values into previously allocated locations  usually end in 

    • A.

      !

    • B.

      []

    • C.

      ()

    • D.

      ?

    Correct Answer
    A. !
    Explanation
    In Ruby, by convention, the names of procedures that store values into previously allocated locations usually end with an exclamation mark (!). This is known as a "bang" method and is used to indicate that the method will modify the object it is called on. It is a way to differentiate between methods that modify the object and methods that do not.

    Rate this question:

  • 13. 

    Provide a ruby function using gsub and regex to delete all whitespaces in a string str

    Correct Answer
    str.gsub!(/\s+/, "")
    Explanation
    The given answer suggests using the `gsub!` method in Ruby along with regular expressions to delete all whitespaces in a string. The regular expression `/\s+/` matches one or more whitespace characters. The `gsub!` method replaces all occurrences of the matched pattern with an empty string, effectively removing all whitespaces from the string. By using `gsub!` instead of `gsub`, the original string is modified in place.

    Rate this question:

  • 14. 

    If ASCII for A = '65', then "AAAA".unpack('C') returns

    • A.

      [65]

    • B.

      [65, 65, 65, 65]

    • C.

      [65, nil, 65, nil, nil]

    • D.

      14

    Correct Answer
    A. [65]
    Explanation
    The given code "AAAA".unpack('C') returns [65]. The unpack method in Ruby converts a string into an array of integers representing the ASCII values of each character in the string. In this case, the string "AAAA" has four 'A' characters, and the ASCII value of 'A' is 65. Therefore, the unpack method returns an array with a single element, which is 65.

    Rate this question:

  • 15. 

    ___________________ calls a block for each item and returns a new array based on the results of the block.

    • A.

      Array.map

    • B.

      Array.select

    • C.

      Array.each

    • D.

      Array.each_with_index

    Correct Answer
    A. Array.map
    Explanation
    Array.map is the correct answer because it calls a block for each item in the array and returns a new array based on the results of the block. This method is commonly used to transform or modify each element of an array according to the logic defined in the block, and then returns the resulting array.

    Rate this question:

  • 16. 

    The command to check the syntax of a ruby file named test.rb is

    Correct Answer
    ruby -c test.rb
    Explanation
    The command "ruby -c test.rb" is used to check the syntax of a Ruby file named test.rb. This command allows you to verify if there are any syntax errors in the code without actually executing it. By running this command, you can ensure that your Ruby code is written correctly and will not encounter any syntax-related issues when it is executed.

    Rate this question:

  • 17. 

    In ruby, to convert a string str to uppercase

    • A.

      Str.upcase

    • B.

      Str.uppercase

    • C.

      Str.upper

    • D.

      Str.UPPER

    Correct Answer
    A. Str.upcase
    Explanation
    The correct answer is "str.upcase" because the "upcase" method in Ruby is used to convert a string to uppercase. This method returns a new string with all the characters in the original string converted to uppercase.

    Rate this question:

  • 18. 

    In ruby, to convert a string str to lowercase

    • A.

      Str.downcase

    • B.

      Str.lower

    • C.

      Str.lowercase

    • D.

      Str.lcase

    Correct Answer
    A. Str.downcase
    Explanation
    The correct answer is "str.downcase" because the downcase method is used in Ruby to convert a string to lowercase. The downcase method returns a new string with all uppercase characters converted to lowercase, while leaving lowercase characters unaffected.

    Rate this question:

  • 19. 

    In ruby,y = 'str'puts 'Test, #{y}'results in

    • A.

      Test, #{y}

    • B.

      Test, str

    • C.

      Test, y

    • D.

      Test,

    Correct Answer
    A. Test, #{y}
    Explanation
    In Ruby, when using single quotes to define a string, any variables or expressions within the string are not evaluated and are treated as literal characters. Therefore, the expression '#{y}' within the string 'Test, #{y}' will not be interpreted as the value of the variable y, but will be treated as literal characters. Hence, the result will be 'Test, #{y}'.

    Rate this question:

  • 20. 

    In ruby, !!0

    • A.

      True

    • B.

      False

    • C.

      Nil

    • D.

      0

    Correct Answer
    A. True
    Explanation
    In Ruby, the double negation operator (!!) is used to convert a value to its boolean equivalent. In this case, the value 0 is converted to its boolean equivalent, which is false. However, when we apply the double negation operator to false, it returns true. Therefore, the expression !!0 evaluates to true.

    Rate this question:

  • 21. 

    In ruby, !!nil means

    • A.

      True

    • B.

      False

    • C.

      Nil

    • D.

      0

    Correct Answer
    B. False
    Explanation
    In Ruby, the expression !!nil is used to convert the value of nil into its boolean equivalent. Since nil represents the absence of a value, the first negation (!nil) turns it into true. The second negation (!true) then converts it back to false. Therefore, the correct answer is false.

    Rate this question:

  • 22. 

    'Lorem ipsum dolor sit ame'  =~ /ip/

    • A.

      0

    • B.

      Nil

    • C.

      True

    • D.

      6

    Correct Answer
    D. 6
    Explanation
    The given code is using the regular expression pattern matching operator "=~" to check if the string "Lorem ipsum dolor sit ame" contains the substring "ip". Since the substring "ip" is present in the given string, the expression will return a truthy value. In Ruby, any non-nil and non-false value is considered truthy. The only option among the given choices that represents a truthy value is 6.

    Rate this question:

  • 23. 

    'Lorem ipsum dolor sit ame'  =~ /L/

    • A.

      0

    • B.

      Nil

    • C.

      True

    • D.

      1

    Correct Answer
    A. 0
    Explanation
    The given regular expression `/L/` is used to search for the letter "L" in the string "Lorem ipsum dolor sit ame". Since the letter "L" is not present in the string, the expression returns a match result of 0, indicating that there is no match found.

    Rate this question:

  • 24. 

    'Lorem ipsum dolor sit ame'  = ~  /fg/   #  =>

    • A.

      0

    • B.

      Nil

    • C.

      True

    • D.

      False

    Correct Answer
    B. Nil
    Explanation
    The given code snippet assigns the value "~ /fg/ # =>" to the string "Lorem ipsum dolor sit ame". However, this value is not a valid expression in the programming language being used. Therefore, the assignment fails and the value of the string remains unchanged, resulting in the output "nil".

    Rate this question:

  • 25. 

    'Lorem ipsum dolor sit ame'  ! ~  /fg/   #  =>

    • A.

      0

    • B.

      True

    • C.

      Nil

    • D.

      False

    Correct Answer
    B. True
    Explanation
    The given answer "true" is correct because in programming, the value "true" typically represents a boolean value that is true or valid. In this case, the given string "Lorem ipsum dolor sit ame" is not relevant to the answer, as it is just a placeholder text. The symbols "! ~ /fg/ # =>" are also not relevant to determining the answer. Therefore, the correct answer is simply "true", indicating that the statement or condition is true.

    Rate this question:

  • 26. 

    The three major types of variables in ruby are

    • A.

      Local(_), Instance($) and Static($$)

    • B.

      Bool, Char & Float

    • C.

      Class (@@), Instance (@) and Global($)

    • D.

      There is no restriction on variable type in ruby

    Correct Answer
    C. Class (@@), Instance (@) and Global($)
    Explanation
    In Ruby, the three major types of variables are Class variables (denoted by @@), Instance variables (denoted by @), and Global variables (denoted by $). Class variables are shared among all instances of a class, while instance variables are unique to each instance of a class. Global variables can be accessed from anywhere in the program. This answer accurately identifies the three major types of variables in Ruby.

    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 18, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 15, 2015
    Quiz Created by
    S_sushant
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.