




















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!





















Q1. Which of the following are NOT reserved words in Ruby? (Choose two.) A. super B. begin C. try D. goto E. alias - ANSW✔️ -A1: C, D The following list shows the reserved words in Ruby2.1.0: BEGIN class ensure nil self when END def false not super while alias defined? for or then yield and do if redo true LINE begin else in rescue FILE break elsif module retry unless ENCODING case end next return until Q2. Which of the following is the correct output for the program given below?
def foo (a, *b) p a end foo(1,2,3,4) A. nil B. 1 C. An error has occurred. D. [] E. [1] - ANSW✔️ -A2: B If you want to pass in a variable length of arguments, you can use the parameter that prefixed with an asterisk. The parameter allows to represent an indefinite number of arguments(zero or more arguments) as an array. Q3. Which of the following is the correct output for the program given below? puts({"members" => 193, "year" => 2014}.size) A. 15 B. 6 C. 4 D. 2 - ANSW✔️ -A3: D Hash#size returns the number of key value pairs in the hash. Q4. Which of the following is the correct output for the program given below?
(5..8).each_with_index do |val,i| puts "#{i} #{val}" end A. 1 2 3 4
E. an error has occurred. - ANSW✔️ -A6: B
A Range object represents an interval that is a group of numbers that includes all integer number between the beginning and the end. You can use ranges by means of the .. and ... operators. ..(inclusive) ...(exclusive) e.g. (1..3).to_a #=>[1,2,3] (1...3).to_a #=>[1,2] ("a".."d").to_a #=>["a","b","c","d"] Range class includes the Enumerable module, which defines a serious of specialized iterators. The Enumerable#each_with_index works like a each method but it calls block with two arguments, the item and its index. e.g. (1..3).each{|num| print num} #=> 123 Hint If you can't make head or tail of this, check the online tutorials. rubymonk https://rubymonk.com/ Q7. Which of the following is the correct output for the program given below? p 100.downto(90).select{|x| x%2==0} A. [100,99,98,97,96,95,94,93,92,91,90] B. [90,92,94,96,98,100] C. [100,98,96,94,92,90]
String#delete Returns a copy of string with all characters in the intersection of its arguments deleted. Q10. An error occurred while trying to create a string using a here document. Which of the following solutions are appropriate? (Choose two.) doc = <>EOF. C. Remove the whitespace in front of the last EOF. D. Replace the first <2, 3=>4} h.(1) p h #=>{} ary = [1, 2] ary.(1) p ary #=>[]
A. clear B. remove C. destroy D. empty? - ANSW✔️ -A11: A Removes all elements from self. Q12. Which of the following can be inserted into (1) in order for the given code to generate the output below? (Choose two.) a = [1,2,3,4,5] p (1) #=>[1,2,3] A. a[0..2] B. a[1..2] C. a.slice(0,3) D. a.slice(0...2) - ANSW✔️ -A12: A, C You can use ranges by means of the .. and ... operators. ..(inclusive) ...(exclusive) e.g. p a[0..1] #=> [1,2] p a[0...1] #=>[1] p a[--2..4] #=>[4.5] p a[--1..1] #=>[] Q13. Which of the following is the correct output for the program given below?
puts io.gets puts io.read io.rewind p lines = io.readlines end A. Return a string describing this IO object. B. Positions ios to the beginning of input, resetting lineno to zero. C. Returns true if ios is associated with a terminal device (tty), false otherwise. D. Returns an integer representing the numeric file descriptor for ios. - ANSW✔️ -A15: B You also should know how the IO#gets,IO#read and IO#readline methods actually works before exam. Q16. Which of the following can be inserted into (1) in order for the given code to generate the output below? (Choose one.) mozart = ["Symphony","Piano Concerto", "Violin Concerto","Horn Concerto","Violin Sonata"] listened = ["Symphony","Violin Concerto","Horn Concerto"] p mozart__(1)__listened [ [ output] "Piano Concerto","Violin Sonata"] A. | B. & C. -- D. + - ANSW✔️ -A16: C e.g.
ary1 = [1,2,3,4,5] ary2 = [3,4,5,6,7] Union p ary1 | ary2 #=>[1,2,3,4,5,6,7] Intersection p ary1 & ary2 #=> [3,4,5] Difference p ary1 - - ary2 #=>[1,2] Q17. Which of the following is the correct output for the program given below? odd = [1,3,5] even = [2,4,6] num = odd + even p num.sort A. nil B. [] C. [1,3,5,2,4,6] D. [1,2,3,4,5,6] - ANSW✔️ -A17: D Q18. Which of the following string match for the regular expression? (Choose two.) /^[hc].*o$/i A. Hello B. holland C. Cello D. h35L320 - ANSW✔️ -A18: A,C
Q21. Which of the following is the correct output for the program given below? class Surface attr_reader :s def initialize(x,y) @s = x * y end end class Volume < Surface attr_reader :v def initialize(x,y,z) super(x,y) @v = x * y * z end end a = Volume.new(2,5,5) puts "#{a.v},#{a.s}" A. 50, B. An error has occurred. C. 50, D. 10,10 - ANSW✔️ -A21: A The super has a special meaning in a method overriding. It invokes a methods with the same name as the current one, in it's superclass of the current class.(self) Q22. Which of the following is the correct output for the program given below?
string = "test code" string.slice(0,4) p string A. "test" B. "test code" C. nil D. "" - ANSW✔️ -A22: B Generally, method end in exclamation marks indicate that methods will overwrite the object it's called on. Meanwhile, without exclamation marks that makes a new copy of the object. e.g. a = [1,2,3] a.map{|num| num2} p a #=>[1,2,3] a.map!{|num| num2} p a #=>[2,4,6] Q23. Which of the following is the correct output for the program given below? puts "ruby".encoding.name A. UTF-- 8 B. An error has occurred. C. US--ASCII D. ASCII--8BIT - ANSW✔️ -A23: A UTF-- 8 is the default character encoding of Ruby 2.0 or later.
Kernel Module Module B. Object Module Object FooClass C. String Module String FooClass D. String Object Object Foo E. Object Object Module Kernel - ANSW✔️ -A25: D Class#superclass
Returns the superclass of class, or nil. Object#class Returns class of object. When you create a class in Ruby, your class automatically extend Object unless you explicitly specify the superclass. Q26. Which of the following can be inserted into (1) , (2) in order for the given code to generate the output below? s = "To be or not to be, that is the question." hash = Hash.new(0) s.(1)((2)) {|i| hash[i] += 1} p hash["be"] [output] 2 A. (1)match (2)/\w+/ B. (1)sub (2)/\w+/ C. (1)scan (2)/\w+/ D. (1)search (2)/\w+/ - ANSW✔️ -A26: C String#scan returns everything that the Regular expression matches, for each match a result is generated and either added to the result array or passed to the block if a block is given. On the other hand, String#match returns the first match as a MatchData object. e.g. s = "Well done is better than well said." s.scan(/\w+/) #=>["Well","done", "is", "better", "than", "well", "said"] s.scan(/\w+/){|i| print i} #=>Welldoneisbetterthanwellsaid
How to handle that. Q29. Which of the following is the correct output for the program given below? ary = [] ary << 1 && false true || ary << 2 false && ary << 3 false || ary << 4 p ary A. [1, 4] B. [1, 2, 3, 4] C. [1] D. [1, 2, 3] - ANSW✔️ -A29: A && operator if lefthand operand is nil or false, then returns that value. In other word, righthand operand is ignored. e.g. ary = [] nil && ary << "hello" p ary #=> [] || operator First, it evaluate its lefthand operand, and if this is any value other than nil or false, it returns that value. if it's nil or false , it evaluates its righthand operand and returns that value.
e.g. ary = [] nil || ary << "hello" p ary #=> ["hello"] Q30. Which of the following is the correct output for the program given below? p "foo" * 2 ** A. An error has occurred. B."foo4" C."foofoofoofoo" D."foofoo" - ANSW✔️ -A30: C C is correct. High precedence operation(*) happen before low precedence operation() This is the precedence table from High to low. !, ~, unary + ** unary - -
<