Test Bank For Java Software Solutions 9th Edition
Test Bank For Java Software Solutions 9th Edition
Test Bank for Java Software Solutions 9th Edition by John Lewis,
William Loftus
TRUE/FALSE
ANS: T
The computer is a digital device meaning that it stores information in one of two states using binary.
We must determine then how to represent meaningful information (such as a name or a program
instruction or an image) in binary.
ANS: T
Java is classified as a high-level programming language but it is also classified as an object-oriented
programming language because it allows the programmer to implement data structures as classes.
ANS: F
Documentation comments follow // marks or are embedded between */ and */.
System.out.print is an instruction used to output a message to the screen (the Java console
window).
4. Java byte codes are directly executable whereas Java source code is not.
ANS: F
Neither Java source code nor Java byte codes are executable. Both must be compiled or interpreted
into machine code. Java byte codes are useful however in that they are machine-independent but
semi-compiled code that allows your Java code to be transmitted over the Internet and executed on
another computer even if that other computer is a completely different type.
ANS: F
The Java compiler can find syntax errors but cannot find either logical errors (errors that are caused
because of poor logic in writing the program) or run-time errors (errors that arise during the execution
of the program).
6. Java is a case-sensitive language which means Current, CURRENT, and current will all
reference the same identifier.
ANS: F
Java is case sensitive which means that Current, CURRENT, and current will all be
recognized as different identifiers. This causes problems with careless programmers who do not spell
an identifier consistently in terms of upper and lower case characters.
7. Code placed inside of comments will not be compiled and, therefore, will not execute.
ANS: T
The compiler discards comments; therefore, any code inside a comment is discarded and is not
compiled. Your executable program consists only of the code that is compiled.
ANS: F
public is a reserved word, but since Java is case sensitive, Public differs from public and
therefore Public is not a reserved word.
9. Reserved words in Java can be redefined by the programmer to mean something other than their
original intentions.
ANS: F
Java reserved words cannot be redefined.
ANS: F
Dividing by 0 is not detected at compile time, and because a computer cannot divide by 0, this is a run-
time error.
ANS: F
Dividing by 0 is not detected at compile time, and because a computer cannot divide by 0, this is a run-
time error.
12. During translation, the compiler puts its output (the compiled Java program) into ROM.
ANS: F
ROM stands for read-only-memory. The compiled output (the byte codes) may be placed into RAM
(writable random access memory) or into a file (on your hard drive, for example).
13. Objects are defined by a class that describes the characteristics common to all instances of the class.
ANS: T
An object is an instance of a class. And, the purpose of a class is to describe these common
characteristics.
ANS: T
Inheritance allows us to capitalize on the similarities among various kinds of classes that have a
common base (parent) class. Thus we reuse the base class each time a class inherits from it.
15. Polymorphism is the idea that we can refer to multiple types of related objects in consistent ways.
ANS: T
Polymorphism allows us to use the same name for similar behaviors that occur among diverse and
possibly unrelated objects. For example, to "open" may refer to a file, or to a device, or to a
communications line, etc. The same term, "open," is being used even though the objects that are
being opened are quite different.
16. In Java, identifiers may be of any length up to a limit determined by the compiler.
ANS: F
Java (and Java compilers) do not limit the length of the identifiers you use. Identifiers may be as
long as you wish. Good programming practice, however, will limit the lengths of the identifiers you
create.
MULTIPLE CHOICE
3. When executing a program, the processor reads each program instruction from
a. secondary memory (storage)
b. the Internet
c. registers stored in the processor
d. main memory
e. Any of these
ANS: D
The program is first loaded from secondary memory into main memory before it is executed so that the
processor is not slowed down by reading each instruction. This idea of executing programs stored in
memory is called the Stored Program Computer and was pioneered by John Von Neumann in the
1940s.
6. Volatility is a property of
a. RAM
b. ROM
c. disk
d. software
e. computer networks
ANS: A
Volatility means that the contents of memory are lost if the electrical power is shut off. This
is true of RAM (Random Access Memory), but not ROM (Read Only Memory) or disk.
Software and computer networks are not forms of memory.
7. The ability to directly obtain a stored item by referencing its address is known as
a. random access
b. sequential access
c. read-only access
d. fetch access
e. volatility
ANS: A
Random access is meant to convey the idea that accessing any item is equally easy, and that any item
is retrievable based solely on its address. Random access is the form of access used by both RAM
and ROM memory. Disk access, called direct access, is a similar idea, and direct and random access
are sometimes referred to synonymously. Sequential access is used by tape.
8. Which phase of the fetch-decode-execute cycle might use a circuit in the arithmetic-logic unit?
a. fetch
b. decode
c. execute
d. during fetch or execute, but not decode
e. any of the phases
ANS: C
The fetch phase retrieves (fetches) the next program instruction from memory. The decode phase
determines which circuit(s) needs to be used to execute the instruction. The instruction is executed
during the execute phase. If the instruction is either an arithmetic operation (like add or multiply) or
a logical operation (like comparing two values), then it is carried out by the ALU.
9. In order for a computer to be accessible over a computer network, the computer needs its own
a. MODEM
b. communication line
c. network address
d. packet
e. router
ANS: C
In order to differentiate between the computers on a network, each is given its own, unique, network
address. In this way, a message intended for one computer can be recognized by that computer
through the message's destination address. A MODEM is a device that is used to allow a computer to
communicate to another computer over a telephone line. A communication line is the network media
itself. A packet is a collection of data that is sent over a network. A router is a hardware device
used to take a message from one network and move it to another based on the message's destination
address.
12. It is important to dissect a problem into manageable pieces before trying to solve the problem because
a. most problems are too complex to be solved as a single, large activity
b. most problems are solved by multiple people and it is easy to assign each piece to a
separate person
c. ir is easier to integrate small pieces of a program into one program than it is to integrate
one big chunk of code into one program
d. the first solution may not solve the problem correctly
e. All of these
ANS: A
Any interesting problem will be too complex to solve easily as a single activity. By decomposing the
problem, we can build small solutions for each piece and then integrate the pieces. Answer D is true,
but it is not the reason why we will break a problem into pieces.
13. Once we have implemented a solution, we are not done with the problem because
a. the solution may not be the best (most efficient)
b. the solution may have errors and need testing and fixing
c. the solution may, at a later date, need revising to handle new specifications
d. the solution may, at a later date, need revising because of new programming language
features
e. All of these
ANS: E
A program should not be considered as a finished product until we are reasonably assured that it is
efficient and error-free. Further, it is common that programs require modification in the future
because of a change to specifications or a change to the language or computer running the program.
15. Of the following, which statement is not true regarding Java as a programming language?
a. Java is a relatively recent language; it was introduced in 1995.
b. Java is a language whose programs do not require translating into machine language
before they are executed.
c. Java is an object-oriented language.
d. Java is a language that embraces the idea of writing programs to be executed with the
World Wide Web.
e. All of these are true
ANS: B
All languages require translation into machine language. The other statements are all true about Java.
23. A unique aspect of Java that allows code compiled on one machine to be executed on a machine with a
different hardware platform is Java's
a. bytecodes
b. syntax
c. use of objects
d. use of exception handling
e. All of these
ANS: A
The translation process for a Java program is to first compile it into bytecodes, which are
architecturally neutral (that is, they can be used no matter what the architectural platform is). To
execute the program, the bytecodes must be further compiled by a Java compiler or interpreted by a
Java Virtual Machine.
25. An error in a program that results in the program outputtinh $100 instead of the correct answer, $250,
is a
a. compiler error
b. syntax error
c. run-time error
d. logical error
e. snafu
ANS: D
While this is an error, programmers classify the type of error in order to more easily solve the problem.
Syntax errors are caught by the compiler and the program cannot run without fixing all syntax errors.
Run-time errors arise during program execution and cause the program to stop running. Logical
errors are errors whereby the program can run to completion, but gives the wrong answer. If the
result should have been $250, then the logic of the program is wrong since it output $100. A snafu is
a term expressing a messed up situation in combat and should not be used by respectable
programmers!
26. Which of the following is true regarding Java syntax and semantics?
a. A Java compiler can determine if you have followed proper syntax but not proper
semantics.
b. A Java compiler can determine if you have followed proper semantics but not proper
syntax.
c. A Java compiler can determine if you have followed both proper syntax and proper
semantics.
d. A Java compiler cannot determine if you have followed either proper syntax or proper
semantics.
e. A Java compiler can determine if you have followed proper syntax but not proper
semantics only if you follow the Java naming convention rules.
ANS: A
Compilers for all languages have the ability to detect syntax errors because improper use of the syntax
leads to situations where the compilers cannot translate the code properly. However, compilers are
unable to follow the semantics of a program because this requires a degree of understanding what the
program is intended to do and computers have no sense of understanding (at least at this point).
27. Using Java naming convention, which of the following would be a good variable name for the current
value of a stock?
a. curstoval
b. theCurrentValueOfThisStock
c. currentStockVal
d. csv
e. current
ANS: C
Java allows long variable names but the programmer must find a good compromise between an
excessive long name (as with B) and names too short to understand their use (A and D). The name
current possibly might be reasonable if there are no other "current" values being referenced in the
program.
29. A color image is broken down into individual pixels (points), each of which is represented by
a. a 1 for white and a 0 for black
b. 3 values denoting the intensity of red, green, and blue in the image
c. a single number indicating the intensity of color between white and black
d. two numbers, where one indicates where the color is between white and black and the
other denotes the brightness
e. None of these; it is not possible to represent color
ANS: B
Black and white images are stored using 0s and 1s while color images are stored using three values,
one each for the degree of red, the degree of blue, and the degree of green.
30. Which of the following characters does not need to have an associated closing character in a Java
program?
a. {
b. (
c. [
d. <
e. All of these require closing characters
ANS: D
{ is used to open a block, and so } is needed to close the block. ( is used to open an expression
and so ) is needed to close an expression. [ is used to start an array index so ] is needed to close
the array index. < is "less than" and > is "greater than" and these are not needed together, so <
requires no closing character.
PROBLEM
ANS:
The one executable statement in the main method is missing a ";" at the end of the line.
Executable statements end with ";".
ANS:
The definition of a class is placed within {} statements, which are missing here.
3. Given the following class definition, what are the reserved words and what are the identifiers?
public class Program3
{
public static void main(String[] args)
{
System.out.println("My third Java program");
}
}
ANS:
The reserved words are public, class, static, void. The identifiers are main, String,
System.out, Program3, and args. main is the name of a method defined within the
Program3 class. string and System.out are classes already defined in Java and println
is a method of System.out. Program3 is a class, defined here, and args is a variable.
4. Provide a brief explanation of the role of main memory, the control unit, the arithmetic logic unit, and
registers. (Refer to figure 1.13 in the text)
ANS:
Main memory is used to store the currently executing processes along with their data. The control
unit performs the fetch-decode-execute cycle, which fetches an instruction from memory, decodes it
and determines how it is to be executed. The arithmetic logic unit comprises a number of circuits that
execute arithmetic and logic instructions. Registers are used to store values in the CPU temporarily
while the current instruction(s) need them.
5. What is the output of the following code when the main method is executed?
public class Question4
{
public static void main(String[] args)
{
System.out.println("hi there");
System.out.println(" ");
System.out.println("how are you doing today? ");
}
}
ANS:
hi there
Notice that while the Java compiler ignores "white space", blanks that appear in a println
statement inside of quote marks are retained and output in that manner.
ANS:
It is missing a closing ". The compiler will look for a second " before the end of the statement.
So, like {}, (), and [], an initial " must have a corresponding closing ".
7. Provide identifier names that would be used to represent a person's social security number, income tax
withheld, and net pay.
ANS:
socialSecurityNumber, or ssn, incomeTaxWithheld or incomeTax, and netPay
all would be reasonable.
8. There are a number of reserved words in Java that have no current meaning (denoted with an * in
figure 1.18 in the text). Why?
ANS:
Java language designers anticipate introducing these statements in future versions, but have not yet
implemented them because they are lower priority, or it has not been decided how they will be
implemented or precisely what they will mean.
9. A document of text is 15 pages long. Each page contains approximately 200 words and the average
length of each word is 5 characters. Also assume one blank space between each word and no
punctuation. How many bytes will it take to store this document in memory or on disk using ASCII?
ANS:
A character is stored in ASCII using 8 bits or 1 byte. Therefore, 5 characters per word plus 1 blank
space between words take 6 bytes per word (except for the first). Each page stores 200 words and
there are 15 pages. So we need 15 * 200 * 6 - 1 (no blank space to start the text) = 17,999 bytes
which is 17.58 kilobytes, or nearly 18 Kbytes.
10. Provide a brief description of the roles of the following hardware elements (that is, what each is used
for):
a) CPU
b) Main memory
c) Secondary memory devices
d) Input/Output devices
ANS:
a) The CPU is the processor. It executes all program instructions. It does this through the fetch-
decode-execute cycle where the next program instruction is fetched from memory, decoded in the
CPU, and then executed by one or more circuits.
b) Main memory is stored on chips on the motherboard and is used for quick access to the current
program for the fetch-decode-execute cycle and to store data being used by this program.
c) Secondary memory devices are storage devices, used to store programs and data not currently
being used. Storage devices, such as the hard disk, also are used to store things for permanence and
archives.
d) Input/Output devices are used to communicate with the computer. Input devices, like the
keyboard, take commands and data from the user and output devices, like the monitor, display the
results of the process/computation.
11. Examine figure 1.7 before answering this question. What 8-bit value comes immediately before and
what 8-bit value comes immediately after 10010111?
ANS:
10010110 comes immediately before 10010111 and 10010100 comes immediately after
10010111.
12. Rewrite the following comment so that is can appear over multiple lines.
// This is one really enormously long comment that might run off
the page
ANS:
We can do this in two ways, preceding each line with // or by enclosing the comment in /* and
*/.
13. Rewrite the following program with better formatting to make it easier to read.
public
class
MyProgram
{ public static void
main(
String[]
args)
{ System.out.println(
"Wow, this is messed up!"
);
} }
ANS:
There are many ways this program might appear. The following would be very acceptable:
public class MyProgram
{
public static void main(String[] args)
{
System.out.println("Wow, this is messed up!");
}
}
14. Write a Java program that will output on two separate lines the names of the authors of this textbook.
ANS:
public class OutputNames
{
public static void main(String[] args)
{
System.out.println("John Lewis"); // 1st author's name
System.out.println("William Loftus");// 2nd author's name
}
}
15. Correct all the syntax errors in the following program.
Public Class Program \\ A problem program
(
Public static voided main[Strings() args]
{
system.out.println('This program'); \* oh, my... *\
system.out.println('has several syntax errors'); \*
lots of errors *\
}
)
ANS:
public class Program // A problem program
{
public static void main(String[] args)
{
System.out.println("This program"); /* oh, my... */
System.out.println("has several syntax errors"); /*
lots of errors */
}
}
16. Write a Java program that will display the following three lines when it is run:
*
* * *
* * * * *
ANS:
public class Stars
{
public static void main(String[] args)
{
System.out.println(" *");
System.out.println(" * * *");
System.out.println("* * * * *");
}
}
17. Name five of the fundamental terms which encompass object-oriented programming.
ANS:
There are seven terms to choose from: object, attribute, method, class, encapsulation,
inheritance, and polymorphism.