Summary: This module contains review questions and answers keyed to the module titled Jb0200: Java OOP: Variables.
This module contains review questions and answers keyed to the module titled Jb0200: Java OOP: Variables .
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 again.
Write a Java application that reads characters from the keyboard until encountering the # character. Echo each character to the screen as it is read. Terminate the program when the user enters the # character.
What is the common name for the Java program element that is used to contain data that changes during the execution of the program?
What must you do to make a variable available for use in a Java program?
True or false? In Java, you are required to initialize the value of all variables when they are declared.
Show the proper syntax for declaring two variables and initializing one of them using a single Java statement.
True or false? The Java compiler will accept statements with type mismatches provided that a suitable type conversion can be implemented by the compiler at compile time.
Show the proper syntax for the declaration of a variable of type String[] in the argument list of the main method of a Java program and explain its purpose.
Describe the purpose of the type definition in Java.
True or false? Variables of type int can contain either signed or unsigned values.
What is the important characteristic of type definitions in Java that strongly supports the concept of platform independence of compiled Java programs?
What are the two major categories of types in Java?
What is the maximum number of values that can be stored in a variable of a primitive type in Java?
List the primitive types in Java.
True or false? Java stores variables of type char according to the 8-bit extended ASCII table.
True or false? In Java, the name of a primitive variable evaluates to the value stored in the variable.
True or false? Variables of primitive data types in Java are true objects.
Why do we care that variables of primitive types are not true objects?
What is the name of the mechanism commonly used to convert variables of primitive types to true objects?
How can you tell the difference between a primitive type and a wrapper for the primitive type when the two are spelled the same?
Show the proper syntax for declaring a variable of type double and initializing its value to 5.5.
Show the proper syntax for declaring a variable of type Double and initializing its value to 5.5.
Show the proper syntax for extracting the value from a variable of type Double .
True or false? In Java, the name of a reference variable evaluates to the address of the location in memory where the variable is stored.
What is a legal identifier in Java?
What are the rules for variable names in Java?
What is meant by the scope of a Java variable?
What are the four possible scope categories for a Java variable?
What is a member variable?
Where are local variables declared in Java?
What is the scope of a local variable in Java?
What defines a block of code in Java?
What is the scope of a variable that is declared within a block of code that is defined within a method and which is a subset of the statements that make up the method?
What is the scope of a variable declared within the initialization clause of a for statement in Java? Provide an example code fragment.
What are method parameters and what are they used for?
What is the scope of a method parameter ?
What are exception handler parameters ?
Write a Java application that illustrates member variables (class and instance) , local variables, and method parameters.
True or false? Member variables in a Java class can be initialized when the class is defined.
How are method parameters initialized in Java?
What is the meaning of the following two images?
This image was inserted here simply to insert some space between the questions and the answers to keep them from being visible on the screen at the same time.
The image is also an example of the kinds of things that we do in my course titled ITSE 2321, Object-Oriented Programming.

This image was also inserted for the purpose of inserting space between the questions and the answers.
Method parameters are initialized by the values passed to the method.
True.
See the application named member1 in this module for an example of such an application.
Exception handler parameters are arguments to exception handlers, which will be discussed in a future module.
The scope of a method parameter is the entire method for which it is a parameter.
Method parameters are the formal arguments of a method. Method parameters are used to pass values into and out of methods.
Java treats the scope of a variable declared within the initialization clause of a for statement to be limited to the total extent of the for statement. A sample code fragment follows where cnt is the variable being discussed:
for(int cnt = 0; cnt < max; cnt++){
//do something
}//end of In Java, the scope can be reduced by placing it within a block of code within the method. The scope extends from the point at which it is declared to the end of the block of code in which it is declared.
A block of code is defined by enclosing it within curly brackets as shown below
{ ... } .
The scope of a local variable extends from the point at which it is declared to the end of the block of code in which it is declared.
In Java, local variables are declared within the body of a method or constructor, or within a block of code contained within the body of a method or constructor.
A member variable is a member of a class ( class variable) or a member of an object instantiated from that class ( instance variable). It must be declared within a class, but not within the body of a method or constructor of the class.
The scope of a variable places it in one of the following four categories:
The scope of a Java variable is the block of code within which the variable is accessible.
The rules for Java variable names are as follows:
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. In addition, the underscore character ( _ ) and the dollar sign ( $ ) are considered letters and may be used as any character including the first one.
False. The name of a reference variable evaluates to either null, or to information that can be used to access an object whose reference has been stored in the variable.
Later versions of Java support either syntax shown in Listing 1 .
| Listing 1: Listing for Answer 22. |
|---|
|
The proper syntax for early versions of Java is shown below. Note the upper-case D . Also note the instantiation of a new object of type Double .
Double myWrappedData = new Double(5.5);Later versions of Java support the following syntax with the new object of type Double being instantiated automatically:
Double myWrappedData = 5.5;The proper syntax is shown below. Note the lower-case d .
double myPrimitiveData = 5.5;The name of the primitive type begins with a lower-case letter and the name of the wrapper type begins with an upper-case letter such as double and Double . Note that in some cases, however, that they are not spelled the same. For example, the Integer class is the wrapper for type int .
Wrapper classes
This has some ramifications as to how variables can be used (passing to methods, returning from methods, etc.) . For example, all variables of primitive types are passed by value to methods meaning that the code in the method only has access to a copy of the variable and does not have the ability to modify the variable.
False. Primitive data types in Java (int, double, etc.) are not true objects.
True.
False. The char type in Java is a 16-bit Unicode character.
Primitive types contain a single value.
Java supports both primitive types and reference (or object) types.
In Java, a variable of a specified type is represented exactly the same way regardless of the platform on which the application or applet is being executed.
False. In Java, all variables of type int contain signed values.
All variables in Java must have a defined type . The definition of the type determines the set of values that can be stored in the variable and the operations that can be performed on the variable.
The syntax is shown in boldface below:
public static void main( String[] args )
In this case, the type of variable declared is an array of type String named args (type String[]) . The purpose of the String array variable in the argument list is to make it possible to capture arguments entered on the command line.
False. Fortunately, Java provides very strict type checking and generally refuses to compile statements with type mismatches.
int firstVariable, secondVariable = 10; False: In Java, it is possible to initialize the value of a variable when it is declared, but initialization is not required. (Note however that in some situations, the usage of the variable may require that it be purposely initialized.) .
To use a variable, you must notify the compiler of the name and the type of the variable (declare the variable).
variable
| Listing 2: Listing for Answer 1. |
|---|
|
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.
-end-