Docsity

Ruby Association certified Ruby programmer silver version 2.1 prep test UPDATED Study Gu, Exams of Medicine

Toggle sidebar
bg1
Ruby Association certified Ruby
programmer silver version 2.1 prep test
UPDATED Study Guide QUESTIONS
AND CORRECT ANSWERS
- Q1. Which of the following are NOT reserved words in Ruby?
(Choose two.)
A. super
B. begin
C. try
D. goto
E. alias - CORRECT ANSWERS 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)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Ruby Association certified Ruby programmer silver version 2.1 prep test UPDATED Study Gu and more Exams Medicine in PDF only on Docsity!

Ruby Association certified Ruby

programmer silver version 2.1 prep test

UPDATED Study Guide QUESTIONS

AND CORRECT ANSWERS

  • Q1. Which of the following are NOT reserved words in Ruby? (Choose two.) A. super B. begin C. try D. goto E. alias - CORRECT ANSWERS 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] - CORRECT ANSWERS 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 - CORRECT ANSWERS A3: D Hash#size returns the number of key value pairs in the hash.

B. z C. 7years D. break E. latitude - CORRECT ANSWERS A5: A, B, E break is a reserved word, and cannot be used as variable name. 7 years is also incorrect. A local variable name must start with a lowercase US-‐ASCII letter or a character with the eight bit set. Q6. 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 5 2 6 3 7 4 8 B. 0 5 1 6 2 7

C.

D.

E. an error has occurred. - CORRECT ANSWERS 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"]

Integer#donwto(min) is the method implemented by integer object (known as an iterator method) works like loops.That iterates from a large number to min number. Q8. Which of the following can be inserted into (1) in order for the given code to generate the output below? (Choose two.) p [1,1,2,3,5,8].(1){|x| x*2} [output] [2,2,4,6,10,16] A. compact B. collect C. map D. flatten E. join - CORRECT ANSWERS A8: B, C Returns a new array with the results of running block once for every element. There's no functional difference between Enumerable#collect and Enumerable#map. Q9. Which of the following is the correct output for the program given below? puts "Ruby on Rails".delete("Rails")

A. An error has occurred. B. Ruby on C. Rails D. uby on E. Ruby - CORRECT ANSWERS A9: D 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 < A. a[0..2] B. a[1..2] C. a.slice(0,3) D. a.slice(0...2) - CORRECT ANSWERS 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? str = "RubyAssociation".chomp A. "RubyAssociation\r" B. "RubyAssociation" C. "RubyAssociation\r\n" D. "RubyAssociation\r\n\r\n" - CORRECT ANSWERS A13: B

One way to remove newline characters is the String#chop method, it will re-‐ move any trailing newline or carriage return characters "\r\n". String#chomp is often a safer alternative, as it leaves the string unchanged if it doesn't end in a record separator. e.g. "Ruby\r\n".chop #=> "Ruby" "Ruby".chop #=> "Rub" "Ruby\r\n".chomp #=> "Ruby" "Ruby".chomp #=>"Ruby" Q14. Which of the following is the correct output for the program given below? str = "RubyAssociation\r\n".chop A. "RubyAssociation" B. "RubyAssociation\r" C. "RubyAssociation\r\n" D. "RubyAssociation\r\n\r\n" - CORRECT ANSWERS A14: A Q15. Which of the following is a correct statement about io.rewind?

["Piano Concerto","Violin Sonata"] A. | B. & C. - ‐ D. + - CORRECT ANSWERS 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] - CORRECT ANSWERS 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 - CORRECT ANSWERS A18: A,C The answers is A and C. /^[hc].o$/i # a word start with h or c and ends with o. "i" modifier ignore case sensitive when matching text. The regular expression topics tested include basic(very short) textual pattern; how to write regexp literals in this exam. Q19. Which of the following is the correct output for the program given below? Greeting = "Hello Ruby" Greeting = "Hi Ruby" p Greeting

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 - CORRECT ANSWERS 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. "" - CORRECT ANSWERS 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]

Hash#has_key?(key) - ‐> bool Hash#include?(key) - ‐> bool Hash#key?(key) - ‐> bool Hash#member?(key) - ‐> bool Q25. Which of the following is the correct output for the program given below? p "Hello there".class p String.superclass class Foo; end class Bar < Foo; end p Foo.superclass p Bar.superclass A. String Kernel Module Module B. Object Module

Object FooClass C. String Module String FooClass D. String Object Object Foo E. Object Object Module Kernel - CORRECT ANSWERS A25: D Class#superclass Returns the superclass of class, or nil. Object#class Returns class of object.