Summary: Part 1 of a self-assessment test designed to help you determine how much you know about the fundamentals of object-oriented programming using Java.
This module is Part 1 of a self-assessment test designed to help you determine how much you know about the fundamentals of object-oriented programming using Java.
The test consists of a series of questions with answers and explanations of the answers. The answers to the questions, and the explanations of those answers are located ( in reverse order) at the end of module.
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.
Source File Names: True or false? All Java source files must end with the extension .class
Separate source File Requirements: True or false? A source file may contain an unlimited number of non-public class definitions.
Class file names: True or false? If a source file includes a public class, the class name must match the file name (without the extension) .
True or false: Each source file that you compile will produce one file with an extension .class
Required programming elements: True or false. If any of the following elements are included in the source file, they must appear in the following order.
Import directives: True or false? The compiler does not require you to use import directives.
Import directives: What output is produced by the program shown in Listing 1 ?
//File Q001_07.java
import java.awt.*;
public class Q001_07{
public static void main
(String args[]){
Button aButton =
new Button("Button");
Label aLabel =
new Label("Label");
System.out.println("OK");
}//end mail
}//end class Q001_07
goto and const: True or false? As in C and C++, goto and const are keywords that are actively used in Java.
Which of the following characters may be used as the first character in a Java identifier (identify all that may be used) ?
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.
Access Control: What output is produced by compiling and running the program shown in Listing 2 ? Note that the instance variable named x is declared private .
//File Q001_10.java
class Q001_10{
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
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
The answer is C. The output produced by compiling and running the program is 15.
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.
The following characters may be used as the first character of a Java identifier:
B (the number 4) cannot be used as the first character of a Java identifier.
In Java, a legal identifier is a sequence of Unicode letters and digits of unlimited length.
The first character must be a letter.
All subsequent characters must be letters or numerals from any alphabet that Unicode supports.
The underscore character (_) and the dollar sign ($) are considered to be letters and may be used as any character including the first one.
False.
goto and const are reserved words, but are not actively used in Java. Although they have no meaning in Java, you cannot use them as identifiers. Apparently Sun did this to avoid confusion, but I have never seen an official explanation (but I admit that I haven't searched very hard for an explanation) .
The answer is C. This program will produce the output OK.
Each import directive can specify the package containing a single class file.
Alternatively, an import directive can use the wildcard character (*) to "import" all of the class files in a specified package. This program uses the following wildcard version of the import directive to import all of the class files in the package named awt .
import java.awt.*;
This includes both Button and Label . Therefore, it is not necessary to provide fully-qualified path and package information when referring to either of these classes in the program code.
A word of caution is in order, however. The purpose of packages is to resolve name clashes among class files having the same names. If you find yourself referring to two or more different class files that have the same name, they must be stored in different packages, and you cannot use import directives for those class files. In that case, you must provide a fully-qualified path and package name each time you refer to one of those classes.
It is possible that you could use the wildcard character to import two different packages that contain one or more duplicate class file names. This could lead to problems.
Therefore, the safest approach is to avoid the use of the wildcard character altogether and to use a separate import directive for each class that you need to refer to in the program. (This will also cause your program to be more self-documenting.) I have worked as a Java consultant for a couple of companies whose internal programming standards prohibit the use of the wildcard character in import directives.
True
The import directive is provided strictly as a convenience to the programmer. The compiler does not require you to use import directives.
However, you can often save yourself a lot of extra work by using import directives. If you don't use import directives, every reference to a class must provide a fully qualified path and package name for the package that contains the class file.
For example, the program shown in Listing 3 contains references to two classes: Button and Label . An import directive is used to inform the compiler where it can find the class file for the Button class. No such import directive is provided for the Label class.
As a result, the code required to refer to the Label class must specify the package that contains the class file for the Label class. Otherwise, the program cannot be compiled.
As you can see, considerably more typing is required to refer to the Label class than is required to refer to the Button class.
//File Q001_06.java
import java.awt.Button;
public class Q001_6{
public static void main
(String args[]){
Button aButton =
new Button("Button");
java.awt.Label aLabel =
new java.awt.Label("Label");
System.out.println(
aButton.getLabel());
System.out.println(
aLabel.getText());
}//end main
}//end class
True.
False.
One class file is produced for each class definition in your program regardless of the number of source files and regardless of whether they are top-level classes or inner classes. Even each anonymous inner class produces an output class file, and the name given to the class file for the anonymous inner class is created by the system.
True. However, see the qualification below.
According to The Complete Java 2 Certification Study Guide , by Roberts, Heller, and Ernest, "This is not actually a language requirement, but is an implementation requirement of many compilers, including the reference compilers from Sun. It is therefore unwise to ignore this convention."
True. A source file may contain an unlimited number of non-public class definitions. In fact, the source file may contain one public class definition and an unlimited number of non-public class definitions. However, the rules change if two or more of the classes are declared to be public.
While it is true that a source file may contain an unlimited number of non-public class definitions, such is not the case if the classes are declared public . Each public class and interface must be contained in its own source file.
The fact that each public class requires its own source file is easy enough to demonstrate. The Java application in Listing 4 fails to compile with a compiler error to the effect that "Public class Dummy must be defined in a file called "Dummy.java"."
If the public modifier is removed from the definition of the class named Dummy , leaving only one public class in the source file, the application will compile and execute successfully.
public class Q001_2{
public static void main
(String args[]){
System.out.println("OK");
}//end main
}//end class
public class Dummy{
//empty class definition
}//end class definition
class AnotherDummy{
//empty class definition
}//end class definition
False. Java source files must not end with the extension .class . Rather, all Java source files must end with the extension .java (except under some advanced circumstances that are beyond the scope of this module).
When you create Java source files, you need to use a text editor that will produce clean character codes (sometimes referred to as ASCII codes) .
The output from the editor must not contain any control characters indicating bold , italics , underline, etc.
Just about any text editor can be used for this purpose. Various text editors are available on the web that can make the creation of source code easier. These editors provide features such as causing the various parts of the program to appear in different colors during the editing process , or automatically indenting each line of source code according to generally accepted indentation standards.
Some of the available editors will allow you to compile and execute the Java program from within the editor program, which can be a real time saver during development. Editors of this type are often referred to as Integrated Development Environments (IDE).
Back to the point of the discussion, as mentioned above, the file name of all source code files must end with the extension .java If your editor doesn't put it there, you will need to rename the file manually to create the correct extension.
When you successfully compile the program, one or more files will be produced by the compiler having the extension .class
-end-