《Java语言程序设计(基础篇)原书第10版》第1章 计算机、程序和Java概述 复习题答案
1.1 What are hardware and software?
A computer is an electronic device thatstores and processes data. A computer includes both hardware and software.
In general, hardware is thephysical aspectof the computer thatcan be seen, and software is theinvisible instructionsthatcontrol the hardwareand make it work.
The hardware of a computer consists of a CPU, cache, memory, hard disk, floppy disk, monitor, printer, and communication devices.
1.2 List five major hardware components of a computer.
Five major hardware components:
CPUMemoryStorage DevicesInput/Out DevicesCommunication Devices
1.3 What does the acronym “CPU” stand for?
CPU stands forCentral Processing Unit. It is the brain of the computer.
1.4 What unit is used to measure CPU speed?
The unit of measurement of clock speed is thehertz (Hz), with 1 hertz equaling 1 pulse per second. The clock speed of a computer is usually stated in megahertz (MHz) (1 MHz is 1 million Hz).
还可用IPS,如MIPS
1.5 What is a bit? What is a byte?
A bit is abinary digit0 or 1.
A byte is a sequence of8 bits.
1.6 What is memory for? What does RAM stand for? Why is memory called RAM?
Memory is like awork areafor programs. Before a program is executed, it is brought into the memory.
RAM stands forrandom-access memory.
It is called RAM because a memory cell can beaccessed directly.
1.7 What unit is used to measure memory size?
Memory size is measured inbytes.
1.8 What unit is used to measure disk size?
Disk size is measured inbytes.
1.9 What is the primary difference between memory and a storage device?
Memory isvolatile, because information is lost when the power is turned off. Programs and data arepermanently storedon storage devices and are moved, when the computer actually uses them, to memory, which is muchfasterthan storage devices.
1.10 What language does the CPU understand?
Themachine languageis a set of primitive instructions built into every computer. This is the language understood by a computer and executed by a computer.
1.11 What is an assembly language?
Assembly language is alow-level programming languagein which amnemonicis used to represent each of the machine language instructions.
1.12 What is an assembler?
Assembler is a software thattranslatesassembly language into machine language.
1.13 What is a high-level programming language?
The high-level languages areEnglish-likeand easy to learn and program.
1.14 What is a source program?
The programwritten in a programming languageis called a source program.
1.15 What is an interpreter?
An interpreter is asoftwarethat readsone statementfrom the source code,translatesit to the machine code or virtual machine code, and thenexecutesit right away.
1.16 What is a compiler?
A compiler is asoftwarethattranslates a programin high-level language into machine language code.
1.17 What is the difference between an interpreted language and a compiled language?
An interpreter readsone statementfrom the source code, and translates it to the machine code or virtual machine code, and then executes it right away. A compiler translates theentiresource code into a machine code file, and the machine code file is then executed.
1.18 What is an operating system? List some popular operating systems.
The operating system (OS) is aprogramthatmanages and controlsa computer’s activities.
The examples of OS are Windows 98, NT, 2000, XP, or ME.Windows. Application programs such as an Internet browser and a word processor run on top of an operating system.
1.19 What are the major responsibilities of an operating system?
Major responsibilities:
a.Controlling and monitoringsystem activities
b.Allocating and assigningsystem resources
c.Schedulingoperations
1.20 What are multiprogramming, multithreading, and multiprocessing?
Multiprogramming allows multiple programs to runsimultaneouslyby sharing the CPU.
Multithreading allows concurrency within a program, so that itssubtaskscan run at the same time.
Multiprocessing, or parallel processing, uses two or moreprocessorstogether to perform a task.
1.21 Who invented Java? Which company owns Java now?
Java was invented by a team led byJames GoslingatSun Microsystemsin 1991. Originally called Oak, it became Java in 1995 when it was redesigned for developing Internet applications.
Oraclebought Sun and Oracle now owns Java.
1.22 What is a Java applet?
Java applet is a specialprogramthat runs from aWeb browser.
1.23 What programming language does Android use?
Android uses theJavaprogramming language.
1.24 What is the Java language specification?
The Java language specification specifies thesyntaxfor the Java language.
1.25 What does JDK stand for? What is JRE stand for?
JDK stands forJava Development Toolkit.
JRE stands forJava Runtime Environment.
1.26 What does IDE stand for?
IDE standsintegrated development environment.
1.27 Are tools like NetBeans and Eclipse different languages from Java, or are they dialects or extensions of Java?
NetBeans and Eclipse arenot programming languages,nor dialects,nor extensionsof Java. They are Javadevelopment tools.
1.28 What is a keyword? List some Java keywords.
Keywords havespecific meaningto thecompilerand cannot be used for other purposes in the program such as variables or method names.
Examples of keywords areclass
,static
, andvoid
.
1.29 Is Java case sensitive? What is the case for Java keywords?
Java source code iscase sensitive.
Java keywords are always inlowercase.
1.30 What is a comment? Is the comment ignored by the compiler? How do you denote a comment line and a comment paragraph?
Comments are used todocumentwhat a program is for andhowa program isconstructed. Comments help the programmers or users to communicate andunderstandthe program.
Comments are not programming statements and areignoredby the compiler.
In Java, comments are preceded by two forward slashes (//
) in alineorenclosedbetween/*
and*/
n multiple lines. When the compiler sees//
, it ignores all text after//
in the same line. When it sees/*
, it scans for the next*/
and ignores any text between/*
and*/
.
1.31 What is the statement to display a string on the console?
System.out.println(string);
1.32 Show the output of the following code:
public class Test {public static void main(String[] args) {System.out.println("3.5 * 4 / 2 – 2.5 is ");System.out.println(3.5 * 4 / 2 – 2.5);}}
Output is
3.5 * 4 / 2 – 2.5 is 4.5
1.33 What is the Java source filename extension, and what is the Java bytecode filename extension?
The source file extension is.java
and the bytecode file extension is.class
.
1.34 What are the input and output of a Java compiler?
The input of a Java compiler is a Javasource code fileand the output is a Javaclass file.
1.35 What is the command to compile a Java program?
javac
is the JDK command to compile a program.
1.36 What is the command to run a Java program?
java
is the JDK command to run a program.
1.37 What is the JVM?
JVM is the Javavirtual machinethat runs a Java program.
1.38 Can Java run on any machine? What is needed to run Java on a computer?
Java can run onany machinewith aJVM.
1.39 If aNoClassDefFoundError
occurs when you run a program, what is the cause of the error?
Java interpretercannot findthe.class
file. Make sure you placed the file in the right place, and invoked java command with appropriate package name.
1.40 If aNoSuchMethodError
occurs when you run a program, what is the cause of the error?
The class does not have amainmethod, or thesignatureof the main method is incorrect.
1.41 Reformat the following program according to the programming style and documentation guidelines. Use the end-of-line brace style.
public class Test {// Main method public static void main(String[] args) {/** Display output */System.out.println("Welcome to Java");}}
把第2行
{
上移
public class Test {// Main method public static void main(String[] args) {/** Display outpput */System.out.println("Welcome to Java");}}
1.42 What are syntax errors (compile errors), runtime errors, and logic errors?
Syntax errors are detected bycompilers.
Runtime errors occur duringexecutionof the program.
Logic errors results in incorrectresults.
1.43 Give examples of syntax errors, runtime errors, and logic errors.
syntax error:
System.out.println("2 / 1 = " + 2)// 少了分号
runtime error:
System.out.println("2 / 0 = " + 2);// 除0
logic error:
System.out.println("2 / 1 = " + 3);// 结果不对
1.44 If you forget to put a closing quotation mark on a string, what kind error will be raised?
Syntax error.
1.45 If your program needs to read integers, but the user entered strings, an error would occur when running this program. What kind of error is this?
Runtime error.
1.46 Suppose you write a program for computing the perimeter of a rectangle and you mistakenly write your program so that it computes the area of a rectangle. What kind of error is this?
Logic error.
1.47 Identify and fix the errors in the following code:
public class Welcome {public void Main(String[] args) {System.out.println('Welcome to Java!');})
Line 2. Main should bemain.
Line 2.staticis missing.
Line 3: Welcome to Java! should be enclosed insidedouble quotationmarks.
Line 5: The last)
should be}
.
public class Welcome {public static void main(String[] args) {System.out.println("Welcome to Java!");}}