wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Ruby

Total questions: 80

Worksheet time: 27mins

Name
Class
Date
1.
Which variable name is valid in Ruby? (Choose one.)
a)
3y
b)
=FALSE()
c)
_9class
d)
xyz$
e)
Pluto
2.

Given the following:

MSG = "hello"

MSG.upcase!

p MSG

What is the correct result? (Choose one.)

a)

An error occurs because MSG is a constant.

b)

HELLO is displayed without warning.

c)

A warning apears because MSG is a constant but HELLO is displayed

d)

hello is displayed since MSG is a constant.

3.
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 *.Constants start with an upper case letter.
4.
Choose one suitable name for the class variable.
a)
@name
b)
@@name
c)
$name
d)
$$name
5.
Select all correct variable names in Ruby
a)
@s
b)
@@s
c)
@@@s
d)
$$s
6.

Given the following:

MSG = "hello"

MSG = MSG.upcase

p MSG

What is the correct result? (Choose one.)

a)

An error occurs because MSG is a constant.

b)

HELLO is displayed without warning.

c)

A warning apears because MSG is a constant but HELLO is displayed

d)

hello is displayed since MSG is a constant.

7.

Which of the following can be inserted into __(1)__ in order to generate the output below? (Choose two.)

$code = "CODE"

__(1)__

a)

puts "i like writing #{$code}"

b)

puts "i like writing #$code"

c)

puts 'i like writing #{$code}'

d)

puts 'i like writing #$code'

8.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)

puts "$foo$".__(1)__("$", "")

[Output] Foo$

a)

sub

b)

chop

c)

delete

d)

delete_prefix

9.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose one.)

r = "a".."e" p r.__(1)__

[Output] ["a", "b", "c", "d", "e"]

a)

array

b)

to_ary

c)

to_a

d)

to_array

10.
Given the following: p "hello ruby world"[6,4] What is the correct result? (Choose one.)
a)
"hello "
b)
"ruby"
c)
" world"
d)
An error occurs at run-time.
11.

Given the following:

str = "bat"

str[1,1] = "o"

p str

Which is the correct output? (Choose one.)

a)

"boo"

b)

"bot"

c)

"oat"

d)

"o"

12.
Given the following: puts 5 * "hi" What is the correct result? (Choose one.)
a)
"hihihihihi"
b)
An error occurs at run-time.
c)
"5hi"
d)
"5*hi"
13.
Select two of the codes below that display 4 when run .
a)
p "Ruby".size
b)
p "Ruby".count
c)
p "Ruby".number
d)
p "Ruby".length
14.

Select the correct one as the output of the code below.

p "090-9999-0000".split('-')

a)

["090", "9999", "0000"]

b)

["09099990000"]

c)

"09099990000"

d)

"090", "9999", "0000"

15.
Choose two methods that do not exist in Ruby's String class .
a)
shift
b)
scan
c)
join
d)
split
16.
Select two code that outputs true when run .
a)
puts "".empty?
b)
puts "".blank?
c)
puts "".nil?
d)
puts [].empty?
17.
Which of the following have true values in Ruby? (Choose two.)
a)
""
b)
0
c)
=FALSE()
d)
nil
18.

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()

19.

amount = 120

size = case amount

when 1..120;

"S"

when 120..170;

"M"

when 170..200;

"L"

else "XL"

end

p size

Which is the correct output? (Choose one.)

a)

"S"

b)

"M"

c)

"L"

d)

"XL"

20.

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

21.

Select the correct one as the result of executing the code below.

x = 10

y = 10

unless x.eql?(y)

puts 'A'

elsif

x.equal?(y)

puts 'B'

else

puts 'C'

End

a)

A is displayed

b)

B is displayed as

c)

C is displayed

d)

I get an error

22.

Select the correct one as the result of executing the code below.

if 0

puts "A"

elsif 0.0

puts "B"

else

puts "C"

End

a)

A is output as

b)

B is output as

c)

C is output as

d)

Exception occurs

23.

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)

syntax error

24.
Given the following: def foo(x: 1, y: 2, z: 3) p [x, y, z] end foo(y: 4, z: 5) Which is the correct result? (Choose one.)
a)
[1, 2, 3]
b)
[1, 4, 5]
c)
A syntax error occurs.
d)
An error occurs at run-time.
25.

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

26.

Given the following:

num = 025

puts num

Which is the correct output? (Choose one.)

a)

nil

b)

25

c)

21

d)

25

27.

Select the correct one as the output of the code below.

P 9 / 2

a)

["090", "9999", "0000"]

b)

1

c)

4

d)

4.5

28.
Choose one code that prints 1.
a)
Puts 9 % 2
b)
Puts 9 / 2
c)
puts 9 mod 2
d)
Puts 9 & 2
29.

Select the correct one as the output of the code below.

x = 5 * 2 ** 3

puts x

a)

27

b)

40

c)

45

d)

1000

30.

Select the correct one as the output of the code below.

x = 10

y = 10.0

print x == y

print ','

print x.eql?(y)

a)

true,true

b)

true,false

c)

false,true

d)

false,false

31.

Given the following:

item = "apple"

["banana", "carrot", "daikon"].each do |item|

puts item

end

puts item

Which is the correct result? (Choose one.)

a)

A syntax error occurs

b)

An exception is raised

c)

banana carrot daikon Daikon

d)

banana carrot daikon Apple

32.

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

33.

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)

[1, 3, 5, 7] [2, 4, 6, 8]

34.

Given the following:

a = [ 2, 4, 6, 8, 10 ]

a.shift

a.pop

a.push(12)

p a

Which is the correct output? (Choose one.)

a)

[2, 4, 6, 8, 10, 12]

b)

[2, 4, 6, 8, 10]

c)

[4, 6, 8, 12]

d)

[4, 6, 8]

35.

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] [7, 5, 3]

a)

X[1, 3]

b)

X[1..-1]

c)

X[-3..-1]

d)

X[-4..-2]

36.

Which of the following can be inserted into (1) in order for the given code to generate the output below? (Choose one.)

ary = [ 1, 2, 3, 4, 5 ]

p ary.__(1)__ { |i| i.odd? }

[Output] [1, 3, 5]

a)

collect

b)

select

c)

map

d)

filter

37.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)

a = [120, 40, 20, 80, 160, 60, 180]

a.__(1)__

p a

[Output] [120, 80, 160, 180]

a)

reject! {|i| i < 80}

b)

reject {|i| i < 80}

c)

delete_if! {|i| i < 80}

d)

delete_if {|i| i < 80}

38.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose one.)

p ["apple", "banana"] __(1)__ ["banana", "carrot"]

[Output] ["apple", "banana", "carrot"]

a)

.concat

b)

&

c)

|

d)

||

39.
Given the following: p %i(x1 x2 x3) What is the correct result? (Choose one.)
a)
"x1 x2 x3"
b)
[1, 2, 3]
c)
["x1", "x2", "x3"]
d)
[:x1, :x2, :x3]
40.
Given the following: p [0,1,2,3,4,5].find {|x| x < 3} Which is the correct output? (Choose one.)
a)
[0, 1, 2]
b)
0
c)
[0, 1, 2, 3]
d)
=TRUE()
41.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)

p [1,16,8,4,2].__(1)__

[Output] [16, 8, 4, 2, 1]

a)

sort_by { |x| -x }

b)

sort_reverse

c)

sort.reverse

d)

reverse.sort

42.
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/
43.
Select two strings that match the regular expression below . /\A\w[^RUBY].[PHP]/
a)
"RUBY"
b)
"RVBP"
c)
"AUHP"
d)
"ASAP"
44.
p "Ruby on Rails".scan(/\s/) Select the correct one as the output of the code below.
a)
[" ", " "]
b)
["Ruby"]
c)
["Ruby", "on", "Rails"]
d)
[]
45.

Select the correct one as the output of the code below.

s = "Ruby on Rails"

p s.scan(/\w/).size

a)

1

b)

2

c)

11

d)

13

46.

Which of the following methods will NOT show you if the element :c exists in the hash key or not? (Choose one.)

h = {a: 2, b: 4, c: 6, d: 8, e: 10}

a)

p h.has_key?(:c)

b)

p h.contain?(:c)

c)

p h.key?(:c)

d)

p h.member?(:c)

47.

Select the correct one as the output of the code below.

h = {"Ruby" => 1, "JS" => 2}

p h.size

a)

0

b)

1

c)

2

d)

4

48.

When I ran the code below, it printed true. Please select two descriptions to fill in the blank [1] .

h = {"Ruby" => 1, "PHP" => 2}

p [1]

a)

h.key?("PHP")

b)

h.has_key?("PHP")

c)

h.key_exist?("PHP")

d)

h.include_key?("PHP")

49.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

h1 = {:a => 1, :b => 2, :c => 3}

h2 = h1.[1]

p h2

output {:a=>1, :c=>3}

a)

reject { |v, k| v == 2 }

b)

reject { |k, v| v == 2 }

c)

delete { |v, k| v == 2 }

d)

delete { |k, v| v == 2 }

50.

Select the correct one as the output of the code below.

h = {"Ruby" => 1, "PHP" => 2, "C++" => 2}

p h.find { |k, v| v == 2 }

a)

["PHP", 2]

b)

{"PHP"=>2}

c)

[["PHP", 2], ["C++", 2]]

d)

{"PHP"=>2, "C++"=>2}

51.

Select the correct one as the result of executing the code below.

h = {"Ruby" => 1, "PHP" => 2}

p h.member?("PHP")

a)

2

b)

=FALSE()

c)

=TRUE()

d)

"PHP"

52.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

h = {"Ruby" => 1, "JS" => 2}

p [1]

output [1, 2]

a)

h.value

b)

h.values

c)

h.fetch

d)

h.select

53.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

h = {"Ruby" => 1, "Perl" => 2}

p h.[1]

output ["Ruby", "Perl"]

a)

key

b)

keys

c)

value

d)

values

54.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

h = {"Ruby" => 1, "PHP" => 2, "C++" => 3}

p h.[1] { |k, v| k == "PHP" }

output {"PHP"=>2}

a)

find

b)

select

c)

find_all

d)

fetch

55.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

h = {"Ruby" => 1, "PHP" => 2, "C++" => 3}

p [1]

output [3]

a)

h["C++"]

b)

h.value_at("C++")

c)

h.values_at("C++")

d)

h.value("C++")

56.
Given the following: begin ans = 100/0 puts ans rescue ZeroDivisionError puts "Error: ZeroDivisionError" exit 1 ensure puts "DONE!" end Which is the correct output? (Choose one.)
a)
0 DONE!
b)
Error: ZeroDivisionError
c)
Error: ZeroDivisionError DONE!
d)
Error: ZeroDivisionError 0
57.

When I run the code below, it says "Exception". Please select one appropriate description to fill in the blanks [1] and [2].

s = "abc"

[1]

puts s - 2

[2]

puts 'Exception'

End

a)

1 begin 2 rescue

b)

1 catch 2throw

c)

1 begin 2throw

d)

1 catch 2rescue

58.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

arr = [1, 2, 3]

begin

puts arr.text

rescue => ex

puts ex.[1]

end

output undefined method `to_r' for [1, 2, 3]:Array

a)

text

b)

message

c)

information

d)

error_message

59.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

begin

puts xy + "z"

rescue [1]

puts ex.message

end

output undefined local variable or method `xy' for main:Object

a)

StandardError >> ex

b)

StandardError = ex

c)

StandardError => ex

d)

StandardError => ex

60.

I ran the code below and got the following output: Please select one appropriate description to fill in the blank [1]. code

begin puts xy + "z"

rescue

puts "abc"

[1]

puts "xyz"

end

output: abc Xyz

a)

ensure

b)

else

c)

retry

d)

catch

61.

Given the following:

class Object

def moo

puts "MOO!"

end

end

"Cow".moo

What is the correct result? (Choose one.)

a)

No output.

b)

An error occurs at run-time.

c)

MOO!

d)

nil

62.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose one.)

class Shouter

def __(1)__(message)

@message = message

end

def greet

puts @message.upcase

end

end

g = Shouter.new("Hello, world!")

g.greet

[Output] HELLO, WORLD!

a)

Shouter

b)

new

c)

initialize

d)

__init__

63.

Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose one.)

class Shouter

def initialize(message)

@message = message

end

def greet

puts @message.upcase

end

end

g = __(1)__("Hello, world!")

g.greet

[Output] HELLO, WORLD!

a)

Shouter

b)

#Shouter

c)

new Shouter

d)

Shouter.new

64.

Given the following:

class Foo

attr_reader :var

def initialize

@var = "apple"

end

end

class Bar < Foo

def initialize

@var = "banana"

super

end

end

bar = Bar.new

puts bar.var

What is the correct result? (Choose one.)

a)

apple

b)

banana

c)

No output.

d)

An error occurs at run-time.

65.
Please select all valid class names.
a)
my_string
b)
myString
c)
MyString
d)
MYSTRING
66.

Select the correct one as the output of the code below.

class User

end

class Admin < User

end

print User.superclass

print ' '

print Admin.superclass

a)

Object Object

b)

Object User

c)

Admin Object

d)

Admin User

67.

When I ran the code below, it printed 600. Please select one appropriate description to fill in the blank [1].

class Test1

def initialize(number)

@number = number * 60

end

end

class Test2 < Test1

def initialize(number)

【 1 】(number)

end

def number

@number

end

end

n = Test2.new(10)

p n.number

a)

initialize

b)

Test1.initialize

c)

super

d)

new

68.

Select the correct one as the output of the code below.

class Test1

def initialize(number)

@number = number * 10

end

end

class Test2 < Test1

def initialize(number)

@number = number * 5

super(number)

end

def number

@number

end

end

n = Test2.new(3)

p n.number

a)

0

b)

3

c)

15

d)

30

69.

Select the correct one as the output of the code below.

class Animal

attr_reader :name

def initialize

@name = "Hello"

end

end

class Cat < Animal

def initialize

@name = "Ruby"

end

end

a = Cat.new

p a.name

a)

""

b)

"Ruby"

c)

"Hello"

d)

nil

70.
The next sentence is about Ruby classes and modules. Please select 3 that are correct .
a)
A Ruby class can have multiple parent classes
b)
A Ruby module cannot be instantiated by itself
c)
A Ruby class can have multiple modules
d)
Ruby modules cannot inherit
71.

Given the following:

File.write("test.txt", "hellorubyworld\n")

File.open("test.txt") do |file|

file.seek(5)

print file.gets

end

Which is the correct output? (Choose one.)

a)

hello

b)

rubyworld

c)

hellor

d)

rubyw

72.

The code below was used to open a file omitting the second argument of the open method.

In this case, which of the following is implicitly specified? (Choose one.)

file = open("sample.txt")

a)

r

b)

r+

c)

a

d)

a+

73.

Which of the following can be inserted into __(1)__ in order for the given code to copy the content of file "test_one.txt" to file "test_two.txt"?

In the case that the “test_two.txt” file already exist, this code should set first the file size to zero and then overwrites its content from the beginning. (Choose one.)

open("test_one.txt") {|source| open("test_two.txt", "__(1)__") {|dest| dest.write(source.read) } }

a)

r+

b)

a

c)

a+

d)

w

74.
Select the correct one as the output of the code below. p File.split("/Users/guest/sample.rb")
a)
["Users", "guest", "sample.rb"]
b)
["/Users", "/guest", "sample.rb"]
c)
["/Users/guest", "sample.rb"]
d)
["/Users/guest/sample.rb"]
75.
Select the correct one as the output of the code below. puts File.join("Users", "guest", "1.rb")
a)
Usersguest1.rb
b)
/Usersguest1.rb
c)
Users guest 1.rb
d)
Users/guest/1.rb
76.

Select the correct one as the result of executing the code below. The content of the file "sample.txt" is as follows. code

File.open("sample.txt", "a") do |io_f|

p io_f.read

end

File contents: Ruby on Rails Rails Tutorial

a)

"Ruby on Rails\nRails Tutorial" is output as

b)

"Ruby on Rails\n" is output as

c)

"Ruby on Rails" is output as

d)

Exception occurs

77.

module Gfg

def portal

puts "Welcome to Rubyl!"

end

end

class RubyWorld

include Gfg

def add

x = 30 + 20

puts x

end

end

obj_class = RubyWorld.new

obj_class.portal

Select the correct one as the output of the code below.

a)

nil

b)

Welcome to Rubyl!

c)

error occur

d)

50

78.

Which of the following can be inserted into __(1)__

module Gfg

def portal

puts "Welcome to Rubyl!"

end

end

class RubyWorld

_1_ Gfg

def add

x = 30 + 20

puts x

end

end

RubyWorld.portal

output: Welcome to Rubyl!

a)

include

b)

includes

c)

extend

d)

extends

79.

module Gym

class Push

def up

puts 40

end

end

end

d = _1_.new

p d.up

output: 40

Which of the following can be inserted into __(1)__

a)

Gym:Push

b)

GymPush

c)

Gym_Push

d)

Gym::Push

80.

module Foo1

def self.class_method_1

puts "this is class method"

end

def foo_name

puts "My name is Boo!!!"

end

end

class Bar1

include Foo1

end

Bar1.new.foo_name

Bar1.new.class_method_1

Foo1.class_method_1

Select the correct one as the output of the code below.

a)

"My name is Boo!!!", exception raise, exception raise

b)

exception raise, "My name is Boo!!!", , "this is class method"

c)

"My name is Boo!!!", exception raise, "this is class method"

d)

"My name is Boo!!!", "My name is Boo!!!", "My name is Boo!!!",