Ruby Questions and Answers Part-3

1. Which of the following is not a valid datatype?
a) Float
b) Integer
c) Binary
d) Timedate

Answer: d
Explanation: datetime is valid datatype but timedate is not a valid datatype.

2. What is the range of octal notation (\nnn)?
a) 0-8
b) 0-7
c) 0-A
d) none of the Mentioned

Answer: b
Explanation: Octal notation ranges from 0-7.

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

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

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

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

5. What is the output of the given code
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

Answer: d
Explanation: Print prints whatever is given and puts in addition to printing it inserts a new blank line.
Output:
Hey Everyone
We are learning Ruby

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

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

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

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

8. What is the output of given code?
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

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)

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

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

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

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