Java quiz questions

Java interview questions

  • 1.

    Which constructs an anonymous inner class instance?

    1. Runnable r = new Runnable() { };

    2. Runnable r = new Runnable(public void run() { });

    3. Runnable r = new Runnable { public void run(){}};

    4. System.out.println(new Runnable() {public void run() { }});

    Answer
  • 2.

    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
  • 3.

    which one create an anonymous inner class from within class Bar?

    class Boo 
    {
        Boo(String s) { }
        Boo() { }
    }
    class Bar extends Boo 
    {
        Bar() { }
        Bar(String s) {super(s);}
        void zoo() 
        {
        // insert code here
        }
    }
    1. Boo f = new Boo(24) { };

    2. Boo f = new Bar() { };

    3. Bar f = new Boo(String s) { };

    4. Boo f = new Boo.Bar(String s) { };

    Answer
  • 4.

    And assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true?

    x = 0;
    if (x1.hashCode() != x2.hashCode() )  x = x + 1;
    if (x3.equals(x4) )  x = x + 10;
    if (!x5.equals(x6) ) x = x + 100;
    if (x7.hashCode() == x8.hashCode() )  x = x + 1000;
    System.out.println("x = " + x);
    1. x2.equals(x1)

    2. x3.hashCode() == x4.hashCode()

    3. x5.hashCode() != x6.hashCode()

    4. x8.equals(x7)

    Answer
  • 5.

    Which two statements are true about comparing two instances of the same class, given that the equals() and hashCode() methods have been properly overridden?

    1. If the equals() method returns true, the hashCode() comparison == must return true.
    2. If the equals() method returns false, the hashCode() comparison != must return true.
    3. If the hashCode() comparison == returns true, the equals() method must return true.
    4. If the hashCode() comparison == returns true, the equals() method might return true.
    1. 1 and 4

    2. 2 and 3

    3. 3 and 4

    4. 1 and 3

    Answer
  • 6.

    What will be the output of the program?

    public class Test 
    { 
        private static float[] f = new float[2]; 
        public static void main (String[] args) 
        {
            System.out.println("f[0] = " + f[0]); 
        } 
    }
    1. f[0] = 0

    2. f[0] = 0.0

    3. Compile Error

    4. Runtime Exception

    Answer
  • 7.

    What will be the output of the program?

    int I = 0;
        outer:
        while (true) 
        {
            I++;
            inner:
            for (int j = 0; j < 10; j++) 
            {
                I += j;
                if (j == 3)
                    continue inner;
                break outer;
            }
            continue outer;
        }
    System.out.println(I);
    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 8.

    Which statement is true?

    public class While 
    {
        public void loop() 
        {
            int x= 0;
            while ( 1 ) /* Line 6 */
            {
                System.out.print("x plus one is " + (x + 1)); /* Line 8 */
            }
        }
    }
    1. There is a syntax error on line 1.

    2. There are syntax errors on lines 1 and 6.

    3. There are syntax errors on lines 1, 6, and 8.

    4. There is a syntax error on line 6.

    Answer
  • 9.

    Which two of the following are legal declarations for nonnested classes and interfaces?

    1. final abstract class Test {}
    2. public static interface Test {}
    3. final public class Test {}
    4. protected abstract class Test {}
    5. protected interface Test {}
    6. abstract public class Test {}
    1. 1 and 4

    2. 2 and 5

    3. 3 and 6

    4. 4 and 6

    Answer
  • 10.

    What will be the output of the program?

    class A 
    { 
        public A(int x){} 
    } 
    class B extends A { } 
    public class test 
    { 
        public static void main (String args []) 
        {
            A a = new B(); 
            System.out.println("complete"); 
        } 
    }
    1. It compiles and runs printing nothing

    2. Compiles but fails at runtime

    3. Compile Error

    4. Prints "complete"

    Answer
  • 11.

    What will be the output of the program?

    int i = (int) Math.random();
    1. i = 0

    2. i = 1

    3. value of i is undetermined

    4. Statement causes a compile error

    Answer
  • 12.

    Select how you would start the program to cause it to print: Arg is 2

    public class Myfile 
    { 
        public static void main (String[] args) 
        {
            String biz = args[1]; 
            String baz = args[2]; 
            String rip = args[3]; 
            System.out.println("Arg is " + rip); 
        } 
    }
    1. java Myfile 222

    2. java Myfile 1 2 2 3 4

    3. java Myfile 1 3 2 2

    4. java Myfile 0 1 2 3

    Answer
  • 13.

    The following block of code creates a Thread using a Runnable target:

    Runnable target = new MyRunnable();
    Thread myThread = new Thread(target);

    Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

    1. public class MyRunnable extends Runnable{public void run(){}}

    2. public class MyRunnable extends Object{public void run(){}}

    3. public class MyRunnable implements Runnable{public void run(){}}

    4. public class MyRunnable implements Runnable{void run(){}}

    Answer
  • 14.

    Which two can be used to create a new Thread?

    1. Extend java.lang.Thread and override the run() method.
    2. Extend java.lang.Runnable and override the start() method.
    3. Implement java.lang.Thread and implement the run() method.
    4. Implement java.lang.Runnable and implement the run() method.
    5. Implement java.lang.Thread and implement the start() method.
    1. 1 and 2

    2. 2 and 3

    3. 1 and 4

    4. 3 and 4

    Answer
  • 15.

    What will be the output of the program?

    public class ThreadDemo 
    { 
        private int count = 1; 
        public synchronized void doSomething() 
        { 
            for (int i = 0; i < 10; i++) 
                System.out.println(count++); 
        } 
        public static void main(String[] args) 
        { 
            ThreadDemo demo = new ThreadDemo(); 
            Thread a1 = new A(demo); 
            Thread a2 = new A(demo); 
            a1.start(); 
            a2.start(); 
        } 
    } 
    class A extends Thread 
    { 
        ThreadDemo demo; 
        public A(ThreadDemo td) 
        { 
            demo = td; 
        } 
        public void run() 
        { 
            demo.doSomething(); 
        } 
    }
    1. It will print the numbers 0 to 19 sequentially

    2. It will print the numbers 1 to 20 sequentially

    3. It will print the numbers 1 to 20, but the order cannot be determined

    4. The code will not compile.

    Answer
  • 16.

    Which class or interface defines the wait()notify(),and notifyAll() methods?

    1. Object

    2. Thread

    3. Runnable

    4. Class

    Answer
  • 17.

    Which of the following will directly stop the execution of a Thread?

    1. wait()

    2. notify()

    3. notifyall()

    4. exits synchronized code

    Answer
  • 18.

    What will be the output of the program?

    public class TestObj 
    {
        public static void main (String [] args) 
        {
            Object o = new Object() /* Line 5 */
            {
                public boolean equals(Object obj) 
                {
                    return true;
                } 
            }      /* Line 11 */
            System.out.println(o.equals("Fred"));
        }
    }
    1. It prints "true".

    2. It prints "Fred".

    3. An exception occurs at runtime.

    4. Compilation fails

    Answer
  • 19.

    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
  • 20.

    Which interface provides the capability to store objects using a key-value pair?

    1. Java.util.Map

    2. Java.util.Set

    3. Java.util.List

    4. Java.util.Collection

    Answer

© 2017 QuizBucket.org