Knowledge of Ruby Skills Test Answer


Read the following statements and select the correct option.Statement 1: When a Ruby program terminates, all running threads are killed, regardless of their states.
Statement 2: You can wait for a particular thread to finish by calling that thread's Thread#wait method.
a. Statement 1 is true, while Statement 2 is false.
b. Statement 1 is false, while Statement 2 is true.
c. Both the statements are true.
d. Both the statements are false.
Which of the following is used to cancel the method definition?
a. defined
b. undef
c. self
d. rescue
What is the output of the following code in Ruby?puts "Ruby String".unpack('A6')
a. A6
b. Ruby
c. R
d. Ruby S
Which of the following statements regarding Ruby variables is incorrect?
a. Local variables begin with a lowercase letter or _.
b. Instance variables are preceded by the at sign (@) followed by the variable name.
c. Class variables are preceded by the sign @@ and are followed by the variable name.
d. Global variables are always preceded by the # character.
What is the purpose of the following array method in Ruby?array.rassoc(key)
a. It searches through the array whose elements are also arrays.
b. It returns a new array containing the items array for which the block is not true.
c. It returns the number of non-nil elements in self.
Which of the following directives used with the method Time.strftime are correct?
a. %m represents minute of the hour (00 to 59).
b. %I represents hour of the day, 12-hour clock (01 to 12).
c. %A represents the abbreviated weekday name (Sun).
d. %j represents the day of the year (001 to 366).
Given below is a Ruby code snippet with line numbers:Note: A file named "test" is located at the location "/var/www/ruby/"

1. aFile = File.new("/var/www/ruby/test", "r")
2. if aFile
3. aFile.syswrite("RUBY Testing")
4. print "Done!"
5. else
6. puts "Unable to open file!"
7. end

What is the outcome when the above code is executed?
a. It gives an error on line number 2.
b. No error is generated, and it prints "Done".
c. It gives an error on line number 3.
d. It gives an error on line number 1.
Consider the following statements regarding "methods" in Ruby:Statement X: Method names should begin with an uppercase letter.
Statement Y: Methods should be defined before calling them, otherwise Ruby will raise an exception for undefined method invoking.

Select an appropriate option from below:
a. Statement X is correct, while Statement Y is incorrect.
b. Statement X is incorrect, while Statement Y is correct.
c. Both the statements are correct.
d. Both the statements are incorrect.
Which of the following statements regarding catch and throw blocks are true?
a. Catch defines a block that is labeled with the given name.
b. The catch block is executed normally even if a throw is encountered.
c. When Ruby encounters a throw, it zips back the call stack looking for a catch block with a matching symbol.
d. If the input does not contain correctly formatted lines, the throw will skip to the end of the corresponding catch.
Consider the following irb quotes. In one of the quotes, => is not the real irb result.
Which of the following is that quote?
a. irb(main):001:0> "gin joint".length
    => 9
b. irb(main):001:0> "hello"[2]
    => "e"
c. irb(main):001:0> "Rick".index("c")
    => 2
d. irb(main):001:0> "e"*3
    => "eee"
e. None of the above
What is the output of the following two lines of codes executed at the Ruby command prompt?puts 'escape the sentence using "\\"';
puts 'That\'s right, this is a test';
a. escape the sentence using "\"
    That's right, this is a test
b. escape the sentence using "\\"
    That's right, this is a test
c. escape the sentence using "\"
    That\'s right, this is a test
d. escape the sentence using "\"
    Thats right, this is a test
Consider the following statements regarding range operators in Ruby:Statement X: The two-dot form, "..", range operator creates a range from start point to end point inclusive.
Statement Y: The three-dot form, "...", range operator creates a range from start point to end point exclusive.
a. Statement X is correct, while Statement Y is incorrect.
b. Statement X is incorrect, while Statement Y is correct.
c. Both the statements are correct.
d. Both the statements are incorrect.
The following code snippet has two methods, namely "method1" and "method2".def method1
   x = "This"
   y = "is"
   z = "test"
end
puts method1
def method2
   x = "This"
   y = "is"
   z = "test"
  return x, y, z
end
print method2

What will be the output of the respective methods when called?
a. Output of "puts method1" will be "test"
b. Output of "print method2" will be "test"
c. Output of "print method2" will be "Thisistest"
d. Output of "puts method1" will be nil
The package that contains information about an exception is an object of class exception or is one of class exception's children . Every exception returns information to understand the nature of the exception. Which of the following are provided by exceptions?
a. Message string
b. Stack Backtrace
c. Code where the exception occurred
d. All of the above
Ruby's hash is an indexed collection. It stores a collection of objects. Which of the following can be used as a key to retrieve a value from the hash?
a. Only integers
b. Only string objects
c. Any type of object
d. An integer or a string object
Which of the following statements regarding Ruby are correct?
a. Ruby is a true object-oriented programming language.
b. Ruby can be used to write Common Gateway Interface (CGI) scripts.
c. Ruby cannot be embedded into Hypertext Markup Language (HTML).
d. Ruby can be installed in both Windows and POSIX environments.
What is the output if the following code is executed at the Ruby command prompt?foo = "testing"
puts defined? foo
puts defined? $_
puts defined? foo2
a. local-variable
    global-variable
    nil
b. testing
    global-variable
    not defined
c. testing
    nil
    nil
d. local-variable
    global-variable
    not defined
Assuming that f is an instance of the File class:
a. f.path returns the pathname used to create f.
b. f.ctime returns the last access time for f.
c. f.chmode(mode) changes the permission mode of f.
d. f.mtime returns the last inode change time for f.
What is the output of the following code in Ruby?puts "hello world".sub(/[aeiou]/, '*')
a. h*ll* w*rld
b. h*llo world
c. h*ll* world
d. hello w*rld
What is the output of the following code?$var = 1
print "1 -- Step 1\n" if $var
print "2 -- Step 2\n" unless $var
$var = false
print "3 -- Step 3\n" unless $var
a. 1 -- Step 1
    2 - Step 2
    3 -- Step 3
b. 1 -- Step 1
    3 -- Step 3
c. 1 -- Step 1
    2 -- Step 2
d. 1 -- Step 1
    nil
    nil
If there are two variables x and y, such that x=10 and y=5, then:
a. x + y =15
b. x - y = 5
c. x % y = 2
d. x ** y = 250
In the Ruby programming language, "true" is a type of :
a. Instance variable
b. Pseudo variable
c. Class variable
d. Constant
In the following statement, what is contained in the argument 'caller'?raise InterfaceException, "Keyboard failure", caller
a. stack trace
b. string 'caller'
c. Previous exception
d. None of the above
Which of the following are correct?
a. puts sayGoodnight "John-Boy"
b. puts sayGoodnight("John-Boy")
c. puts(sayGoodnight "John-Boy")
d. puts(sayGoodnight("John-Boy"))
e. All of the above
Ruby makes it easy to write multi-threaded programs with the Thread class. Ruby threads are a lightweight and efficient way to achieve parallelism in the code. Which of the following calls have the same functionality as "Thread.new"?
a. Thread.start
b. Thread.fork
c. Thread.current
d. Thread.join
What is the purpose of the following hash method in Ruby?hash.rehash
a. It rebuilds hash based on the current values for each key.
b. It returns hash (self).
c. It creates a new hash, inverting keys and values from hash. That is, in the new hash, keys from hash become values, and values become keys.
d. It tests whether hash is empty, returning true or false.
Assuming that d is an instance of Dir class:
a. d.rewind moves the position in d to the first entry.
b. d.tell returns the current position in d.
c. d.read returns the current entry from d.
d. d.close closes the directory stream.
DBD::Mysql implements some driver-specific functions to be used in Ruby. Given below is one such function:dbh.func(:insert_id) => Fixnum

What is the purpose of this function?
a. It returns the protocol being used for the communication.
b. It returns the current thread ID.
c. It returns the most recent AUTO_INCREMENT value for a connection.
d. It returns the current state of the database.
What is the output of the following code?$debug=56
print "Sample Text\n" if $debug
a. 56
b. Sample Text
c. No output
d. An Error is given
Which of the following are correct in Ruby syntax?
a. "gin joint".length >> 9
b. "Rick".index("c") >> 2
c. -1942.abs >> 1942
d. number = Math.abs(number)
Given below are two different cases of class definition, and object instantiation in Ruby.Case 1:
-----------
class Demo
end
  p = Demo.new
Case 2:
---------
class Demo2
  def initialize(x,y)
    @x, @y = x, y
  end
end
  p = Demo2.initialize(15,20)

What happens when both these cases are executed separately?
a. Case 1 gives a compilation error indicating that no structure is defined.
b. Case 2 gives an error for the "initialize" method.
c. Case 1 gives no error.
d. Case 2 gives no error.
Read the following statements and select the correct option.Statement 1: A thread shares all global, instance, and local variables that are in existence at the time the thread starts.
Statement 2: Ruby threads are totally out-of-process, and are implemented outside the Ruby interpreter.
a. Statement 1 is true, while Statement 2 is false.
b. Statement 1 is false, while Statement 2 is true.
c. Both the statements are true.
d. Both the statements are false.
Which of the following statements regarding the Ruby programming language are correct?
a. Ruby identifiers are case insensitive.
b. Ruby identifier names may consist of alphanumeric characters and the underscore character ( _ ).
c. "rescue" is a reserved word in RUBY, but it can be used as a method name.
What is the output of the following code, when executed at the Ruby command prompt?puts "This is sentence 1."
BEGIN {
    puts "This is sentence 2."
}
a. This is sentence 2.    This is sentence 1.
b. This is sentence 1.
    This is sentence 2.
c. This is sentence 1.This is sentence 2.
d. The code will give a syntax error.
Which of the following operators indicate continuation of a statement in Ruby?
a. ;
b. +
c. -
d. \
What is the output of the following code in Ruby?x= "Ruby" + "Strings"
puts x
y= "Ruby" << "Strings"
puts y
z = "Ruby".concat("Strings")
puts z
a. RubyStrings
    RubyStrings
    RubyStrings
b. RubyStrings
    Strings
    RubyStrings
c. RubyStrings
    Ruby
    nil
d. RubyStrings
    nil
    RubyStrings
Consider the following statements regarding environment variables in Ruby:Statement 1: Environment variable "RUBYLIB" defines the search path for libraries.
Statement 2: Environment variable "RUBYLIB_PREFIX" modifies the "RUBYLIB" search path by replacing the prefix of a particular library.
a. Statement 1 is correct, while Statement 2 is incorrect.
b. Statement 2 is correct, while Statement 1 is incorrect.
c. Both the statements are correct.
d. Both the statements are incorrect.
What is the output of the following code, when executed at the Ruby command prompt?print <<EOF
This is a sample text
EOF
print <<"EOF";
This is a sample text too
EOF
a. This is a sample text    This is a sample text too
b. This is a sample text
    This is a sample text too
c. This is a sample textThis is a sample text too
d. The code gives a syntax error.
Which of the following statements regarding ".eql?" and ".equal?" are correct?
a. .eql? returns true if the receiver and argument have the same object id.
b. .equal? returns true if the receiver and argument have the same object id.
c. .eql? returns true if the receiver and argument have both the same type and equal values.
d. .equal? returns true if the receiver and argument have both the same type and equal values.
In a Ruby installed environment, what happens when "irb" is typed in the command prompt?
a. It starts an Interactive Ruby Session.
b. It enables parser debug mode.
c. It displays an overview of command-line options.
d. It overwrites the file contents with program output.
What is the output of the following line of code executed at the Ruby command prompt?puts "The product of 5,10,15 is #{5*10*15}";
a. 750
b. The product of 5,10,15 is 750
c. The product of 5,10,15 is #{5*10*15}
d. It will give typecasting error.
What is the output of the Ruby code (with line numbers) shown below?1. (0...5).each do |i|
2. puts "Value of local variable is #{i}"
3. end
a. Value of local variable is 0
    Value of local variable is 1
    Value of local variable is 2
    Value of local variable is 3
    Value of local variable is 4
    Value of local variable is 5
b. Value of local variable is 1
    Value of local variable is 2
    Value of local variable is 3
    Value of local variable is 4
c. Value of local variable is 0
    Value of local variable is 1
    Value of local variable is 2
    Value of local variable is 3
    Value of local variable is 4
d. The code gives an error on line number "1"
Consider the following two commands issued at the Ruby command prompt:Command 1: $ ruby -c
Command 2: $ ruby -C dir

Which of the following statements regarding these two commands are correct?
a. Command 1 checks syntax only, without executing the program.
b. Command 1 changes the directory before executing.
c. Command 2 checks syntax only, without executing the program.
d. Command 2 changes the directory before executing.
What is the purpose of the following hash method in Ruby?hash.to_a
a. It creates a two-dimensional array from hash. Each key/value pair is converted to an array, and all these arrays are stored in a containing array.
b. It returns a new array containing all the values of hash.
c. It returns a new array containing the values from hash that are associated with the given key or keys.
d. It converts hash to an array, and then converts that array to a string.
Given below is a Ruby code demonstrating Exception Handling with begin/end block, and rescue clauses.begin
    puts 'This is before the raise.'
    raise 'Raising an Error'
    puts 'This is after the raise.'
rescue
    puts 'This is being rescued'
end
puts 'This is after the begin block.'

What is the output the above code?
a. This is being rescued
    This is after the begin block.
b. This is before the raise.
    This is after the raise.
    This is after the begin block.
c. This is before the raise.
    This is being rescued
    This is after the begin block.
Consider an array "colors" which has the following contents:colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]

Which of the given options will print the following output?

Red
Blue
Yellow
Green
Violet
Black
a. colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]
    for color in 0...colors.length
       print colors[-color -1], "\n";
    end
b. colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]
    colors.each {|color| puts color}
c. colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]
    for color in 0..colors.length
       print colors[color], "\n";
    end
d. colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]
    for color in 0...colors.length
       print colors[color], "\n";
    end
What is the difference between "redo" and "retry" statements in Ruby?
a. "redo" restarts an iteration of the most internal loop, without checking loop condition.
b. "retry" restarts the invocation of an iterator call. Also, arguments to the iterator are re-evaluated.
c. "redo" restarts the invocation of an iterator call. Also, arguments to the iterator are re-evaluated.
d. "retry" restarts an iteration of the most internal loop, without checking loop condition.
What is the output of the following code in Ruby?a = "hello world"
puts a.scan(/(..)(..)/)
a. he
    ll
    o
    wo
b. [["he", "ll"], ["o ", "wo"]]
c. hello
    world
d. ["hello", "world"]
Assuming that f is an instance of the File class:
a. f.path returns the pathname used to create f.
b. f.ctime returns the last access time for f.
c. f.chmod(mode) changes the permission mode of f.
d. f.mtime returns the last inode change time for f.
What is the output if the following code is executed at the Ruby command prompt?foo = "testing"
puts defined? foo
puts defined? $_
a. local-variable
    global-variable
b. testing
    global-variable
c. testing
    nil
d. None of the above
What is the output of the following code, when executed at the Ruby command prompt?print <<EOF
This is a sample text
EOF
print <<"EOF";
This is a sample text too
EOF
a. This is a sample text
    This is a sample text too
b. This is a sample text
    This is a sample text too
c. This is a sample textThis is a sample text too
d. The code gives a syntax error.
What is the output of the following code, when executed at the Ruby command prompt?puts "This is sentence 1."
BEGIN {
    puts "This is sentence 2."
}
a. This is sentence 2.
    This is sentence 1.
b. This is sentence 1.
    This is sentence 2.
c. This is sentence 1.This is sentence 2.
d. The code will give a syntax error.
What is the output of the following code?$var = 1
print "1 -- Step 1\n" if $var
print "2 -- Step 2\n" unless $var
$var = false
print "3 -- Step 3\n" unless $var
a. 1 -- Step 1
    2 - Step 2
    3 -- Step 3
b. 1 -- Step 1
    3 -- Step 3
c. 1 -- Step 1
    2 -- Step 2
d. 1 -- Step 1
    nil
    nil
Consider the following irb quotes. In one of the quotes, => is not the real irb result.
Which of the following is that quote
a. irb(main):001:0> "gin joint".length
    => 9
b. irb(main):001:0> "hello"[2]
    => "e"
c. irb(main):001:0> "Rick".index("c")
    => 2
d. irb(main):001:0> "e"*3
    => "eee"
e. None of the above
What is the output of the following code in Ruby?x= "Ruby" + "Strings"
puts x
y= "Ruby" << "Strings"
puts y
z = "Ruby".concat("Strings")
puts z
a. RubyStrings
    RubyStrings
    RubyStrings
b. RubyStrings
    Strings
    RubyStrings
c. RubyStrings
    Ruby
    nil
d. RubyStrings
    nil
    RubyStrings
What is the output of the Ruby code (with line numbers) shown below?1. (0...5).each do |i|
2.    puts "Value of local variable is #{i}"
3. end
a. Value of local variable is 0
    Value of local variable is 1
    Value of local variable is 2
    Value of local variable is 3
    Value of local variable is 4
    Value of local variable is 5
b. Value of local variable is 1
    Value of local variable is 2
    Value of local variable is 3
    Value of local variable is 4
c. Value of local variable is 0
    Value of local variable is 1
    Value of local variable is 2
    Value of local variable is 3
    Value of local variable is 4
d. The code gives an error on line number "1"
Which of the following are correct in Ruby syntax?
a. "gin joint".length >> 9
b. "Rick".index("c") >> 2
c. -1942.abs >> 1942
d. number = Math.abs(number)
Given below is a Ruby code demonstrating Exception Handling with begin/end block, and rescue clauses.begin
    puts 'This is before the raise.'
    raise 'Raising an Error'
    puts 'This is after the raise.'
rescue
    puts 'This is being rescued'
end
puts 'This is after the begin block.'

What is the output the above code?
a. This is being rescued
    This is after the begin block.
b. This is before the raise.
    This is after the raise.
    This is after the begin block.
c. This is before the raise.
    This is being rescued
    This is after the begin block.
What is the output of the following code in Ruby?
a. = "hello world"
    puts a.scan(/(..)(..)/)
    a.he
    ll
    o
    wo
b. [["he", "ll"], ["o ", "wo"]]
c. hello
    world
d. ["hello", "world"]
What is the output if the following code is executed at the Ruby command prompt?foo = "testing"
puts defined? foo
puts defined? $_
a. local-variable
    global-variable
b. testing
    global-variable
c. testing
    nil
d. None of the above