Docsity

ABSOLUTE JAVA 6TH EDITION SOLVED QUESTIONS AND COMPLETE ANSWERS, Exams of Java Programming

Toggle sidebar
bg1
ABSOLUTE JAVA 6TH EDITION SOLVED QUESTIONS AND
COMPLETE ANSWERS
Question 1.
(1.1) Give a statement or statements that can be used in a Java program to write the
following to the screen: I like Java. You like tea.
Answer: System.out.println("I like Java."); System.out.println("You like tea.");
Question 2.
(1.1) Write a complete Java program that uses System.out.println to output the following
to the screen when run: Hello World!
Answer: public class HelloWorld { public static void main(String[] args) {
System.out.println("Hello World!"); } }
Question 3.
(1.1) What is a compiler?
Answer: A compiler translates a program written in a programming language
such as Java into a program in a low-level language. When you compile a Java
program, the compiler translates your Java program into a program expressed
in Java byte-code.
Question 4.
(1.1) What is a source program?
Answer: The program that is input to a compiler is called the source program.
Question 5.
(1.1) What is an object program?
Answer: The translated program that is produced by a compiler is called the
object program or object code.
Question 6.
(1.1) What do you call a program that runs Java byte-code instructions?
Answer: A program that runs Java byte-code instructions is called an
interpreter. It is also often called the Java Virtual Machine (JVM).
Question 7.
(1.1) Suppose you define a class named NiceClass in a file. What name should the file
have?
Answer: NiceClass.java
pf3
pf4
pf5

Partial preview of the text

Download ABSOLUTE JAVA 6TH EDITION SOLVED QUESTIONS AND COMPLETE ANSWERS and more Exams Java Programming in PDF only on Docsity!

ABSOLUTE JAVA 6TH EDITION SOLVED QUESTIONS AND

COMPLETE ANSWERS

Question 1.

(1.1) Give a statement or statements that can be used in a Java program to write the following to the screen: I like Java. You like tea.

Answer: System.out.println("I like Java."); System.out.println("You like tea.");

Question 2.

(1.1) Write a complete Java program that uses System.out.println to output the following to the screen when run: Hello World!

Answer: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }

Question 3.

(1.1) What is a compiler?

Answer: A compiler translates a program written in a programming language such as Java into a program in a low-level language. When you compile a Java program, the compiler translates your Java program into a program expressed in Java byte-code.

Question 4.

(1.1) What is a source program?

Answer: The program that is input to a compiler is called the source program.

Question 5.

(1.1) What is an object program?

Answer: The translated program that is produced by a compiler is called the object program or object code.

Question 6.

(1.1) What do you call a program that runs Java byte-code instructions?

Answer: A program that runs Java byte-code instructions is called an interpreter. It is also often called the Java Virtual Machine (JVM).

Question 7.

(1.1) Suppose you define a class named NiceClass in a file. What name should the file have?

Answer: NiceClass.java

(1.1) Suppose you compile the class NiceClass. What will be the name of the file with the resulting byte-code?

Answer: NiceClass.class

Question 9.

(1.2) Which of the following may be used as variable names in Java? rate1, 1stPlayer, myprogram.java, long, TimeLimit, numberOfWindows

Answer: 1stPlayer may not be used because it starts with a digit; myprogram.java may not be used because it contains an illegal symbol, the dot; long may not be used because it is a keyword. All the others may be used as variable names. However, TimeLimit, while legal, violates the style rule that all variable names begin with a lowercase letter.

Question 10.

(1.2) Can a Java program have two different variables named number and Number?

Answer: Yes, a Java program can have two different variables named number and Number, because Java is case sensitive. However, it would not be good style to do so.

Question 11.

(1.2) Give the declaration for two variables called feet and inches. Both variables are of type int and both are to be initialized to zero in the declaration.

Answer: int feet = 0, inches = 0;

Question 12.

(1.2) Give the declaration for two variables called count and distance. count is of type int and is initialized to zero. distance is of type double and is initialized to 1.5.

Answer: int count = 0; double distance = 1.5;

Question 13.

(1.2) Write a Java assignment statement that will set the value of the variable distance to the value of the variable time multiplied by 80. All variables are of type int.

Answer: distance = time * 80;

Question 14.

(1.2) Write a Java assignment statement that will set the value of the variable interest to the value of the variable balance multiplied by the value of the variable rate. The variables are of type double.

Answer: interest = balance * rate;

(1.3) What is the output produced by the following? String test = "abcdefg"; System.out.println(test.length()); System.out.println(test.charAt(1));

Answer: 7 b

Question 23.

(1.3) What is the output produced by the following? String test = "abcdefg"; System.out.println(test.substring(3));

Answer: defg

Question 24.

(1.2) Given the following fragment that purports to convert from degrees Celsius to degrees Fahrenheit, answer the following questions: double celsius = 20; double fahrenheit; fahrenheit = (9 / 5) * celsius + 32.0; a. What value is assigned to fahrenheit? b. Explain what is actually happening, and what the programmer likely wanted. c. Rewrite the code as the programmer intended.

Answer: a. 52. b. 9/5 has int value 1; because the numerator and denominator are both of type int, integer division is done; the fractional part is discarded. The programmer probably wanted floating-point division, which does not discard the part after the decimal point. c. fahrenheit = (9.0/5) * celsius + 32.0; or fahrenheit = 1.8 * celsius + 32.0;

Question 25.

(1.3) What is the output produced by the following? System.out.println("abc\ndef");

Answer: abc def

Question 26.

(1.2) What is the output produced by the following lines of program code? int n = (int)3.9; System.out.println("n == " + n);

Answer: n = = 3

Question 27.

(1.3) What is the output produced by the following? System.out.println("abc\ndef");

Answer: abc\ndef

Question 28.

(1.3) What is the output produced by the following? String test = "Hello Tony"; test = test.toUpperCase(); System.out.println(test);

Answer: HELLO TONY

(1.3) What is the output of the following two lines of Java code? System.out.println("2 + 2 = " + (2 + 2)); System.out.println("2 + 2 = " + 2 + 2);

Answer: The output is 2 + 2 = 4 2 + 2 = 22 In the expression "2 + 2 = " + (2 + 2), the integers 2 and 2 in (2 + 2) are added to obtain the integer 4. When 4 is connected to the string "2 + 2" with a plus sign, the integer 4 is converted to the string "4" and the result is the string "2 + 2 = 4". However "2 + 2 = " + 2 + 2 is interpreted by Java to mean ("2 + 2 = " + 2) + 2 The first integer 2 is changed to the string "2" because it is being combined with the string "2 + 2". The result is the string "2 + 2 = 2". The last integer 2 is combined with the string "2 + 2 = 2". So, the last 2 is converted to the string "2". So the final result is "2 + 2 = 2" + "2" which is "2 + 2 = 22".

Question 30.

(1.3) Suppose sam is an object of a class named Person and suppose increaseAge is a method for the class Person that takes one argument that is an integer. How do you write an invocation of the method increaseAge using sam as the calling object and using the argument 10? The method increaseAge will change the data in sam so that it simulates sam aging by 10 years.

Answer: sam.increaseAge(10);

Question 31.

(1.3) The following code is supposed to output the string in lowercase letters but it has an error. What is wrong? String test = "WHY ARE YOU SHOUTING?"; test.toLowerCase(); System.out.println(test);

Answer: The method toLowerCase doesn't change the string test. To change it, we must set test equal to the string returned by to LowerCase: test = test.toLowerCase();

Question 32.

(1.4) What are the two kinds of comments in Java?

Answer: The two kinds of comments are // comments and / / comments. Everything following a // on the same line is a comment. Everything between a / and a matching / is a comment.

Question 33.

(1.4) What is the output produced by the following Java code? /** Code for Exercise. */ System.out.println("Hello"); //System.out.print("Mr. or Ms. "); System.out.println("Student");

Answer: Hello Student