Java quiz questions

Java interview questions

  • 1.

    Which is true about a method-local inner class?

    1. It must be marked final.

    2. It can be marked abstract.

    3. It can be marked public.

    4. It can be marked static.

    Answer
  • 2.

    Which statement is true for the class java.util.ArrayList?

    1. The elements in the collection are ordered.

    2. The collection is guaranteed to be immutable.

    3. The elements in the collection are guaranteed to be unique.

    4. The elements in the collection are accessed using a unique key.

    Answer
  • 3.

    What will be the output of the program?

    public class Test 
    {  
        public static void aMethod() throws Exception 
        {
            try /* Line 5 */
            {
                throw new Exception(); /* Line 7 */
            } 
            finally /* Line 9 */
            {
                System.out.print("finally "); /* Line 11 */
            } 
        } 
        public static void main(String args[]) 
        {
            try 
            {
                aMethod();  
            } 
            catch (Exception e) /* Line 20 */
            {
                System.out.print("exception "); 
            } 
            System.out.print("finished"); /* Line 24 */
        } 
    }
    1. finally

    2. exception finished

    3. finally exception finished

    4. Compilation fails

    Answer
  • 4.

    What will be the output of the program?

    Float f = new Float("12"); 
    switch (f) 
    {
        case 12: System.out.println("Twelve"); 
        case 0: System.out.println("Zero"); 
        default: System.out.println("Default"); 
    }
    1. Zero

    2. Twelve

    3. Default

    4. Compilation fails

    Answer
  • 5.

    Select the correct propositions.

    public void foo( boolean a, boolean b)
    { 
        if( a ) 
        {
            System.out.println("A"); /* Line 5 */
        } 
        else if(a && b) /* Line 7 */
        { 
            System.out.println( "A && B"); 
        } 
        else /* Line 11 */
        { 
            if ( !b ) 
            {
                System.out.println( "notB") ;
            } 
            else 
            {
                System.out.println( "ELSE" ) ; 
            } 
        } 
    }
    1. If a is true and b is true then the output is "A && B"

    2. If a is true and b is false then the output is "notB"

    3. If a is false and b is true then the output is "ELSE"

    4. If a is false and b is false then the output is "ELSE"

    Answer
  • 6.

    Which two statements are equivalent?

    1. 3/2
    2. 3<2
    3. 3*4
    4. 3<<2
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. 1 and 4

    Answer
  • 7.

    What will be the output of the program?

    class BoolArray 
    {
        boolean [] b = new boolean[3];
        int count = 0;
    
        void set(boolean [] x, int i) 
        {
            x[i] = true;
            ++count;
        }
    
        public static void main(String [] args) 
        {
            BoolArray ba = new BoolArray();
            ba.set(ba.b, 0);
            ba.set(ba.b, 2);
            ba.test();
        }
    
        void test() 
        {
            if ( b[0] && b[1] | b[2] )
                count++;
            if ( b[1] && b[(++count - 2)] )
                count += 7;
            System.out.println("count = " + count);
        }
    }
    1. count = 0

    2. count = 2

    3. count = 3

    4. count = 4

    Answer
  • 8.

    What will be the output of the program?

    class SC2 
    {
        public static void main(String [] args) 
        {
            SC2 s = new SC2();
            s.start();
        }
    
        void start() 
        {
            int a = 3;
            int b = 4;
            System.out.print(" " + 7 + 2 + " ");
            System.out.print(a + b);
            System.out.print(" " + a + b + " ");
            System.out.print(foo() + a + b + " ");
            System.out.println(a + b + foo());
        }
    
        String foo() 
        {
            return "foo";
        }
    }
    1. 9 7 7 foo 7 7foo

    2. 72 34 34 foo34 34foo

    3. 9 7 7 foo34 34foo

    4. 72 7 34 foo34 7foo

    Answer
  • 9.

    What will be the output of the program?

    class A 
    {
        final public int GetResult(int a, int b) { return 0; } 
    } 
    class B extends A 
    { 
        public int GetResult(int a, int b) {return 1; } 
    } 
    public class Test 
    {
        public static void main(String args[]) 
        { 
            B b = new B(); 
            System.out.println("x = " + b.GetResult(0, 1));  
        } 
    }
    1. x = 0

    2. x = 1

    3. Compilation fails.

    4. An exception is thrown at runtime.

    Answer
  • 10.

    Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?

    1. TreeMap

    2. HashMap

    3. LinkedHashMap

    4. The answer depends on the implementation of the existing instance.

    Answer
  • 11.

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

    Which statement is true given the following?

    Double d = Math.random();

    1. 0.0 < d <= 1.0

    2. 0.0 <= d < 1.0

    3. Compilation fail

    4. Cannot say.

    Answer
  • 13.

    What will be the output of the program?

    class Test 
    {
        public static void main(String [] args) 
        {
            int x= 0;
            int y= 0;
            for (int z = 0; z < 5; z++) 
            {
                if (( ++x > 2 ) || (++y > 2)) 
                {
                    x++;
                }
            }
        System.out.println(x + " " + y);
        }
    }
    1. 5 3

    2. 8 2

    3. 8 3

    4. 8 5

    Answer
  • 14.

    What will be the output of the program?

    public class CommandArgsTwo 
    {
        public static void main(String [] argh) 
        {
            int x;
            x = argh.length;
            for (int y = 1; y <= x; y++) 
            {
                System.out.print(" " + argh[y]);
            }
        }
    }

    and the command-line invocation is

    java CommandArgsTwo 1 2 3

    1. 0 1 2

    2. 1 2 3

    3. 0 0 0

    4. An exception is thrown at runtime

    Answer
  • 15.

    What will be the output of the program, if this code is executed with the command line:

    java F0091 world

    public class F0091 
    {    
        public void main( String[] args ) 
        {  
            System.out.println( "Hello" + args[0] ); 
        } 
    }

     

    1. Hello

    2. Hello Foo91

    3. Hello world

    4. The code does not run.

    Answer
  • 16.

    which line is an example of an inappropriate use of assertions?

    public class Test2 
    {
        public static int x;
        public static int foo(int y) 
        {
            return y * 2;
        }
        public static void main(String [] args) 
        {
            int z = 5;
            assert z > 0; /* Line 11 */
            assert z > 2: foo(z); /* Line 12 */
            if ( z < 7 )
                assert z > 4; /* Line 14 */
    
            switch (z) 
            {
                case 4: System.out.println("4 ");
                case 5: System.out.println("5 ");
                default: assert z < 10;
            }
    
            if ( z < 10 )
                assert z > 4: z++; /* Line 22 */
            System.out.println(z);
        }
    }
    1. Line 11

    2. Line 12

    3. Line 14

    4. Line 22

    Answer
  • 17.

    which of these will create and start this thread?

    public class MyRunnable implements Runnable 
    {
        public void run() 
        {
            // some code here
        }
    }
    1. new Runnable(MyRunnable).start();

    2. new Thread(MyRunnable).run();

    3. new Thread(new MyRunnable()).start();

    4. new MyRunnable().start();

    Answer
  • 18.

    Which is the valid declarations within an interface definition?

    1. public double methoda();

    2. public final double methoda();

    3. static void methoda(double d1);

    4. protected void methoda(double d1);

    Answer
  • 19.

    Which is a reserved word in the Java programming language?

    1. method

    2. native

    3. subclasses

    4. reference

    5. array

    Answer
  • 20.

    which two statements, added independently at beginning of the program, allow the code to compile?

    /* Missing statements ? */
    public class NewTreeSet extends java.util.TreeSet
    {
        public static void main(String [] args) 
        {
            java.util.TreeSet t = new java.util.TreeSet();
            t.clear();
        }
        public void clear() 
        {
            TreeMap m = new TreeMap();
            m.clear();
        }
    }
    1. No statement is required
    2. import java.util.*;
    3. import.java.util.Tree*;
    4. import java.util.TreeSet;
    5. import java.util.TreeMap;
    1. 1 only

    2. 2 and 5

    3. 3 and 4

    4. 3 and 5

    Answer

© 2017 QuizBucket.org