Rubyがミニツク

あなたのRubyの技術に関するより詳しい知識と理解を増やします。

Certified Ruby Programmer Silver Preparation Course

このコースについて

This course contains a set of drills that you can study to prepare yourself for the Certified Ruby Programmer Silver Examination.

コンテンツ一覧

ヒント レッスンの名前をクリックすると、その内容を表示します。

次のレッスン ホームへ戻る

Certified Ruby Programmer Silver Preparation Course - Part 5

コンテンツ概要

Prepare Yourself for the Ruby Association Certified Ruby Programmer (Silver) Examination with the practice questions in this series of drills. The series cover the test curriculum as set out by the Ruby Association. This part deals with Ruby's built-in IO, File and Dir classes. Note, the questions in this drill assume Ruby version 1.8.7 unless otherwise stated.

このドリルは14問の問題があります。


はじめる

問題

In the program shown below, which method calls are needed in place of (1) and (2), assuming that we are to produce the “Intended output”?

Intended output:

R u b y
u
b
y
File.open("fancy.txt", "w") do |f|
  f.(1)("R u b y\n")
  f.(2)(["u","b","y"])
end
puts File.read("fancy.txt")

選択肢を選ぶ

(d) puts, print

(b) write, puts

(a) puts, write

(c) write, print


次の問題へ進む

問題

Which one of the four choices represents the output of the program below?

p File.join("pocahontas", "tepee.rb")

選択肢を選ぶ

(c) #

(b) "pocahontas/tepee.rb"

(a) #

(d) #


次の問題へ進む

問題

Which of the four choices correspond to the output of running the following program?

p File.split("/home/john/bookmarks/").join

選択肢を選ぶ

(b) "/home/john/bookmarks"

(a) "homejohnbookmarks"

(d) "/home/johnbookmarks"

(c) "/home/john/bookmarks/"


次の問題へ進む

問題

Choose the correct replacement for (A) in the following program, given that the program must produce the specified output.

["bin/hallo.py", "ola.lua", "/tmp/hej.cpp"].each do |filename|
  puts (A)
end

Output:

#=> hallo.rb
#=> ola.rb
#=> hej.rb

選択肢を選ぶ

(c) File.basename(filename, ".*") + ".rb"

(d) File.basename(filename, ".*") + "rb"

(a) File.basename(filename) + ".rb"

(b) File.basename(filename, ".*") + File.extname(filename)


次の問題へ進む

問題

What is the common return type of the File.extname, File.basename, File.dirname methods?

選択肢を選ぶ

(d) Dir

(a) String

(b) IO

(c) File


次の問題へ進む

問題

Choose the correct replacement for (A) in the following program, given that the program must produce the specified output.

# Create a file with a list of the planets in our solar system
File.open("planets.txt", "w") do |f|
  f.puts(%w[Mercury Venus Earth Mars Jupiter Uranus Neptune])
end
f = File.open("planets.txt")
(A)

Output:

"Mercury\nVenus\nEarth"

選択肢を選ぶ

(c) p f.read(3)

(a) p f.gets("Mars")

(b) p f.readline("Earth")

(d) p f.readlines("Pluto")[0]


次の問題へ進む

問題

Choose the method name which does not correspond to a file timestamp retriever method in Ruby’s built-in File class.

選択肢を選ぶ

(c) ctime

(a) atime

(b) btime

(d) mtime


次の問題へ進む

問題

Which of the following choices does not represent a valid way of looking up a file timestamp?

選択肢を選ぶ

(d) File::Stat.atime(filename)

(c) File.open(filename).mtime

(b) File.stat(filename).atime

(a) File.ctime(filename)


次の問題へ進む

問題

Choose the correct replacement for (A) in the following program, given that the program must produce the specified output (as specified with #=>).

puts File.read("readme.txt") #=> apples
File.open("readme.txt", (A)) do |f|
  f.write(" bananas")
end
puts File.read("readme.txt") #=> apples bananas

選択肢を選ぶ

(d) "a"

(c) "w"

(b) a+

(a) a


次の問題へ進む

問題

Given that the “tmp” directory does not exist in the current working directory, how many “rocking.rb” files are created during the following program’s execution? And how many remains after the execution finishes?

["tmp", "tmp/lang",  "tmp/lang/ruby", "tmp/lang/python"].each do |dir|
  Dir.mkdir(dir)
end
Dir.chdir("tmp/lang")
Dir.new(".").each do |entry|
  filename = File.join(entry, "rocking.rb")
  File.open(filename, "w")
end
Dir.rmdir("python")

選択肢を選ぶ

(a) 2 are created, 1 remains

(b) 2 are created, 2 remain

(c) 4 are created, 3 remain

(d) 4 are created, 4 remain


次の問題へ進む

問題

What is the result of running the following program?

# Create a file with a list of hat types, each type on a line of its own
File.open("hats.txt", "w") do |f|
  f.puts(%w[Bowler Deerstalker Dunce\ cap Fedora Fez])
end

File.open("hats.txt") do |f|
  4.times { f.gets() }
  puts f.gets
  puts f.readline
end

選択肢を選ぶ

(a) outputs "Fez\n" then "Bowler\n" to the standard output

(c) outputs "Fez\n" to the standard output, then exits with an EOFError exception

(b) outputs "Fedora\n" then "Fez\n" to the standard output

(d) outputs "Fez\n" then "nil" to the standard output


次の問題へ進む

問題

Given a file “authors.txt” with the content shown below, what is the output of the following program?

Authors.txt:

Kawabata
Hemingway
Kafka
File.open("authors.txt") do |file|
  2.times { file.gets }
  file.lineno = 0
  puts file.gets
end

選択肢を選ぶ

(c) Kafka

(d) nil

(b) Hemingway

(a) Kawabata


次の問題へ進む

問題

Which one of the four statements holds true after executing the following program?

file = File.open("hello.rb", "w")
stat = file.stat
mtime1 = stat.mtime
file.puts("new data")
file.flush
mtime2 = stat.mtime

選択肢を選ぶ

(a) mtime1 > mtime2

(b) mtime1 < mtime2

(d) file.mtime is unchanged

(c) mtime1 == mtime2


次の問題へ進む

問題

What is the content of the file “program.rb” after the following program have been executed?

File.open("program.rb", "w") do |f|
  f.puts("=begin\n  A Ruby program\n=end")
end

選択肢を選ぶ

(a) The file's prior content followed by

  =begin
    A Ruby program
  =end

(b) The file's prior content, unchanged

(d) The file is empty

(c) The file contains

  =begin
    A Ruby program
  =end


次のレッスン ホームへ戻る