Published: February 9, 2001
By Richard G. Baldwin
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.
2. Which, if any, of the following declarations are legal?
3. True or false? Access modifiers control which classes may use a feature. A class' features are:
4. True or false? Top-level classes that are declared private may be accessed only by other classes in the same package.
5. What output is produced by the following program?
class Q19{ public static void main( String args[]){ AClass ref1 = new AClass(5); AClass ref2 = new AClass(10); System.out.println(ref1.add(ref2)); }//end main() }//end class definition class AClass{ private int x; AClass(int x){//constructor this.x = x; }// end constructor int add(AClass ref){ return x + ref.x; }//end add() }//end class AClass |
6. What output is produced by the following program?
class Q21{ public static void main( String args[]){ SubClass ref1 = new SubClass(5); SubClass ref2 = new SubClass(10); System.out.println(ref1.add(ref2)); }//end main() }//end class definition class AClass{ private int x; AClass(int x){//constructor this.x = x; }// end constructor }//end class AClass class SubClass extends AClass{ SubClass(int x){ super(x); }//end constructor int add(AClass ref){ return x + ref.x; }//end add() }//end class SubClass |
7. True or false? friendly is a Java keyword that is used to specify a level of access.
8. Preface: Friendly is the name given by Roberts, Heller, and Ernest, to the default access of classes, variables, and methods. If you don't specify an access modifier such as public, private, or protected, it is said to be friendly.
True or false? A class's friendly features are directly accessible to any class in the same package as the class in question. (In this case, directly accessible means that it is not necessary to go through a public member to gain access.)
9. True or false? The Java runtime environment considers that all class files in its current working directory constitute a package.
10. What output is produced by the following program?
class Q31{ public static void main( String args[]){ SubClass ref1 = new SubClass(5); SubClass ref2 = new SubClass(10); System.out.println( ref1.add(ref2)); }//end main() }//end class definition class AClass{ protected int x; AClass(int x){//constructor this.x = x; }// end constructor }//end class AClass class SubClass extends AClass{ SubClass(int x){ super(x); }//end constructor int add(AClass ref){ return x + ref.x; }//end add() }//end class SubClass |
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.
The answer is true.
The answer is C, 15.
False.
True according to The Complete Java 2 Certification
Study Guide, by Roberts, Heller, and Ernest.
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.
-end-