0% found this document useful (0 votes)
20 views

Ruby MCQ

IMPORTANT MCQ

Uploaded by

Raja Daniyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Ruby MCQ

IMPORTANT MCQ

Uploaded by

Raja Daniyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basics of Ruby

 Sample MCQ: What is the file extension for Ruby files?


o A) .r
o B) .rb
o C) .ruby
o D) .py
Answer: B) .rb

2. Data Types and Variables

 Sample MCQ: Which of the following is a Ruby symbol?


o A)
o B) name
o C) @name
o D) #name
Answer: A)

3. Operators

 Sample MCQ: What is the result of 5 % 2 in Ruby?


o A) 2
o B) 0
o C) 1
o D) 5
Answer: C) 1

4. Control Structures

 Sample MCQ: Which of the following is the correct way to write an if statement
in Ruby?
o A) if x > 10 then
o B) if x > 10 do
o C) if x > 10
o D) if x greater than 10
Answer: C) if x > 10
5. Loops (for, while, until)

 Sample MCQ: Which loop continues until the condition becomes false in Ruby?
o A) for loop
o B) while loop
o C) until loop
o D) break loop
Answer: C) until loop

6. Methods

 Sample MCQ: What is the correct way to define a method in Ruby?


o A) method_name do
o B) def method_name
o C) function method_name
o D) define method_name
Answer: B) def method_name

7. Blocks, Procs, and Lambdas

 Sample MCQ: Which of the following can be passed to a method in Ruby as a


block?
o A) &block
o B) %block
o C) *block
o D) block&
Answer: A) &block

8. Classes and Objects

 Sample MCQ: In Ruby, which keyword is used to create a class?


o A) new
o B) class
o C) def
o D) object
Answer: B) class
9. Inheritance and Modules

 Sample MCQ: In Ruby, a class inherits from another class using which symbol?
o A) :
o B) @
o C) <<
o D) <
Answer: D) <

10. Exception Handling

 Sample MCQ: Which of the following is used to handle exceptions in Ruby?


o A) try-except
o B) begin-rescue
o C) throw-catch
o D) raise
Answer: B) begin-rescue

11. Input/Output (I/O)

 Sample MCQ: What is the method used to print a message to the console in Ruby?
o A) print
o B) echo
o C) puts
o D) write
Answer: C) puts

12. Ruby Gems and Libraries

 Sample MCQ: What command is used to install a gem in Ruby?


o A) gem install
o B) gem add
o C) ruby install
o D) gem new
Answer: A) gem install

13. Arrays and Hashes


 Sample MCQ: What is the correct syntax to define a hash in Ruby?
o A) {key1: value1, key2: value2}
o B) [key1 => value1, key2 => value2]
o C) {key1 => value1, key2 => value2}
o D) (key1, value1, key2, value2)
Answer: C) {key1 => value1, key2 => value2}

14. Enumerables and Iterators

 Sample MCQ: Which method is used to iterate over an array in Ruby?


o A) each
o B) forEach
o C) iterate
o D) loop
Answer: A) each

15. Regular Expressions

 Sample MCQ: What syntax does Ruby use for regular expressions?
o A) /regex/
o B) |regex|
o C) !regex!
o D) $regex$
Answer: A) /regex/

16. File Handling

 Sample MCQ: Which method is used to open a file in Ruby?


o A) file.open
o B) open()
o C) File.open()
o D) File.new()
Answer: C) File.open()

17. Memory Management and Garbage Collection


 Sample MCQ: Which of the following handles garbage collection in Ruby?
o A) Manual memory management
o B) A separate garbage collection thread
o C) The operating system
o D) The Ruby interpreter
Answer: D) The Ruby interpreter

You might also like