Ruby Questions and Answers – Library Functions

This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Library Functions”.

1. Which of the following is not a valid library function?
a) Puts
b) Print
c) Gets
d) Get
View Answer

Answer: d
Explanation: gets is used for taking input from the user.

2. Why is the library function ‘puts’used for?
a) Prints whatever is given and print it to the screen
b) Prints whatever is given and insert a new(blank) line
c) Gets input from the user
d) None of the mentioned
View Answer

Answer: b
Explanation: Puts is a library function which prints and then insert a new blank line.

3. What is the output of the given code?

advertisement
advertisement
print "Hey"
   puts "Everyone!"
   print "We are learning Ruby"

a) Error
b) Hey everyone we are learning Ruby
c) Hey everyone
d) Hey Everyone
We are learning Ruby
View Answer

Answer: d
Explanation: Print prints whatever is given and puts in addition to printing it inserts a new blank line.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Output:
Hey Everyone
We are learning Ruby

4. We use semicolon or parentheses after every print or puts statement.
a) True
b) False
View Answer

Answer: b
Explanation: No semicolon or parentheses is required.
advertisement

5. For getting an input from the user which method is used?
a) get
b) gets.chomp
c) get-s
d) chomp
View Answer

Answer: b
Explanation: The method gets.chomp takes the input from the user.

6. What is the output of given code?

advertisement
puts "what is your first name?"
   name=gets.chomp
   puts "what is your surname?"
   surname=gets.chomp

a) Error in compilation
b) What is your first name?
xyz(enter any name)
What is your surname?
wtu(enter your surname)
c) Name=xyz
d) None of the mentioned
View Answer

Answer: b
Explanation: It will print and ask you to input the name and surname

Output:
  What is your first name?
  xyz(enter any name)
  What is your surname?
  wtu(enter your surname)

7. Which sequence can be used to substitute the value of any ruby expression in a string?
a) #(expr)
b) #{expr}
c) #expr
d) None of the mentioned
View Answer

Answer: b
Explanation: #{expr} is valid.

8. Why is gets not preferred instead of gets.chomp?
a) Gets add an extra new line while chomp removes it
b) Gets remove an extra new line
c) Chomp erases the value stored in the variable
d) All of the mentioned
View Answer

Answer: a
Explanation: gets.chomp will remove the extra blank space.

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

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

10. 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
View Answer

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 

11. 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
View Answer

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

Output:
Name variable not defined

12. 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
View Answer

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 #

13. 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
View Answer

Answer: b
Explanation: Curly braces {} are used for printing the value instead of square braces [].

Output:
(ruby): warning: already initialized constant Ans

Sanfoundry Global Education & Learning Series – Ruby Programming.

To practice all areas of Ruby Programming, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.