Page 177 - ComputerScience_Class_11
P. 177

While using operators, we must first understand what the operands are. The operands are the objects on which an operation is
                       performed. In a programming language, an operand can be a variable or a constant.
                        Suppose,
                        a + b = 5;
                        Here,
                        •  a and b are operands.
                        •  + and = are operators.
                        •  5 is a constant.
                    7.  Explain the precedence and associativity of operators in Java with an example.
                   Ans.  Operator precedence refers to the priority of operators in an expression.
                        An operator with higher precedence is evaluated before an operator with lower precedence.
                        For example, multiplication (*), division (/) and modulus (%) have higher precedence than addition (+) and subtraction (-).
                        Similarly, relational operators are evaluated before logical operators.
                        Operator associativity is used when two or more operators of the same precedence appear in an expression.
                        It decides whether the evaluation will take place from left to right or right to left.
                        Most arithmetic operators such as +, -, * and % follow left-to-right associativity.

                       int m = 10, n = 20, p = 12, q = 4;
                       int e;
                       e = m - n * (q % p) + 10 * q;
                    8.  Explain the concept of a final variable in Java with an example.
                   Ans.  Variables can be made fixed only. This is done by the keyword “final”. When the keyword final is used, the value in the variable
                       must be assigned and it will be fixed in the whole program.

                       class FinalExample {
                           public static void main(String[] args) {
                               final int marks = 100;
                               System.out.println("Marks: " + marks);
                       // Error: cannot assign a value to final variable //'marks'
                               //marks = 120;
                           }
                     }

                 D.  Higher Order Thinking Skills (HOTS)
                    1.  You are tasked with designing a program to calculate the maximum of three numbers a = 12, b = 15 and c = 10 using a nested
                       ternary (conditional) operator. How would you write this in Java? What is the output when the values of a, b and c are given?
                   Ans.  The ternary operator can be used as follows:
                        int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
                        •  Logic:
                         °   First, compare a and b. If a > b, compare a with c to find the largest.
                         °   If a <= b, compare b with c to find the largest.
                        Given a = 12, b = 15 and c = 10:
                        •  a > b is false, so we move to compare b and c.
                        •  b > c is true, so b is the largest.
                        Thus, the output is:
                        •  Max value: 15
                    2.  You are working with a shift operator to manipulate data in a program. If you have an integer num = 32, you need to shift the bits
                       of num 3 positions to the left and 2 positions to the right. What will be the results of these shift operations?
                   Ans.  •  Left Shift (num << 3):
                         °   num = 32, in binary: 100000
                         °   Shifting the bits 3 positions to the left: 1000000000, which is 256 in decimal.


                                                                                            Variables and Expressions  175
   172   173   174   175   176   177   178   179   180   181   182