Java quiz questions

Java interview questions

  • 1.

    What will be the output of the program?

    public class Test 
    { 
        public static void main (String args[]) 
        {
            String str = NULL; 
            System.out.println(str); 
        } 
    }
    1. NULL

    2. Compile Error

    3. Code runs but no output

    4. Runtime Exception

    Answer
  • 2.

    Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?

    1. java.util.HashSet

    2. java.util.LinkedHashSet

    3. java.util.List

    4. java.util.ArrayList

    Answer
  • 3.

    What will be the output of the program?

    public class Switch2 
    {
        final static short x = 2;
        public static int y = 0;
        public static void main(String [] args) 
        {
            for (int z=0; z < 4; z++) 
            {
                switch (z) 
                {
                    case x: System.out.print("0 ");
                    default: System.out.print("def ");
                    case x-1: System.out.print("1 ");  
                                break;
                    case x-2: System.out.print("2 ");
                }
            }
        }
    }
    1. 0 def 1

    2. 2 1 0 def 1

    3. 2 1 0 def def

    4. 2 1 0 def 1 def 1

    Answer
  • 4.

    What will be the output of the program?

    public class Switch2 
    {
        final static short x = 2;
        public static int y = 0;
        public static void main(String [] args) 
        {
            for (int z=0; z < 3; z++) 
            {
                switch (z) 
                {
                    case y: System.out.print("0 ");   /* Line 11 */
                    case x-1: System.out.print("1 "); /* Line 12 */
                    case x: System.out.print("2 ");   /* Line 13 */
                }
            }
        }
    }
    1. 0 1 2

    2. 0 1 2 1 2 2

    3. Compilation fails at line 11.

    4. Compilation fails at line 12.

    Answer
  • 5.

    What will be the output of the program?

    public class Switch2 
    {
        final static short x = 2;
        public static int y = 0;
        public static void main(String [] args) 
        {
            for (int z=0; z < 3; z++) 
            {
                switch (z) 
                {
                    case x: System.out.print("0 ");
                    case x-1: System.out.print("1 ");
                    case x-2: System.out.print("2 ");
                }
            }
        }
    }
    1. 0 1 2

    2. 0 1 2 1 2 2

    3. 2 1 0 1 0 0

    4. 2 1 2 0 1 2

    Answer
  • 6.

    Which two are acceptable types for x?

    switch(x) 
    { 
        default:  
            System.out.println("Hello"); 
    }
    1. byte
    2. long
    3. char
    4. float
    5. Short
    6. Long
    1. 1 and 3

    2. 2 and 4

    3. 3 and 5

    4. 4 and 6

    Answer
  • 7.

    Which is a valid declaration within an interface?

    1. public static short stop = 23;

    2. protected short stop = 23;

    3. transient short stop = 23;

    4. final void madness(short stop);

    Answer
  • 8.

    You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

    1. public

    2. private

    3. protected

    4. default access

    Answer
  • 9.

    What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

    1. public

    2. abstract

    3. protected

    4. synchronized

    5. default access

    Answer
  • 10.

    What will be the output of the program?

    public class ExamQuestion7 
    {  
        static int j; 
        static void methodA(int i)
        {
            boolean b; 
            do
            { 
                b = i<10 | methodB(4); /* Line 9 */
                b = i<10 || methodB(8);  /* Line 10 */
            }while (!b); 
        } 
        static boolean methodB(int i)
        {
            j += i; 
            return true; 
        } 
        public static void main(String[] args)
        {
            methodA(0); 
            System.out.println( "j = " + j ); 
        } 
    }
    1. j = 0

    2. j = 4

    3. j = 8

    4. The code will run with no output

    Answer
  • 11.

    What will be the output of the program?

    int i = 1, j = 10; 
    do 
    {
        if(i++ > --j) /* Line 4 */
        {
            continue; 
        } 
    } while (i < 5); 
    System.out.println("i = " + i + "and j = " + j); /* Line 9 */
    1. i = 6 and j = 5

    2. i = 5 and j = 5

    3. i = 6 and j = 6

    4. i = 5 and j = 6

    Answer
  • 12.

    What will be the output of the program?

    public class Test178 
    { 
        public static void main(String[] args) 
        {
            String s = "foo"; 
            Object o = (Object)s; 
            if (s.equals(o)) 
            { 
                System.out.print("AAA"); 
            } 
            else 
            {
                System.out.print("BBB"); 
            } 
            if (o.equals(s)) 
            {
                System.out.print("CCC"); 
            } 
            else 
            {
                System.out.print("DDD"); 
            } 
        } 
    }
    1. AAACCC

    2. AAADDD

    3. BBBCCC

    4. BBBDDD

    Answer
  • 13.

    What will be the output of the program?

    public class ObjComp 
    {
        public static void main(String [] args ) 
        {
            int result = 0;
            ObjComp oc = new ObjComp();
            Object o = oc;
            if (o == oc)  
                result = 1;
            if (o != oc)  
                result = result + 10;
            if (o.equals(oc) )  
                result = result + 100;
            if (oc.equals(o) )  
                result = result + 1000;
            System.out.println("result = " + result);
        }
    }
    1. 1

    2. 10

    3. 101

    4. 1101

    Answer
  • 14.

    Which statement is true about assertions in the Java programming language?

    1. Assertion expressions should not contain side effects.

    2. Assertion expression values can be any primitive type.

    3. Assertions should be used for enforcing preconditions on public methods.

    4. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.

    Answer
  • 15.

    Which statement is true?

    1. Calling Runtime.gc() will cause eligible objects to be garbage collected.

    2. The garbage collector uses a mark and sweep algorithm.

    3. If an object can be accessed from a live thread, it can't be garbage collected.

    4. If object 1 refers to object 2, then object 2 can't be garbage collected.

    Answer
  • 16.

    Which statement is true?

    1. A static method cannot be synchronized.

    2. If a class has synchronized code, multiple threads can still access the nonsynchronized code.

    3. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.

    4. When a thread sleeps, it releases its locks.

    Answer
  • 17.

    What will be the output of the program?

    class MyThread extends Thread 
    {
        public static void main(String [] args) 
        {
            MyThread t = new MyThread();
            Thread x = new Thread(t);
            x.start(); /* Line 7 */
        }
        public void run() 
        {
            for(int i = 0; i < 3; ++i) 
            {
                System.out.print(i + "..");
            }
        }
    }
    1. Compilation fails.

    2. 1..2..3..

    3. 0..1..2..3..

    4. 0..1..2..

    Answer
  • 18.

    What will be the output of the program?

    class Happy extends Thread 
    { 
        final StringBuffer sb1 = new StringBuffer(); 
        final StringBuffer sb2 = new StringBuffer(); 
        public static void main(String args[]) 
        { 
            final Happy h = new Happy(); 
            new Thread() 
            { 
                public void run() 
                { 
                    synchronized(this) 
                    { 
                        h.sb1.append("A"); 
                        h.sb2.append("B"); 
                        System.out.println(h.sb1); 
                        System.out.println(h.sb2); 
                    } 
                } 
            }.start(); 
            new Thread() 
            { 
                public void run() 
                { 
                    synchronized(this) 
                    { 
                        h.sb1.append("D"); 
                        h.sb2.append("C"); 
                        System.out.println(h.sb2); 
                        System.out.println(h.sb1); 
                    } 
                } 
            }.start(); 
        } 
    }
    1. ABBCAD

    2. ABCBCAD

    3. CDADACB

    4. Output determined by the underlying platform.

    Answer
  • 19.

    What will be the output of the program?

    public class Test 
    { 
        public static void main (String[] args) 
        {
            String foo = args[1]; 
            String bar = args[2]; 
            String baz = args[3]; 
            System.out.println("baz = " + baz); /* Line 8 */
        } 
    }

    And the command line invocation:

    java Test red green blue

    1. baz =

    2. baz = null

    3. baz = blue

    4. Runtime Exception

    Answer
  • 20.

    Which of the following are Java reserved words?

    1. run
    2. import
    3. default
    4. implement
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. 2 and 4

    Answer

© 2017 QuizBucket.org