MCQs > IT & Programming > Ruby MCQs > Basic Ruby MCQs

Basic Ruby MCQ

1.

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.


Answer

Correct Answer:

Both the statements are correct. 


Note: This Question is unanswered, help us to find answer for this one

2. Which of the following operators indicate continuation of a statement In Ruby?

Answer

Correct Answer: +
-
\

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

3.

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.


Answer

Correct Answer:

Statement 1 is true. while Statement 2 is false.


Note: This Question is unanswered, help us to find answer for this one

4.

What is the purpose of the following array method in Ruby?

array.rassoc(key)


Answer

Correct Answer:

it searches through the array whose elements are also arrays. 


Note: This Question is unanswered, help us to find answer for this one

5.

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

}


Answer

Correct Answer:

This is sentence 2.

This is sentence 1.



Note: This Question is unanswered, help us to find answer for this one

6.

What is the purpose of the following hash method in Ruby?

hash.rehash


Answer

Correct Answer:

it rebuilds hash based on the current values for each key. 


Note: This Question is unanswered, help us to find answer for this one

7.

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


Answer

Correct Answer:

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 



Note: This Question is unanswered, help us to find answer for this one

8.

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'

Answer

Correct Answer:

escape the sentence using "\"

That's right, this is a test


Note: This Question is unanswered, help us to find answer for this one

9.

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

Answer

Correct Answer:

This is a sample text

This is a sample text too   



Note: This Question is unanswered, help us to find answer for this one

10. Which of the following options represent correct Ruby syntax?

Answer

Correct Answer:

if "Rick".index("c") &gt;&gt; 2


if -1942.abs &gt;&gt; 1942  


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

11.

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?


Answer

Correct Answer:

It returns the most recent AUTO_lNCREMENT value for a connection.  


Note: This Question is unanswered, help us to find answer for this one

12.

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.

Answer

Correct Answer:

Both the statements are correct.


Note: This Question is unanswered, help us to find answer for this one

13.

What is the output of the following code in Ruby?

a = "hello world"

puts a.scan(/(..)(..)/)


Answer

Correct Answer:

he

ll

o

wo


Note: This Question is unanswered, help us to find answer for this one

14.

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?

Answer

Correct Answer:

It gives an error on line number 3.  


Note: This Question is unanswered, help us to find answer for this one

15.

What is the purpose of the following hash method in Ruby?


hash.to_a


Answer

Correct Answer:

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.    



Note: This Question is unanswered, help us to find answer for this one

16. Which of the following statements regarding catch and throw blocks are true?

Answer

Correct Answer: U Catch def‌ines a block that is labeled with the given name.
Ll When Ruby encounters a throw. it zips back the call stack looking for a catch block with a matching symbol.
D it the input does not contain correctly formatted lines. the throw will skip to the end of the corresponding catch.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

17.

In the Ruby programming language, "true" is a type of :

Answer

Correct Answer: Pseudo variable

Note: This Question is unanswered, help us to find answer for this one

18.

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?


Answer

Correct Answer:

irb(main):001:0&gt; "hello"[2]<br> =&gt; "e"


Note: This Question is unanswered, help us to find answer for this one

19.

What is the output of the following code?

$debug=56

print "Sample Text\n" if $debug


Answer

Correct Answer:

Sample Text


Note: This Question is unanswered, help us to find answer for this one

20. Which of the following statements regarding the Ruby programming language are correct?

Answer

Correct Answer: Ruby identifier names may consist of alphanumeric characters and the underscore character ( _ ).
'rescue' is a reserved word in RUBY, but it can be used as a method name.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

21.

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

Answer

Correct Answer:

1 -- Step 1

3 - Step 3



Note: This Question is unanswered, help us to find answer for this one

22.

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?

Answer

Correct Answer:

Case 2 gives an error for the "initialize" method.  


Case 1 gives no error.


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

23.

In the following statement, what is contained in the argument 'caller'?
raise InterfaceException, "Keyboard failure", caller

Answer

Correct Answer:

stack trace 


Note: This Question is unanswered, help us to find answer for this one

24.

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‘?

Answer

Correct Answer:

 Thread.start


Thread.fork


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

25. Which of the following statements regarding Ruby are correct?

Answer

Correct Answer: LJ Ruby is a true object-oriented programming language.
D Ruby can be used to write Common Gateway interface (CGI) scripts.
Ci Ruby can be installed in both Windows and POSIX environments.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

26.

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?

Answer

Correct Answer:

This is before the raise.

This is being rescued

This is after the begin block. 



Note: This Question is unanswered, help us to find answer for this one

27.

What is the output of the following code in Ruby?

puts "Ruby String".unpack('A6')

Answer

Correct Answer:

Ruby S 


Note: This Question is unanswered, help us to find answer for this one

28.

What is the output of the following code in Ruby?
puts "hello world".sub(/[aeiou]/, '*')

Answer

Correct Answer:

h*llo world


Note: This Question is unanswered, help us to find answer for this one

29.

Which of the following statements regarding ".eql?" and ".equal?" are correct?

Answer

Correct Answer: .equal? returns true it the receiver and argument have the same object id.
.eqi? returns true if the receiver and argument have both the same type and equal values.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

30.

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?

Answer

Correct Answer:

Output of "puts method1" will be "test"


Output of "print method2" will be "Thisistest" 


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

31. What is the difference between “redo" and "retry" statements in Ruby?

Answer

Correct Answer: "redo" restarts an iteration of the most internal loop, without checking loop condition.
“retry" restarts the invocation of an iterator call. Also. arguments to the iterator are re-evaluated.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

32. if there are two variables x and y, such that x=10 and y=5, then:

Answer

Correct Answer: x + y =15
x -y = 5

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

33.

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

Answer

Correct Answer:

colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]

colors.each {|color| puts color} 


colors= ["Red", "Blue", "Yellow", "Green", "Violet", "Black"]

for color in 0...colors.length


print colors[color], "\n"

end  


Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

34.

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?

Answer

Correct Answer:

Command 1 checks syntax only, without executing the program


Command 2 changes the directory before executing.



Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

35. Assuming that f is an instance of the File class:

Answer

Correct Answer: f.path returns the pathname used to create f.
f.chmod(mode) changes the permission mode of f.

Note: This question has more than 1 correct answers

Note: This Question is unanswered, help us to find answer for this one

36. Which of the following is used to cancel the method definition?

Answer

Correct Answer: undef

Note: This Question is unanswered, help us to find answer for this one

37.

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:


Answer

Correct Answer:

StatementX is incorrect. while Statement Y is correct


Note: This Question is unanswered, help us to find answer for this one

38.

What is the output if the following code is executed at the Ruby command prompt?

foo = "testing"
puts defined? foo
puts defined? $_


Answer

Correct Answer:

testing

global-variable



Note: This Question is unanswered, help us to find answer for this one

39.

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

Answer

Correct Answer:

Statement 1 is true. while Statement 2 is false.


Note: This Question is unanswered, help us to find answer for this one

40.

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}";

Answer

Correct Answer:

The product of 5,10,15 is 750


Note: This Question is unanswered, help us to find answer for this one

41. In a Ruby installed environment, what happens when "lrb" is typed in the command prompt?

Answer

Correct Answer: it starts an interactive Ruby Session.

Note: This Question is unanswered, help us to find answer for this one

42.

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


Answer

Correct Answer:

RubyStrings

RubyStrings

RubyStrings 



Note: This Question is unanswered, help us to find answer for this one

43. Which of the following are correct?

Answer

Correct Answer: All of the above

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: Statement 1 is true, while Statement 2 is false.

Note: This Question is unanswered, help us to find answer for this one

45. In the Ruby programming language, "true" is a type of :

Answer

Correct Answer: Pseudo variable

Note: This Question is unanswered, help us to find answer for this one

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


Answer

Correct Answer: Statement 1 is true, while Statement 2 is false.

Note: This Question is unanswered, help us to find answer for this one

47. What is the output of the following code in Ruby?

puts "hello world".sub(/[aeiou]/, '*')


Answer

Correct Answer: h*llo world

Note: This Question is unanswered, help us to find answer for this one

48. 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?


Answer

Correct Answer: irb(main):001:0&gt; "hello"[2]<br> =&gt; "e"

Note: This Question is unanswered, help us to find answer for this one