Page 149 - ComputerScience_Class_11
P. 149

Ans.  Rules for naming a variable are:
                       •  A variable name should start with a letter.
                       •  A variable name may contain any number of letters, digits and underscores.
                       •  The name should have a meaning related to the program.
                       •  There should not be any space between the letters in the name of the variable.
                       •  Variable names cannot be reserved words.
                    8.  What is the difference between Pure Arithmetic Expression and Mixed Arithmetic Expression?
                   Ans.  The difference between them is as follows:
                                     Pure Arithmetic Expression                      Mixed Arithmetic Expression
                       When  the  variables and constants in an expression  are  of  When the  variables and constants in  an expression  are  of
                       the same data types, then the expression is known as a pure  different data types, then the expression is known as a mixed
                       arithmetic expression.                            arithmetic expression.
                       For example:                                      For example:
                       double dia=34,r,pi=3.142,ar_circle;               int a=5;
                       r=34/2.0;                                         char ch='a';
                       ar_circle=pi*r*r;                                 double sum = a+ch;
                       System.out.println("Area of circle "+ar_          System.out.println("Result : " + sum);
                       circle);                                          //Output: Result : 102.0
                       //Output : Area of circle  908.038
                    9.  What are operators in Java? Write a program to demonstrate the use of arithmetic operators by performing basic mathematical
                       operations.
                   Ans.  Operators  are  the  special  symbols  that  signify  the  compiler  to  perform  some  specific  mathematical  or  non-mathematical
                       operations on one or more operands.
                       A program using arithmetic operators:
                       public class ArithmeticExample {
                           public static void main(String[] args) {
                               int a = 10;
                               int b = 5;

                               // Addition
                               int sum = a + b;
                               System.out.println("Sum: " + sum);

                               // Subtraction
                               int difference = a - b;
                               System.out.println("Difference: " + difference);

                               // Multiplication
                               int product = a * b;
                               System.out.println("Product: " + product);

                               // Division
                               int quotient = a / b;
                               System.out.println("Quotient: " + quotient);
                           }




                                                                      Primitive Values, Wrapper Classes, Types and Casting  147
   144   145   146   147   148   149   150   151   152   153   154