Questions
Lesson 4
September 23, 2000
Welcome
I recommend that you also make use of my online Java tutorial lessons which are designed from a more conventional textbook approach. Those tutorial lessons are published at Gamelan.com.
For your convenience, I also maintain a consolidated Table of Contents on my personal web site that links to the individual lessons on the Gamelan site.
Insofar as possible, I will make use of Sun Java in these lessons. However, it will not be possible for me to go back and do a full update each time Sun releases a new version, so over the course of time, I expect to use different versions of Sun Java.
Just in case you would like to sneak a peek, the answers to the questions, and the explanations of those answers are located (in reverse order) at the end of this file.
The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back. It is recommended that you make your first pass through the questions in the order that they appear so as to avoid inadvertently seeing the answer to a question before you provide your own answer.
1. What is the syntax for specifying the size of an array in Java?
2. What output is produced by the following program”
- A. A compiler error.
- B. A runtime error.
- C. 10
- D. 20
class Q004_02{ |
3. What output is produced by the following program”
- A. A compiler error.
- B. A runtime error.
- C. 10
- D. 20
class Q004_03{ |
4. True or false? Element values in Java arrays are automatically initialized when the array is constructed using the new operator.
5. When an array of char elements is constructed, the elements are automatically initialized to which of the following values:
- A. ‘0’
- B. ‘u0000’
6. What output is produced by the following program”
- A. A compiler error.
- B. A runtime error.
- C. 30
- D. 31
- E. 0
- F. 1
class Q004_06{ |
7. What output is produced by the following program? (Note the placeholder comma in the boldface portion.)
- A. A compiler error.
- B. A runtime error.
- C. 0
class Q004_07{ |
8. What is the name of the property of an array object that always contains the size of the array and how may it be used?
9. True or false? All Java applications and applets require a main() method.
Bonus question. The following question is considerably more difficult than the previous nine questions, and is included here to challenge you if the previous nine questions have been too easy.
10. What output is produced by the following program?
- A. A compiler error
- B. A runtime error
- C. 15
class Q004_10{ |
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
Answers and Explanations
Answer 10
Explanation 10
Code in a method belonging to an object of a subclass does not have direct access to private instance variables declared in the superclass, even though those variable are inherited into and belong to the object of the subclass. Private members can only be accessed directly by an instance of the class in which they are declared.
Answer 9
Explanation 9
Answer 8
Explanation 8
class Q004_08{ |
This program produces the following output, showing that the array contains four elements.
d 4
Note that although length is commonly referred to as a property of the array object, it is not a property in the sense of a JavaBean design-pattern property. If it were, it would not be directly accessible as shown in the above program. Rather, it would be accessible by invoking a method named getLength(). (I will have a lot more to say about JavaBean design patterns in a subsequent lesson.)
Answer 7
Explanation 7
Answer 6
Explanation 6
char[] a1 = {‘u0030′,’u0031’};
This syntax
- Declares a reference variable to refer to an array object.
- Instantiates the array object for a specified size (the size matches the number of initial values provided).
- Initializes the values of each of the array elements to the specified values.
Just to make things interesting, this program also uses the unicode hexadecimal notation to initialize the two array elements to values corresponding to the characters ‘0’ and ‘1’. This is how you can specify the values for unicode characters for which there are no corresponding character keys on your keyboard.
Answer 5
Explanation 5
This is easily demonstrated by the following simple program.
class Q004_05{ |
This program produced the following output:
0 48 49
This output resulted from casting three values to type int and displaying the three values.
The first value displayed was the value that was placed in the array element of type char through array initialization. As you can see, the value was 0. It was necessary to cast this to type int in order to display it because there is no displayable character associated with the unicode character with a value of 0.
The next two values displayed were the unicode values for the characters ‘0’ and ‘1’. These values were displayed simply to demonstrate that the unicode value for the character ‘0’ is not numeric zero, but instead is numeric 48.
Answer 4
Explanation 4
The default values are:
- false for boolean
- zero value for numeric
- null for object reference
This is easily demonstrated by the following program.
class Q004_04{ |
This program displays the default value for one element from each of the four arrays and produces the following output:
0 0.0 false null
Answer 3
Explanation 3
Answer 2
Explanation 2
Incompatible type for new. Explicit cast needed to convert long to int.
While it is allowable to use a variable of type byte, short, or int to specify the size of an array, you cannot specify the size of an array using a variable of type long.
Answer 1
Explanation 1
Note that an array is a special kind of object in Java, and instantiation of an array object is similar to, but different from the instantiation of an ordinary object. For example, the code to instantiate an ordinary String object might be as follows:
String myVar = new String(“Data”);
Note the differences:
- The array object is instantiated using square brackets whereas the ordinary object is instantiated using parentheses.
- For an array object, the new operator is applied to the type of the array whereas for an ordinary object, the new operator is applied to the constructor for the class from which the object is being instantiated.
When instantiating an array object, you can specify the size using either a literal integer value, or a variable.
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.