Monday, October 18, 2010

Questions On Java Programming

Q Is a line in a Java program the same thing as a statement?

No. Although the programs that you will create in this book put one statement on each line, this is done to make the programs easier to understand; it's not required. The Java compiler does not consider lines, spacing, or other formatting issues when compiling a program. The compiler just wants to see semicolons at the end of each statement. You can put more than one statement on a line, although this is not generally recommended.

Q Is there a reason to set up a variable without giving it a value right away?

For many of the simple programs that you will be creating in the first several hours, no. However, there are many circumstances where it makes more sense to give a variable a value at some point later in the program. One example would be a calculator program. The variable that stores the result of a calculation will not be needed until a user tries out the program's calculator buttons. Therefore, you would not need to set up an initial value when creating a
result variable.

Q What's the specific range for the long variable type?

In Java, a
long integer variable can be anything from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This range ought to give your mathematical expressions plenty of breathing room when you can't use int, which has a range of -2,147,483,648 to 2,147,483,647.

Q Why should the first letter of a variable name be lowercase, as in gameOver?

It makes the variable easier to spot among all of the other elements of a Java program. Also, by following a consistent style in the naming of variables, you eliminate errors that can occur when you use a variable in several different places in a program. The style of naming used in this book has become popular since Java's release.

Q Can two variables have the same letters but different capitalization, as in highScore and HighScore?

Each of the differently capitalized names would be treated as its own variable, so it's possible to use the same name twice in this way. However, it seems likely to cause a lot of confusion when you or someone else is attempting to figure out how the program works. It also increases the likelihood of using the wrong variable name somewhere in your program, which is an error that will not be caught during compilation. Errors like that make it into the finished product and are called logic errors. They must be caught by an attentive programmer during testing.
Test your knowledge of variables, expressions, and the rest of the information in this hour by answering the following questions.
1. What do you call a group of statements contained with an opening bracket and a closing bracket?

(a) A block statement
(b) Groupware
(c) Bracketed statements

boolean variable is used to store true-or-false values.

(a) True
(b) False
(c) No, thanks. I already ate.

What characters cannot be used to start a variable name?

(a) A dollar sign
(b) Two forward slash marks (//)
(c) A letter
1. a. The grouped statements are called a block statement or a block.

2. a. True or false are the only answers a
boolean variable can store.

3. b. Variables can start with a letter, dollar sign ($), or an underscore character (
_). If you started a variable name with two slash marks, the rest of the line would be ignored because the slash marks are used to start a comment line.

No comments:

Post a Comment