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: 3846173 • 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. What will be displayed by the following code block?

A. apple, orange, pear

B. orange, pear

C. pear

D. apple, orange

5. 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

6. 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;
}

Explanation / Answer

1.    To test for equality between two values, use the == operator.
  
   For eg: if(a == b) //Compares variables a and b for equality


2.    Opening and Closing braces can be omitted if they enclose a single statement in an if construct, but doing so is error-prone.
      
   For eg:
  
   if(a >= 10)
   {
       b = a + 5;
   }
  
   This if statement can be re-written as:
  
   if(a >= 10)
       b = a + 5;
      
   However, eliminating braces leads to confusion while working with complex codes.
  

3.    Expression: if (number % 2 == 0){
  
   Operator % is used for finding modulus. This expression checks for values that are divisible by 2
  

5.   Value of the variable income is 4001. First if statement if (income > 3000) is executed and the expression results in true (4001 > 3000).   
   Hence output will be: Income is greater than 3000
  
  
6.   Observe that in switch case, break statements are omitted. If a break statement is not present, starting from matched case, remaining all case statements including default gets executed.

   Value of x is initially 3 and value of y is 4.
  
   (i)    x + 3 evaluates to 6
   (ii)   First case statement is matched and updates the value of y to 0
   (iii)   As break statement is not present, next case statement (case 7) also gets executed and updated value of y to 1
   (iv)   Default statement gets executed and updates value of y to 2 (1+1).
  
Hence value of y after switch statement execution is 2