# Connexions

You are here: Home » Content » Ji0010: OOP Self-Assessment Test, Part 1 - Old version

### Recently Viewed

This feature requires Javascript to be enabled.

# Ji0010: OOP Self-Assessment Test, Part 1 - Old version

Module by: R.G. (Dick) Baldwin. E-mail the author

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.

## Preface

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.

## Questions

### Question 1 .

Source File Names: True or false? All Java source files must end with the extension .class

### Question 2 .

Separate source File Requirements: True or false? A source file may contain an unlimited number of non-public class definitions.

### Question 3 .

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

### Question 4 .

True or false: Each source file that you compile will produce one file with an extension .class

### Question 5 .

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.

• A. package declaration
• B. import declarations
• C. class definitions

### Question 6 .

Import directives: True or false? The compiler does not require you to use import directives.

### Question 7 .

Import directives: What output is produced by the program shown in Listing 1 ?

• A. A compiler error
• B. A runtime error
• C. OK

#### Listing 1: Code for Question 7.

//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


### Question 8 .

goto and const: True or false? As in C and C++, goto and const are keywords that are actively used in Java.

### Question 9 .

Which of the following characters may be used as the first character in a Java identifier (identify all that may be used) ?

• A. x (the letter x)
• B. 4 (the number 4)
• C. $(the dollar sign character) • D. _ (the underscore character) ### Question 10 . 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 . • A. A compiler error • B. A runtime error • C. 15 #### Listing 2: Code for Question 10. //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  ## Listings ## Miscellaneous This section contains a variety of miscellaneous information. ### Note: Housekeeping material • Module name: Ji0010: OOP Self-Assessment Test, Part 1 • File: Ji0010.htm • Originally published: August 9, 2000 • Published at cnx.org: November 15, 2012 ### Note: Disclaimers: 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. ## Answers ### Answer 10 . The answer is C. The output produced by compiling and running the program is 15. #### Explanation 10 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 9 . The following characters may be used as the first character of a Java identifier: • A. x (the letter x) • C.$ (the dollar sign character)
• D. _ (the underscore character)

B (the number 4) cannot be used as the first character of a Java identifier.

#### Explanation 9

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.

#### Explanation 8

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.

#### Explanation 7

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

#### Explanation 6

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.

##### Listing 3: Listing for Answer 6.
//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.

#### Explanation 4

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.

#### Explanation 3

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.

#### Explanation 2

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.

##### Listing 4: Listing for Answer 2.
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).

#### Explanation 1

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-

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks