Ruby Questions and Answers Part-4

1. first_name,Last_name=gets.chomp,gets.chomp is the correct way to get the input from the user.
a) True
b) False

Answer: a
Explanation: It will take the input from user and store its value in the specified variables.

2. What is the output of the given code?
print "What's your address"
city,state,pin=gets.chomp,gets.chomp,gets.chomp
puts "Iam from #{city} city, #{state} state, pincode: #{pin} "
a) Error in compilation
b) Give your address
c) What’s your address? Chennai
Tamilnadu
600048
Iam from Chennai city, Tamilnadu state, pincode: 600048
d) none of the Mentioned

Answer: c
Explanation: It gets the input from used and prints it using the #{} sequence.
Output:
What's your address? Chennai
Tamilnadu
600048
Iam from Chennai city, Tamilnadu state, pincode: 600048

3. What is the output of the the given code?
puts "My name is #{Name}"
a) Error in compilation
b) Name variable not defined earlier
c) My name is #{Name}
d) Undefined Error

Answer: b
Explanation: Ruby doesn’t recognize the Name variable as it is not declared.
Output:
Name variable not defined

4. What is the output of the given code?
Ans=Ruby
puts "#{Ans} is an oop language"
puts "It is very efficient langauge"
puts "#{expr} is used on rails platform"
a) Error, no output
b) Ruby is an oop language
It is very efficient langauge
undefined local variable
c) Ruby is an oop language
It is very efficient langauge
#{expr} is used on rails platform
d) None of the mentioned

Answer: b
Explanation: The Local variable used is expr which is not defined.
Output:
Ruby is an oop language
It is very efficient langauge
undefined local variable or method `expr' for #

5. What is the output of the given code?
Ans=Ruby
puts "#[Ans] is an oop language"
a) Error, no output
b) Ruby is an oop language
c) Warning:already initialized constant Ans
d) None of the mentioned

Answer: b
Explanation: Curly braces {} are used for printing the value instead of square braces [].
Output:
(ruby): warning: already initialized constant Ans

6. Which of the following is the valid string method?
a) The .length method
b) The .upcase method
c) The .downcase method
d) The .irreverse method

Answer: d
Explanation: There is no predefined method which can reverse an already reversed string.

7. The .length method is used to check number of characters.
a) True
b) False

Answer: b
Explanation: The .length method is used to check number of characters, spaces and even symbols.

8. What is the output of the following?
"Iam learning ruby language".length
a) 26
b) 23
c) 20
d) 18

Answer: a
Explanation: It checks the number of blanks, number of characters and symbols in the given string.

9. What is the output of the following?
ruby.reverse
a) Error in compilation
b) ybur
c) 4
d) Undefined local variable ruby

Answer: d
Explanation: String should always be in double quotes and then the .reverse method can be used.
Output:
undefined local variable or method `ruby' for #<Context:0x7420ac>

10. What will be the output of the following?
"Eric".irreverse
a) Eric
b) cirE
c) undefined method
d) none of the mentioned

Answer: c
Explanation: There is no predefined method named irreverse in ruby.
Output: undefined method `irreverse' for "eric":String