Frequently asked questions and answers of Ruby in Programming Technologies of Computer Science to enhance your skills, knowledge on the selected topic. We have compiled the best Ruby Interview question and answer, trivia quiz, mcq questions, viva question, quizzes to prepare. Download Ruby FAQs in PDF form online for academic course, jobs preparations and for certification exams .
Intervew Quizz is an online portal with frequently asked interview, viva and trivia questions and answers on various subjects, topics of kids, school, engineering students, medical aspirants, business management academics and software professionals.
Question-1. What is Ruby?
Answer-1: Ruby is an object-oriented, dynamic programming language known for its simplicity and productivity.
Question-2. What are Ruby's key features?
Answer-2: Key features include dynamic typing, garbage collection, object-oriented design, and metaprogramming.
Question-3. What is the difference between puts, print, and p in Ruby?
Answer-3: puts adds a newline, print does not, and p shows a more detailed representation of objects.
Question-4. What are Ruby's data types?
Answer-4: Common types include String, Integer, Float, Array, Hash, Symbol, and NilClass.
Question-5. What is a Ruby variable?
Answer-5: A variable is a reference to an object and can be of types like local, instance, class, or global.
Question-6. How do you define a method in Ruby?
Answer-6: Use the def keyword followed by the method name and optional parameters. Example: def method_name; end.
Question-7. What are Ruby's access control methods?
Answer-7: Ruby provides public, protected, and private methods to control access.
Question-8. What is a block in Ruby?
Answer-8: A block is a chunk of code enclosed in do...end or {...} that can be passed to methods.
Question-9. What is the difference between Proc and Lambda?
Answer-9: Both are objects for blocks, but lambda checks the number of arguments, while Proc does not.
Question-10. What is Ruby's yield keyword?
Answer-10: yield transfers control from a method to an associated block of code.
Question-11. What are Ruby's iterators?
Answer-11: Iterators like each, map, and select are methods that loop through elements in collections.
Question-12. What is the difference between require and load in Ruby?
Answer-12: require loads a file once; load loads it every time it's called.
Question-13. What is the difference between require and include?
Answer-13: require loads external files; include mixes a module's methods into a class.
Question-14. What is a Ruby module?
Answer-14: A module is a collection of methods and constants, used for namespaces and mix-ins.
Question-15. How does Ruby handle inheritance?
Answer-15: Ruby allows single inheritance; classes inherit from one superclass.
Question-16. What is the difference between class and module in Ruby?
Answer-16: Classes support instantiation and inheritance; modules are used for mix-ins and cannot be instantiated.
Question-17. What is Ruby's garbage collection?
Answer-17: Ruby uses automatic garbage collection to free memory by removing unused objects.
Question-18. What are Ruby's symbols?
Answer-18: Symbols are immutable, memory-efficient identifiers, often used as keys in hashes.
Question-19. What is the difference between == and === in Ruby?
Answer-19: == checks value equality; === is used for case statements and matches patterns or types.
Question-20. What is the purpose of the initialize method in Ruby?
Answer-20: initialize is a special method called when an object is instantiated.
Question-21. How do you handle exceptions in Ruby?
Answer-21: Use begin, rescue, ensure, and optionally else blocks to handle exceptions.
Question-22. What is the purpose of Ruby's raise keyword?
Answer-22: raise generates an exception, optionally with a custom error message.
Question-23. What is the difference between Array and Hash in Ruby?
Answer-23: Arrays are ordered collections; Hashes are key-value pairs.
Question-24. What are Ruby's mixins?
Answer-24: Mixins are modules included in classes to add shared behavior without inheritance.
Question-25. How do you define a class in Ruby?
Answer-25: Use the class keyword followed by the class name. Example: class ClassName; end.
Question-26. What is Ruby's singleton class?
Answer-26: A singleton class allows you to define methods for a single object.
Question-27. What are Ruby's attr_accessor, attr_reader, and attr_writer?
Answer-27: These are shortcuts for creating getter, setter, or both methods for instance variables.
Question-28. What is the difference between nil and false in Ruby?
Answer-28: Both are falsy, but nil represents the absence of a value; false is a Boolean value.
Question-29. What is the super keyword in Ruby?
Answer-29: super calls the same method in the superclass, passing arguments to it automatically.
Question-30. What is a mixin in Ruby?
Answer-30: A mixin is a module mixed into a class to add shared functionality.
Question-31. What are Ruby's control structures?
Answer-31: Control structures include if, unless, case, while, for, and until.
Question-32. What is Ruby's method lookup path?
Answer-32: Ruby searches for methods in the class, included modules, superclass, and ancestors.
Question-33. What is Ruby's Enumerable module?
Answer-33: Enumerable provides collection traversal and searching methods, commonly used with arrays and hashes.
Question-34. What is the difference between to_s and inspect?
Answer-34: to_s returns a string representation for display; inspect returns a string for debugging.
Question-35. What are Ruby's predefined global variables?
Answer-35: Examples include $LOAD_PATH, $0, and $_.
Question-36. What is the difference between == and eql??
Answer-36: == checks value equality; eql? checks both value and type equality.
Question-37. What are Ruby's scopes for variables?
Answer-37: Scopes include local, instance (@), class (@@), and global ($) variables.
Question-38. What is the difference between clone and dup in Ruby?
Answer-38: clone copies an object's state and frozen state; dup excludes the frozen state.
Question-39. How does Ruby support threads?
Answer-39: Ruby provides Thread class for creating and managing threads.
Question-40. What is the difference between map and collect in Ruby?
Answer-40: They are aliases and both transform arrays or collections.
Question-41. How do you create a hash in Ruby?
Answer-41: Use curly braces: { key => value }.
Question-42. What is the difference between freeze and constant in Ruby?
Answer-42: freeze makes an object immutable; constants are variables whose values should not change.
Question-43. What is Ruby's self keyword?
Answer-43: self refers to the current object.
Question-44. What is a monkey patch in Ruby?
Answer-44: A monkey patch is a modification or addition to a class or module at runtime.
Question-45. How does Ruby implement polymorphism?
Answer-45: Through method overriding, duck typing, and dynamic dispatch.
Question-46. What is the purpose of the method_missing method in Ruby?
Answer-46: method_missing is called when an undefined method is invoked on an object.
Question-47. What are Ruby's metaprogramming capabilities?
Answer-47: Ruby allows runtime changes to classes and objects using features like define_method and method_missing.
Question-48. What is the difference between Proc and Block in Ruby?
Answer-48: Blocks are not objects; Proc is an object. A block is converted into a Proc when passed to a method.
Question-49. What is Ruby's File class used for?
Answer-49: The File class handles file I/O operations.
Question-50. How does Ruby manage gems?
Answer-50: Ruby uses RubyGems, a package manager, to install and manage libraries (gems).
Frequently Asked Question and Answer on Ruby
Ruby Interview Questions and Answers in PDF form Online
Ruby Questions with Answers
Ruby Trivia MCQ Quiz