• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 4 fuera de 93 páginas
Examen

Java Foundations, Lewis - Solutions, summaries, and outlines. 2022 updated

Document preview thumbnail
Vista previa 4 fuera de 93 páginas

Description: INCLUDES Some or all of the following - Supports different editions ( newer and older) - Answers to problems & Exercises. in addition to cases - Outlines and summary - Faculty Approved answers. - Covers ALL chapters.

Vista previa del contenido

Chapter 1 Exercise Solutions
EX 1.1. Give examples of the two types of Java comments and explain the
differences between them.
One kind of comment begins with a double slash (//) and continues to the end of the
line. A second kind of comment begins following an initiating slash-asterisk (/*) and
terminates immediately preceding a terminating asterisk-slash (*/). The second type of
comment can span multiple lines.

EX 1.2. Which of the following are not valid Java identifiers? Why?
a. Factorial
Valid
b. anExtremelyLongIdentifierIfYouAskMe
Valid
c. 2ndLevel
Invalid because it begins with a digit
d. level2
Valid
e. MAX_SIZE
Valid
f. highest$
Valid
g. hook&ladder
Invalid because it contains an ampersand (&)

EX 1.3. Why are the following valid Java identifiers not considered good identi-
fiers?
a. q
The identifier q is a meaningless name.

b. totVal
The identifier totalValue would be more meaningful than the abbreviation.

c. theNextValueInTheList
Unnecessarily lengthy; nextValue would serve as well.

EX 1.4. Java is case sensitive. What does that mean?
Uppercase characters are considered to be distinct from lowercase letters. Therefore
the identifier HELLO is distinct from Hello which is distinct from hello.

,EX 1.5. What do we mean when we say that the English language is ambiguous?
Give two examples of English ambiguity (other than the example used in
this chapter) and explain the ambiguity. Why is ambiguity a problem for
programming languages?
Something is ambiguous if it has two or more possible meanings. For example, the
statement, “Mary is the nicest teaching assistant who has helped me all day long” might
mean 1) of all the teaching assistants who have helped me today, Mary is the nicest, or
2) of those teaching assistants who have helped me for an entire day, Mary is the
nicest. As another example, the statement, “Bananas help those who help themselves”
might mean 1) bananas are good for those who attend to their own welfare or 2)
bananas are good for those who eat as many bananas as they please. If a
programming language statement could be interpreted in two or more ways, it would be
impossible to predict with certainty how it would be interpreted and what result would
be produced.

EX 1.6. Categorize each of the following situations as a compile-time error, run-
time error, or logical error.
a. multiplying two numbers when you meant to add them
A logical error
b. dividing by zero
A run-time error
c. forgetting a semicolon at the end of a programming statement
A compile-time error
d. spelling a word wrong in the output
A logical error
e. producing inaccurate results
A logical error
f. typing a { when you should have typed (

A compile-time error

, Chapter 2 Exercise Solutions
EX 1.1. Explain the following programming statement in terms of objects and the
services they provide.
System.out.println ("I gotta be me!");
The System.out object has a println method which accepts a character string as a
parameter. The service the println method provides is to display the string on the
monitor.

EX 1.2. What output is produced by the following code fragment? Explain.
System.out.print ("Here we go!");
System.out.println ("12345");
System.out.print ("Test this if you are not sure.");
System.out.print ("Another.");
System.out.println ();
System.out.println ("All done.");
The output produced is:
Here we go!12345
Test this if you are not sure.Another.
All done.
After printing its data, the println method moves to the next line of output, whereas the
print method does not. A println statement with no data has the effect of moving down
to the next line.

EX 1.3. What is wrong with the following program statement? How can it be
fixed?
System.out.println ("To be or not to be, that is the
question.");
The string to be printed is not all on one line. The problem can be fixed by splitting the
long string into two and using the string concatenation operator (+) to reconstruct it, or
by using a print statement for part of the string and a println statement for the
remainder of the string.

EX 1.4. What output is produced by the following statement? Explain.
System.out.println ("50 plus 25 is " + 50 + 25);
The output produced is:
50 plus 25 is 5025
First the string “50 plus 25 is ” is concatenated with the integer 50; since one of the
operands is a string, the result is a string. Then the string “50 plus 25 is 50” is
concatenated with the integer 25, producing the final result.

, EX 1.5. What is the output produced by the following statement? Explain.
System.out.println ("He thrusts his fists\n\tagainst"
+
" the post\nand still insists\n\the sees
the \"ghost\"");
The output produced is:
He thrusts his fists
against the post
and still insists
he sees the "ghost"
Escape characters are used to go to the beginning of new lines (\n), to tab (\t), and to
print quotation marks (\").

EX 1.6. What value is contained in the integer variable size after the following
statements are executed?
size = 18;
size = size + 12;
size = size * 2;
size = size / 4;
The final value stored in size is 15.

EX 1.7. What value is contained in the floating point variable depth after the
following statements are executed?
depth = 2.4;
depth = 20 – depth * 4;
depth = depth / 5;
The final value stored in depth is 2.08.

EX 1.8. What value is contained in the integer variable length after the following
statements are executed?
length = 5;
length *= 2;
length *= length;
length /= 100;
The final value stored in length is 1.

EX 1.9. Write four different program statements that increment the value of an
integer variable total.
total = total + 1;

Libro relacionado
 image
John Lewis, Peter DePasquale, Joe Chase Java Foundations
Editorial: 2013 ISBN: 9780133437355 Edición: Desconocido

Información del documento

Subido en
3 de abril de 2022
Número de páginas
93
Escrito en
2021/2022
Tipo
Examen
Contiene
Preguntas y respuestas
$40.99

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
TestBanks2022
4.0
(355)
Vendido
2202
Seguidores
1703
Artículos
2238
Última venta
2 semanas hace



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes