1.
In Ruby, the difference between using double quotes and single quotes to make a string, is
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.
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
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).
3.
The string method in ruby that helps remove trailing newline character from a string str is
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.
4.
The right way to copy an array a to b is
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.
5.
______________________ Returns the operating system’s temporary file path.
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.
6.
Instance variables need not be declared
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.
7.
Which of the following statements is correct pertaining to instance variables in ruby
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.
8.
Instance variables in ruby are prefixed by _____ symbol
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.
9.
The ________________ method removes all duplicates and retains all distinct elements in an array.
Correct Answer
A. Uniq
Explanation
The "uniq" method is used to remove all duplicates from an array and only retain the distinct elements.
10.
In Ruby, the stream that reads from a file passed via command line is called
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.
11.
The ruby method to trim all leading trailing whitespaces is
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.
12.
In ruby, By convention, the names of procedures that store values into previously allocated locations usually end in
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.
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.
14.
If ASCII for A = '65', then "AAAA".unpack('C') returns
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.
15.
___________________ calls a block for each item and returns a new array based on the results of the block.
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.
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.
17.
In ruby, to convert a string str to uppercase
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.
18.
In ruby, to convert a string str to lowercase
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.
19.
In ruby,y = 'str'puts 'Test, #{y}'results in
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}'.
20.
In ruby, !!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.
21.
In ruby, !!nil means
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.
22.
'Lorem ipsum dolor sit ame' =~ /ip/
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.
23.
'Lorem ipsum dolor sit ame' =~ /L/
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.
24.
'Lorem ipsum dolor sit ame' = ~ /fg/ # =>
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".
25.
'Lorem ipsum dolor sit ame' ! ~ /fg/ # =>
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.
26.
The three major types of variables in ruby are
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.