JavaEnterprise JavaTest Your Java Knowledge: Fundamentals, Part 3

Test Your Java Knowledge: Fundamentals, Part 3

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Lesson 3


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.

For your convenience, I also maintain a consolidated  Table of Contents on my personal web site that links to the individual lessons on the Gamelan site.

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.  Show six different ways to express the decimal value 28 as an integral literal.

Answer and Explanation

2.  True or false?  By default, an integral literal is a 64 bit value.

Answer and Explanation

3.  There are seven characters, at least one of which must be included to indicate a floating point literal.  What are they?

Answer and Explanation

4.  True or false?  The default for a floating-point literal without an F or a D is a 32-bit float.

Answer and Explanation

5.  Show how to represent a String literal.

Answer and Explanation

6.  A Java array is an ordered collection of three kinds of things.  What are they?

Answer and Explanation

7.  True or false?  All elements in a Java array must be of the same type.

Answer and Explanation

8.  There are two different formats that can be used to declare a reference variable capable of containing a reference to a single-dimensional array.  What are they?

Answer and Explanation

9.  True or false?  The size of a Java array can be specified using a variable or a literal.

Answer and Explanation

Bonus question.  The following question is more difficult than the previous nine questions, and is included here to challenge you if the previous nine questions have been too easy.

10.  Consider the following simple program, which initializes the instance variable named myIntVar to a value of 10 when the instance variable is initialized.

What value will be displayed by this program?
 

class Q56{
  int myIntVar = 10;//instance variable  

  Q56(){//constructor
    myIntVar = 20;
  }//end constructor
  
  public static void main(String args[]){
    System.out.println(new Q56().myIntVar);
  }//end main()
}//end class Q7

Answer and Explanation


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.

About the author

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.

baldwin.richard@iname.com


 

Answers and Explanations

Answer 10

The value 20 will be displayed.

Back to Question 10

Explanation 10

According to The Complete Java 2 Certification Study Guide, instance variables that are initialized when they are declared are initialized just before the class constructor is executed.  Therefore, the assignment statement in the constructor changes the value in the instance variable from its initialized value of 10 to a new value of 20.  Thus, you might say that the constructor has the last word in this case.

Answer 9

True.

Back to Question 9

Explanation 9

According to The Complete Java 2 Certification Study Guide by Roberts, Heller, and Ernest, “Since array size is not used until runtime, it is legal to specify size with a variable rather than a literal.”

This is a very powerful capability because it means that you do not need to know the size of an array when you write and compile a program.  Rather, you can defer that decision until runtime.

For example, this capability makes it possible to write code such as the following.  Although in this case, I initialized the value stored in the variable named size when I declared it, that value could come from anywhere (such as the length of a file) before being used to establish the size of the array.
 

class Q003_09{
  public static void main(
                          String args[]){
    int size = 10;
    int[] array = new int[size];
    size = 20;
    System.out.println(
              size + ” ” + array.length);
  }//end main()
}//end class definition

It is very important to understand, however, that once the value stored in a variable is used to establish the size of the array, changing the value stored in the variable does not cause the size of the array to change.

Answer 8

String[] variableName;
String variableName[];

Back to Question 8

Explanation 8

Java allows you to use either of the two formats given above when you declare a reference variable that will be used to refer to a single-dimensional array object.  The significant thing is that the square brackets, [], can either follow the declaration of the type of elements that will be stored in the array object, or can follow the name of the reference variable.  Of course, the name of the reference variable can differ from one declaration to the next.

Answer 7

The answer is true, but this answer depends somewhat on terminology, so it is best to understand the answer rather than to just memorize the answer.

Back to Question 7

Explanation 7

When you declare a reference that will later be used to refer to an array object, you must specify the type of the elements that will be stored in the array.  That seems to indicate that the answer to the question should true.

It is also true that all of the elements in an array that contains primitives must be of the same primitive type.  (However, under certain circumstances, primitives stored in an array will be automatically converted to the type of elements declared for the array if they are not already of that type.)

However, through the use of polymorphism, it is possible to store references to many different true types of objects in a Java array.  When you store an object reference in an array, that reference can be stored in a reference that has been declared to be any of the following types:

  • The class from which the object was instantiated.
  • Any superclass of the class from which the object was instantiated.
  • Any interface implemented by the class from which the object was instantiated.
  • Any interface implemented by any superclass of the class from which the object was instantiated.

Thus, a Java array can be a very generic container, and a simple true/false answer doesn’t do a very good job of describing the actual situation.

Answer 6

A Java array is an ordered collection of the following three kinds of things:

  • primitives
  • object references
  • references to other arrays

Back to Question 6

Explanation 6

Actually, it could be argued that there are only two kinds of things.  The second and third items in the above list are both object references because an array is a special kind of object in Java.  I have separated them here to highlight the fact that you should keep ordinary object references and references to Java arrays separated in your mind for a variety of reasons.  One reason is that the syntax to create them is significantly different.  Later, you will learn other reasons for keeping them separate in your mind, such as what kinds of conversions are allowable on ordinary object references and references to Java arrays.

Answer 5

“This is a String literal.”

Back to Question 5

Explanation 5

All that is necessary to create a String literal in Java is to enclose none, one, or more characters inside double quotation marks.  Unlike other programming environments, double quotes and single quotes (also known as apostrophes) are not interchangeable in Java.  Single quotes or apostrophes have a completely different use in Java.

Answer 4

False.

Back to Question 4

Explanation 4

The default is a 64-bit double.

Answer 3

You must include at least one of the following characters:
 

. E e F f D d

Back to Question 3

Explanation 3

These characters are used in the following ways:

  • Decimal point as in 1.414
  • Letter E or e as in 4.23E+21
  • Suffix F or f, indicating a 32-bit float literal, as in 1.828f
  • Suffix D or d, indicating a 64-bit double literal, as in 1234D

Answer 2

False.

Back to Question 2

Explanation 2

The default is 32 bits.  To indicate a long (64 bits) literal, append the suffix L to the literal expression.  The suffix can also be lower case, but then it looks like a numeral 1 which can be very confusing.

Answer 1

You can represent the decimal value 28 in the following six ways:
 

28, 034, 0x1c, 0x1C, 0X1c, 0X1C

Back to Question 1

Explanation 1

If you preface the literal value with a single zero (0), that indicates that you are specifying the value as an octal value.  An octal number is a number that is represented  according to the base eight.

Octal representation was widely used in the earlier days when the number of bits in each word of computer memory was often a multiple of three, such as 12, 24, 36, etc.  However, with the advent of memory word sizes that are not multiples of three, the usefulness of octal representation has diminished.

In case you are interested, octal numbers consist of the following digits:  0, 1, 2, 3, 4, 5. 6. and 7.  Note that there are eight unique digits in the octal number system.

If you preface the literal value with either 0x or 0X, that indicates that you are specifying the value as a hexadecimal value.  A hexadecimal number is a number that is represented according to the base sixteen.

Hexadecimal representation came into wide use when the defacto standard for the number of bits in each byte of computer memory became eight which is a multiple of four.  Hexadecimal numbers are commonly represented using the following digits:  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.  Note that there are sixteen unique digits in the hexadecimal number system.  In Java, the individual letters that are used as digits can be either upper case or lower case.

Because the X used to preface the literal value and the letter used in the literal vale can be either upper case or lower case, there are four different ways to express the decimal value 28 in hexadecimal notation.

The following list shows six ways to express the decimal value 28 as a literal integer.  Four of these use hexadecimal notation, one is octal, and the other is decimal.  The second column identifies the notation being used.

  • 28    decimal
  • 034   octal
  • 0x1c  hex
  • 0x1C  hex
  • 0X1c  hex
  • 0X1C  hex


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.

About the author

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.

baldwin.richard@iname.com

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories