Page 115 - CA_Blue( J )_Class9
P. 115

11.  Rewrite the following using ternary operator:
                      if(x % 2 == 0)
                           System.out.print("EVEN");
                       else
                           System.out.print("ODD");
                   Ans.  System.out.print(((x % 2 == 0)? "EVEN":"ODD"));
                    12.  Give the output of the following expression:
                       a += a++ + ++a + --a + a--; when a = 7.
                   Ans.  a  = a + a++ + ++a + --a + a--;
                       = 7 + 7 + 9 + 8 + 8
                       = 14 + 9 + 8 + 8
                       = 23 + 8 + 8
                       = 31 + 8
                       = 39
                    13.  What are the values of x and y when the following statements are executed?
                       int a = 63, b = 36;
                       boolean x = (a > b)? true : false;
                       int y = (a < b)? a : b;
                   Ans.  x = true
                     y = 36
                     [Explanation: x=63>36, so true
                       Y=63<36, so false]
                    14.  State the values of n and ch.
                      char c = 'A';
                       int n = c + 1;
                       char ch = (char)n;
                   Ans.  n = 66  , ch = ‘B’
                      [Explanation: ASCII value of ‘A’ is 65,
                          n = 65+1            (implicit conversion)
                          ch = B              (explicit conversion)
                 D.  Picture Study.

                                            FORMS OF OPERATOR





                            UNARY                 BINARY                 TERNARY

                    1.  A++ uses which of the following operator?
                        a.  Unary                                      b.  Binary
                        c.  Ternary                                    d.  None of these
                    2.  An operator that works with two operands is called ___________ operator.
                        a.  Unary                                      b.  Binary
                        c.  Ternary                                    d.  True
                    3.  String s = (5>5)? “5 is not greater than 5”: “5 is equal to 5”.
                        a.  true                                       b.  5 is not greater than 5
                        c.  5 is equal to 5                            d.  False





                                                                                               Operators in Java   113
   110   111   112   113   114   115   116   117   118   119   120