Test Your Java Knowledge:  Using Modifiers, Part 2

True or false?  Friendly or package access applies to all classes in the current working directory that are not declared to be public, private, or protected.? Show off your smarts or learn a thing or two in our quiz series.

Published: February 23, 2001
By Richard G. Baldwin

Questions

Lesson 14


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?  The only access control modifiers supported by Java are Answer and Explanation

2.  What output is produced by the following program?

class Q32{
  public static void main(
                        String args[]){
    SubSubClass ref1 = 
                    new SubSubClass(5);
    SubSubClass ref2 = 
                   new SubSubClass(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
  
}//end class SubClass

class SubSubClass extends SubClass{
  SubSubClass(int x){
    super(x);
  }//end constructor
  
  int add(AClass ref){
    return x + ref.x;
  }//end add()
    
}//end class SubSubClass

Answer and Explanation

3.  What output is produced by the following program?

class Q33{
  public static void main(
                        String args[]){
    SubSubClass ref1 = 
                    new SubSubClass(5);
    SubSubClass ref2 = 
                   new SubSubClass(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
  
  protected int add(AClass ref){
    return x;
  }//end add()

}//end class AClass

class SubClass extends AClass{
  SubClass(int x){
    super(x);
  }//end constructor
  
}//end class SubClass

class SubSubClass extends SubClass{
  SubSubClass(int x){
    super(x);
  }//end constructor
  
  int add(AClass ref){//overridden
    return x + ref.x;
  }//end add()
    
}//end class SubSubClass

Answer and Explanation

4.  Which of the following are valid syntax for the main() method required by a Java application?

Answer and Explanation

5.  True or false?  Friendly or package access applies to all classes in the current working directory that are not declared to be public, private, or protected.

Answer and Explanation

6.  What output is produced by the following program?

import java.awt.*;
class Q37{
  final Label var = new Label("AAA");
  public static void main(
                        String args[]){
    Q37 obj = new Q37();
    obj.var = new Label("BBB");
    System.out.println(
                    obj.var.getText());
  }//end main()
}//end class definition

Answer and Explanation

7.  What output is produced by the following program?

import java.awt.*;
class Q38{
  final Label var = new Label("AAA");
  public static void main(
                       String args[]){
    Q38 obj = new Q38();
    obj.var.setText("BBB");
    System.out.println(
                   obj.var.getText());
  }//end main()
}//end class definition

Answer and Explanation

8.  What output is produced by the following program?

class Q39{
  public static void main(
                       String args[]){
    new AClass().aMethod();
  }//end main()
    
  final void aMethod(){
    System.out.println("AAA");
  }//end aMethod()
}//end class definition

class AClass extends Q39{
  void aMethod(){//overridden method
    System.out.println("BBB");
  }//end aMethod()

}//end class AClass

Answer and Explanation

9.  What output is produced by the following program?

final class Q40{
  public static void main(
                       String args[]){
    new AClass().aMethod();
  }//end main()
    
  void aMethod(){
    System.out.println("AAA");
  }//end aMethod()
}//end class definition


class AClass extends Q40{
  void aMethod(){//overridden method
    System.out.println("BBB");
  }//end aMethod()

}//end class AClass

Answer and Explanation

10.  What output is produced by the following program?

abstract class Q41{
  public static void main(
                        String args[]){
    new Q41().aMethod();
  }//end main()
    
  void aMethod(){
    System.out.println("AAA");
  }// end aMethod()
}//end class definition

class AClass extends Q41{
  void aMethod(){//overridden method
    System.out.println("BBB");
  }//end aMethod()

}//end class AClass

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 A, compiler error.

Back to Question 10

Explanation 10

You cannot instantiate an object of an abstract class.

Answer 9

The answer is A, compiler error.

Back to Question 9

Explanation 9

You cannot extend a final class.

Answer 8

The answer is A, compiler error.

Back to Question 8

Explanation 8

You cannot override a final method.

Answer 7

The answer is C, BBB.

Back to Question 7

Explanation 7

While you cannot modify the value stored in a final reference variable, you can access and possibly modify the values stored in the object to which the reference refers.

Answer 6

The answer is A, a compiler error.

Back to Question 6

Explanation 6

You cannot assign a value to a final variable after it is declared and initialized.

Answer 5

The answer is true.

Back to Question 5

Explanation 5

This makes it easy to develop and test applications quickly without the requirement to deal with complex accessibility requirements.
 

Answer 4

All but E are valid syntax.

Back to Question 4

Explanation 4

Java doesn't care about the order of modifiers.  Also, two different forms of syntax are allowed for arrays.  Although selection E will compile, it will not execute because the argument to main() must be a reference to an array of type String.

Answer 3

The answer is A, compiler error.

Back to Question 3

Explanation 3

A method cannot be overridden to make it more restrictive from an accessibility viewpoint.  Accessibility from most to least restrictive is:  private, friendly, protected, public.  The above program attempts to override the protected method named add with a friendly version, which is more restrictive.

Answer 2

The answer is C, 15.

Back to Question 2

Explanation 2

protected access is not limited to only one generation of inheritance.
 

Answer 1

False.

Back to Question 1

Explanation 1

There is another access control modifier, which is the total lack of a modifier.  Some authors refer to this as friendly access.  Other authors refer to it as package access.



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-