Java quiz questions

Java interview questions

  • 1.

    Which of the following class level (nonlocal) variable declarations will not compile?

    1. protected int a;

    2. transient int b = 3;

    3. private synchronized int e;

    4. volatile int d;

    Answer
  • 2.

    At what point is the Bar object, created on line 6, eligible for garbage collection?

    class Bar { } 
    class Test 
    {  
        Bar doBar() 
        {
            Bar b = new Bar(); /* Line 6 */
            return b; /* Line 7 */
        } 
        public static void main (String args[]) 
        { 
            Test t = new Test();  /* Line 11 */
            Bar newBar = t.doBar();  /* Line 12 */
            System.out.println("newBar"); 
            newBar = new Bar(); /* Line 14 */
            System.out.println("finishing"); /* Line 15 */
        } 
    }
    1. after line 12

    2. after line 14

    3. after line 7, when doBar() completes

    4. after line 15, when main() completes

    Answer
  • 3.

    What will be the output of the program?

    String d = "bookkeeper";
    d.substring(1,7);
    d = "w" + d;
    d.append("woo");  /* Line 4 */
    System.out.println(d);
    1. wookkeewoo

    2. wbookkeeper

    3. wbookkeewoo

    4. Compilation fails.

    Answer
  • 4.

    What will be the output of the program?

    class MyThread extends Thread 
    {
        MyThread() 
        {
            System.out.print(" MyThread");
        }
        public void run() 
        {
            System.out.print(" bar");
        }
        public void run(String s) 
        {
            System.out.println(" baz");
        }
    }
    public class TestThreads 
    {
        public static void main (String [] args) 
        {
            Thread t = new MyThread() 
            {
                public void run() 
                {
                    System.out.println(" foo");
                }
            };
            t.start();
        }
    }
    1. foo

    2. MyThread foo

    3. MyThread bar

    4. foo bar

    Answer
  • 5.

    Which of the following statements is true?

    1. If assertions are compiled into a source file, and if no flags are included at runtime, assertions will execute by default.

    2. As of Java version 1.4, assertion statements are compiled by default.

    3. With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.

    4. When evaluating command-line arguments, the VM gives -ea flags precedence over -da flags.

    Answer
  • 6.

    Which is true about an anonymous inner class?

    1. It can extend exactly one class and implement exactly one interface.

    2. It can extend exactly one class and can implement multiple interfaces.

    3.  It can extend exactly one class or implement exactly one interface.

    4. It can implement multiple interfaces regardless of whether it also extends a class.

    Answer
  • 7.

    Which statement is true about a static nested class?

    1. You must have a reference to an instance of the enclosing class in order to instantiate it.

    2. It does not have access to nonstatic members of the enclosing class.

    3. It's variables and methods must be static.

    4. It must extend the enclosing class.

    Answer
  • 8.

    Which of the following statements about the hashcode() method are incorrect?

    1. The value returned by hashcode() is used in some collection classes to help locate objects.
    2. The hashcode() method is required to return a positive int value.
    3. The hashcode() method in the String class is the one inherited from Object.
    4. Two new empty String objects will produce identical hashcodes.
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. 1 and 4

    Answer
  • 9.

    What will be the output of the program?

    try 
    { 
        int x = 0; 
        int y = 5 / x; 
    } 
    catch (Exception e) 
    {
        System.out.println("Exception"); 
    } 
    catch (ArithmeticException ae) 
    {
        System.out.println(" Arithmetic Exception"); 
    } 
    System.out.println("finished");
    1. finished

    2. Exception

    3. Compilation fails.

    4. Arithmetic Exception

    Answer
  • 10.

    When finally block gets executed?

    1. Always when try block get executed, no matter exception occured or not.

    2. Always when a method get executed, no matter exception occured or not.

    3. Always when a try block get executed, if exception do not occur.

    4. Only when exception occurs in try block code.

    Answer
  • 11.

    What is runtime polymorphism?

    1. Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

    2. Runtime polymorphism is a process in which a call to an overloaded method is resolved at runtime rather than at compile-time.

    3. Both of the above.

    4. None of the above.

    Answer
  • 12.

    What happens when thread's yield() method is called?

    1. Thread returns to the ready state.

    2. Thread returns to the waiting state.

    3. Thread starts running.

    4. None of the above.

    Answer
  • 13.

    When static binding occurs?

    1. Static binding occurs during Compile time.

    2. Static binding occurs during load time.

    3. Static binding occurs during runtime.

    4. None of the above.

    Answer
  • 14.

    What is JRE?

    1. JRE is a java based GUI application.

    2. JRE is an application development framework.

    3. JRE is an implementation of the Java Virtual Machine which executes Java programs.

    4. None of the above.

    Answer
  • 15.

    What is Abstraction?

    1. Abstraction is a technique to define different methods of same type.

    2. Abstraction is the ability of an object to take on many forms.

    3. It refers to the ability to make a class abstract in OOP.

    4. None of the above.

    Answer
  • 16.

    What is polymorphism?

    1. Polymorphism is a technique to define different objects of same type.

    2. Polymorphism is the ability of an object to take on many forms.

    3. Polymorphism is a technique to define different methods of same type.

    4. None of the above.

    Answer
  • 17.

    What is the default value of long variable?

    1. 0

    2. 0.0

    3. 0L

    4. not defined

    Answer
  • 18.

    What is the size of boolean variable?

    1. 8 bit

    2. 16 bit

    3. 32 bit

    4. not precisely defined

    Answer
  • 19.

    What is correct syntax for main method of a java class?

    1. public int main(String[] args)

    2. public static int main(String[] args)

    3. public static void main(String[] args)

    4. None of the above

    Answer
  • 20.

    which of the following will happen?

    import java.util.Arrays;
    import java.util.Comparator;
    
    public class X {
    
      private int x;
    
      public X(int x) {
        this.x = x;
      }
    
      public String toString() {
        return Integer.toString(x);
      }
    
      public static void main(String[] args) {
        X[] array = new X[3];
        array[0] = new X(3);
        array[1] = new X(1);
        array[2] = new X(2);
    
        Arrays.sort(array, new Comparator() {
          public int compare(X x1, X x2) {
            return x2.x - x1.x;
          }
        });
    
        System.out.println(Arrays.toString(array));
      }
    }
    1. compile time error: X is not Comparable

    2. run time error: X is not Comparable

    3. compile time error: accessing private member X.x in compare()

    4. output: [1, 2, 3]

    5. output: [3, 2, 1]

    Answer

© 2017 QuizBucket.org