JHTP自测题_第二章_Java应用、输入、输出及运算符简介



Self-Review Exercises

2.1 Fill in theblanks in each of the following statements:

a) A(n) { beginsthe body of every method, and a(n) } ends the body of every method.

b) You can use the ifstatement to make decisions.

c) // begins anend-of-line comment.

d) Space characters, newlines  and tabs are called whitespace.

e) Keywords arereserved for use by Java.

f) Java applicationsbegin execution at main method .

g) Methods System.out.print, System.out.println and System.out.printf displayinformation in a command window.

2.2 State whethereach of the following is true or false. If false, explain why.

a) Comments cause thecomputer to print the text after the // on the screenwhen the program executes. (False)

b) All variables must begiven a type when they’re declared. (True)

c) Java considers thevariables number and NuMbEr to be identical. (False)

d) The remainder operator (%) can be used only withinteger operands. (False)

e) The arithmeticoperators *, /, %, + and - all have the same level of precedence. (False)

2.3 Writestatements to accomplish each of the following tasks:

a) Declare variables c, thisIsAVariable, q76354 and number to be of typeint.

b) Prompt the user toenter an integer.

c) Input an integer andassign the result to int variable value. Assume Scanner variable input can be used to read a value from the keyboard.

d) Print "Thisis a Java program" on one line in the command window. Use method System.out.println.

e) Print "Thisis a Java program" on two lines in the command window. The first line should endwith Java. Use method System.out.printf and two %s formatspecifiers.

f) If the variable number is not equalto 7, display "The variable number is not equal to 7".

2.4 Identify andcorrect the errors in each of the following statements:

a) if (c <7);

System.out.println("c isless than 7");

b) if (c=> 7)

System.out.println("c isequal to or greater than 7");

2.5 Writedeclarations, statements or comments that accomplish each of the followingtasks:

a) State that a programwill calculate the product of three integers.

b) Create a Scanner called input that readsvalues from the standard input.

c) Declare the variables x, y, z and result to be of typeint.

d) Prompt the user toenter the first integer.

e) Read the first integerfrom the user and store it in the variable x.

f) Prompt the user toenter the second integer.

g) Read the secondinteger from the user and store it in the variable y.

h) Prompt the user toenter the third integer.

i) Read the third integerfrom the user and store it in the variable z.

j) Compute the product ofthe three integers contained in variables x, y and z, and assign theresult to the variable result.

k) Use System.out.printfto display the message "Product is"followed by the value of the variable result.

2.6 Using thestatements you wrote in Exercise 2.5, write a complete program that calculates andprints the product of three integers.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值