您的位置:首页 > 编程语言 > PHP开发

JHTP自测题_第四章_控制语句Part 1-赋值、++、--运算符

2016-06-17 13:03 555 查看


4.1
Fill in theblanks in each of the following statements:

a) All programs can bewritten in terms of three types of control structures:
sequence, selection andrepetition.

b) The
if…else statementis used to execute one action when a condition is true and another when thatcondition is false.

c) Repeating a set ofinstructions a specific number of times is called
counter-controlled/definiterepetition.

d) When it’s not known inadvance how many times a set of statements will be repeated, a(n)
sentinel/flag/dummyvalue can be used to terminate the repetition.

e) The
sequence structureis built into Java; by default, statements execute in the order they appear.

f) Instance variables oftypes
char,
byte,
short,
int,
long,
float
and
double
are all given the
zero value by default.

g) Java is a(n)
stronglytyped language; it requires all variables to have a type.

h) If the
prefixed incrementoperator is to a variable, first the variable is incremented by 1, then its newvalue is used in the expression.

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

a) An algorithm is aprocedure for solving a problem in terms of the actions to execute and theorder in which they execute. (True)

b) A set of statementscontained within a pair of parentheses is called a block. (False)

c) A selection statementspecifies that an action is to be repeated while some condition remains true. (False)

d) A nested controlstatement appears in the body of another control statement. (True)

e) Java provides thearithmetic compound assignment operators
+=,
-=,
*=,
/=
and
%=
for abbreviatingassignment expressions. (True)

f) The primitive types (boolean,
char,
byte,
short,
int,
long,
float
and
double) areportable across only Windows platforms. (False)

g) Specifying the order inwhich statements execute in a program is called program control. (True)

h) The unary castoperator
(double)
creates a temporary integer copy of its operand. (False)

i) Instance variables oftype
boolean
are given the value
true
by default. (False)

j) Pseudocode helps youthink out a program before attempting to write it in a programming language. (True)

4.3
Write fourdifferent Java statements that each add 1 to integer variable
x.

x=x+1;

x+=1;

x++;

++x;

4.4
Write Javastatements to accomplish each of the following tasks:

a) Use one statement toassign the sum of
x
and
y
to
z, then increment
x
by 1.

Z=x++ + y;

b) Test whether variable
count
is greaterthan 10. If it is, print
"Count is greater than 10".

If (count >10)

System.out.println("Countis greater than 10");

c) Use one statement todecrement the variable
x
by 1, then subtract it from variable
total
and store theresult in variable
total.

total-=total-++x;

d) Calculate theremainder after
q
is divided by
divisor, and assign the resultto
q. Write this statement in two different ways.

q=q/divisor;

q/=divisior;

4.5
Write a Javastatement to accomplish each of the following tasks:

a) Declare variables
sum
of type
int
andinitialize it to
0.

Int sum=0;

b) Declare variables
x
of type
int
andinitialize it to
1.

Int x=1;

c) Add variable
x
to variable
sum, and assignthe result to variable
sum.

sum+=x;

d) Print
"Thesum is: ", followed by the value of variable
sum.

System.out.print(“The sumis:”+sum);

4.6
Combine thestatements that you wrote in Exercise 4.5 into a Java application thatcalculates and prints the sum of the integers from 1 to 10. Use a
while
statement toloop through the calculation and increment statements. The loop shouldterminate when the value of
x
becomes 11.

4.7
Determine thevalue of the variables in the statement
product *= x++;
after thecalculation is performed. Assume that all variables are type
int
and initiallyhave the value 5.

4.8
Identify andcorrect the errors in each of the following sets of code:

a)
while
(c <=
5)

{

product *= c;

++c;

b)
if
(gender ==1)

System.out.println("Woman");

else;

System.out.println("Man");

4.9
What is wrongwith the following
while
statement?

while
(z >=
0)

sum += z;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: