Ruby Programming Quiz

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 S_sushant
S
S_sushant
Community Contributor
Quizzes Created: 2 | Total Attempts: 22,409
| Attempts: 660 | Questions: 26
Please wait...
Question 1 / 26
0 %
0/100
Score 0/100
1. Instance variables in ruby are prefixed by _____ symbol

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.

Submit
Please wait...
About This Quiz
Ruby 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!

Personalize your quiz and earn a certificate with your name on it!
2. The three major types of variables in ruby are

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.

Submit
3. In ruby, to convert a string str to uppercase

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.

Submit
4. Instance variables need not be declared

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.

Submit
5. In ruby, to convert a string str to lowercase

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.

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

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.

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

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.

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

Explanation

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

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

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.

Submit
10. 'Lorem ipsum dolor sit ame'  =~ /ip/

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.

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

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.

Submit
12. In ruby, !!0

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.

Submit
13. 'Lorem ipsum dolor sit ame'  =~ /L/

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.

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

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.

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

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

Submit
16. 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

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

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

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.

Submit
18. In ruby, !!nil means

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.

Submit
19. ______________________  Returns the operating system's temporary file path.

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.

Submit
20. The ruby method to trim all leading trailing whitespaces is

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.

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

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.

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

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

Submit
23. The right way to copy an array a to b is

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.

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

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.

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

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.

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

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.

Submit
View My Results

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

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
Cancel
  • All
    All (26)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Instance variables in ruby are prefixed by _____ symbol
The three major types of variables in ruby are
In ruby, to convert a string str to uppercase
Instance variables need not be declared
In ruby, to convert a string str to lowercase
The string method in ruby that helps remove trailing newline character...
___________________ calls a block for each item and returns a new...
The ________________ method removes all duplicates and retains all...
In Ruby, the difference between using double quotes and single quotes...
'Lorem ipsum dolor sit ame'  =~ /ip/
Which of the following statements is correct pertaining to instance...
In ruby, !!0
'Lorem ipsum dolor sit ame'  =~ /L/
In ruby, By convention, the names of procedures that store values...
In ruby,y = 'str'puts 'Test, #{y}'results in
In Ruby, the order of operations for numeric variables follows the...
The command to check the syntax of a ruby file named test.rb is
In ruby, !!nil means
______________________  Returns the operating system's temporary...
The ruby method to trim all leading trailing whitespaces is
If ASCII for A = '65', then...
'Lorem ipsum dolor sit ame'  = ~  /fg/   #...
The right way to copy an array a to b is
'Lorem ipsum dolor sit ame'  ! ~  /fg/   #...
In Ruby, the stream that reads from a file passed via command line is...
Provide a ruby function using gsub and regex to delete all whitespaces...
Alert!

Advertisement