Java How to Program学习笔记_第五章_控制语句Part 2-逻辑运算符——自测题



Self-Review Exercises

5.1 Fill in theblanks in each of the following statements:

a) Typically, for statementsare used for counter-controlled repetition and while statements forsentinel-controlled repetition.

b) The dowhile statementtests the loop-continuation condition after executing the loop’s body;therefore, the body always executes at least once.

c) The switchstatement selects among multiple actions based on the possible values of aninteger variable or expression, or a String.

d) The continue statement,when executed in a repetition statement, skips the remaining statements in theloop body and proceeds with the next iteration of the loop.

e) The && operatorcan be used to ensure that two conditions are both true before choosinga certain path of execution.

f) If theloop-continuation condition in a for header isinitially false , the program does not execute the for statement’sbody.

g) Methods that performcommon tasks and do not require objects are called static methods.

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

a) The default case isrequired in the switch selection statement. (false)

b) The break statement isrequired in the last case of a switch selection statement. (false)

c) The expression ((x > y) &&(a < b)) is true if either x > y is true or a < b is true. (false)

d) An expressioncontaining the || operator is true if either or both of its operands are true. (true)

e) The comma (,) formattingflag in a format specifier (e.g., %,20.2f) indicatesthat a value should be output with a thousands separator. (true)

f) To test for a range ofvalues in a switch statement, use a hyphen () between thestart and end values of the range in a case label. (false)

g) Listing cases consecutivelywith no statements between them enables the cases to perform the same set ofstatements. (true)

5.3 Write a Javastatement or a set of Java statements to accomplish each of the followingtasks:

a) Sum the odd integersbetween 1 and 99, using a for statement. Assume that the integer variables sum and count have beendeclared.

b) Calculate the value of2.5 raised to the power of 3, using the pow method.

c) Print the integersfrom 1 to 20, using a while loop and the counter variable i. Assume thatthe variable i has been declared, but not initialized. Print only five integersper line.

[Hint: Use thecalculation i % 5. When the value of this expression is 0, print a newline character;otherwise, print a tab character. Assume that this code is an application. Use theSystem.out.println() method to output thenewline character, and use the System.

out.print('\t') method tooutput the tab character.]

d) Repeat part (c), usinga for statement.

5.4 Find theerror in each of the following code segments, and explain how to correct it:

a) i = 1;

while (i <= 10);

++i;

}

b) for (k = 0.1; k != 1.0; k += 0.1)

System.out.println(k);

c) switch (n)

{

case 1:

System.out.println("Thenumber is 1");

case 2:

System.out.println("Thenumber is 2");

break;

default:

System.out.println("Thenumber is not 1 or 2");

break;

}

d) The following codeshould print the values 1 to 10:

n = 1;

while (n < 10)

System.out.println(n++);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值