Connexions

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

Recently Viewed

This feature requires Javascript to be enabled.

Ji0030: OOP Self-Assessment Test, Part 3 - Old version

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

Summary: Part 3 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 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 .

Show six different ways to express the decimal value 28 as an integral literal.

Question 2 .

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

Question 3 .

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

Question 4 .

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

Question 5 .

Show how to represent a String literal.

Question 6 .

What are the four kinds of things that can be contained in the elements of a Java array object.

Question 7 .

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

Question 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?

Question 9 .

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

Question 10 .

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.

Consider the program shown in Listing 1 , 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?

Listing 1: Listing for Question 10.

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


Miscellaneous

This section contains a variety of miscellaneous information.

Note:

Housekeeping material
• Module name: Ji0030: OOP Self-Assessment Test, Part 3
• File: Ji0030.htm
• Originally published: September 9, 2000
• Published at cnx.org: November 29, 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.

The value 20 will be displayed.

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.

True

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 shown in Listing 2 . 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.

Listing 2: Listing for Answer 9.
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. In fact, you cannot change the size of a Java array once the object that encapsulates that array is instantiated.

The two different formats are illustrated below:

• String[] variableName;
• String variableName[];

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

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.

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. (Under certain circumstances, primitive values 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 an array element 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.
• Any interface extended by any interface implemented by the class from which the object was instantiated or implemented by any superclass of the class from which the object was instantiated.
• Probably some other possibilities involving superclasses and superinterfaces.

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.

The four kinds of things that can be contained in the elements of a Java array object are:

• primitive values
• object references
• references to other array objects
• null

Explanation 6

Actually, it could be argued that there are only three 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.

"This is a String literal."

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.

False

Explanation 4

The default is a 64-bit double .

You must include at least one of the following characters to indicate a floating point literal:

• . (period)
• E
• e
• F
• f
• D
• d

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

False

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.

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

• 28
• 034
• 0x1c
• 0x1C
• 0X1c
• 0X1C

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 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 numbers are formed from the following characters:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

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

-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