Java quiz questions

Java interview questions

  • 1.

    which answer most closely indicates the behavior of the program?

    public class MyProgram 
    {
        public static void throwit() 
        {
            throw new RuntimeException();
        }
        public static void main(String args[])
        {
            try 
            {
                System.out.println("Hello world ");
                throwit();
                System.out.println("Done with try block ");
            }
            finally 
            {
                System.out.println("Finally executing ");
            }
        }
    }
    1. The program will not compile.

    2. The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.

    3. The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.

    4. The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.

    Answer
  • 2.

    What will be the output of the program?

    class Exc0 extends Exception { } 
    class Exc1 extends Exc0 { } /* Line 2 */
    public class Test 
    {  
        public static void main(String args[]) 
        { 
            try 
            {  
                throw new Exc1(); /* Line 9 */
            } 
            catch (Exc0 e0) /* Line 11 */
            {
                System.out.println("Ex0 caught"); 
            } 
            catch (Exception e) 
            {
                System.out.println("exception caught");  
            } 
        } 
    }
    1. Ex0 caught

    2. exception caught

    3. Compilation fails because of an error at line 2.

    4. Compilation fails because of an error at line 9.

    Answer
  • 3.

    What will be the output of the program?

    boolean bool = true; 
    if(bool = false) /* Line 2 */
    {
        System.out.println("a"); 
    } 
    else if(bool) /* Line 6 */
    {
        System.out.println("b"); 
    } 
    else if(!bool) /* Line 10 */
    {
        System.out.println("c"); /* Line 12 */
    } 
    else 
    {
        System.out.println("d"); 
    }
    1. a

    2. b

    3. c

    4. d

    Answer
  • 4.

    What will be the output of the program?

    for(int i = 0; i < 3; i++) 
    { 
        switch(i) 
        { 
            case 0: break; 
            case 1: System.out.print("one "); 
            case 2: System.out.print("two "); 
            case 3: System.out.print("three "); 
        } 
    } 
    System.out.println("done");
    1. done

    2. one two done

    3. one two three done

    4. one two three two three done

    Answer
  • 5.

    What will be the output of the program?

    int i = l, j = -1; 
    switch (i) 
    {
        case 0, 1: j = 1; /* Line 4 */
        case 2: j = 2; 
        default: j = 0; 
    } 
    System.out.println("j = " + j); 
    1. j = -1

    2. j = 0

    3. j = 1

    4. Compilation fails.

    Answer
  • 6.

    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 2

    2. 5 3

    3. 6 3

    4. 6 4

    Answer
  • 7.

    What will be the output of the program?

    class Super 
    { 
        public Integer getLength() 
        {
            return new Integer(4); 
        } 
    } 
    
    public class Sub extends Super 
    { 
        public Long getLength() 
        {
            return new Long(5); 
        } 
    
        public static void main(String[] args) 
        { 
            Super sooper = new Super(); 
            Sub sub = new Sub(); 
            System.out.println( 
            sooper.getLength().toString() + "," + sub.getLength().toString() ); 
        } 
    }
    1. 4, 4

    2. 4, 5

    3. 5, 4

    4. Compilation fails.

    Answer
  • 8.

    Which three are valid method signatures in an interface?

    1. private int getArea();
    2. public float getVol(float x);
    3. public void main(String [] args);
    4. public static void main(String [] args);
    5. boolean setFlag(Boolean [] test);
    1. 1 and 2

    2. 2, 3 and 5

    3. 3, 4, and 5

    4. 2 and 4

    Answer
  • 9.

    Which cause a compiler error?

    1. int[ ] scores = {3, 5, 7};

    2. int [ ][ ] scores = {2,7,6}, {9,3,45};

    3. String cats[ ] = {"Fluffy", "Spot", "Zeus"};

    4. boolean results[ ] = new boolean [] {true, false, true};

    5. Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

    Answer
  • 10.

    What two statements are true about the result obtained from calling Math.random()?

    1. The result is less than 0.0.
    2. The result is greater than or equal to 0.0..
    3. The result is less than 1.0.
    4. The result is greater than 1.0.
    5. The result is greater than or equal to 1.0.
    1. 1 and 2

    2. 2 and 3

    3. 3 and 4

    4. 4 and 5

    Answer
  • 11.

    What will be the output of the program?

    class Tree { } 
    class Pine extends Tree { } 
    class Oak extends Tree { } 
    public class Forest1 
    { 
        public static void main (String [] args)
        { 
            Tree tree = new Pine(); 
            if( tree instanceof Pine ) 
                System.out.println ("Pine"); 
            else if( tree instanceof Tree ) 
                System.out.println ("Tree"); 
            else if( tree instanceof Oak ) 
                System.out.println ( "Oak" ); 
            else 
                System.out.println ("Oops "); 
        } 
    }
    1. Pine

    2. Tree

    3. Forest

    4. Oops

    Answer
  • 12.

    What will be the output of the program?

    String x = "xyz";
    x.toUpperCase(); /* Line 2 */
    String y = x.replace('Y', 'y');
    y = y + "abc";
    System.out.println(y);
    1. abcXyZ

    2. abcxyz

    3. xyzabc

    4. XyZabc

    Answer
  • 13.

    Which statement is true?

    1. Assertions can be enabled or disabled on a class-by-class basis.

    2. Conditional compilation is used to allow tested classes to run at full speed.

    3. Assertions are appropriate for checking the validity of arguments in a method.

    4. The programmer can choose to execute a return statement or to throw an exception if an assertion fails.

    Answer
  • 14.

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

    What will be the output of the program (when you run with the -ea option) ?

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

    2. Compilation fails.

    3. An AssertionError is thrown.

    4. An AssertionError is thrown and finished is output.

    Answer
  • 16.

    Which statement is true?

    1. Memory is reclaimed by calling Runtime.gc().

    2. Objects are not collected if they are accessible from live threads.

    3. An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.

    4. Objects that have finalize() methods always have their finalize() methods called before the program ends.

    Answer
  • 17.

    Which statement is true?

    1. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
    2. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
    3. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
    4. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 18.

    What will be the output of the program?

    class Test116 
    { 
    static final StringBuffer sb1 = new StringBuffer(); 
    static final StringBuffer sb2 = new StringBuffer(); 
    public static void main(String args[]) 
    { 
        new Thread() 
        { 
            public void run() 
            { 
                synchronized(sb1) 
                { 
                    sb1.append("A"); 
                    sb2.append("B"); 
                } 
            } 
        }.start(); 
    
        new Thread() 
        { 
            public void run() 
            { 
                synchronized(sb1) 
                { 
                    sb1.append("C"); 
                    sb2.append("D"); 
                } 
            } 
        }.start(); /* Line 28 */
    
        System.out.println (sb1 + " " + sb2); 
        } 
    }
    1. main() will finish before starting threads.

    2. main() will finish in the middle of one thread.

    3. main() will finish after one thread.

    4. Cannot be determined.

    Answer
  • 19.

    What will be the output of the program?

    public class SyncTest 
    {
        public static void main (String [] args) 
        {
            Thread t = new Thread() 
            {
                Foo f = new Foo();
                public void run() 
                {
                    f.increase(20);
                }
            };
        t.start();
        }
    }
    class Foo 
    {
        private int data = 23;
        public void increase(int amt) 
        {
            int x = data;
            data = x + amt;
        }
    }

    and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?

    1. Synchronize the run method.

    2. Wrap a synchronize(this) around the call to f.increase().

    3. The existing code will cause a runtime exception.

    4. Synchronize the increase() method

    Answer
  • 20.

    Which of the following will not directly cause a thread to stop?

    1. notify()

    2. wait()

    3. InputStream access

    4. sleep()

    Answer

© 2017 QuizBucket.org