Our experts have gathered these Ruby MCQs through research, and we hope that you will be able to see how much knowledge base you have for the subject of Ruby by answering these 50+ multiple-choice questions.
Get started now by scrolling down!
A. irb(main):001:0> "gin joint".length<br> => 9
B. irb(main):001:0> "hello"[2]<br> => "e"
C. irb(main):001:0> "Rick".index("c")<br> => 2
D. irb(main):001:0> "e"*3<br> => "eee"
E. None of the above<br>
A. h*ll* w*rld
B. h*llo world
C. h*ll* world
D. hello w*rld
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.
A. Instance variable
B. Pseudo variable
C. Class variable
D. Constant
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.<br>
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
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
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
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.
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
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.
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
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.
StatementX is incorrect. while Statement Y is correct
C.
Both the statements are correct.
D.
Both the statements are incorrect.
A. def‌ined
B. undef
C. self
D. rescue
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.
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.
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
A. x + y =15
B. x -y = 5
C. x % y = 2
D. x ** y = 250
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.
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 method1 and method2 when called with puts and print?
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
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 it the receiver and argument have the same object id.
C. .eqi? returns true if the receiver and argument have both the same type and equal values.
D. .equal? returns true it the receiver and argument have both the same type and equal values.
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 in Ruby?
puts "Ruby String".unpack('A6')
A.
A6
B.
Ruby
C.
R
D.
Ruby SÂ
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.Â
A. LJ Ruby is a true object-oriented programming language.
B. D Ruby can be used to write Common Gateway interface (CGI) scripts.
C. Ll Ruby cannot be embedded into Hypertext Markup Language (HTML).
D. Ci Ruby can be installed in both Windows and POSIX environments.
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 “Threadnew‘?
A.
 Thread.start
B.
Thread.fork
C.
Thread.current
D.
Thread.join
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
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.
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
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?
$debug=56
print "Sample Text\n" if $debug
A.
56
B.
Sample Text
C.
No output
D.
All Error is given
Consider the following Interactive Ruby Shell console 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<br> => 9
B.
irb(main):001:0> "hello"[2]<br> => "e"
C.
irb(main):001:0> "Rick".index("c")<br>
=> 2
D.
irb(main):001:0> "e"*3<br>
=> "eee"
E.
None of the above
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).
In the Ruby programming language, "true" is a type of :
A. instance variable
B. Pseudo variable
C. Class variable
D. Constant
A. U Catch def‌ines a block that is labeled with the given name.
B. D The catch block is executed normally even if a throw is encountered.
C. Ll When Ruby encounters a throw. it zips back the call stack looking for a catch block with a matching symbol.
D. D it the input does not contain correctly formatted lines. the throw will skip to the end of the corresponding catch.
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 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
C.
It gives an error on line number 3. Â
D.
It gives an error on line number 1.
What is the output of the following code in Ruby?
a = "hello world"
puts a.scan(/(..)(..)/)
A.
he
ll
o
wo
B.
[['he'. ‘ll“]. ['0 “. ‘wo']]
C.
hello
world
D.
["hello", "world"]
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.
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 @Q and are followed by the variable name.
D. Global variables are always preceded by the # character.
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_lNCREMENT value for a connection. Â
D.
It returns the current state of the database.
A.
if "gin joint".length >> 9
B.
if "Rick".index("c") >> 2
C.
if -1942.abs >> 1942
D.
if number = Math.abs(number)
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 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
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 oi 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
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.
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 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.
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.
A. i_i d.rewind moves the position in d to the f‌irst entry.
B. D d.tell returns the current position in d.
C. U d.read returns the current entry front d.
D. D d.close closes the directOry stream.