Java quiz questions

Java interview questions

  • 1.

    What will be the output of the program?

    int i = 0, j = 5; 
    tp: for (;;) 
        {
            i++;  
            for (;;) 
            {
                if(i > --j) 
                {
                    break tp; 
                } 
            } 
            System.out.println("i =" + i + ", j = " + j);
    1. i = 1, j = 0

    2. i = 1, j = 4

    3. i = 3, j = 4

    4. Compilation fails.

    Answer
  • 2.

    What will be the output of the program?

    int i = 0; 
    while(1) 
    {
        if(i == 4) 
        {
            break;
        } 
        ++i; 
    } 
    System.out.println("i = " + i);
    1. i = 0

    2. i = 3

    3. i = 4

    4. Compilation fails.

    Answer
  • 3.

    What will be the output of the program?

    public class If1 
    {
        static boolean b;
        public static void main(String [] args) 
        {
            short hand = 42;
            if ( hand < 50 && !b ) /* Line 7 */
                hand++;
            if ( hand > 50 );     /* Line 9 */
            else if ( hand > 40 ) 
            {
                hand += 7;
                hand++;    
            }
            else
                --hand;
            System.out.println(hand);
        }
    }
    1. 41

    2. 42

    3. 50

    4. 51

    Answer
  • 4.

    What will be the output of the program?

    public class If2 
    {
        static boolean b1, b2;
        public static void main(String [] args) 
        {
            int x = 0;
            if ( !b1 ) /* Line 7 */
            {
                if ( !b2 ) /* Line 9 */
                {
                    b1 = true;
                    x++;
                    if ( 5 > 6 ) 
                    {
                        x++;
                    }
                    if ( !b1 ) 
                        x = x + 10;
                    else if ( b2 = true ) /* Line 19 */
                        x = x + 100;
                    else if ( b1 | b2 ) /* Line 21 */
                        x = x + 1000;
                }
            }
            System.out.println(x);
        }
    }
    1. 0

    2. 1

    3. 101

    4. 111

    Answer
  • 5.

    What will be the output of the program?

    class Two 
    {
        byte x;
    }
    
    class PassO 
    {
        public static void main(String [] args) 
        {
            PassO p = new PassO();
            p.start();
        }
    
        void start() 
        {
            Two t = new Two();
            System.out.print(t.x + " ");
            Two t2 = fix(t);
            System.out.println(t.x + " " + t2.x);
        }
    
        Two fix(Two tt) 
        {
            tt.x = 42;
            return tt;
        }
    }
    1. null null 42

    2. 0 0 42

    3. 0 42 42

    4. 0 0 0

    Answer
  • 6.

    which two statements, added independently at beginning of the program, allow the code to compile?

    1. No statement is required
    2. import java.util.*;
    3. import.java.util.Tree*;
    4. import java.util.TreeSet;
    5. import java.util.TreeMap;
    /* Missing statements ? */
    public class NewTreeSet extends java.util.TreeSet
    {
        public static void main(String [] args) 
        {
            java.util.TreeSet t = new java.util.TreeSet();
            t.clear();
        }
        public void clear() 
        {
            TreeMap m = new TreeMap();
            m.clear();
        }
    }
    1. 1 only

    2. 2 and 5

    3. 3 and 4

    4. 3 and 5

    Answer
  • 7.

    Which statement is true?

    class A 
    { 
        A( ) { } 
    } 
    
    class B extends A 
    { }
    1. Class B'S constructor is public.

    2. Class B'S constructor has no arguments.

    3. Class B'S constructor includes a call to this( ).

    4. None of these.

    Answer
  • 8.

    What will be the output of the program?

    public class ArrayTest 
    { 
        public static void main(String[ ] args)
        { 
            float f1[ ], f2[ ]; 
            f1 = new float[10]; 
            f2 = f1; 
            System.out.println("f2[0] = " + f2[0]); 
        } 
    }
    1. It prints f2[0] = 0.0

    2. It prints f2[0] = NaN

    3. An error at f2 = f1; causes compile to fail.

    4. It prints the garbage value.

    Answer
  • 9.

    Which of the following class level (nonlocal) variable declarations will not compile?

    1. protected int a;

    2. transient int b = 3;

    3. private synchronized int e;

    4. volatile int d;

    Answer
  • 10.

    What will be the output of the program (in jdk1.6 or above)?

    public class BoolTest 
    {
        public static void main(String [] args) 
        {
            Boolean b1 = new Boolean("false");
            boolean b2;
            b2 = b1.booleanValue();
            if (!b2) 
            {
                b2 = true;
                System.out.print("x ");
            }
            if (b1 & b2) /* Line 13 */
            {
                System.out.print("y ");
            }
            System.out.println("z");
        }
    }
    1. z

    2. x z

    3. y z

    4. Compilation fails.

    Answer
  • 11.

    What will be the output of the program?

    public class Test138 
    { 
        public static void stringReplace (String text) 
        {
            text = text.replace ('j' , 'c'); /* Line 5 */
        } 
        public static void bufferReplace (StringBuffer text) 
        { 
            text = text.append ("c");  /* Line 9 */
        } 
        public static void main (String args[]) 
        { 
            String textString = new String ("java"); 
            StringBuffer textBuffer = new StringBuffer ("java"); /* Line 14 */
            stringReplace(textString); 
            bufferReplace(textBuffer); 
            System.out.println (textString + textBuffer); 
        } 
    }
    1. java

    2. javac

    3. javajavac

    4. Compile error

    Answer
  • 12.

    What will be the output of the program?

    public class NFE 
    {
        public static void main(String [] args) 
        {
        String s = "42";
            try 
            {
                s = s.concat(".5");  /* Line 8 */
                double d = Double.parseDouble(s);
                s = Double.toString(d);
                int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
                System.out.println(x);
            }
            catch (NumberFormatException e) 
            {
                System.out.println("bad number");
            }
        }
    }
    1. 42

    2. 42.5

    3. 43

    4. bad number

    Answer
  • 13.

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

    Which of the following statements is true?

    1. In an assert statement, the expression after the colon ( : ) can be any Java expression.

    2. If a switch block has no default, adding an assert default is considered appropriate.

    3. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.

    4. It is appropriate to handle assertion failures using a catch clause.

    Answer
  • 15.

    What will be the output of the program?

    public class Test 
    {
        public static int y;
        public static void foo(int x) 
        {
            System.out.print("foo ");
            y = x;
        }
        public static int bar(int z) 
        {
            System.out.print("bar ");
            return y = z;
        }
        public static void main(String [] args ) 
        {
            int t = 0;
            assert t > 0 : bar(7);
            assert t > 1 : foo(8); /* Line 18 */
            System.out.println("done ");
        }
    }
    1. bar

    2. bar done

    3. foo done

    4. Compilation fails

    Answer
  • 16.

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

    What will be the output of the program?

    class s implements Runnable 
    { 
        int x, y; 
        public void run() 
        { 
            for(int i = 0; i < 1000; i++) 
                synchronized(this) 
                { 
                    x = 12; 
                    y = 12; 
                } 
            System.out.print(x + " " + y + " "); 
        } 
        public static void main(String args[]) 
        { 
            s run = new s(); 
            Thread t1 = new Thread(run); 
            Thread t2 = new Thread(run); 
            t1.start(); 
            t2.start(); 
        } 
    }
    1. DeadLock

    2. It print 12 12 12 12

    3. Compilation Error

    4. Cannot determine output.

    Answer
  • 18.

    What will be the output of the program?

    class MyThread extends Thread 
    { 
        MyThread() {} 
        MyThread(Runnable r) {super(r); } 
        public void run() 
        { 
            System.out.print("Inside Thread ");
        } 
    } 
    class MyRunnable implements Runnable 
    { 
        public void run() 
        { 
            System.out.print(" Inside Runnable"); 
        } 
    } 
    class Test 
    {  
        public static void main(String[] args) 
        { 
            new MyThread().start(); 
            new MyThread(new MyRunnable()).start(); 
        } 
    }
    1. Prints "Inside Thread Inside Thread"

    2. Prints "Inside Thread Inside Runnable"

    3. Does not compile

    4. Throws exception at runtime

    Answer
  • 19.

    What will be the output of the program?

    class MyThread extends Thread 
    {
        public static void main(String [] args) 
        {
            MyThread t = new MyThread();
            t.start();
            System.out.print("one. ");
            t.start();
            System.out.print("two. ");
        }
        public void run() 
        {
            System.out.print("Thread ");
        }
    }
    1. Compilation fails

    2. An exception occurs at runtime.

    3. It prints "Thread one. Thread two."

    4. The output cannot be determined.

    Answer
  • 20.

    Which of the following line of code is suitable to start a thread ?

    class X implements Runnable 
    { 
        public static void main(String args[]) 
        {
            /* Missing code? */
        } 
        public void run() {} 
    }
    1. Thread t = new Thread(X);

    2. Thread t = new Thread(X); t.start();

    3. X run = new X(); Thread t = new Thread(run); t.start();

    4. Thread t = new Thread(); x.run();

    Answer

© 2017 QuizBucket.org