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 (Exception ex) 
            {
                System.out.print("B"); 
            }  
            finally 
            {
                System.out.print("C"); 
            }  
            System.out.print("D"); 
        }  
        public static void badMethod() {} 
    }
    1. AC

    2. BC

    3. ACD

    4. ABCD

    Answer
  • 2.

    What will be the output of the program?

    public class RTExcept 
    {
        public static void throwit () 
        {
            System.out.print("throwit ");
            throw new RuntimeException();
        }
        public static void main(String [] args) 
        {
            try 
            {
                System.out.print("hello ");
                throwit();
            }
            catch (Exception re ) 
            {
                System.out.print("caught ");
            }
            finally 
            {
                System.out.print("finally ");
            }
            System.out.println("after ");
        }
    }
    1. hello throwit caught

    2. Compilation fails

    3. hello throwit RuntimeException caught after

    4. hello throwit caught finally after

    Answer
  • 3.

    What will be the output of the program?

    public class Test 
    {
        public static void main(String [] args) 
        {
            int I = 1;
            do while ( I < 1 )
            System.out.print("I is " + I);
            while ( I > 1 ) ;
        }
    }
    1. I is 1

    2. I is 1 I is 1

    3. No output is produced.

    4. Compilation error

    Answer
  • 4.

    Which two statements are equivalent?

    1. 16*4
    2. 16>>2
    3. 16/2^2
    4. 16>>>2
    1. 1 and 2

    2. 2 and 4

    3. 3 and 4

    4. 1 and 3

    Answer
  • 5.

    Which of the following are legal lines of code?

    1. int w = (int)888.8;
    2. byte x = (byte)1000L;
    3. long y = (byte)100;
    4. byte z = (byte)100L;
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. All statements are correct.

    Answer
  • 6.

    What will be the output of the program?

    class Test 
    {
        public static void main(String [] args) 
        {
            int x=20;
            String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
            System.out.println(sup);
        }
    }
    1. small

    2. tiny

    3. huge

    4. Compilation fails

    Answer
  • 7.

    Which three statements are true?

    1. The default constructor initialises method variables.
    2. The default constructor has the same access as its class.
    3. The default constructor invokes the no-arg constructor of the superclass.
    4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
    5. The compiler creates a default constructor only when there are no other constructors for the class.
    1. 1, 2 and 4

    2. 2,3 and 5

    3. 3, 4 and 5

    4. 1, 2 and 3

    Answer
  • 8.

    which two code fragments inserted at end of the program, will allow to compile?

    interface DoMath 
    {
        double getArea(int rad); 
    }
    interface MathPlus 
    {
        double getVol(int b, int h); 
    }
    /* Missing Statements ? */
    1. class AllMath extends DoMath { double getArea(int r); }
    2. interface AllMath implements MathPlus { double getVol(int x, int y); }
    3. interface AllMath extends DoMath { float getAvg(int h, int l); }
    4. class AllMath implements MathPlus { double getArea(int rad); }
    5. abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } }
    1. 1 only

    2. 2 only

    3. 3 and 5

    4. 1 and 4

    Answer
  • 9.

    Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

    1. final

    2. static

    3. private

    4. protected

    5. volatile

    Answer
  • 10.

    What will be the output of the program?

    public class BoolTest 
    {
        public static void main(String [] args) 
        {
            int result = 0;
            Boolean b1 = new Boolean("TRUE");
            Boolean b2 = new Boolean("true");
            Boolean b3 = new Boolean("tRuE");
            Boolean b4 = new Boolean("false");
            if (b1 == b2)  /* Line 10 */
                result = 1;
            if (b1.equals(b2) ) /* Line 12 */
                result = result + 10;
            if (b2 == b4)  /* Line 14 */
                result = result + 100;
            if (b2.equals(b4) ) /* Line 16 */
                result = result + 1000;
            if (b2.equals(b3) ) /* Line 18 */
                result = result + 10000;
            System.out.println("result = " + result);
        }
    }
    1. 1

    2. 0

    3. 10

    4. 10010

    Answer
  • 11.

    What will be the output of the program?

    public class Test 
    {  
        public static void main(String[] args) 
        { 
            int x = 0;  
            assert (x > 0) ? "assertion failed" : "assertion passed" ; 
            System.out.println("finished");  
        } 
    }
    1. finished

    2. Compiliation fails.

    3. An AssertionError is thrown and finished is output.

    4. An AssertionError is thrown with the message "assertion failed."

    Answer
  • 12.

    Which will contain the body of the thread?

    1. run();

    2. start();

    3. stop();

    4. main();

    Answer
  • 13.

    Which three guarantee that a thread will leave the running state?

    1. yield()
    2. wait()
    3. notify()
    4. notifyAll()
    5. sleep(1000)
    6. aLiveThread.join()
    7. Thread.killThread()
    1. 1, 2 and 4

    2. 2, 5 and 6

    3. 3, 4 and 7

    4. 4, 5 and 7

    Answer
  • 14.

    which statement, inserted at line 10, creates an instance of Bar?

    class Foo 
    {
        class Bar{ }
    }
    class Test 
    {
        public static void main (String [] args) 
        {
            Foo f = new Foo();
            /* Line 10: Missing statement ? */
        }
    }
    1. Foo.Bar b = new Foo.Bar();

    2. Foo.Bar b = f.new Bar();

    3. Bar b = new f.Bar();

    4. Bar b = f.new Bar();

    Answer
  • 15.

    What will be the output of the program?

    TreeSet map = new TreeSet();
    map.add("one");
    map.add("two");
    map.add("three");
    map.add("four");
    map.add("one");
    Iterator it = map.iterator();
    while (it.hasNext() ) 
    {
        System.out.print( it.next() + " " );
    }
    1. one two three four

    2. four three two one

    3. four one three two

    4. one two three four one

    Answer
  • 16.

    What will be the output of the program?

    import java.util.*; 
    class H 
    {
        public static void main (String[] args) 
        { 
            Object x = new Vector().elements(); 
            System.out.print((x instanceof Enumeration)+","); 
            System.out.print((x instanceof Iterator)+","); 
            System.out.print(x instanceof ListIterator); 
        } 
    }
    1. Prints: false,false,false

    2. Prints: false,false,true

    3. Prints: false,true,false

    4. Prints: true,false,false

    Answer
  • 17.

    Which is valid declaration of a float?

    1. float f = 1F;

    2. float f = 1.0;

    3. float f = "1";

    4. float f = 1.0d;

    Answer
  • 18.

    Which statement is true?

    1. catch(X x) can catch subclasses of X where X is a subclass of Exception.

    2. The Error class is a RuntimeException.

    3. Any statement that can throw an Error must be enclosed in a try block.

    4. Any statement that can throw an Exception must be enclosed in a try block.

    Answer
  • 19.

    and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?

    import java.io.*;
    public class MyProgram 
    {
        public static void main(String args[])
        {
            FileOutputStream out = null;
            try 
            {
                out = new FileOutputStream("test.txt");
                out.write(122);
            }
            catch(IOException io) 
            {
                System.out.println("IO Error.");
            }
            finally 
            {
                out.close();
            }
        }
    }
    1. This program will compile successfully.

    2. This program fails to compile due to an error at line 4.

    3. This program fails to compile due to an error at line 6.

    4. This program fails to compile due to an error at line 18.

    Answer
  • 20.

    What will be the output of the program?

    public class Test 
    {  
        public static void main(String args[]) 
        { 
            int i = 1, j = 0; 
            switch(i) 
            { 
                case 2: j += 6; 
                case 4: j += 1; 
                default: j += 2; 
                case 0: j += 4; 
            } 
            System.out.println("j = " + j); 
        } 
    }
    1. j = 0

    2. j = 2

    3. j = 4

    4. j = 6

    Answer

© 2017 QuizBucket.org