

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































































**Question 1. Which of the following best describes Ruby’s core philosophy? ** A) Compile-time type checking B) Programmer happiness and productivity C) Low-level memory management D) Strict procedural programming Answer: B Explanation: Ruby was designed by Yukihiro Matsumoto with the goal of making programming enjoyable, emphasizing readability and developer happiness. Question 2. Who created the Ruby programming language? A) Guido van Rossum B) James Gosling C) Yukihiro “Matz” Matsumoto D) Bjarne Stroustrup Answer: C Explanation: Matz released Ruby in 1995, aiming to blend parts of Perl, Smalltalk, and other languages. Question 3. Which command starts an interactive Ruby session? A) ruby -i B) irb C) ruby console D) ruby -e
Answer: B Explanation: irb launches the Interactive Ruby Shell where code can be entered and evaluated instantly. Question 4. How do you write a multi-line comment in Ruby? A) // comment B) # comment C) =begin … =end D) /* comment */ Answer: C Explanation: Ruby uses =begin and =end to delimit block comments spanning multiple lines. Question 5. Which method prints a string followed by a newline? A) print B) puts C) p D) echo Answer: B Explanation: puts appends a newline after the output, whereas print does not. Question 6. What does the p method do? A) Prints without newline B) Prints the object’s inspect representation and returns the object C) Prints raw bytes
B) max_count C) _maxCount D) $MAX_COUNT Answer: A Explanation: Constants begin with an uppercase letter; MAX_COUNT follows this rule. Question 10. Which variable scope is accessible across the entire Ruby program? A) Local variable B) Instance variable C) Global variable D) Class variable Answer: C Explanation: Global variables (prefixed with $) can be accessed from any scope, though their use is discouraged. Question 11. What symbol is used to denote an instance variable? A) @@ B) @ C) $ D) % Answer: B Explanation: Instance variables begin with a single @ and are tied to a particular object instance.
Question 12. How do you declare a class variable? A) @variable B) @@variable C) $variable D) variable Answer: B Explanation: Class variables start with @@ and are shared among the class and all its subclasses. Question 13. Which of the following is a correct parallel assignment? A) a = b = c = 1 B) a, b, c = 1, 2, 3 C) a = (b, c) = 1 D) a; b; c = 1 Answer: B Explanation: Ruby allows multiple variables to be assigned simultaneously using comma-separated lists. Question 14. What is the result of 5 / 2 in Ruby? A) 2. B) 2 C) 3 D) 2. Answer: B Explanation: Both operands are integers, so Ruby performs integer division, discarding the remainder.
Explanation: In Ruby, size and length are aliases and both return the string’s character count. Question 18. What does the reverse method do to a string? A) Changes case of each character B) Returns a new string with characters in opposite order C) Sorts characters alphabetically D) Removes whitespace Answer: B Explanation: reverse creates a new string where the characters appear in reverse order. Question 19. Which method replaces the first occurrence of a pattern in a string? A) gsub B) sub C) replace D) scan Answer: B Explanation: sub replaces only the first match, while gsub replaces all matches. **Question 20. How do you create a multi-line string using a Here Document? ** A) <<-EOS … EOS B) <<
D) %q{ … } Answer: A Explanation: <<-EOS (or `<
B) unshift C) shift D) prepend Answer: A Explanation: push (or <<) appends an element to the array’s tail. Question 27. What does the pop method return? A) The first element of the array B) The last element of the array and removes it C) The array’s size D) Nil if the array is empty Answer: B Explanation: pop removes and returns the last element; returns nil for an empty array. Question 28. Which method removes the first element of an array? A) shift B) unshift C) pop D) delete_at(0) Answer: A Explanation: shift removes and returns the element at index 0. **Question 29. How can you retrieve a sub-array from index 2 to 4 inclusive? ** A) array.slice(2,4)
B) array[2..4] C) array[2...4] D) array.slice(2..4) Answer: B Explanation: The range 2..4 includes both endpoints; array[2..4] returns elements at indices 2, 3, 4. Question 30. Which method returns a string made by joining array elements with a separator? A) concat B) join C) merge D) combine Answer: B Explanation: join concatenates array elements, inserting the optional separator between them. Question 31. What does the uniq method do to an array? A) Sorts the array B) Removes duplicate elements, returning a new array C) Returns the first unique element D) Reverses the array Answer: B Explanation: uniq eliminates duplicates while preserving order, producing a new array.
Explanation: hash1.merge(hash2) returns a new hash where overlapping keys take values from hash2; merge! mutates the receiver. **Question 35. What does the keys method return when called on a hash? ** A) An array of all values B) An array of all key objects C) A string of keys separated by commas D) Nothing; it prints keys to STDOUT Answer: B Explanation: hash.keys yields an array containing each key in the hash. Question 36. Which range expression includes the end value? A) (1...5) B) (1..5) C) (1..5) exclusive D) (1...5) inclusive Answer: B Explanation: The double-dot (..) creates an inclusive range; the triple-dot (...) excludes the last value. Question 37. How can a range be used directly in a for loop? A) for i in 1..5 do … end B) for i from 1 to 5 … end C) for i = 1 to 5 … end D) loop(1..5) { |i| … }
Answer: A Explanation: Ruby’s for loop iterates over any enumerable, including ranges like 1..5. Question 38. Which conditional structure is the opposite of if? A) unless B) else C) case D) when Answer: A Explanation: unless executes its block only when the condition evaluates to false. Question 39. What does the following code output? puts "Ruby" if false A) Ruby B) nil C) (nothing) D) error Answer: C Explanation: The modifier if false prevents execution, so nothing is printed. Question 40. Which keyword is used to exit a loop prematurely? A) exit B) break C) stop D) quit
D) nil Answer: C Explanation: The spaceship operator returns 1 when the left operand is greater, 0 if equal, -1 if less. Question 44. Which operator has higher precedence in Ruby? A) && B) || C) == D) ** Answer: D Explanation: Exponentiation (**) has higher precedence than comparison (==) and logical (&&, ||) operators. Question 45. How do you define a method that takes a variable number of arguments? A) def foo(*args) … end B) def foo(args…) … end C) def foo(args[]) … end D) def foo(args...) … end Answer: A Explanation: The splat operator (*) collects any extra arguments into an array named args. Question 46. What does the return keyword do inside a method?
A) Prints a value to STDOUT B) Exits the method and returns the given value C) Continues to the next line of code D) Raises an exception Answer: B Explanation: return terminates method execution and yields the specified value (or nil if omitted). Question 47. Which of the following is true about method return values in Ruby? A) A method always returns the value of the last evaluated expression unless an explicit return is used. B) Methods must end with a return statement. C) Only return statements produce output. D) Methods return nil by default, regardless of the code inside. Answer: A Explanation: Ruby implicitly returns the result of the last evaluated expression if return is not present. Question 48. How can you make a method accept keyword arguments? A) def foo(**options) … end B) def foo(key: value) … end C) def foo(key = nil) … end D) Both A and B Answer: D Explanation: Ruby supports required or optional keyword arguments (def foo(key:)) and a catch-all hash (**options).
Answer: C Explanation: Both lambda {} and the literal -> create lambda objects with the same behavior. Question 52. What is the difference between a Proc and a lambda regarding argument handling? A) Lambdas enforce the exact number of arguments; Procs are lenient. B) Procs enforce the exact number of arguments; lambdas are lenient. C) Both enforce exact arguments. D) Neither enforce any arguments. Answer: A Explanation: Lambdas behave like methods regarding arity, raising an error if the count mismatches, while Procs ignore extra arguments and assign nil to missing ones. Question 53. Which keyword is used to define a class method? A) def self.method_name … end B) def class.method_name … end C) def method_name(class) … end D) def static method_name … end Answer: A Explanation: Prefixing the method name with self. inside the class body defines a class (singleton) method. Question 54. How do you create a subclass named Dog that inherits from Animal? A) class Dog < Animal end
B) class Dog inherits Animal end C) subclass Dog : Animal end D) class Dog : Animal end Answer: A Explanation: Ruby uses the < syntax for inheritance. Question 55. Which method is automatically called when a new object is instantiated? A) initialize B) new C) construct D) setup Answer: A Explanation: initialize runs after ClassName.new creates the object, allowing custom initialization. Question 56. What does the attr_reader :name line inside a class do? A) Creates a setter method for @name B) Creates a getter method for @name C) Deletes the instance variable @name D) Makes @name a constant Answer: B Explanation: attr_reader defines a method that returns the value of the instance variable.