Java Assignment
Perform the following problems in Exercises, Problems and Projects
Complete 1-3, 39 and 44
1. Given the following code declaring and initializing two int variables a and b with respective values 3 and 5, indicate whether the value of each expression is true or false.
|
Expression |
true |
false |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
2. Given the following code declaring and initializing three boolean variables a, b, and c, with respective values true, true, and false, indicate whether the value of each expression is true or false.
|
Expression |
true |
false |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
|
|
____ |
____ |
3. Given two boolean variables a and b, are the following expressions equivalent?
! ( !a )
a
39. You coded the following in class Test.java:
At compile time, you get the following error:
Explain what the problem is and how to fix it.
44. Write a program that calculates the area of the following figures:
a square of side 0.666666667
a rectangle of sides and 4
Test the two calculated areas for equality; discuss your result.
Part B
Perform the following problems in Exercises, Problems and Projects
Complete 1-3, 5, and 8.
1 How do you discover that you have an infinite loop in your code?
The code does not compile.
The code compiles and runs but gives the wrong result.
The code runs forever.
The code compiles, but there is a run-time error.
2 If you want to execute a loop body at least once, what type of loop would you use?
for loop
while loop
do/while loop
none of the above
3 What best describes a for loop?
It is a count-controlled loop.
It is an event-controlled loop.
It is a sentinel-controlled loop.
5. What is the output of this code sequence? (The user successively enters 3, 5, and −1.)
8. What are the values of i and sum after this code sequence is executed?
Part A
P
erform the following problems in E
xercises, Problems and Projects
Complete 1
-
3, 39 and 4
4
1.
Given the following code declaring and initializing two
int
variables
a
and
b
with respective values
3 and 5, indicate whether the value of each expression is
true
or
false
.
Expression
true
false
a < b
____
____
a
!= b
____
____
a == 4
____
____
(
b
-
a
)<= 1
____
____
Math.abs(
a
-
b
) >= 2
____
____
(b % 2
== 1
)
____
____
b <= 5
____
____
2.
Given the following code declaring and initializing three
boolean
variables
a, b
, and
c
, with
respective values
true, true
, and
false
, indicate whether the value of each expression is
true
or
false
.
Expression
true
false
Part A
Perform the following problems in Exercises, Problems and Projects
Complete 1-3, 39 and 44
1. Given the following code declaring and initializing two int variables a and b with respective values
3 and 5, indicate whether the value of each expression is true or false.
Expression true false
a < b ____ ____
a != b ____ ____
a == 4 ____ ____
( b - a )<= 1 ____ ____
Math.abs( a - b ) >= 2 ____ ____
(b % 2 == 1 ) ____ ____
b <= 5 ____ ____
2. Given the following code declaring and initializing three boolean variables a, b, and c, with
respective values true, true, and false, indicate whether the value of each expression is true or false.
Expression true false