Java quiz questions

Java interview questions

  • 1.

    What two statements are true about properly overridden hashCode() and equals() methods?

    1. hashCode() doesn't have to be overridden if equals() is.
    2. equals() doesn't have to be overridden if hashCode() is.
    3. hashCode() can always return the same value, regardless of the object that invoked it.
    4. equals() can be true even if it's comparing different objects.
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. 1 and 3

    Answer
  • 2.

    What will be the output of the program?

    package foo; 
    import java.util.Vector; /* Line 2 */
    private class MyVector extends Vector 
    {
        int i = 1; /* Line 5 */
        public MyVector() 
        { 
            i = 2; 
        } 
    } 
    public class MyNewVector extends MyVector 
    {
        public MyNewVector () 
        { 
            i = 4; /* Line 15 */
        } 
        public static void main (String args []) 
        { 
            MyVector v = new MyNewVector(); /* Line 19 */
        } 
    }
    1. Compilation will succeed.

    2. Compilation will fail at line 3.

    3. Compilation will fail at line 5.

    4. Compilation will fail at line 15.

    Answer
  • 3.

    Which interface does java.util.Hashtable implement?

    1. Java.util.Map

    2. Java.util.List

    3. Java.util.HashTable

    4. Java.util.Collection

    Answer
  • 4.

    What will be the output of the program?

    public class X 
    {  
        public static void main(String [] args) 
        {
            try 
            {
                badMethod(); /* Line 7 */
                System.out.print("A"); 
            } 
            catch (Exception ex) /* Line 10 */
            {
                System.out.print("B"); /* Line 12 */
            } 
            finally /* Line 14 */
            {
                System.out.print("C"); /* Line 16 */
            }  
            System.out.print("D"); /* Line 18 */
        } 
        public static void badMethod() 
        {
            throw new RuntimeException(); 
        } 
    }
    1. AB

    2. BC

    3. ABC

    4. BCD

    Answer
  • 5.

    What will be the output of the program?

    public class X 
    {  
        public static void main(String [] args) 
        {
            try 
            {
                badMethod();  
                System.out.print("A"); 
            }  
            catch (Exception ex) 
            {
                System.out.print("B");  
            } 
            finally 
            {
                System.out.print("C"); 
            } 
            System.out.print("D"); 
        }  
        public static void badMethod() 
        {
            throw new Error(); /* Line 22 */
        } 
    }
    1. ABCD

    2. Compilation fails.

    3. C is printed before exiting with an error message.

    4. BC is printed before exiting with an error message.

    Answer
  • 6.

    Which two statements are true for any concrete class implementing the java.lang.Runnable interface?

    1. You can extend the Runnable interface as long as you override the public run() method.
    2. The class must contain a method called run() from which all code for that thread will be initiated.
    3. The class must contain an empty public void method named run().
    4. The class must contain a public void method named runnable().
    5. The class definition must include the words implements Threads and contain a method called run().
    6. The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.
    1. 1 and 3

    2. 2 and 4

    3. 1 and 5

    4. 2 and 6

    Answer
  • 7.

    What will be the output of the program?

    import java.util.*;
    public class NewTreeSet2 extends NewTreeSet 
    {
        public static void main(String [] args) 
        {
            NewTreeSet2 t = new NewTreeSet2();
            t.count();
        }
    }
    protected class NewTreeSet
    {
        void count() 
        {
            for (int x = 0; x < 7; x++,x++ ) 
            {
                System.out.print(" " + x);
            }
        }
    }
    1. 0 2 4

    2. 0 2 4 6

    3. Compilation fails at line 2

    4. Compilation fails at line 10

    Answer
  • 8.

    What will be the output of the program?

    class Base
    { 
        Base()
        {
            System.out.print("Base");
        }
    } 
    public class Alpha extends Base
    { 
        public static void main(String[] args)
        { 
            new Alpha(); /* Line 12 */
            new Base(); /* Line 13 */
        } 
    }
    1. Base

    2. BaseBase

    3. Compilation fails

    4. The code runs with no output

    Answer
  • 9.

    What will be the output of the program?

    class Super
    { 
        public int i = 0; 
    
        public Super(String text) /* Line 4 */
        {
            i = 1; 
        } 
    } 
    
    class Sub extends Super
    {
        public Sub(String text)
        {
            i = 2; 
        } 
    
        public static void main(String args[])
        {
            Sub sub = new Sub("Hello"); 
            System.out.println(sub.i); 
        } 
    }
    1. 0

    2. 1

    3. 2

    4. Compilation fails.

    Answer
  • 10.

    What will be the output of the program?

    public class StringRef 
    {
        public static void main(String [] args) 
        {
            String s1 = "abc";
            String s2 = "def";
            String s3 = s2;   /* Line 7 */
            s2 = "ghi";
            System.out.println(s1 + s2 + s3);
        }
    }
    1. abcdefghi

    2. abcdefdef

    3. abcghidef

    4. abcghighi

    Answer
  • 11.

    What will be the output of the program?

    class Q207 
    { 
        public static void main(String[] args) 
        {
            int i1 = 5; 
            int i2 = 6; 
            String s1 = "7"; 
            System.out.println(i1 + i2 + s1); /* Line 8 */
        } 
    }
    1. 18

    2. 117

    3. 567

    4. Compiler error

    Answer
  • 12.

    What will be the output of the program?

    public class WrapTest 
    {
        public static void main(String [] args) 
        {
            int result = 0;
            short s = 42;
            Long x = new Long("42");
            Long y = new Long(42);
            Short z = new Short("42");
            Short x2 = new Short(s);
            Integer y2 = new Integer("42");
            Integer z2 = new Integer(42);
    
            if (x == y) /* Line 13 */
                result = 1;
            if (x.equals(y) ) /* Line 15 */
                result = result + 10;
            if (x.equals(z) ) /* Line 17 */
                result = result + 100;
            if (x.equals(x2) ) /* Line 19 */
                result = result + 1000;
            if (x.equals(z2) ) /* Line 21 */
                result = result + 10000;
    
            System.out.println("result = " + result);
        }
    }
    1. result = 1

    2. result = 10

    3. result = 11

    4. result = 11010

    Answer
  • 13.

    Which of the following statements is true?

    1. It is sometimes good practice to throw an AssertionError explicitly.

    2. Private getter() and setter() methods should not use assertions to verify arguments.

    3. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.

    4. It is proper to handle assertion statement failures using a catch (AssertionException ae) block.

    Answer
  • 14.

    What allows the programmer to destroy an object x?

    1. x.delete()

    2. x.finalize()

    3. Runtime.getRuntime().gc()

    4. Only the garbage collection system can destroy an object.

    Answer
  • 15.

    What will be the output of the program?

    public class Test107 implements Runnable 
    { 
        private int x; 
        private int y; 
    
        public static void main(String args[]) 
        {
            Test107 that = new Test107(); 
            (new Thread(that)).start(); 
            (new Thread(that)).start(); 
        } 
        public synchronized void run() 
        {
            for(int i = 0; i < 10; i++) 
            { 
                x++; 
                y++; 
                System.out.println("x = " + x + ", y = " + y); /* Line 17 */
            } 
        } 
    }
    1. Compilation error.

    2. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.

    3. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.

    4. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...

    Answer
  • 16.

    What will be the output of the program?

    public class ThreadTest extends Thread 
    { 
        public void run() 
        { 
            System.out.println("In run"); 
            yield(); 
            System.out.println("Leaving run"); 
        } 
        public static void main(String []argv) 
        { 
            (new ThreadTest()).start(); 
        } 
    }
    1. The code fails to compile in the main() method

    2. The code fails to compile in the run() method

    3. Only the text "In run" will be displayed

    4. The text "In run" followed by "Leaving run" will be displayed

    Answer
  • 17.

    What will be the output of the program?

    class MyThread extends Thread 
    {
        public static void main(String [] args) 
        {
            MyThread t = new MyThread(); /* Line 5 */
            t.run();  /* Line 6 */
        }
    
        public void run() 
        {
            for(int i=1; i < 3; ++i) 
            {
                System.out.print(i + "..");
            }
        }
    }
    1. This code will not compile due to line 5.

    2. This code will not compile due to line 6.

    3. 1..2..

    4. 1..2..3..

    Answer
  • 18.

    What will be the output of the program?

    public class Q126 implements Runnable 
    { 
        private int x; 
        private int y; 
    
        public static void main(String [] args) 
        { 
            Q126 that = new Q126(); 
            (new Thread(that)).start( ); /* Line 8 */
            (new Thread(that)).start( ); /* Line 9 */
        } 
        public synchronized void run( ) /* Line 11 */
        { 
            for (;;) /* Line 13 */
            { 
                x++; 
                y++; 
                System.out.println("x = " + x + "y = " + y); 
            } 
        } 
    }
    1. An error at line 11 causes compilation to fail
    2. Errors at lines 8 and 9 cause compilation to fail.
    3. The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
    4. The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 19.

    What will be the output of the program?

    public abstract class AbstractTest 
    {
        public int getNum() 
        {
            return 45;
        }
        public abstract class Bar 
        {
            public int getNum() 
            {
                return 38;
            }
        }
        public static void main (String [] args) 
        {
            AbstractTest t = new AbstractTest() 
            {
                public int getNum() 
                {
                    return 22;
                }
            };
            AbstractTest.Bar f = t.new Bar() 
            {
                public int getNum() 
                {
                    return 57;
                }
            };
            
            System.out.println(f.getNum() + " " + t.getNum());
        }
    }
    1. 57 22

    2. 45 38

    3. 45 57

    4. An exception occurs at runtime.

    Answer
  • 20.

    What will be the output of the program?

    public class HorseTest 
    {
        public static void main (String [] args) 
        {
            class Horse 
            {
                public String name; /* Line 7 */
                public Horse(String s) 
                {
                    name = s;
                }
            } /* class Horse ends */
            
            Object obj = new Horse("Zippo"); /* Line 13 */
            Horse h = (Horse) obj; /* Line 14 */
            System.out.println(h.name);
        }
    } /* class HorseTest ends */
    1. An exception occurs at runtime at line 10.

    2. It prints "Zippo".

    3. Compilation fails because of an error on line 7.

    4. Compilation fails because of an error on line 13.

    Answer

© 2017 QuizBucket.org