Learning Language Fundamentals

1. A class declaration contains field declarations, method declarations, constructor declarations, and other initializer (instance and class) declarations.

2. Identifier transient is a reserved word in Java. Identifier delegate is not a reserved word in Java.

3. A variable is a memory location whose value can change.

4. Character is Java's only unsigned primitive type. It is represented in source code via the char reserved word.

5. The difference between an instance field and a class field is that each object (instance) gets its own copy of an instance field, whereas all objects share a single copy of a class field.

6. An array is a multivalued variable in which each element holds one of these values.

7. You declare a one-dimensional array variable with a single set of square brackets, as in String[] cities;. You declare a two-dimensional array variable with two sets of square brackets, as in double[][] temperatures;.

8. Scope refers to a variable's accessibility. For example, the scope of a private field is restricted to the class in which it is declared. Also, the scope of a parameter is restricted to the method in which the parameter is declared. Another word for scope is visibility.

9. String literal "The quick brown fox \jumps\ over the lazy dog." is illegal because, unlike \", \j and \ (a backslash followed by a space character) are not valid escape sequences. To make this string literal legal, you must escape these backslashes, as in "The quick brown fox \\jumps\\ over the lazy dog.".

10. The purpose of the cast operator is to convert from one type to another type. For example, you can use this operator to convert from floating-point type to 32-bit integer type.

11. The new operator is used to create an object.

12. You cannot nest multiline comments.

13. The answer is true: when declaring a method that takes a variable number of arguments, you must specify the three consecutive periods just after the rightmost parameter's type name.

14. Given a two-dimensional array x, x.length returns the number of rows in the array.

The difference between the while and do-while statements is that a while statement performs zero or more iterations, whereas a do-while statement performs one or more iterations.

16. Initializing the sines array using the new syntax yields double[] sines = new double[360];. Initializing the cosines array using the new syntax yields double[] cosines = new double[360];.

17. It is okay for an expression assigned to an instance field to access a class field that is declared after the instance field because all class fields are initialized before any instance fields are initialized. The compiler knows that the virtual machine will know about the class fields before an object is created. As a result, this situation does not result in an illegal forward reference.

18. Creating an array of objects requires that you first use new to create the array, and then assign an object reference to each of the array's elements.

19. You prevent a field from being shadowed by changing the name of a same-named local variable or parameter, or by qualifying the local variable's name or a parameter's name with this or the class name followed by the member access operator.

20. You chain together instance method calls by having each participating method specify the name of the class in which the method is declared as the method's return type, and by having the method return this.

21. Calculating the greatest common divisor of two positive integers, which is the greatest positive integer that divides evenly into both positive integers, provides another example of tail recursion. Listing 2 presents the source code.

Listing 2. Recursively calculating the greatest common divisor public static int gcd(int a, int b)

// The greatest common divisor is the largest positive integer that // divides evenly into two positive integers a and b. For example, // GCD(12,18) is 6.

if (b == 0) // Base problem return a; else return gcd(b, a%b);

As with the Math class's various static methods, the gcd() method is declared to be static because it does not rely on any instance fields.

22. Merging the various CheckingAccount code fragments into a complete application results in something similar to Listing 3.

Listing 3. A CheckingAccount class that is greater than the sum of its code fragments public class CheckingAccount {

private String owner; private int balance; public static int counter;

public CheckingAccount(String acctOwner, int acctBalance) {

owner = acctOwner; balance = acctBalance;

counter++; // keep track of created CheckingAccount objects

public CheckingAccount(String acctOwner) {

this(acctOwner, 100); // New account requires $100 minimum balance

public CheckingAccount printBalance() {

System.out.println(owner+"'s balance:");

int magnitude = (balance < 0) ? -balance : balance;

String balanceRep = (balance < 0) ? "(" : "";

balanceRep += magnitude;

balanceRep += (balance < 0) ? ")" : "";

System.out.println(balanceRep);

return this;

public CheckingAccount deposit(int amount) {

System.out.println("cannot deposit a negative or zero amount"); else balance += amount; return this;

public CheckingAccount withdraw(int amount) {

System.out.println("cannot deposit a negative or zero amount"); else if (balance-amount < 0)

System.out.println("cannot withdraw more funds than are available"); else balance -= amount; return this;

public static void main(String[] args) {

new CheckingAccount("Jane Doe", 1000).withdraw(2000).printBalance(); CheckingAccount ca = new CheckingAccount("John Doe"); ca.printBalance().withdraw(50).printBalance().deposit(80).printBalance(); System.out.println ("Number of created CheckingAccount objects = "+ ca.counter);

Was this article helpful?

0 0

Post a comment