Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. To test for equality between two values, use the ________ operator. 2. ______

ID: 3846172 • Letter: 1

Question

1. To test for equality between two values, use the ________ operator.

2. ________ can be omitted if they enclose a single statement in an if construct, but doing so is error-prone.

3. Complete the following if statement to test whether the expression number modulo 2 is equal to 0 (the variable name is number). Fill in the expression between the parentheses, use one space before the operator, after the operator, before the relational operator, and after the relational operator, with no other spaces or parentheses.

if (__________){

4. Complete the following if statement to test whether the radius of a circle is positive (assume the variable name is radius). Fill in the expression between the parentheses, use one space before the the relational operator, and one space after the relational operator, with no other spaces.

if (__________){

5. What will be displayed by the following code block?

A. apple, orange, pear

B. orange, pear

C. pear

D. apple, orange

6. If the variable income has the value is 4001, what is the output of the following code?

if (income > 3000) {

  System.out.println("Income is greater than 3000");

}

else if (income > 4000) {

  System.out.println("Income is greater than 4000");

}

A. no output

B. Income is greater than 3000

C. Income is greater than 3000 followed by Income is greater than 4000

D. Income is greater than 4000

E. Income is greater than 4000 followed by Income is greater than 3000

7. Assume x = 4 and y = 5, which of the following is true?

A. x < 5 && y < 5

B. x < 5 || y < 5

C. x > 5 && y > 5

D. x > 5 || y > 5

8. What is y after the following switch statement is executed?

int x = 3, y = 4;
switch (x + 3) {
    case 6: y = 0;
    case 7: y = 1;
    default: y += 1;
}

9. What is y after the following statement is executed?

x = 0;
y = (x > 0) ? 10 : -10;

Explanation / Answer

NOTE: Image for 5th question is not visible. So i cannot answer the question

1. To test for equality between two values, use the ________ operator.
Ans) '==' operator. It is used to test equality between two values.

2. ________ can be omitted if they enclose a single statement in an if construct, but doing so is error-prone.
Ans) {} can be omitted. We can omit flower braces if there is a single statement for if condition but it is not encouraged as it is error prone and not readable.


3. Complete the following if statement to test whether the expression number modulo 2 is equal to 0 (the variable name is number). Fill in the expression between the parentheses, use one space before the operator, after the operator, before the relational operator, and after the relational operator, with no other spaces or parentheses.
if (__________){
Ans) if (number % 2 == 0)


4. Complete the following if statement to test whether the radius of a circle is positive (assume the variable name is radius). Fill in the expression between the parentheses, use one space before the the relational operator, and one space after the relational operator, with no other spaces.
if (__________){
Ans) if(radius > 0)

5. What will be displayed by the following code block?
xid-34590758_1
A. apple, orange, pear
B. orange, pear
C. pear
D. apple, orange
Ans) Image not visible. So cannot answer the question.

6. If the variable income has the value is 4001, what is the output of the following code?
if (income > 3000) {
System.out.println("Income is greater than 3000");
}
else if (income > 4000) {
System.out.println("Income is greater than 4000");
}
A. no output
B. Income is greater than 3000
C. Income is greater than 3000 followed by Income is greater than 4000
D. Income is greater than 4000
E. Income is greater than 4000 followed by Income is greater than 3000
Ans) B
As income value is 4001 the first 'if' condition "income>3000" matches and prints output "Income is greater than 3000". Even though other if condition also is true but will match only if the first 'if' condition is false. The thumb rule is "else if" statement executes only if the 'if' condition before it is false.

7. Assume x = 4 and y = 5, which of the following is true?
A. x < 5 && y < 5
B. x < 5 || y < 5
C. x > 5 && y > 5
D. x > 5 || y > 5
Ans) B
Because in the condition "x<5 || y<5" the condition "x<5" holds true. As it is an OR expression if one of the condition is true the entire expression evaluates to true.

8. What is y after the following switch statement is executed?
int x = 3, y = 4;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
Ans) y value is '2'
If you see the switch statement makes x+3 expression as 6 with x value being 3. The first case statement "case 6" matches which makes y to 0. Then it moves to other case statement and assigns y to 1 even though case statement does not match. After that it moves to default y statement and adds 1 to it. Thats why the value of y is 2.

The reason why other case statement matches is because there is no break statement for each of the case statement. So it is always good to put a break statement in every case statement so that we will not have wrong case statement executes.

9. What is y after the following statement is executed?
x = 0;
y = (x > 0) ? 10 : -10;
Ans) y value is -10
with x value as 0 and 0>0 is false, in the conditional operator -10 will be assigned to y