Java Questions 1 - 10   «Prev  Next»

Java Questions 6

  1. Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer.
    A. public static int main(char args[]) 
    B. public static void main(String args[]) 
    C. public static void MAIN(String args[]) 
    D. public static void main(String args) 
    E. public static void main(char args[]) 
    


    Answer: B
    public static void main(String [] args)

  2. What is the output when the following code is compiled and run?
    Select the three correct answers.
    public class Xyz {
     public static void main(String args[]) {
      for(int i = 0; i < 2; i++) {
       for(int j = 2; j>= 0; j--) {
        if(i == j) break;
         System.out.println("i=" + i + " j="+j);
        }
       }
      }
    }
    

    a.	i=0 j=0 
    b.	i=0 j=1 
    c.	i=0 j=2 
    d.	i=1 j=0 
    e.	i=1 j=1 
    f.	i=1 j=2 
    g.	i=2 j=0 
    h.	i=2 j=1 
    i.	i=2 j=2 
    


    Answer:c, b, f : In that order.

  3. What gets printed when the following code is compiled and run with the following command
    java Test 2 
    
    Select the one correct answer.
    public class Test {
     public static void main(String args[]) { 
      Integer intObj=Integer.valueOf(args[args.length-1]);
       int i = intObj.intValue();
       if(args.length > 1) 
        System.out.println(i);
       if(args.length > 0)
        System.out.println(i - 1);
       else 
        System.out.println(i - 2);
     }
    }
    

    A.	Test 
    B.	Test -1 
    C.	0 
    D.	1 
    E.	2 
    


    Answer: D

  4. In Java, what expression can be used to represent the number of elements in an array named arr ?

    Answer:
    arr.length

  5. Java Virtual Machine
  6. How would the number 5 be represented in hex using up-to four characters?

    Answer:
    In Decimal base 10 the number 5 is also 5 in Hex base 16.
    Decimal number (base 10) 5 is in Hexadecimal (base 16) also 5.

  7. Which of the following is a Java keyword. (Select the four correct answers.)
    a.	extern 
    b.	synchronized 
    c.	volatile 
    d.	friend 
    e.	friendly 
    f.	transient 
    g.	this 
    h.	then 
    


    Answer: b, c, f, g
    See the following link: Java Keywords and identifiers

  8. Is the following statement true or false.
    The constructor of a class must not have a return type.
    A. true
    B. false


    Answer:
    True
    If it has a return type, it is not a constructor, it is a method.

  9. What is the number of bytes used by Java primitive long?
    Select the one correct answer.
    The number of bytes is compiler dependent.
    a. 2
    b. 4
    c. 8
    d. 64

    Answer: d
    Explanation: The data type long supports 64-bit integer values whose values range from -(263) to 263 - 1.

  10. What is returned when the method substring (2, 4) is invoked on the string "example"?
    Include the answer in quotes as the result is of type String.

    Answer:
    public class Test1 {
     public static void main(String[] args) {
      String s1=  "example";
      System.out.println(s1.substring (2, 4));
     s}
    }
    

    "am"

  11. Which of the following is correct? Select the two correct answers.
    1. The native keyword indicates that the method is implemented in another language like C/C++.
    2. The only statements that can appear before an import statement in a Java file are comments.
    3. The method definitions inside interfaces are public and abstract. They cannot be private or protected.
    4. A class constructor may have public or protected keyword before them, nothing else.


    Answer: a, c
    Explanation:
    a) The native keyword is applied to a method to indicate that the method is implemented in native code using JNI.
    b) The method definitions inside an interface are public and abstract methods.
    3) Yes, a constructor can be private. There are different uses of this.
    One such use is for the singleton design anti-pattern, which I would advise against you using.
    Another, more legitimate use is in delegating constructors; you can have one constructor that takes many different options that is really an implementation detail, so you make it private, but then your remaining constructors delegate to it.