Chapter 1: The Way of the Program

1.7 Exercises

Exercise 1.1

Computer scientists have the annoying habit of using common English words to mean something different from their common English meaning. For example, in English, a statement and a comment are pretty much the same thing, but when we are talking about a program, they are very different.

The glossary at the end of each chapter is intended to highlight words and phrases that have special meanings in computer science. When you see familiar words, don’t assume that you know what they mean!

a. In computer jargon, what’s the difference between a statement and a comment?

b. What does it mean to say that a program is portable?

c. In computer jargon, what is an exception?

Exercise 1.2

Before you do anything else, find out how to run a Ruby program in your environment. Send reader to a tutorial?

a. Type in the “Hello, world” program, save the file as hello.rb, then run it.

b. Add a second puts statement that outputs a second message after the “Hello, world.” Something witty like, “How are you?” Run the program again.

c. Add a comment line to the program (anywhere). Run the program again. The new comment should not affect the execution of the program.

Exercise 1.3

It is a good idea to commit as many errors as you can think of, so that you see what error messages the interpreter produces. Sometimes Ruby will tell you exactly what is wrong, and all you have to do is fix it. Sometimes, though, it will produce wildly misleading messages. You will develop a sense for when you can trust the interpreter and when you have to figure things out yourself.

a. Remove the opening quotation mark.

b. Remove the closing quotation mark.

c. Remove one of the # symbols.

d. Instead of puts, write put.

e. Instead of a quotation mark, write an apostrophe.

f. Instead of both quotation marks, write apostrophes (trick exercise).

g. Remove the space between puts and "Hello, world." (trick exercise).

h. Replace puts with print. This one is tricky because it is a logical error, not a syntax error. The statement print is legal, but it may or may not do what you expect.

GNU Free Documentation License Valid HTML Valid CSS Built with BBEdit
Friendly links for Google “juice”