Solved MCQs on Ruby Language


BASICS

1. Ruby is an object oriented general purpose programming language.
a) True
b) False
View Answer
Answer: a
Explanation: Ruby allow users to manipulate data structures called objects to build and execute programs.
2. Which of the following is supported by Ruby?
a) Multiple Programming Paradigms
b) Dynamic Type System
c) Automatic Memory Management
d) All of the Mentioned
View Answer
Answer: d
Explanation: Ruby supports all the features because it is a object oriented programming language.
3. Which of the following features does the 2.0 version of ruby supports?
a) Method keyword arguments
b) New literals
c) Security fixes
d) All of the mentioned
View Answer
Answer: d
Explanation: Ruby2.0 has several new added features and it is much stable than its older versions.
4. Which of the following languages syntax matches with the Ruby’s syntax?
a) Perl
b) PHP
c) Java
d) Jquery
View Answer
Answer: a
Explanation: Most of the syntax of perl language matches with that of Ruby’s.
5. What is the extension used for saving the ruby file?
a) .ruby extension
b) .rb extension
c) .rrb extension
d) None of the mentioned
View Answer
Answer: b
Explanation: Ruby files must be saved with the extension .rb.
7. Which of the following are valid floating point literal?
a) .5
b) 2.
c) 0.5
d) None of the mentioned
View Answer
Answer: 0.5
Explanation: floating point literals are valid only when they have digits on both the sides of decimal point.
8. Ruby can be embedded into Hypertext Markup Language(HTML).
a) True
b) False
View Answer
Answer: a
Explanation: Ruby can easily be encapsulated into HTML.
9. It is must for Ruby to use a compiler.
a) True
b) False
View Answer
Answer: b
Explanation: No compiler is required for ruby as it is an interpreted language.
10. Ruby can be used for developing internet and intranet applications.
a) True
b) False
View Answer
Answer: a
Explanation: Ruby is a server side scripting language used to create CGI(common gateway interface) scripts and hence can be used for developing internet and intranet applications.
DATATYPES
1. Which of the following datatypes are valid in Ruby?
a) Numbers
b) Boolean
c) String
d) All of the mentioned
View Answer
Answer: d
Explanation: Ruby supports all the three (numbers,strings,boolean) data types.
2. What will any variable evaluate to if it is of Boolean data type?
a) True
b) Nil
c) False
d) Either True or False
View Answer
Answer: d
Explanation: Boolean data types will give the value either as True or False depending on the condition.
3. Why can not we use quotation marks (‘ or “) with boolean?
a) It indicates that we are talking about a string
b) It indicates that we are assining a value
c) It indicates that that we are replacing boolean data type with string data type
d) None of the mentioned
View Answer
Answer: a
Explanation: Quotation marks are used only with strings.
4. Ruby is a case-sensitive language.
a) True
b) False
View Answer
Answer: a
Explanation: Ruby differentiates between small and capital letters and their meaning.
5. What is the size of an integer data type in ruby?
a) -2^30 to 2^30
b) -2^30 to 2^(30-1)
c) -2^29 to 2^30
d) -2^60 to 2^61
View Answer
Answer: b
Explanation: Integer ranges from -2^30 to 2^29 and lies in object of class Fixnum.
6. Ruby can deal with both numbers and floating point values.
a) True
b) False
View Answer
Answer: a
Explanation: We can assingn a integer and a floating point value to a variable.
7. What is the output of the given code?
boolean_1 = 77<78
puts(boolean_1)
a) Nil
b) True
c) False
d) Error
View Answer
Answer: b
Explanation: As 77 is less than 78 hence the boolean value will be printed as true.
Output:
True
8. Objects of which class does the integer from the range -2^30 to 2^(30-1) belong to?
a) Bignum
b) Octal
c) Fixnum
d) Binary
View Answer
Answer: c
Explanation: Integer within this range are objects of the class fixnum.
9. What does the notataion \b stands for?
a) Escape
b) Space
c) Backspace
d) Newline
View Answer
Answer: c
Explanation: This notataion is used for backspace.
10. What is the sequence of ruby strings?
a) 16-bit bytes
b) 8-bit bytes
c) 10-bit bytes
d) None of the mentioned
View Answer
Answer: b
Explanation: They are simply the sequence of 8-bit bytes.
11. What is the output of the given code?
my_string=Ruby
puts(my_string)
a) Ruby
b) Nil
c) Error
d) None of the mentioned
View Answer
Answer: c
Explanation: Ruby should be written in double quotes inorder to indicate a string.
Output:
Error
12. Which of the following is not a valid datatype?
a) Float
b) Integer
c) Binary
d) Timedate
View Answer
Answer: d
Explanation: datetime is valid datatype but timedate is not a valid datatype.
13. What is the range of octal notation (\nnn)?
a) 0-8
b) 0-7
c) 0-A
d) None of the mentioned
View Answer
Answer: b
Explanation: Octal notation ranges from 0-7.
STRING METHODS

1. 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
View Answer
Answer: d
Explanation: There is no predefined method which can reverse an already reversed string.
2. The .length method is used to check number of characters.
a) True
b) False
View Answer
Answer: b
Explanation: The .length method is used to check number of characters, spaces and even symbols.
3. What is the output of the following?
 "Iam learning ruby language".length
a) 26
b) 23
c) 20
d) 18
View Answer
Answer: a
Explanation: It checks the number of blanks, number of characters and symbols in the given string.
Output:
26
4. What is the output of the following?
 ruby.reverse
a) Error in compilation
b) ybur
c) 4
d) Undefined local variable ruby
View Answer
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>
5. What will be the output of the following?
"Eric".irreverse
a) Eric
b) cirE
c) undefined method
d) None of the mentioned
View Answer
Answer: c
Explanation: There is no predefined method named irreverse in ruby.
Output:
undefined method `irreverse' for "eric":String
6. What does the .upcase method do?
a) Convert the string to uppercase
b) Convert only lowercase string to uppercase and vice-versa
c) Convert the string to lowercase
d) None o the mentioned
View Answer
Answer: a
Explanation: .upcase method is used to change the given string to uppercase.
7. What will be the output of the following?
"Ruby".reverse.upcase
a) RUBY
b) ybuR
c) YBUR
d) YBUr
View Answer
Answer: c
Explanation: First the given string is reversed and then .upcase method is used to capitalize it.
Output:
"YBUR"
8. What will be the output of the given code?
"I'am learning Ruby Language".length.reverse.upcase
a) 27
b) egaugnal ybuR gninreal ma’I
c) ERAUGNAL YBUR GNINREAL MA’I
d) Undefined method `reverse’ for 27:Fixnum
View Answer
Answer: d
Explanation: The .length method finds the length which is a number and can’t be reversed.
Output:
undefined method `reverse' for 27:Fixnum
9. What will be the output of the given code?
"I'am learning Ruby Language".reverse.upcase.length
a) 27
b) egaugnal ybuR gninreal ma’I
c) ERAUGNAL YBUR GNINREAL MA’I
d) Undefined method `reverse’ for 27:Fixnum
View Answer
Answer: a
Explanation: The .reverse will reverse it then .upcase will capitalize it and .length method give the length.
Output:
27
10. The downcase method changes the whole string to smallcase letters.
a) True
b) False
View Answer
Answer: a
Explanation: Changes everything to smallcase.
11. What will we the output of the given code?
 "I'am Learning RUBY Language".downcase
a) iam learning ruby language
b) i’AM lEARNING ruby lANGUAGE
c) “i’am learning ruby language”
d) None of the mentioned
View Answer
Answer: c
Explanation: Everything is changed to smallcase because of downcase method is used.
Output:
"i'am learning ruby language"
12. What is the output of given code?
 string="I'am Learning RUBY Language".downcase
string.upcase
a) Undefined method
b) “I’AM LEARNING RUBY LANGUAGE”
c) I’am Learning RUBY Language
d) None of the mentioned
View Answer
Answer: b
Explanation: First the given string is changed to uppercase and stored in variable named string and then the string is changed to uppercase.
Output:
"I'AM LEARNING RUBY LANGUAGE"
13. What will be the output of the given code?
"Come let's learn.reverse Ruby.length language".upcase
a) nreal s’tel emoC 4 LANGUAGE
b) Undefined error
c) “COME LET’S LEARN.REVERSE RUBY.LENGTH LANGUAGE”
d) None of the above
View Answer
Answer: c
Explanation: Methods are never written inside double quotes.
Output:
"COME LET'S LEARN.REVERSE RUBY.LENGTH LANGUAGE"
14. What will we the output of the given code?
I'am Learning RUBY Language.downcase
a) iam learning ruby language
b) i’AM lEARNING ruby lANGUAGE
c) “i’am learning ruby language”
d) unterminated string meets end of file
View Answer
Answer: d
Explanation: Strings should be in double quotes in Ruby language.
Output:
Unterminated string meets end of file
15. Methods should not be written inside double quotes.
a) True
b) False
View Answer
Answer: a
Explanation: Always make sure to write methods outside double quotes.
FORMATTING STRING METHODS
1. The .upcase and .capitalize method are used for capitalizing the whole string.
a) True
b) False
View Answer
Answer: b
Explanation: The .capitalize method doesn’t change the whole string to uppercase, it just capitalizes the first letter.
2. What is the use of .capitalize method?
a) It capitalizes the entire string
b) It capitalize on the first letter of the string
c) It capitalize the strings which are in small case
d) All of the mentioned
View Answer
Answer: b
Explanation: It capitalizes only the first letter.
3. What is the role of ! at the end of the capitalize method?
a) It is the syntax for using capitalize method
b) It modifies the value stored in the variable
c) It indicates the termination of string
d) None of the mentioned
View Answer
Answer: b
Explanation: It is used for modifying the variable.
4. What is the output of the code?
print "What's your first name?"
first_name=gets.chomp
a=first_name.capitalize
first_name.capitalize!
print "What's your last name?"
last_name=gets.chomp
b=last_name.capitalize
last_name.capitalize!
puts "My name is #{first_name} #{last_name}"
a) Amil Jones
b) What’s your first name? amil
What’s your last name? jones
My name is Amil Jones
c) My name is Amil Jones
d) None of the mentioned
View Answer
Answer: b
Explanation: First it takes the input from user and then modifies accordingly and displays output.
Output:
What's your first name? amil
What's your last name? jones
My name is Amil Jones
5. What is the output of the code?
print "What's your first name?"
first_name=gets.chomp
a=first_name.capitalize
first_name.capitalize!
print "What's your last name?"
last_name=gets.chomp
b=last_name.capitalize
last_name.capitalize
puts "My name is #{first_name} #{last_name}"
a) Amil Jones
b) What’s your first name? amil
What’s your last name? jones
My name is Amil Jones
c) What’s your first name? amil
What’s your last name? jones
My name is Amil jones
d) None of the mentioned
View Answer
Answer: b
Explanation: Capitalize method was not called at the time of last name, jones was not printed correctly it was not modified because ! was not used.
Output :
What's your first name? amil
What's your last name? jones
My name is Amil jones
6. What is the output of the given code?
print "what's your first name?"
first_name=gets.chomp
a=first_name.capitalize
"first_name.capitalize!".reverse
puts"My name is #{first_name}"
a) amil jones
b) What’s your first name? amil
My name is amil
c) My name is lima
d) My name is limA
View Answer
Answer: b
Explanation: “first_name.capitalize!” can not be in double quotes because it is a method.
Output:
What's your first name? amil
My name is amil
7. What is the output of the given code?
print "what's your first name?"
first_name=gets.chomp
a=first_name.capitalize
"a".reverse
puts"My name is #{a}"
a) amil jones
b) What’s your first name? amil
My name is Amil
c) My name is lima
d) My name is limA
View Answer
Answer: b
Explanation: The string is reversed but it is not stored in any variable.
Output:
What's your first name? amil
My name is amil
8. What is the output of the given code?
 print "What's your first name?"
first_name=gets.chomp
a=first_name.capitalize
a=a.reverse
puts"My name is #{a}"
a) amil jones
b) What’s your first name? amil
My name is Amil
c) What’s your first name? amil
My name is limA
d) My name is limA
View Answer
Answer: b
Explanation: The string is reversed then and stored in a variable.
Output:
what's your first name? amil
My name is limA
9. What do we mean by expression substitution?
a) Embedding the value of Ruby expression into a string using #{ }
b) Substituting string functions
c) Storing string value
d) None of the mentioned
View Answer
Answer: a
Explanation: Strings are displayed #{} sequence.
10. What is the output of the given code?
 x, y, z = 12, 36, 72
puts "The value of x is #{ x }."
puts "The sum of x and y is #{ x + y }."
puts "The average was #{ (x + y + z)/3 }."
a) 12,48,40
b) Syntax error
c) The value of x is 12.
The sum of x and y is 48.
The average was 40.
d) None of the mentioned
View Answer
Answer: c
Explanation: x, y, z assigned the values 12, 36, 72 and then arithmetic operations performed on them.
Output:
The value of x is 12.
The sum of x and y is 48.
The average was 40.
11. What does %{Learn ruby language} represents?
a) “Learn Ruby language”
b) “%{Learn Ruby language}”
c) ” Learn Ruby language ”
d) None of the mentioned
View Answer
Answer: a
Explanation: Anything written inside %{} is same as anything written inside “”.
12. What does %Q{ Learn ruby language } represents?
a) ” Learn Ruby language ”
b) “%{ Learn Ruby language }”
c) ” Learn Ruby language ”
d) None of the mentioned
View Answer
Answer: a
Explanation: Anything inside %Q{} is written inside “”.
13. What does %x!ls! represents?
a) Same as back tick command output `ls`
b) Same as ‘x’
c) Same as “xls”
d) None of the mentioned
COMMENTS
1. Why do we use comments?
a) For later use to understand the working of the program
b) It is an efficient way of programming
c) It makes easy for the third person to understand
d) All of the mentioned
View Answer
Answer: d
Explanation: It is the efficient way of programming and easy to understand.
2. Which of the following type of comments are valid in ruby?
a) Single line comments
b) Multi line comments
c) Double line comments
d) All of the mentioned
View Answer
Answer: d
Explanation: Double line comments comes under multiline comment.
3. How to comment multiple lines in ruby?
a) Using multiline comments
b) Using single line comments
c) No option to comment
d) All of the above
View Answer
Answer: a
Explanation: Multiple lines can be commented using multiline comments.
4. How to comment a single line?
a) Using #
b) Using begin and end
c) Using //
d) None of the mentioned
View Answer
Answer: a
Explanation: Use # to comment single line.
5. This the right way to comment a single line.
“Ruby”.length # I’m a single line comment!
a) True
b) False
View Answer
Answer: a
Explanation: We use # in a single line comment.
6. What is the output of the given code?
“Ruby”.length #to find the length of given string
a) 4 to find the length of given string
b) 4
c) To find the length of given string
d) Ruby
View Answer
Answer: b
Explanation: It finds the length of string.
7. The following syntax is used for multiline comment.
=begin
iam in a multi line comment
=end
a) True
b) False
View Answer
Answer: a
Explanation : We use =begin and =end to mark the beginning and end of the comment.
8. The following is the correct way to use multiline comment.
 = begin
# comment
= end
a) True
b) False
View Answer
Answer: b
Explanation: No space should be given between = and begin and = or end.
9. Why do we use =begin and =end?
a) To mark the start and end of multiline comment
b) To comment multiple lines
c) To avoid the use of # again and again
d) All of the above
View Answer
Answer: d
Explanation: Easier way to comment multiple lines.
10. What is the output of the given code?
"Ruby Language".length
=begin
calculate length
=end
a) 13
b) 13 calculate length
c) 12
d) None of the mentioned
View Answer
Answer: a
Explanation: Calculate the length and is commented using =begin and =end.
Output:
13
11. What is the output of the given code?
"Ruby Language".length
= begin
calculate length
= end
a) 13
b) 12
c) Error
d) None of the mentioned
View Answer
Answer: c
Explanation: No space between = and begin and end
Output:
Syntax error, unexpected '=', expecting $end
= begin
12. Space between 2+5 or 2 + 5 is valid but =begin and = begin is not valid.
a) True
b) False
View Answer
Answer: a
Explanation: Ruby recognizes =begin and = begin in different way.



View Answer
Answer: a
Explanation: It is same as back tick command output `ls`.