# Test Yourself: Fundamentals, Part 2

Friday Oct 13th 2000 by Richard G. Baldwin
Share:

The purpose of this series of tutorial lessons is to help you learn Java by approaching it from a question and answer viewpoint. Here, we continue with the basics.

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

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.  Primitive Data Types:  The following table purports to list the primitive data types and their representative sizes in bits.  Identify which lines, if any, contain errors.

 Type Size in Bits A boolean 1 B char 8 C byte 8 D short 16 E int 32 F long 64 G float 32 H double 64

2. boolean states:  Which of the following are the possible states of a boolean variable?

• A.  true
• B.  uncertain
• C.  false

3. Range of integer types:  The following table purports to show the numeric range of the integer types in terms of a power of two.  Identify which lines, if any, contain errors.  (The syntax 2eX means 2 raised to the X power.)

 Type Minimum Maximum A byte -2e7 (2e7)-1 B short -2e15 (2e15)-1 C int -2e31 (2e31)-1 E long -2e63 (2e63)-1

4.  Signed versus unsigned  True or false?  All of the integral types, including char are signed.

5.  Character representation:  True or false?  Characters in Java are represented by the 8-bit Extended ASCII character set.

6.  Unicode to ASCII relationship:  What is the relationship between the 16-bit Unicode character set and the 7-bit ASCII character set.

7.  Invalid numeric values:  As a result of certain arithmetic operations (such as division by zero), certain primitives can take on bit patterns that do not represent valid numeric values.  These possibilities are represented by symbolic constants in the various classes.  Which of the following are not valid symbolic constants?

• A.  Integer.NaN
• B.  Integer.NEGATIVE_INFINITY
• C.  Integer.POSITIVE_INFINITY
• D.  Float.NaN
• E.  Float.NEGATIVE_INFINITY
• F.  Float.POSITIVE_INFINITY
• G.  Double.NaN
• H.  Double.NEGATIVE_INFINITY
• I.  Double.POSITIVE_INFINITY

8.  Literal values:  Which, if any, of the following are not valid literal values for type char?

• A.  '\u1632'
• B.  '\t'
• C.  "a"
• D.  'end'

9.  Escape characters:  Which of the following are valid escape characters in Java?

• A.  '\n'
• B.  '\r'
• C.  '\t'
• D.  '\b'
• E.  '\f'
• F.  '\''
• G.  '\q'
• H.  '\s'
• I.  '\"'
• J.  '\\'

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.

10.  Bit manipulations:  What output is produced by the following program?

• A.  1
• B.  -1
• C.  2
• D.  -2
 class Q002_10{    public static void main(String args[]){      int x = 1;      int y = ~x + 1;      System.out.println(y);    }//end main()  }//end class definition

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.

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.

### Explanation 10

If you invert all of the bits in the binary twos complement representation of the decimal value 1, the decimal value of the resulting bit pattern is -2.  If you then add decimal 1 to that, the result is -1

In fact, this is how you convert a positive twos complement binary value to a negative twos complement binary value (or vice versa).  First you invert all of the bits.  Then you add 1.

All except G and H are valid escape characters.

### Explanation 9

Here is the same chart with the different escape characters identified as to their usage:
• A.  '\n' Newline
• B.  '\r' Return
• C.  '\t' Tab
• D.  '\b' Backspace
• E.  '\f' Formfeed
• F.  '\'' Apostrophe or single quote
• G.  '\q' Not valid
• H.  '\s' Not valid
• I.  '\"' Double quote
• J.  '\\' Backslash

C and D are not valid literal values for type char.

### Explanation 8

A.  '\u1632' is a valid literal value of type char.  This is how you can express Unicode characters in hexadecimal format.  For example, this is how you can express Unicode characters that do not appear on the keyboard that you use to write your source code.

B.  '\t' is also a valid literal value of type char.  This is the so-called escape character that represents a Tab.  There are about eight such escape characters supported by Java that represent various things like Tab, Newline, Return, Backspace, etc.

C.  "a" is not a valid literal of type char.  Rather, this is a String literal.

In Java, unlike some other languages, quotation marks (") and apostrophes (') have entirely different meanings.

Zero or more characters surrounded by quotation marks are interpreted by the compiler to constitute a String literal.

Any single character surrounded by apostrophes is interpreted by the compiler to be a char literal.  When confronted with such a literal, the compiler produces the numeric value that represents that character.  For example, the following code fragment will cause the value 65 to be displayed because the numeric value that represents the upper-case A in the Unicode character set is 65.

int data = 'A';
System.out.println(data);

D.  'end' has no meaning in Java.  Unlike some other languages, such as Python, you cannot interchange quotation marks and apostrophes when creating strings.  For example, the following statement will produce the compiler error "Invalid character constant"

System.out.println('end');

A, B, and C are not valid symbolic constants.

### Explanation 7

As a result of arithmetic operations (such as division by zero), both float and double can take on bit patterns that do not represent valid numeric values.  These possibilities are represented by symbolic constants in the Float and Double classes.  However, no such possibility exists for the Integer class.

An object of the Integer class is intended to encapsulate an int value.  There are no operations that you can perform with an int that results in a bit pattern that does not represent a valid numeric value.

While it is possible to end up with erroneous int values as a result of some arithmetic operations, they are still valid numeric values and so there is no need for the kind of symbolic constants described for float and double in this question.

The Unicode character set matches the 7-bit ASCII set for the least significant seven bits.  In other words, if the nine most significant bits of a Unicode character are all zero, then the encoding is the same as 7-bit ASCII.

### Explanation 6

This is very convenient for those of us who have been writing programs in other languages for some time and who are making the transition to Unicode.  For example, the character whose decimal value is 65 is an upper case A in both ASCII and Unicode.

As it turns out, for those of us who program using the U.S./English alphabet, the change from Extended ASCII to Unicode isn't very significant.  We can pretty much continue doing what we have been doing for years.

However, those who program using alphabets from other spoken languages now have the ability to reflect those languages in the character representations contained in their programs.

False

### Explanation 5

For many years, programming languages have used the 7-bit ASCII character set, and more recently, the 8-bit Extended ASCII character set.  (To learn more about the ASCII character set and the Extended ASCII character set, visit the following URL.)

However, Java does not use the ASCII character set.  Java characters are expressed as 16-bit Unicode characters. (To learn more about Unicode characters, visit the following URL.)

False

### Explanation 4

All of the integral types other than char are signed.  However, although the char type is integral, it is unsigned.  Its range is from 0 to 65,535.

None of the lines are in error (unless I made a typing error).

### Explanation 3

Each of the four primitive types identified in this question are signed integers.  Signed integers in Java are represented in two's complement binary notation.

In summary, this means that a variable containing n bits has the following numeric range (where 2eX means 2 raised to the X power):

Minimum = -2e(n-1)
Maximum = (2e(n-1))-1

Consider the following simple example.  Assume that n is equal to 3.  Then:
Minimum = -2e(3-1) = -4
Maximum = (2e(3-1))-1 = 4-1 = 3

A three-bit variable can represent eight different combinations of bits.  In two's complement notation, it can represent the range of integer values from -4 to +3.  Here is how the bits would be organized to represent those values in two's complement binary notation.

 Bits Value 000 0 001 1 010 2 011 3 100 -4 101 -3 110 -2 111 -1

The correct answer is A and C are the possible states of a boolean variable.

### Explanation 2

A boolean variable can have only two states, true and false.

The boolean type is often used in conditional expressions to make a choice between two possibilities.  For example, the following construct would not work properly if the boolean were allowed to have the third value uncertain.  In that case, you might find yourself stranded on the roadside with an empty gas tank.

if(gasInTank < 1)
else

Line B, char is in error.  This line should show that the size of a char type is 16 bits.

### Explanation 1

Whereas many earlier programming languages, such as C and C++, have used 8-bit characters, the char or character type in Java is based on the 16-bit Unicode character set.

You may wonder why this is the case.  One of the primary reasons is the need to internationalize computer technology.  An 8-bit character set can represent only 255 different characters.  During that part of history when most computer technology was based on the U.S./English alphabet, this was sufficient.  However, computer technology is no longer so confined, and there is a need to be able to represent more than 255 characters.

The 16-bit Unicode set makes it possible to represent more than 65,000 different characters.  Hopefully this will be sufficient to represent the alphabets of all countries in the international computer technology arena.

You may also wonder about the correctness of line A which indicates that the boolean type is 1 bit in size.  I personally don't know exactly how a boolean variable is represented in memory, but I would have guessed that it is stored in an 8-bit byte.  I also don't know how to write any code that will expose the manner in which a boolean variable is stored in memory.

The size of one bit for a boolean is based on information from the book entitled The Complete Java 2 Certification Study Guide by Roberts, Heller, and Ernest.  A boolean certainly could be stored in a single bit since only two different values must be represented.  This assumes, of course, that the processor has the ability to address memory down to the single-bit level.

In the final analysis, it probably doesn't matter.  Unlike C and C++, you cannot do arithmetic with a boolean, and I can't think of any other operation that you can perform on a boolean where the manner in which it is stored in memory makes any difference (except possibly for speed).

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.