JAVA MCQs-2




  1. Which of the following is an incorrect way of initializing a variable in Java?
    1. Different line declaration and initialization
      int a,b;
      a=1;
      b=2;
    2. Single line declaration and initialization (One at a each line)
      int a = 1;
      int b = 2;
    3. Single line declaration and initialization (One line multiple variable)
      int a=l,b=2;
    4. Single line declaration and initialization (One line multiple variable)
      int a, b = 1, 2;

  2. Which of the following is an invalid method overloading?
    1. static double add (int a, double b) {return a*b;}
      static double add (double a, int b) {return a*b; }
    2. static int add (int a, int b) {return a*b; }
      static int add (int a, int b, int c) {return a*b*c; }
    3. static int add (int a, int b) {return a*b; }
      static double add (int a, int b, double c) (return a*b*c;}
    4. static int add (int a,int b) {return a*b; }
      static double add (int a, int b) {return a*b; }

  3. Which of the following is TRUE regarding the string array provided as a parameter to the main method in Java?
    1. It can be used to get command line arguments from the user.
    2. It is mandatory to name the string array as 'args'.
    3. Accessibility of the values provided as command line argument is restricted only to the main method.
    4. Only one command line argument input is allowed at a time.

  4. Which of the following can be used to take input from user during the execution of a program?
    1. Using the string array provided as a parameter to the main method.
    2. getText() method can be used to get user input from the command line.
    3. Scanner class can be used by passing the predefined object System.in
    4. Once the execution starts, there is no way to provide user input

  5. Which of the following is/are TRUE about print and println methods?
    1. print() prints in a single line only and multiple lines cannot be printed in any way.
    2. println() prints and then appends a line break
    3. println() prints in a single line only and multiple lines cannot be printed.
    4. print() prints and then appends a line break
  6. Which of the following is called when a method having the same name as that the name of the class where it is defined?
    1. abstract
    2. this
    3. constructor
    4. final
  7. Which of the following is NOT true about a method in a class?
    1. A method can be defined recursively
    2. A method can be defined without any statement in it
    3. constructor is a special kind of method, which can be defined recursively
    4. A method can be defined with variable number of arguments passed into it


  8. Consider the following program.

    Public class Questions {
    public static void main (String args[]){
    char a='2';
    int b=8;
    System.out.println (a+b);
    }
    }

    What will be the output of the above program ?
    1. 10
    2. 58
    3. 82
    4. 106

  9. What will happen during the execution of the following code for the command line input?

    Public class Questions {
    public static void main (String args[]){
    System.out.println (args[5]);
    }
    }

    Consider the following input on command line and select the options with the correct output
    Input:
    A I love my country
    B 1 2 3 4 5 6
    C I love my country india
    D 12345


    a.A ArrayIndexOutOfBounds Exception
    b. B:6
    C C: india
    d D: 12345
    1. a
    2. b
    3. c
    4. d

  10. Which of the following is a Class in Java?
    1. int
    2. String
    3. short
    4. double

No comments:

Post a Comment