Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
44-141 Exercise 4
1.
What is the value of each of the following expressions:
(a) (3 > 6 && 7 > 4)
false
(b) (4 > 6 || 10 < 2 * 6)
true
(c) (7 >= 3 + 4 || 6 < 4 && 2 < 5) true
(d) !(5 <= 4 || 6 != 5 && 10 >= 4)
false
1 point each
2.
Assume we have declared and initialized the variables
below:
int x =
int y =
int z =
boolean
boolean
5;
3;
2;
a = true;
b = false;
What is the value of each of the following expressions:
(a)
(x – z == y) true
(b)
(x * z > z * y || b) true
(c)
(x * z < z * y && a) false
(d)
(x * z > z * y && a || b) true
(e)
!(x * z > z * y && a || b) false
1 point each
3.
Assume x and y are variables of type int.
phrase into an equivalent boolean expression:
(a)
x is less than 20
_________x < 20____________________
(b)
x is between 1 and 100 (inclusive)
__x > 1 && x <= 100_______________
(c)
y is either 1 or 5 or 10
___y == 1 || y == 5 || y==10______
(d)
Both x and y are positive
____x > 0 && y >0________________
(e)
Neither x nor y is positive
____ x < 0 && y <0__________________
Translate each
1 point each
4.
Assume the following variables have been declared:
boolean isVoter, validExamScore, evenNumber;
int age, examScore, number;
What value is stored in the boolean variable after the execution
of each set of statements:
(a)
age = 30;
isVoter = age <= 18;
false
(b)
age = 16;
isVoter = age <= 18;
true
(c)
number = 11;
evenNumber = number % 2 == 0;
false
(d)
number = 4;
evenNumber = number % 2 == 0;
true
(e)
examScore = 60;
validExamScore = examScore >= 0 && examScore <= 100;
true
(f)
examScore = 110;
validExamScore = examScore >= 0 && examScore <= 100;
false
1 point each
Find the output of each of the code segments below:
5.
int a = 10;
if( a > 10)
{
System.out.println("bigger than 10");
}
else
{
System.out.println ("smaller or equal to 10");
}
System.out.println("statement is printed after if");
Output:
smaller or equal to 10
statement is printed after if
2 points (1 per line)
6. Assume the variable number has been declared to be of type int.
Use the code segment to answer the following questions.
number = 1;
if (number < 100)
{
number = number * 2;
}
else if (number > 500)
{
number = number * 3;
}
else
{
number = number * 5;
}
System.out.print("Number: ");
System.out.println(number);
Determine the output assuming number contains each of the following
values (instead of the number=1):
(a)
(b)
(c)
number = 90;
number = 1000;
number = 200;
Output:
Output:
Output:
Number: 180
Number: 3000
Number: 1000
3 points (1 each)
7. Given the int variables x, y and z, where x is 3, y is 7, and z
is 6, what is the output for each of the following Java code
segments?
(a)
if (x < 3)
System.out.println("The value of x is " );
System.out.println( x );
System.out.print("The value of x is ");
System.out.println( x );
Output:
3
The value of x is 3
2 points (1 each line)
(b)
if (x != 1)
{
System.out.print("The
System.out.println( x
}
else
{
System.out.print("The
System.out.println( y
}
Output:
The value of x is 3
2 points
value of x is " );
);
value of y is " );
);
(c)
if (y + 3 < x + z)
{
System.out.print("The
System.out.println( x
System.out.print("The
System.out.println( z
}
else
{
System.out.print("The
System.out.println( y
}
value of x is " );
);
value of z is " );
);
value of y is " );
);
Output:
The value of y is 7
2 points
8. Evaluate the following Java code segments and determine the
output.
(a)
Given that examScore contains 60, determine the output
if (examScore >= 60)
{
System.out.println("pass" );
}
else
{
System.out.println("fail" );
}
Output:
pass
1 point
(b)
Given that examScore contains 59, determine the output
if (examScore >= 60)
{
System.out.println("pass" );
}
else
{
System.out.println("fail" );
}
Output:
fail
1 point
9. Evaluate the following Java code segments and determine the
output.
(a)
int sales = 10000;
int target = 12000;
float bonus;
if (sales <= target)
{
System.out.println("Needs improvement" );
bonus = 0.0f;
System.out.print("Your bonus is " );
System.out.println( bonus );
}
else
{
System.out.println("Great performance! " );
bonus = .20f * sales;
System.out.print("Your bonus is " );
System.out.println( bonus );
}
Output:
Needs improvement
Your bonus is 0.0
2 points
(b)
int sales = 30000;
int target = 20000;
float bonus;
if (sales <= target)
{
System.out.println("Needs improvement" );
bonus = 0.0f;
System.out.print("Your bonus is " );
System.out.println( bonus );
}
else
{
System.out.println("Great performance! " );
bonus = .20f * sales;
System.out.print("Your bonus is " );
System.out.println( bonus );
}
Output:
Great performance!
Your bonus is 6000.0
2 points
2.
Evaluate the output of the following if statement.
if (average >= 70)
{
System.out.println("Satisfactory" );
}
else if (average >= 60)
{
System.out.println("Needs improvement" );
}
else
{
System.out.println( "Failing" );
}
Determine the output assuming average contains each of the
following values:
(a)
(b)
(c)
average = 59;
average = 75
average = 65
Output:
Output: Output:
Failing
3 points (1 each)
Satisfactory
Needs improvement
10. The following Java code segment contains a cascaded if statement.
if (n == 3)
System.out.println ( “Three”);
else if (n == 4)
System.out.println( “Four”);
else if (n == 5)
System.out.println( “Five”);
else
System.out.println( “All other numbers” );
Determine the output assuming n contains each of the following
values:
(a)
(b)
(c)
n = 3;
n = 4
n = 10
Output:
Output: Output:
Three
3 points (1 each)
Four
All other
numbers
11. Assume the variables b, c, and d have been declared to be of
type int. Find the output of each of the Java code segments
Output:
below.
1.
3
1
1
1
2.
4
1
1
1
1
b = 20;
while( b > 0 )
{
System.out.print( "b = ");
System.out.println( b );
b = b - 6;
}
System.out.println( "Done" );
b = 20
b = 14
b = 8
b = 2
Done
points:
for the “decrease by 6” sequence,
for terminating at proper value,
for “Done”
c = 0;
while( c < 25 )
{
c = c + 2;
if( c > 10 )
{
c = c + 5;
}
System.out.print( "c = " );
System.out.println( c );
}
points:
for the “increase by 2” sequence,
for proper transition to increase by 5,
for increasing by 5,
for terminating at proper value,
Output:
c
c
c
c
c
c
c
c
=
=
=
=
=
=
=
=
2
4
6
8
10
17
24
31