Page 132 - ComputerScience_Class_11
P. 132

3. import java.util.Scanner;
                    class Factorial {
                        public static void main(String[] args) {
                            Scanner sc = new Scanner(System.in);
                            System.out.print("Enter a number: ");
                            int num = sc.nextInt();
                            int factorial = 1;
                            for (int i = 1; i <= num; i++) {
                                factorial * i;
                            }
                            System.out.println("Factorial of " + num + " is: " + factorial);
                        }
                    }
                Ans.  import java.util.Scanner;
                    class Factorial {
                        public static void main(String[] args) {
                            Scanner sc = new Scanner(System.in);
                            System.out.print("Enter a number: ");
                            int num = sc.nextInt();
                            int factorial = 1;
                            for (int i = 1; i <= num; i++) {
                                factorial *= i;
                            }
                            System.out.println("Factorial of " + num + " is: " + factorial);
                        }
                    }

                   Unsolved Questions


              A.  Tick ( ) the correct option.

                  1.  Which of the following is a function or method that has the same name as the class?
                    a.  Variable                                    b.  Constructor
                    c.  Method                                      d.  Object
                  2.  If there is any error in an arithmetic operation, then …………………. error is thrown.
                    a.  IOException                                 b.  NullPointerException
                    c.  ArithmeticException                         d.  None of these
                  3.  The interpreter converts high-level program statements into …………………. .
                    a.  machine language                             b.  assembly language
                    c.  Both a and b                                 d.  None of these
                  4.  When the …………………. statement executes, the nearest catch statement is checked for checking what type of exception has
                    thrown.
                    a.  try                                         b.  catch
                    c.  throw                                       d.  finally
                  5.  A Java Virtual Machine (JVM) converts Java bytecode to …………………. so that it could be executed.
                    a.  Java programs                               b.  Machine-dependent code
                    c.  Programming languages                       d.  Java bytecode




                  130  Touchpad Computer Science (Ver. 3.0)-XI
   127   128   129   130   131   132   133   134   135   136   137