Java quiz questions

Java interview questions

  • 1.

    What will be the output of the program?

    int i = 1, j = 10; 
    do 
    {
        if(i > j) 
        {
            break; 
        } 
        j--; 
    } while (++i < 5); 
    System.out.println("i = " + i + " and j = " + j);
    1. i = 6 and j = 5

    2. i = 5 and j = 5

    3. i = 6 and j = 4

    4. i = 5 and j = 6

    Answer
  • 2.

    Which statement is true?

    public void test(int x) 
    { 
        int odd = 1; 
        if(odd) /* Line 4 */
        {
            System.out.println("odd"); 
        } 
        else 
        {
            System.out.println("even"); 
        } 
    }
    1. Compilation fails.

    2. "odd" will always be output.

    3. "even" will always be output.

    4. "odd" will be output for odd values of x, and "even" for even values.

    Answer
  • 3.

    which two of the following statements, inserted independently, could legally be inserted into missing section of this code?

    1. boolean test = (Component instanceof t);
    2. boolean test = (t instanceof Ticker);
    3. boolean test = t.instanceof(Ticker);
    4. boolean test = (t instanceof Component);
    import java.awt.*;
    class Ticker extends Component 
    {
        public static void main (String [] args) 
        {
            Ticker t = new Ticker();
            /* Missing Statements ? */
        }
    }
    1. 1 and 4

    2. 2 and 3

    3. 1 and 3

    4. 2 and 4

    Answer
  • 4.

    What will be the output of the program?

    class Test 
    {
        static int s;
        public static void main(String [] args) 
        {
            Test p = new Test();
            p.start();
            System.out.println(s);
        }
    
        void start() 
        {
            int x = 7;
            twice(x);
            System.out.print(x + " ");
        }
    
        void twice(int x) 
        {
            x = x*2;
            s = x;
        }
    }
    1. 7 7

    2. 7 14

    3. 14 0

    4. 14 14

    Answer
  • 5.

    What will be the output of the program?

    class PassS 
    {
        public static void main(String [] args) 
        {
            PassS p = new PassS();
            p.start();
        }
    
        void start() 
        {
            String s1 = "slip";
            String s2 = fix(s1);
            System.out.println(s1 + " " + s2);
        }
    
        String fix(String s1) 
        {
            s1 = s1 + "stream";
            System.out.print(s1 + " ");
            return "stream";
        }
    }
    1. slip stream

    2. slipstream stream

    3. stream slip stream

    4. slipstream slip stream

    Answer
  • 6.

    What will be the output of the program?

    class Test 
    {
        public static void main(String [] args) 
        {
            Test p = new Test();
            p.start();
        }
    
        void start() 
        {
            boolean b1 = false;
            boolean b2 = fix(b1);
            System.out.println(b1 + " " + b2);
        }
    
        boolean fix(boolean b1) 
        {
            b1 = true;
            return b1;
        }
    }
    1. true true

    2. false true

    3. true false

    4. false false

    Answer
  • 7.

    Which is valid in a class that extends class A?

    class A 
    {  
        protected int method1(int a, int b) 
        {
            return 0; 
        } 
    }
    1. public int method1(int a, int b) {return 0; }

    2. private int method1(int a, int b) { return 0; }

    3. public short method1(int a, int b) { return 0; }

    4. static protected int method1(int a, int b) { return 0; }

    Answer
  • 8.

    which two code fragments will compile?

    1. interface Base2 implements Base {}
    2. abstract class Class2 extends Base 
      { public boolean m1(){ return true; }}
    3. abstract class Class2 implements Base {}
    4. abstract class Class2 implements Base 
      { public boolean m1(){ return (7 > 4); }}
    5. abstract class Class2 implements Base 
      { protected boolean m1(){ return (5 > 7) }}
    interface Base 
    {
        boolean m1 ();
        byte m2(short s);
    }
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. 1 and 5

    Answer
  • 9.

    You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

    1. public

    2. private

    3. protected

    4. transient

    Answer
  • 10.

    What will be the output of the program?

    String a = "ABCD"; 
    String b = a.toLowerCase(); 
    b.replace('a','d'); 
    b.replace('b','c'); 
    System.out.println(b);
    1. abcd

    2. ABCD

    3. dccd

    4. dcba

    Answer
  • 11.

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

    public Object m() 
    {  
        Object o = new Float(3.14F); 
        Object [] oa = new Object[l];
        oa[0] = o; /* Line 5 */
        o = null;  /* Line 6 */
        oa[0] = null; /* Line 7 */
        return o; /* Line 8 */
    }
    1. just after line 5

    2. just after line 6

    3. just after line 7

    4. just after line 8

    Answer
  • 12.

    Which statement is true?

    1. The notifyAll() method must be called from a synchronized context.

    2. To call wait(), an object must own the lock on the thread.

    3. The notify() method is defined in class java.lang.Thread.

    4. The notify() method causes a thread to immediately release its locks.

    Answer
  • 13.

    Under which conditions will a currently executing thread stop?

    1. When an interrupted exception occurs.
    2. When a thread of higher priority is ready (becomes runnable).
    3. When the thread creates a new thread.
    4. When the stop() method is called.
    1. 1 and 3

    2. 2 and 4

    3. 1 and 4

    4. 2 and 3

    Answer
  • 14.

    Which two of the following methods are defined in class Thread?

    1. start()
    2. wait()
    3. notify()
    4. run()
    5. terminate()
    1. 1 and 4

    2. 2 and 3

    3. 3 and 4

    4. 2 and 4

    Answer
  • 15.

    Which two are valid constructors for Thread?

    1. Thread(Runnable r, String name)
    2. Thread()
    3. Thread(int priority)
    4. Thread(Runnable r, ThreadGroup g)
    5. Thread(Runnable r, int priority)
    1. 1 and 3

    2. 2 and 4

    3. 1 and 2

    4. 2 and 5

    Answer
  • 16.

    which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?

    public class MyOuter 
    {
        public static class MyInner 
        {
            public static void foo() { }
        }
    }
    1. MyOuter.MyInner m = new MyOuter.MyInner();

    2. MyOuter.MyInner mi = new MyInner();

    3. MyOuter m = new MyOuter();

      MyOuter.MyInner mi = m.new MyOuter.MyInner();

    4. MyInner mi = new MyOuter.MyInner();

    Answer
  • 17.

    Which is true about an anonymous inner class?

    1. It can extend exactly one class and implement exactly one interface.

    2. It can extend exactly one class and can implement multiple interfaces.

    3. It can extend exactly one class or implement exactly one interface.

    4. It can implement multiple interfaces regardless of whether it also extends a class.

    Answer
  • 18.

    What line of code should replace the missing statement to make this program compile?

    /* Missing Statement ? */
    public class foo 
    {
        public static void main(String[]args)throws Exception 
        {
            java.io.PrintWriter out = new java.io.PrintWriter(); 
            new java.io.OutputStreamWriter(System.out,true); 
            out.println("Hello"); 
        } 
    }
    1. No statement required.

    2. import java.io.*;

    3. include java.io.*;

    4. import java.io.PrintWriter;

    Answer
  • 19.

    You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?

    1. java.util.Map

    2. java.util.Set

    3. java.util.List

    4. java.util.Collection

    Answer
  • 20.

    Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

    1. java.lang.String

    2. java.lang.Double

    3. java.lang.StringBuffer

    4. java.lang.Character

    Answer

© 2017 QuizBucket.org