Java quiz questions

Java interview questions

  • 1.

    and given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?

    System.out.print("Start ");
    try 
    {
        System.out.print("Hello world");
        throw new FileNotFoundException();
    }
    System.out.print(" Catch Here "); /* Line 7 */
    catch(EOFException e) 
    {
        System.out.print("End of file exception");
    }
    catch(FileNotFoundException e) 
    {
        System.out.print("File not found");
    }
    1. The code will not compile.

    2. Code output: Start Hello world File Not Found.

    3. Code output: Start Hello world End of file exception.

    4. Code output: Start Hello world Catch Here File not found.

    Answer
  • 2.

    What will be the output of the program?

    public class MyProgram 
    {
        public static void main(String args[])
        {
            try 
            {
                System.out.print("Hello world ");
            }
            finally 
            {
                System.out.println("Finally executing ");
            }
        }
    }
    1. Nothing. The program will not compile because no exceptions are specified.

    2. Nothing. The program will not compile because no catch clauses are specified.

    3. Hello world.

    4. Hello world Finally executing

    Answer
  • 3.

    which three statements are true?

    1. f1 == f2
    2. f1 == f3
    3. f2 == f1[1]
    4. x == f1[0]
    5. f == f1[0]
    import java.awt.Button;
    class CompareReference 
    {
        public static void main(String [] args) 
        {
            float f = 42.0f;
            float [] f1 = new float[2];
            float [] f2 = new float[2];
            float [] f3 = f1;
            long x = 42;
            f1[0] = 42.0f;
        }
    }
    1. 1, 2 and 3

    2. 2, 4 and 5

    3. 3, 4 and 5

    4. 1, 4 and 5

    Answer
  • 4.

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

    What will be the output of the program?

    public class A
    { 
        void A() /* Line 3 */
        {
            System.out.println("Class A"); 
        } 
        public static void main(String[] args) 
        { 
            new A(); 
        } 
    }
    1. Class A

    2. Compilation fails.

    3. An exception is thrown at line 3.

    4. The code executes with no output.

    Answer
  • 6.

    What will be the output of the program?

    public class Test 
    {  
        public static void main(String args[])
        { 
            class Foo 
            {
                public int i = 3;
            } 
            Object o = (Object)new Foo();
            Foo foo = (Foo)o;
            System.out.println("i = " + foo.i);
        }
    }
    1. i = 3

    2. Compilation fails.

    3. i = 5

    4. ClassCastException will occur.

    Answer
  • 7.

    Which two cause a compiler error?

    1. float[ ] f = new float(3);
    2. float f2[ ] = new float[ ];
    3. float[ ]f1 = new float[3];
    4. float f3[ ] = new float[3];
    5. float f5[ ] = {1.0f, 2.0f, 2.0f};
    1. 2, 4

    2. 3, 5

    3. 4, 5

    4. 1, 2

    Answer
  • 8.

    What is the widest valid returnType for methodA in line 3?

    public class ReturnIt 
    { 
        returnType methodA(byte x, double y) /* Line 3 */
        { 
            return (long)x / y * 2; 
        } 
    }
    1. int

    2. byte

    3. long

    4. double

    Answer
  • 9.

    What is the prototype of the default constructor?

    public class Test { }
    1. Test( )

    2. Test(void)

    3. public Test( )

    4. public Test(void)

    Answer
  • 10.

    What will be the output of the program?

    String a = "newspaper";
    a = a.substring(5,7);
    char b = a.charAt(1);
    a = a + b;
    System.out.println(a);
    1. apa

    2. app

    3. apea

    4. apep

    Answer
  • 11.

    What will be the output of the program?

    public class SqrtExample 
    {
        public static void main(String [] args) 
        {
            double value = -9.0;
            System.out.println( Math.sqrt(value));
        }
    }
    1. 3.0

    2. -3.0

    3. NaN

    4. Compilation fails.

    Answer
  • 12.

    What causes compilation to fail?

    public class Test 
    { 
        public void foo() 
        {
            assert false; /* Line 5 */
            assert false; /* Line 6 */
        } 
        public void bar()
        {
            while(true)
            {
                assert false; /* Line 12 */
            } 
            assert false;  /* Line 14 */
        } 
    }
    1. Line 5

    2. Line 6

    3. Line 12

    4. Line 14

    Answer
  • 13.

    After line 8 runs. how many objects are eligible for garbage collection?

    public class X 
    {
        public static void main(String [] args) 
        {
            X x = new X();
            X x2 = m1(x); /* Line 6 */
            X x4 = new X();
            x2 = x4; /* Line 8 */
            doComplexStuff();
        }
        static X m1(X mx) 
        {
            mx = new X();
            return mx;
        }
    }
    1. 0

    2. 1

    3. 2

    4. 3

    Answer
  • 14.

    When is the Demo object eligible for garbage collection?

    class Test 
    {  
        private Demo d; 
        void start() 
        {  
            d = new Demo(); 
            this.takeDemo(d); /* Line 7 */
        } /* Line 8 */
        void takeDemo(Demo demo) 
        { 
            demo = null;  
            demo = new Demo(); 
        } 
    }
    1. After line 7

    2. After line 8

    3. After the start() method completes

    4. When the instance running this code is made eligible for garbage collection.

    Answer
  • 15.

    At what point is the Bar object, created on line 6, eligible for garbage collection?

    class Bar { } 
    class Test 
    {  
        Bar doBar() 
        {
            Bar b = new Bar(); /* Line 6 */
            return b; /* Line 7 */
        } 
        public static void main (String args[]) 
        { 
            Test t = new Test();  /* Line 11 */
            Bar newBar = t.doBar();  /* Line 12 */
            System.out.println("newBar"); 
            newBar = new Bar(); /* Line 14 */
            System.out.println("finishing"); /* Line 15 */
        } 
    }
    1. after line 12

    2. after line 14

    3. after line 7, when doBar() completes

    4. after line 15, when main() completes

    Answer
  • 16.

    What will be the output of the program?

    public class Foo 
    {
        Foo() 
        {
            System.out.print("foo");
        }
        
    class Bar
    {
        Bar() 
        {
            System.out.print("bar");
        }
        public void go() 
        {
            System.out.print("hi");
        }
    } /* class Bar ends */
    
        public static void main (String [] args) 
        {
            Foo f = new Foo();
            f.makeBar();
        }
        void makeBar() 
        {
            (new Bar() {}).go();
        }
    }/* class Foo ends */
    1. Compilation fails.

    2. An error occurs at runtime.

    3. It prints "foobarhi"

    4. It prints "barhi"

    Answer
  • 17.

    which statement is true?

    class Test1 
    {
        public int value;
        public int hashCode() { return 42; }
    }
    class Test2 
    {
        public int value;
        public int hashcode() { return (int)(value^5); }
    }
    1. class Test1 will not compile.

    2. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.

    3. The Test1 hashCode() method is less efficient than the Test2 hashCode() method.

    4. class Test2 will not compile.

    Answer
  • 18.

    Which statement is true?

    1. A try statement must have at least one corresponding catch block.

    2. Multiple catch statements can catch the same class of exception more than once.

    3. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.

    4. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.

    Answer
  • 19.

    What will be the output of the program?

    try 
    { 
        int x = 0; 
        int y = 5 / x; 
    } 
    catch (Exception e) 
    {
        System.out.println("Exception"); 
    } 
    catch (ArithmeticException ae) 
    {
        System.out.println(" Arithmetic Exception"); 
    } 
    System.out.println("finished");
    1. finished

    2. Exception

    3. Compilation fails.

    4. Arithmetic Exception

    Answer
  • 20.

    What will be the output of the program?

    int I = 0;
    label:
        if (I < 2) {
        System.out.print("I is " + I);
        I++;
        continue label;
    }
    1. I is 0

    2. I is 0 I is 1

    3. Compilation fails.

    4. None of the above

    Answer

© 2017 QuizBucket.org