1. What is the value of each variable after the if statement? a. int n = 1; int
ID: 3539707 • Letter: 1
Question
1.
What is the value of each variable after the if statement?
a. int n = 1; int k = 2; int r = n;
if (k < n) { r = k; }
b. int n = 1; int k = 2; int r;
if (n < k) { r = k; }
else { r = k + n; }
c. int n = 1; int k = 2; int r = k;
if (r < k) { n = r; }
else { k = n; }
d. int n = 1; int k = 2; int r = 3;
if (r < n + k) { r = 2 * n; }
else { k = 2 * r; }
2.
Explain the difference between
s = 0;
if (x > 0) { s++; }
if (y > 0) { s++; }
and
s = 0;
if (x > 0) { s++; }
else if (y > 0) { s++; }
3.
Find the errors in the following if statements.
a. if x > 0 then System.out.print(x);
b. if (1 + x > Math.pow(x, Math.sqrt(2)) { y = y + x; }
c. if (x = 1) { y++; }
d. x = in.nextInt();
if (in.hasNextInt())
{
sum = sum + x;
}
else
{
System.out.println("Bad input for x");
}
e. String letterGrade = "F";
if (grade >= 90) { letterGrade = "A"; }
if (grade >= 80) { letterGrade = "B"; }
if (grade >= 70) { letterGrade = "C"; }
if (grade >= 60) { letterGrade = "D"; }
4
What do these code fragments print?
a. int n = 1;
int m = -1;
if (n < -m) { System.out.print(n); }
else { System.out.print(m); }
b. int n = 1;
int m = -1;
if (-n >= m) { System.out.print(n); }
else { System.out.print(m); }
c. double x = 0;
double y = 1;
if (Math.abs(x - y) < 1) { System.out.print(x); }
else { System.out.print(y); }
d. double x = Math.sqrt(2);
double y = 2;
if (x * x == y) { System.out.print(x); }
else { System.out.print(y); }
5 Suppose x and y are variables of type double. Write a code fragment that sets y to x if x
is positive and to 0 otherwise.
3.6 Suppose x and y are variables of type double. Write a code fragment that sets y to the
absolute value of x without calling the Math.abs function. Use an if statement.
3.7 Explain why it is more difficult to compare floating-point numbers than integers.
Write Java code to test whether an integer n equals 10 and whether a floating-point
number x is approximately equal to 10.
3.8 It is easy to confuse the = and == operators. Write a test program containing the
statement
if (floor = 13)
What error message do you get? Write another test program containing the
statement
count == 0;
What does your compiler do when you compile the program?
Explanation / Answer
1.
a.
Consider the code segment:
int n = 1;
int k = 2;
int r = n;
if (k < n)
{
r = k;
}
Here if statement is fail, so the values of each variable remain same.
The value are n=1, k=2, and r=1.
b.
int n = 1;
int k = 2;
int r;
if (n < k)
{
r = k;
}
else
{
r = k + n;
}
Here if statement is satisfied, so the values of each variable are n=1, k=2, and r=2.
c.
int n = 1;
int k = 2;
int r = k;
if (r < k)
{
n = r;
}
else
{
k = n;
}
Here else statement is executed, so values of each variable are n=1, k=1, and r=2.
d.
int n = 1;
int k = 2;
int r = 3;
if (r < n + k)
{
r = 2 * n;
}
else
{
k = 2 * r;
}
Here else statement is executed, so values of each variable are n=1, k=6, and r=3.
2.
Consider the two code segments:
Code segment1:
s = 0;
if (x > 0)
{
s++;
}
if (y > 0)
{
s++;
}
And
Code segment2:
s = 0;
if (x > 0)
{
s++;
}
else if (y > 0)
{
s++;
}
In the code segment1, s can be incremented two times. If both the values of x and y are greater than 1, the statement s++ is executed twice.
In the code segment2, s can be incremented only once, either in if statement or in else statement.
3.
a.
Consider the error statement:
if x > 0 then System.out.print(x);
There are two errors in the statement:
1. Braces are missing for the if statement x>0.
2. then is not involved in the syntax of if statement in Java.
Correct statement would be:
if (x > 0)
{
System.out.print(x);
}
b.
Consider the statement:
if (1 + x > Math.pow(x, Math.sqrt(2))
{
y = y + x;
}
Here Braces are not properly balanced at the end of the if condition.
So the correct statement would be
if (1 + x > Math.pow(x, Math.sqrt(2)))
{
y = y + x;
}
c.
Consider the code segment:
if (x = 1)
{
y++;
}
In the if statement assign statement is used instead of comparison statement.
So the correct statement is:
if (x ==1)
{
y++;
}
d.
Consider the code segment:
x = in.nextInt();
if (in.hasNextInt())
{
sum = sum + x;
}
else
{
System.out.println("Bad input for x");
}
Here code is to validates the input first, then read the variable. Instead of reads the integer into the body of the if statement is as follows:
if (in.hasNextInt())
{
x = in.nextInt();
sum = sum + x;
}
else
{
System.out.println("Bad input for x");
}
e)
The if statements should be the order of if-else if-else. More than one if statement will be executed for any grade higher than 60 and the letter grade will be wrongly assigned.
4.
a.
The code segment prints -1, because if statement is satisfied so the value of n is printed.
b.
The code segment prints 1, because if statement is satisfied so the value of n is printed.
c.
The code segment prints 1.0, because else statement is executed so the value of y is printed.
d.
The code segment prints 2.0, because else statement is executed so the value of y is printed.
5.
Code segment is as follows:
//if x is positive
if (x > 0.0)
{
//set y to x
y = x;
}
//otherwise
else
{
//set 0
y = 0.0;
}
3.6
Code fragment is as follows:
//set the absolute value
if (x < 0.0)
{
y = -x;
}
else
{
y = x;
}
3.7
Floating-point numbers only have limited precision, so it’s very likely that any mathematical operation (like addition, multiplication, etc.) will introduce small errors into the result.
To check to see if an integer equals 10:
To check to see if a floating-point number approximately equals 10:
3.8
In the first case when comparing if (floor = 13)
Error is “Type mismatch: cannot convert from int to boolean".
In the statement count == 0;
Error is :“Syntax error on token “==”, invalid assignment operator”.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.