Java quiz questions

Java interview questions

  • 1.

    What will be the output of the program?

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

    2. BCD

    3. BDE

    4. BCDE

    Answer
  • 2.

    What will be the output of the program?

    int x = 3; 
    int y = 1; 
    if (x = y) /* Line 3 */
    {
        System.out.println("x =" + x); 
    }
    1. x = 1

    2. x = 3

    3. Compilation fails.

    4. The code runs with no output.

    Answer
  • 3.

    What will be the output of the program?

    for (int i = 0; i < 4; i += 2) 
    { 
        System.out.print(i + " "); 
    } 
    System.out.println(i); /* Line 5 */
    1. 0 2 4

    2. 0 2 4 5

    3. 0 1 2 3 4

    4. Compilation fails.

    Answer
  • 4.

    What will be the output of the program?

    int x = l, y = 6; 
    while (y--) 
    {
        x++; 
    } 
    System.out.println("x = " + x +" y = " + y);
    1. x = 6 y = 0

    2. x = 7 y = 0

    3. x = 6 y = -1

    4. Compilation fails.

    Answer
  • 5.

    What will be the output of the program?

    class SSBool 
    {
        public static void main(String [] args) 
        {
            boolean b1 = true;
            boolean b2 = false;
            boolean b3 = true;
            if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
                System.out.print("ok ");
            if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
                System.out.println("dokey");
        }
    }
    1. ok

    2. dokey

    3. ok dokey

    4. No output is produced

    5. Compilation error

    Answer
  • 6.

    What will be the output of the program?

    class Bitwise 
    {
        public static void main(String [] args) 
        {
            int x = 11 & 9;
            int y = x ^ 3;
            System.out.println( y | 12 );
        }
    }
    1. 0

    2. 7

    3. 8

    4. 14

    Answer
  • 7.

    What will be the output of the program?

    class Equals 
    {
        public static void main(String [] args) 
        {
            int x = 100;
            double y = 100.1;
            boolean b = (x = y); /* Line 7 */
            System.out.println(b);
        }
    }
    1. true

    2. false

    3. Compilation fails

    4. An exception is thrown at runtime

    Answer
  • 8.

    What will be the output of the program?

    class PassA 
    {
        public static void main(String [] args) 
        {
            PassA p = new PassA();
            p.start();
        }
    
        void start() 
        {
            long [] a1 = {3,4,5};
            long [] a2 = fix(a1);
            System.out.print(a1[0] + a1[1] + a1[2] + " ");
            System.out.println(a2[0] + a2[1] + a2[2]);
        }
    
        long [] fix(long [] a3) 
        {
            a3[1] = 7;
            return a3;
        }
    }
    1. 12 15

    2. 15 15

    3. 3 4 5 3 7 5

    4. 3 7 5 3 7 5

    Answer
  • 9.

    What will be the output of the program?

    public class Test 
    {
        public int aMethod()
        {
            static int i = 0;
            i++;
            return i;
        }
        public static void main(String args[])
        {
            Test test = new Test();
            test.aMethod();
            int j = test.aMethod();
            System.out.println(j);
        }
    }
    1. 0

    2. 1

    3. 2

    4. Compilation fails.

    Answer
  • 10.

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

    What will be the output of the program?

    public class Test 
    { 
        public static void main(String[] args) 
        {
            final StringBuffer a = new StringBuffer(); 
            final StringBuffer b = new StringBuffer(); 
    
            new Thread() 
            { 
                public void run() 
                {
                    System.out.print(a.append("A")); 
                    synchronized(b) 
                    { 
                        System.out.print(b.append("B")); 
                    } 
                } 
            }.start(); 
                
            new Thread() 
            {
                public void run() 
                {
                    System.out.print(b.append("C")); 
                    synchronized(a) 
                    {
                        System.out.print(a.append("D")); 
                    } 
                } 
            }.start(); 
        } 
    }
    1. ACCBAD

    2. ABBCAD

    3. CDDACB

    4. Indeterminate output

    Answer
  • 12.

    What will be the output of the program?

    System.out.println(Math.sqrt(-4D));

    1. -2

    2. NaN

    3. Compile Error

    4. Runtime Exception

    Answer
  • 13.

    What will be the output of the program?

    String x = new String("xyz");
    String y = "abc";
    x = x + y;

    How many String objects have been created?

    1. 2

    2. 3

    3. 4

    4. 5

    Answer
  • 14.

    Which of the following would compile without error?

    1. int a = Math.abs(-5);

    2. int b = Math.abs(5.0);

    3. int c = Math.abs(5.5F);

    4. int d = Math.abs(5L);

    Answer
  • 15.

    After line 11 runs, how many objects are eligible for garbage collection?

    class X2 
    {
        public X2 x;
        public static void main(String [] args) 
        {
            X2 x2 = new X2();  /* Line 6 */
            X2 x3 = new X2();  /* Line 7 */
            x2.x = x3;
            x3.x = x2;
            x2 = new X2();
            x3 = x2; /* Line 11 */
            doComplexStuff();
        }
    }
    1. 0

    2. 1

    3. 2

    4. 3

    Answer
  • 16.

    When is the B object, created in line 3, eligible for garbage collection?

    void start() {  
        A a = new A(); 
        B b = new B(); 
        a.s(b);  
        b = null; /* Line 5 */
        a = null;  /* Line 6 */
        System.out.println("start completed"); /* Line 7 */
    }
    1. after line 5

    2. after line 6

    3. after line 7

    4. There is no way to be absolutely certain.

    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 method registers a thread in a thread scheduler?

    1. run();

    2. construct();

    3. start();

    4. register();

    Answer
  • 19.

    Which cannot directly cause a thread to stop executing?

    1. Calling the SetPriority() method on a Thread object.

    2. Calling the wait() method on an object.

    3. Calling notify() method on an object.

    4. Calling read() method on an InputStream object.

    Answer
  • 20.

    Which three are methods of the Object class?

    1. notify();
    2. notifyAll();
    3. isInterrupted();
    4. synchronized();
    5. interrupt();
    6. wait(long msecs);
    7. sleep(long msecs);
    8. yield();
    1. 1, 2, 4

    2. 2, 4, 5

    3. 1, 2, 6

    4. 2, 3, 4

    Answer

© 2017 QuizBucket.org