Docsity

Ruby Association Certified Ruby Examination Silver Sample Questions UPDATED Study Guide, Exams of Medicine

Toggle sidebar
bg1
Ruby Association Certified Ruby
Examination Silver Sample Questions
UPDATED Study Guide QUESTIONS
AND CORRECT ANSWERS
- Which of the following have true values in Ruby? (Choose two.)
(a) ""
(b) 0
(c) false
(d) nil - CORRECT ANSWERS A1: (a) and (b)
In Ruby, all objects have a logical value for use in conditional
statements.
The objects false and nil are treated as logically false, all other objects
are treated as logically true.
Which of the following are reserved words in Ruby? (Choose two.)
(a) each
(b) rand
(c) class
(d) send
(e) true - CORRECT ANSWERS (c) and (e)
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
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Ruby Association Certified Ruby Examination Silver Sample Questions UPDATED Study Guide and more Exams Medicine in PDF only on Docsity!

Ruby Association Certified Ruby

Examination Silver Sample Questions

UPDATED Study Guide QUESTIONS

AND CORRECT ANSWERS

  • Which of the following have true values in Ruby? (Choose two.) (a) "" (b) 0 (c) false (d) nil - CORRECT ANSWERS A1: (a) and (b) In Ruby, all objects have a logical value for use in conditional statements. The objects false and nil are treated as logically false, all other objects are treated as logically true. Which of the following are reserved words in Ruby? (Choose two.) (a) each (b) rand (c) class (d) send (e) true - CORRECT ANSWERS (c) and (e)

Which variable name is valid in Ruby? (Choose one.) (a) 3y (b) false (c) _9class (e) xyz$ - CORRECT ANSWERS c) Ruby variable names must begin with a lowercase letter or underscore, and may contain only letters, numbers, and underscore characters. Variable names must not conflict with keywords (e.g. you cannot have a variable called class), but unambiguous names that contain reserved words are acceptable (i.e. both classy and _class are valid Ruby variable names) Which of the following can be inserted into (1) in order to generate the output below? (Choose two.) $code = "CODE" (1) [Output] i like writing CODE (a) puts "i like writing #{$code}" (b) puts "i like writing #$code" (c) puts 'i like writing #{$code}'

A leading zero in an integer literal indicates 'octal-mode' in Ruby, i.e. a number in base 8 format. However, all print functions in Ruby will output numeric values in base 10 by default. Should you need to output numbers in something other than base 10, there are many different functions in Ruby for formatted numeric output (e.g. String#%, Numeric#to_s(base), Kernel#sprintf) Given the following: x = "Hello" y = x.empty?? 1 : 2 p y Which is the correct output? (Choose one.) (a) 1 (b) 2 (c) "Hello" (d) true - CORRECT ANSWERS (b) The ternary operator (cond? expr1 : expr2) is a compact form of if/else which will return expr1 if cond is true, otherwise will return expr2. It is most suitable for short statements that easily fit on a single line. Given the following:

amount = 120 size = case amount when 1..120; "S" when 120..170; "M" when 170..200; "L" else "XL" end p size (a) "S" (b) "M" (c) "L" (d) "XL" Which is the correct output? (Choose one.) - CORRECT ANSWERS (a) Ruby case statements will select the first branch to match its when condition. Because Ruby's two-dot range literal is an inclusive range, the end value is included as part of the range. So although both 1..120 and 120..170 include 120, the when 1.. branch is matched because it appears first in the case statement.

daikon apple - CORRECT ANSWERS (d) Although local variables from the surrounding scope are accessible within blocks, block parameters themselves are always block-local variables. This means that when a block parameter has the same name as a local variable from the surrounding scope, within the block any references will refer to the block-local variable. This prevents accidental modification of variables from the outside scope due to naming collisions. Defining block parameters with the same name as a local variable from the surrounding scope is considered an antipattern and may be a sign of an accidental programming error. To catch this problem, run ruby with the - w flag, and you will see warnings like warning: shadowing outer local variable - item wherever this problem occurs. Given the following: x = 0 4.times do |i| x += i end p x Which is the correct result? (Choose one.)

(a) 0 (b) 4 (c) 6 (d) A syntax error occurs - CORRECT ANSWERS (c) The Integer#times method yields values starting at zero, up to one less than the specified integer. Although block variables with the same name of local variables from the surrounding scope are shadowed (see A8), other local variables are accessible and can be modified. This is because Ruby blocks are closures. Which of the following are correct to specify Windows-1252 as the encoding of the source code? (Choose all that apply.) (a) # charset: Windows- 1252 (b) # chars: Windows- 1252 (c) # coding: Windows- 1252 (d) # encoding: Windows- 1252 - CORRECT ANSWERS (c), (d) The encoding magic comment must appear as the first line of the file, unless a UNIX shebang line is present (in that case, the encoding line would be placed on the second line).

puts "hello".encoding.name Which is the correct result? (Choose one.) (a) ASCII-8BIT (b) UTF- 8 (c) Windows- 1252 (d) A syntax error occurs. - CORRECT ANSWERS (b) When a coding: comment is omitted, Ruby will use UTF-8 by default for its source encoding. Given the following: s = "pear" if s.empty? puts "blank" elsif s.length < 5 puts "short" else puts "long" end Which is the correct result? (Choose one.)

(a) blank (b) short (c) long (d) an exception is raised - CORRECT ANSWERS (b) In an if/elsif/else conditional statement, the first matching if or elsif branch will be executed. If none match, then the else branch will be run. Given the following: s = "daikon" if s.empty? puts "blank" elsif s.length < 5 puts "short" else puts "long" end Which is the correct result? (Choose one.) (a) blank (b) short (c) long

a source of confusion if you're coming to Ruby from another language, and the only solution is to memorize the specific term used in each language. Which of the following regular expressions only match the string "Ruby" or "ruby"? (Choose two.) (a) /\A[Rr]uby\z/ (b) /\ARuby|ruby\z/ (c) /\A[Rr][u][b][y]\z/ (d) /\AR|ruby\z/ - CORRECT ANSWERS (a) and (c) Character classes ([...]) match any single character from within the brackets. Alternatives (...|...) are used to match any one of many possible subexpressions. The \A anchor matches the beginning of a string, and the \z anchor matches the end of a string. Note that the reason that (b) is not a correct answer is because its subexpressions are \ARuby and ruby\z, allowing matches for things like Ruby Which of the following regular expressions has a match only when a string has at least one character, and consists of lowercase letters only? (Choose one.)

(a) /\A[a-z]\z/ (b) /\A[a-z][a-z]\z/ (c) /\A[a-z][^a-z]\z/ (d) /\A[a-z][a-z].\z/ - CORRECT ANSWERS (b) The expression /\A[a-z][a-z]\z/ could be described in words as "A string which begins with a lowercase letter, followed by zero or more additional lowercase letters and nothing else." Here are some additional notes for understanding the other patterns in this question: The * quantifier matches a subexpression zero or more times. The. quantifier matches any character except a newline. The ^ inverts a character class, causing it to match anything except the named chars. (see A16 for a recap of other features that were already covered in that question) Given the following: MSG = 42 MSG += 5 p MSG What is the correct result? (Choose one.)

(d) hello is displayed since MSG is a constant. - CORRECT ANSWERS (b) No warning is shown because the constant is not being redefined; instead the object it references is being modified. By convention, objects referenced by constants are usually treated as immutable. But there are certain rare cases where that convention would not apply. Which of the following statements are true? (Choose two.): (a) Local variables start with a lower case letter, and are two or more characters in length. (b) Global variables start with $. (c) Instance variables start with *. (d) Class variables start with $. (e) Constants start with an upper case letter. - CORRECT ANSWERS (b) and (e) Some notes on Ruby variable naming rules: Global variables start with $. Class variables start with @@. Instance variables start with @. Local variables must begin with a lowercase letter or an underscore.

The remaining characters in any variable type are limited to letters, numbers, and underscores. Given the following: x = [1,2,3,4,5,6,7,8] y = x x.reject! { |e| e.even? } p x p y Which is the correct output? (Choose one) (a) [1,3,5,7] [1,2,3,4,5,6,7,8] (b) [1,2,3,4,5,6,7,8] [1,2,3,4,5,6,7,8] (c) [1,3,5,7] [1,3,5,7] (d)

shift removes the first element of an array and returns its value. pop removes the last element of an array and returns its value push adds the specified element to the end of an array. Which of the following can be inserted into (1) in order for the given code to generate the output below? (Choose one.) x = true x (1) exit(1) puts("succeeded!") [Output] succeeded! (a) | (b) || (c) & (d) && - CORRECT ANSWERS (b) The logical || and && operators short-circuit, only executing the right side of the expression if necessary. The special | and & operators provided on Ruby's boolean objects do not short circuit, so the right side of the expression is always evaluated.

Note that all Ruby objects support the || and && operators, but not all objects implement | and &. Given the following: m = true m or n = true p n Which is the correct result? (Choose one.) (a) true (b) false (c) nil (d) A syntax error occurs. - CORRECT ANSWERS (c) Although the or operator short circuits and the n = true expression is never executed, the n local variable is still statically declared. Therefore, the variable is present but its value is nil. Which of the following can be inserted into (1) in order for the given code to generate the output below? (Choose two.) x = [ 9, 7, 5, 3, 1 ] p (1) [Output]