Summary: Part of a self-assessment test designed to help you determine how much you know about Java primitive types.
Note: You are viewing an old version of this document. The latest version is available here.
This module is part of a self-assessment test designed to help you determine how much you know about object-oriented programming using Java.
Questions and answers
The test consists of a series of questions with answers and explanations of the answers.
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.
Programming challenge questions
The module also contains a section titled Programming challenge questions . This section provides specifications for one or more programs that you should be able to write once you understand the answers to all of the questions. (Note that it is not always possible to confine the programming knowledge requirement to this and earlier modules. Therefore, you may occasionally need to refer ahead to future modules in order to write the programs.)
Unlike the other questions, solutions are not provided for the Programming challenge questions . However, in most cases, the specifications will describe the output that your program should produce.
Listings
I recommend that you open another copy of this document in a separate browser window and use the links to under Listings to easily find and view the listings while you are reading about them.
What output is produced by the program in Listing 1 ?
public class Ap001{
public static void main(
String args[]){
new Worker().hello();
}//end main()
}//end class definition
class Worker{
public void hello(){
System.out.println("Hello World");
}//end hello()
}//end class definitionWhat is the largest (algebraic) value of type int?
What is the smallest (algebraic) value of type int?
What two values are displayed by the program in Listing 2 ?
public class Ap003{
public static void main(
String args[]){
new Worker().printDouble();
}//end main()
}//end class definition
class Worker{
public void printDouble(){
System.out.println(
Double.MAX_VALUE);
System.out.println(
Double.MIN_VALUE);
}//end printDouble()
}//end class definition
What output is produced by the program in Listing 3 ?
public class Ap004{
public static void main(
String args[]){
new Worker().printBoolean();
}//end main()
}//end class definition
class Worker{
private boolean myVar;
public void printBoolean(){
System.out.println(myVar);
}//end printBoolean()
}//end class definition
What output is produced by the program shown in Listing 4 ?
public class Ap005{
public static void main(
String args[]){
new Worker().printBoolean();
}//end main()
}//end class definition
class Worker{
public void printBoolean(){
boolean myVar;
System.out.println(myVar);
}//end printBoolean()
}//end class definition
What output is produced by the program shown in Listing 5 ?
public class Ap006{
public static void main(
String args[]){
new Worker().printBoolean();
}//end main()
}//end class definition
class Worker{
public void printBoolean(){
boolean myVar = true;
myVar = false;
System.out.println(myVar);
}//end printBoolean()
}//end class definition
The plus (+) character can be used to perform numeric addition in Java. What output is produced by the program shown in Listing 6 ?
public class Ap007{
public static void main(
String args[]){
new Worker().printBoolean();
}//end main()
}//end class definition
class Worker{
public void printBoolean(){
boolean myVar = true;
System.out.println(1 + myVar);
}//end printBoolean()
}//end class definition
The plus (+) character can be used to perform numeric addition in Java. What output is produced by the program shown in Listing 7 ?
public class Ap008{
public static void main(
String args[]){
new Worker().printMixed();
}//end main()
}//end class definition
class Worker{
public void printMixed(){
double x = 3;
int y = 3;
System.out.println(x+y);
}//end printMixed()
}//end class definition
The slash (/) character can be used to perform numeric division in Java. What output is produced by tthe program shown in Listing 8 ?
public class Ap009{
public static void main(
String args[]){
new Worker().printMixed();
}//end main()
}//end class definition
class Worker{
public void printMixed(){
System.out.println(1.0/3);
}//end printMixed()
}//end class definition
Write the program described in Listing 9 .
/*File Ap0010a1.java Copyright 2012, R.G.Baldwin
Instructions to student:
This program refuses to compile without errors.
Make the necessary corrections to cause the program to
compile and run successfully to produce the output shown
below:
ITSE
2321
**********************************************************/
public class Ap0010a1{
public static void main(String args[]){
System.out.println("ITSE");
new Worker().doIt();
}//end main()
}//end class definition
//=======================================================//
Class Worker{
public void doIt(){
System.out.println("2321");
}//end doIt()
}//end class definition
//=======================================================//
Write the program described in Listing 10 .
/*File Ap0010b1.java Copyright 2012, R.G.Baldwin
Instructions to student:
Beginning with the code fragment shown below, write a
method named doIt that:
1. Receives and displays an incoming parameter of type int.
The result should be similar to the following but the
values should be different each time the program is
run.
484495695
484495695
**********************************************************/
//Student is not expected to understand import directives
// at this point.
import java.util.Random;
import java.util.Date;
public class Ap0010b1{
public static void main(String args[]){
//Create a random number for testing. Student is not
// expected to understand how this works at this point.
Random random = new Random(new Date().getTime());
int intVar = random.nextInt();
//Student should understand the following
int var = intVar;
System.out.println(var);
new Worker().doIt(var);
}//end main()
}//end class definition
//=======================================================//
class Worker{
//-----------------------------------------------------//
//Student: insert the method named doIt between these
// lines.
//-----------------------------------------------------//
}//end class definition
//=======================================================//
Write the program described in Listing 11 .
/*File Ap0010c1.java Copyright 2012, R.G.Baldwin
Instructions to student:
Beginning with the code fragment shown below, write a
method named doIt that returns the largest value of type
int as type float.
The result should be 2.14748365E9
**********************************************************/
public class Ap0010c1{
public static void main(String args[]){
float val = new Worker().doIt();
System.out.println(val);
}//end main()
}//end class definition
//=======================================================//
class Worker{
//-----------------------------------------------------//
//Insert the method named doIt between these lines.
//-----------------------------------------------------//
}//end class definition
//=======================================================//
Write the program described in Listing 12 .
/*File Ap0010d1.java Copyright 2012, R.G.Baldwin
Instructions to student:
Beginning with the code fragment shown below, write a
method named doIt that:
1. Receives an incoming parameter of type double.
2. Converts that value to type int.
3. Returns the int
The result should be similar to the following but the
values should be different each time the program is
run.
6.672032181818181E8
667203218
**********************************************************/
//Student is not expected to understand import directives
// at this point.
import java.util.Random;
import java.util.Date;
public class Ap0010d1{
public static void main(String args[]){
//Create a random number for testing. Student is not
// expected to understand how this works at this point.
Random random = new Random(new Date().getTime());
int intVar = random.nextInt();
//Student should understand the following
double var = intVar/1.1;
System.out.println(var);
System.out.println(new Worker().doIt(var));
}//end main()
}//end class definition
//=======================================================//
class Worker{
//-----------------------------------------------------//
//Student: insert the method named doIt between these
// lines.
//-----------------------------------------------------//
}//end class definition
//=======================================================//
Write the program described in Listing 13.
/*File Ap0010e1.java Copyright 2012, R.G.Baldwin
Instructions to student:
This program refuses to compile without errors.
Make the necessary corrections to cause the program to
compile and run successfully to produce an output similar
to that shown below. Note that the values should be
different each time the program is
run.
-1.30240579E8
-1.30240579E8
**********************************************************/
//Student is not expected to understand import directives
// at this point.
import java.util.Random;
import java.util.Date;
public class Ap0010e1{
public static void main(String args[]){
//Create a random number for testing. Student is not
// expected to understand how this works at this point.
Random random = new Random(new Date().getTime());
double doubleVar = random.nextInt()/1.0;
//Student should understand the following
double var = doubleVar;
System.out.println(doubleVar);
new Worker().doIt(doubleVar);
}//end main()
}//end class definition
//=======================================================//
class Worker{
public void doIt(double val){
int var = val;
System.out.println(var);
}//end doIt()
}//end class definition
//=======================================================//
Write the program described in Listing 14 .
/*File Ap0010f1.java Copyright 2012, R.G.Baldwin
Instructions to student:
Beginning with the code shown below, modify the
code in the method named doIt so that the program
displays
3.3333333333333335 instead of 3
Then modify the method again so that the program displays
3.3333333 instead of 3
**********************************************************/
public class Ap0010f1{
public static void main(String args[]){
new Worker().doIt();
}//end main()
}//end class definition
//=======================================================//
class Worker{
public void doIt(){
System.out.println(10/3);
}//end doIt()
}//end class definition
//=======================================================//
Write the program described in Listing 15 .
/*File Ap0010g1.java Copyright 2012, R.G.Baldwin
Instructions to student:
Beginning with the code shown below, modify the
code in the method named doIt so that the program
displays
2048 instead of 2730
Did you notice anything particularly interesting about the
values involved?
**********************************************************/
public class Ap0010g1{
public static void main(String args[]){
new Worker().doIt(16384);
}//end main()
}//end class definition
//=======================================================//
class Worker{
public void doIt(int val){
System.out.println(val/6);
}//end doIt()
}//end class definition
//=======================================================//
Write the program described in Listing 16 .
/*File Ap0010h1.java Copyright 2012, R.G.Baldwin
Instructions to student:
This program refuses to compile without errors.
Make the necessary corrections to cause the program to
compile and run successfully to produce the output shown
below:
false
**********************************************************/
public class Ap0010h1{
public static void main(String args[]){
new Worker().doIt();
}//end main()
}//end class definition
//=======================================================//
class Worker{
public void doIt(){
boolean var;
System.out.println(var);
}//end doIt()
}//end class definition
//=======================================================//
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.
D. 0.3333333333333333
Divide floating type by integer type
This program divides the literal floating value of 1.0 by the literal integer value of 3 (no decimal point is specified in the integer literal value) .
Automatic conversion from narrow to wider type
To begin with, whenever division is performed between a floating type and an integer type, the integer type is automatically converted (sometimes called promoted) to a floating type and floating arithmetic is performed.
What is the actual floating type, float or double?
The real question here is, what is the type of the literal shown by 1.0 (with a decimal point separating the 1 and the 0) . Is it a double or a float ?
Type double is the default
By default, a literal floating value is treated as a double .
The result is type double
Consequently, this program divides a double type by an integer type, producing a result of type double . This is somewhat evident in the output, which shows about 17 digits plus a decimal point in the result. (Recall that the maximum value for a float shown earlier had only about eight digits plus the decimal point and the exponent.)
How can you create literals of type float?
What if you don't want your literal floating value to be treated as a double , but would prefer that it be treated as a float instead.
You can usually force this to be the case by adding a suffix of either F or f to the end of the literal value (as in 1.0F) . If you were to modify this program to cause it to divide 1.0F by 3, the output would be 0.33333334 with only nine digits in the result.
D. 6.0
Declare and initialize two local variables
This program declares and initializes two local variables, one of type double and the other of type int . Each variable is initialized with the integer value 3.
Automatic conversion to floating type double
However, before the value of 3 is stored in the double variable, it is automatically converted to type double .
Automatic conversion in mixed-type arithmetic
Numeric addition is performed on the two variables. Whenever addition is performed between a floating type and an integer type, the integer type is automatically converted to a floating type and floating arithmetic is performed.
A floating result
This produces a floating result. When this floating result is passed to the println method for display, a decimal point and a zero are displayed to indicate a floating type, even though in this case, the fractional part of the result is zero.
A. Compiler Error
Initialize boolean variable to true
This program declares and initializes a boolean variable with the value true . Then it attempts to add the literal value 1 to the value stored in the boolean variable named myVar .
Arithmetic with boolean values is not allowed
As mentioned earlier, unlike C++, boolean types in Java cannot participate in arithmetic expressions.
Therefore, this program will not compile. The compiler error produced by this program under JDK 1.3 reads partially as follows:
nullD. false
Format for variable initialization
This program declares a local boolean variable and initializes it to the value true . All variables, local or otherwise, can be initialized in this manner provided that the expression on the right of the equal sign evaluates to a value that is assignment compatible with the type of the variable. (I will have more to say about assignment compatibility in a future module) .
Value is changed before display
However, before calling the println method to display the initial value of the variable, the program uses the assignment operator (=) to assign the value false to the variable. Thus, when it is displayed, the value is false .
A. Compiler Error
A local boolean variable
In this program, the primitive variable named myVar is a local variable belonging to the method named printBoolean .
Local variables are not automatically initialized
Unlike instance variables, if you fail to initialize a local variable, the variable is not automatically initialized.
Cannot access value from uninitialized local variable
If you attempt to access and use the value from an uninitialized local variable before you assign a value to it, you will get a compiler error. The compiler error produced by this program under JDK 1.3 reads partially as follows:
nullMust initialize or assign value to all local variables
Thus, the programmer is responsible for either initializing all local variables, or assigning a value to them before attempting to access their value with code later in the program. The good news is that the system won't allow you to compute with garbage left over in memory occupied by variables, either local variables or member variables.
B. false
The boolean type
In this program, the primitive variable named myVar is an instance variable of the type boolean .
What is an instance variable?
An instance variable is a variable that is declared inside a class, outside of all methods and constructors of the class, and is not declared static. Every object instantiated from the class has one. That is why it is called an instance variable.
Cannot use uninitialized variables in Java
One of the great things about Java is that it is not possible to make the mistake of using variables that have not been initialized.
Can initialize when declared
All Java variables can be initialized when they are declared.
Member variables are automatically initialized
If the programmer doesn't initialize the variables declared inside the class but outside of a method (often referred to as member variables as opposed to local variables) , they are automatically initialized to a default value. The default value for a boolean variable is false.
Did you know the boolean default value?
I wouldn't be overly concerned if you had selected the answer A. true, because I wouldn't necessarily expect you to memorize the default initialization value.
Great cause for concern
However, I would be very concerned if you selected either C. 1 or D. 0.
Java has a true boolean type
Unlike C++, Java does not represent true and false by the numeric values of 1 and 0. (At least the numeric values that represent true and false are not readily accessible by the programmer.)
Thus, you cannot include boolean types in arithmetic expressions, as is the case in C++.
Floating type versus integer type
If you missed this one, shame on you!
I didn't expect you to memorize the maximum and minimum values represented by the floating type double, but I did expect you to be able to distinguish between the display of a floating value and the display of an integer value.
Both values are positive
Note that both of the values given above are positive values.
Unlike the integer types discussed earlier, the constants named MAX_VALUE and MIN_VALUE don't represent the ends of a signed number range for type double . Rather, they represent the largest and smallest (non-zero) values that can be expressed by the type.
An indication of granularity
MIN_VALUE is an indication of the degree of granularity of values expressed as type double . Any double value can be treated as either positive or negative.
Two floating types are available
Java provides two floating types: float and double . The double type provides the greater range, or to use another popular terminology, it is the wider of the two.
What is the value range for a float?
In case you are interested, using the same syntax as above, the value range for type float is from 1.4E-45 to 3.4028235E38
Double is often the default type
There is another thing that is significant about type double . In many cases where a value is automatically converted to a floating type, it is converted to type double rather than to type float. This will come up in future modules.
A. -2147483648
Could easily have guessed
As a practical matter, you had one chance in two of guessing the correct answer to this question, already having been given the value of the largest algebraic value for type int.
And the winner is ...
Did you answer B. -2147483647? -- WRONG
If so, you may be wondering why the most negative value isn't equal to the negative version of the most positive value?
A twos-complement characteristic
Without going into the details of why, it is a well-known characteristic of binary twos-complement notation that the value range extends one unit further in the negative direction than in the positive direction.
What about the other two values?
Do the values of -32768 and 32767 in the set of multiple-choice answers to this question represent anything in particular?
Yes, they represent the extreme ends of the value range for a 16-bit binary number in twos-complement notation.
Does Java have a 16-bit integer type?
Just in case you are interested, the short type in Java is represented in 16-bit binary twos-complement signed notation, so this is the value range for type short.
What about type byte?
Similarly, a value of type byte is represented in 8-bit binary twos-complement signed notation, with a value range extending from -128 to 127.
B. 2147483647
First question on types
This is the first question on Java types in this group of self-assessment modules.
32-bit signed twos-complement integers
In Java, values of type int are stored as 32-bit signed integers in twos-complement notation.
Can you calculate the values?
There are no unsigned integer types in Java, as there are in C++. If you are handy with binary notation, you could calculate the largest positive value that can be stored in 32 bits in twos-complement notation.
See documentation for the Integer class
Otherwise, you can visit the documentation for the Integer class, which provides a symbolic constant (public static final variable) named MAX_VALUE . The description of MAX_VALUE reads as follows:
"The largest value of type int. The constant value of this field is 2147483647."
C. Hello World
The answer to this first question is intended to be easy. The purpose of the first question is to introduce you to the syntax that will frequently be used for program code in this group of self-assessment modules.
The controlling class and the main method
In this example, the class named Ap001 is the controlling class . It contains a method named main , with a signature that matches the required signature for the main method. When the user executes this program, the Java virtual machine automatically calls the method named main in the controlling class.
Create an instance of Worker
The main method uses the new operator along with the default constructor for the class named Worker to create a new instance of the class named Worker (an object of the Worker class) . This is often referred to as instantiating an object.
A reference to an anonymous object
The combination of the new operator and the default constructor for the Worker class returns a reference to the new object. In this case, the object is instantiated as an anonymous object , meaning that the object's reference is not saved in a named reference variable. (Instantiation of a non-anonymous object will be illustrated later.)
Call hello method on Worker object
The main method contains a single executable statement.
As soon as the reference to the new object is returned, the single statement in the main method calls the hello method on that reference.
Output to standard output device
This causes the hello method belonging to the new object (of the class named Worker ) to execute. The code in the hello method calls the println method on the static variable of the System class named out .
Lots of OOP embodied in the hello method
I often tell my students that I can tell a lot about whether a student really understands object-oriented programming in Java by asking them to explain everything that they know about the following statement:
System.out.println("Hello World");
I would expect the answer to consume about ten to fifteen minutes if the student really understands Java OOP.
The one-minute version
When the virtual machine starts a Java application running, it automatically instantiates an I/O stream object linked to the standard output device (normally the screen) and stores a reference to that object in the static variable named out belonging to the class named System .
Call the println instance method on out
Calling the println method on that reference, and passing a literal string ("Hello World") to that method causes the contents of the literal String object to be displayed on the standard output device.
Display Hello World on the screen
In this case, this causes the words Hello World to be displayed on the standard output device. This is the answer to the original question.
Time for main method to terminate
When the hello method returns, the main method has nothing further to do, so it terminates. When the main method terminates in a Java application, the application terminates and returns control to the operating system. This causes the system prompt to reappear.
A less-cryptic form
A less cryptic form of this program is shown in Listing 17 .
public class Ap002{
public static void main(
String args[]){
Worker refVar = new Worker();
refVar.hello();
}//end main()
}//end class definition
class Worker{
public void hello(){
System.out.println("Hello World");
}//end hello()
}//end class definition
Decompose single statement into two statements
In this version, the single statement in the earlier version of the main method is replaced by two statements.
A non-anonymous object
In the class named Ap002 shown in Listing 2 , the object of the class named Worker is not instantiated anonymously. Rather, a new object of the Worker class is instantiated and the object's reference is stored in (assigned to) the named reference variable named refVar .
Call hello method on named reference
Then the hello method is called on that reference in a separate statement.
Produces the same result as before
The final result is exactly the same as before. The only difference is that a little more typing is required to create the source code for the second version.
Will often use anonymous objects
In order to minimize the amount of typing required, I will probably use the anonymous form of instantiation whenever appropriate in these modules.
Now that you understand the framework ...
Now that you understand the framework for the program code, I can present more specific questions. Also, the explanations will usually be shorter.
-end-