Test Your Java Knowledge:  Using Modifiers, Part 1

True or false?  Top-level classes that are declared private may be accessed only by other classes in the same package. Show off your smarts or learn a thing or two in our quiz series.

Published: February 9, 2001
By Richard G. Baldwin

Questions

Lesson 13


Welcome

The purpose of this series of tutorial lessons is to help you learn Java by approaching it from a question and answer viewpoint.

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.  True or false?  Any of the access modifiers, public, protected, or private, can be applied to a top-level class.

Answer and Explanation

2.  Which, if any, of the following declarations are legal?

Answer and Explanation

3.  True or false?  Access  modifiers control which classes may use a feature.  A class' features are:

Answer and Explanation

4.  True or false?  Top-level classes that are declared private may be accessed only by other classes in the same package.

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

7.  True or false?  friendly is a Java keyword that is used to specify a level of access.

Answer and Explanation

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.)

Answer and Explanation

9.  True or false?  The Java runtime environment considers that all class files in its current working directory constitute a package.

Answer and Explanation

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

Answer and Explanation



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.

baldwin.richard@iname.com



 

Answers and Explanations

Answer 10

The answer is C, 15.

Back to Question 10

Explanation 10

Code in a method belonging to an object of a subclass has direct access to protected variables declared in the superclass.

Answer 9


The answer is true.

Back to Question 9

Explanation 9

This is sometimes referred to as the default package.

Answer 8

The answer is true.

Back to Question 8

Explanation 8

No explanation needed for this simple factual answer.

Answer 7

False.

Back to Question 7

Explanation 7

friendly is not a Java keyword (I told you that in the answer to question 2 above).  It is simply a name that is given, by some authors, to the access level that results from not specifying an access modifier.  Other authors use different names such as package for this level of access.

Answer 6

The answer is A, compiler error.

Back to Question 6

Explanation 6

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 5

Ans:

The answer is C, 15.

Back to Question 5

Explanation 5

Even though the instance variable named x is private, the variable in the object referred to by ref2 can be accessed by the object referred to by ref1.  The important point is that access modifiers dictate which classes, not which instances, of a class may access features.  Thus, a private instance variable can be accessed by any instance of the class in which the private instance variable is defined.

Answer 4


False.

Back to Question 4

Explanation 4

A top-level class may not be declared private.

Answer 3


True according to The Complete Java 2 Certification Study Guide, by Roberts, Heller, and Ernest.

Back to Question 3

Explanation 3

However, apparently when Roberts refers to the "class variables", he is referring to all of the member variables of the class without regard to the use of the static keyword to differentiate between class variables and instance variables.

Answer 2

Only A and D are legal.

Back to Question 2

Explanation 2

B is illegal because a feature may have at most one access modifier.  C is illegal because friendly is not a Java keyword.  D is the legal version of friendly access.
 

Answer 1

False.

Back to Question 1

Explanation 1

The only access modifier (other than friendly or package) that can be applied to a top-level class is public.



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.

baldwin.richard@iname.com

-end-