MCQ Questions

Question Bank › Technical › Java › MCQ

123

Other questions

Q201.Hard
What will be the output of this Java program?

int i = 1, j = 10;
do {
  if (i++ > --j) {
    continue;
  }
} while (i < 5);
System.out.println("i = " + i + " and j = " + j);
  1. i = 5 and j = 6
  2. i = 6 and j = 5
  3. i = 5 and j = 5
  4. i = 6 and j = 6
Show answer & explanation
Answer: (A) i = 5 and j = 6
Explanation: Step 1: i++ uses i then increments; --j decrements j then uses it. Comparison uses the new j and old i. Step 2: Iter1: compare 1 > 9? No. i becomes 2, j becomes 9. i<5 true, loop again. Step 3: Iter2: 2 > 8? No. i=3, j=8. Loop again. Step 4: Iter3: 3 > 7? No. i=4, j=7. Loop again. Step 5: Iter4: 4 > 6? No. i=5, j=6. Now i<5 is false, loop ends. Step 6: continue never runs; prints i = 5 and j = 6.
Q202.Hard
What will be the output of the program?
public class WrapTest {
  public static void main(String[] args) {
    int result = 0;
    short s = 42;
    Long x = new Long("42");
    Long y = new Long(42);
    Short z = new Short("42");
    Short x2 = new Short(s);
    Integer y2 = new Integer("42");
    Integer z2 = new Integer(42);
    if (x == y) /* Line 13 */
      result = 1;
    if (x.equals(y)) /* Line 15 */
      result = result + 10;
    if (x.equals(z)) /* Line 17 */
      result = result + 100;
    if (x.equals(x2)) /* Line 19 */
      result = result + 1000;
    if (x.equals(z2)) /* Line 21 */
      result = result + 10000;
    System.out.println("result = " + result);
  }
}
  1. result = 1
  2. result = 10
  3. result = 11
  4. result = 11010
Show answer & explanation
Answer: (B) result = 10
Explanation: 1. Line 13: x == y compares references of two different Long objects, so false; result stays 0. 2. Line 15: x.equals(y) compares values 42 and 42 of same class Long, so true; result = 10. 3. Line 17: x.equals(z) compares Long with Short, different classes, so false. 4. Line 19: x.equals(x2) compares Long with Short, false. 5. Line 21: x.equals(z2) compares Long with Integer, false. 6. Final result = 10.
Q203.Hard
What will be the output of this program?

public class Race {
    public static void main(String[] args) {
        final StringBuffer a = new StringBuffer();
        final StringBuffer b = new StringBuffer();
        new Thread() {
            public void run() {
                System.out.print(a.append("X"));
                synchronized(b) { System.out.print(b.append("Y")); }
            }
        }.start();
        new Thread() {
            public void run() {
                System.out.print(b.append("Z"));
                synchronized(a) { System.out.print(a.append("W")); }
            }
        }.start();
    }
}
  1. XZZYXW
  2. XYZWXZ
  3. Indeterminate output
  4. Compilation fails
Show answer & explanation
Answer: (C) Indeterminate output
Explanation: 1) Two threads run concurrently with no coordination between them. 2) Each thread appends to a shared StringBuffer without synchronizing on that same buffer, and the two synchronized blocks lock different objects (b in thread 1, a in thread 2). 3) The interleaving of appends and prints depends on thread scheduling, so the output differs from run to run. 4) Hence the output is indeterminate.
Q204.Hard
Which of the following are valid declarations of a char in Java?
char c1 = 054321;
char c2 = 'word';
char c3 = 0xabcd;
char c4 = \u0041;
char c5 = '\iabc';
char c6 = '\u0042';
  1. 1, 2, 4
  2. 1, 3, 6
  3. 3, 5
  4. 5 only
Show answer & explanation
Answer: (B) 1, 3, 6
Explanation: 1) c1 = 054321 is an octal integer literal; its value fits in unsigned 16 bits, so it is valid. 2) c2 = 'word' is invalid because a char literal can hold only one character. 3) c3 = 0xabcd is a hex integer literal within 0-65535, so it is valid. 4) c4 = \u0041 lacks the surrounding single quotes, so it is invalid. 5) c5 = '\iabc' looks like a Unicode escape but starts with \i instead of \u, so it is invalid. 6) c6 = '\u0042' is a proper Unicode char literal, so it is valid. 7) Valid ones are 1, 3, 6.
Q205.Hard
Which of the following are true statements?
1. The Iterator interface declares only three methods: hasNext, next and remove.
2. The ListIterator interface extends both the List and Iterator interfaces.
3. The ListIterator interface provides forward and backward iteration capabilities.
4. The ListIterator interface provides the ability to modify the List during iteration.
5. The ListIterator interface provides the ability to determine its position in the List.
  1. 2, 3, 4 and 5
  2. 1, 3, 4 and 5
  3. 3, 4 and 5
  4. 1, 2 and 3
Show answer & explanation
Answer: (B) 1, 3, 4 and 5
Explanation: 1) Statement 1 is true: Iterator declares hasNext, next and remove. 2) Statement 2 is false: ListIterator extends only the Iterator interface, not List. 3) Statement 3 is true: ListIterator supports forward and backward iteration. 4) Statement 4 is true: it can modify the list during iteration (add/set/remove). 5) Statement 5 is true: it can report its position via nextIndex/previousIndex. 6) So the true statements are 1, 3, 4 and 5.
Q206.Hard
What will be the output of this Java program?

class FlagArray {
    boolean[] f = new boolean[3];
    int count = 0;
    void set(boolean[] x, int i) {
        x[i] = true;
        ++count;
    }
    public static void main(String[] args) {
        FlagArray fa = new FlagArray();
        fa.set(fa.f, 0);
        fa.set(fa.f, 2);
        fa.check();
    }
    void check() {
        if (f[0] && f[1] | f[2])
            count++;
        if (f[1] && f[(++count - 2)])
            count += 5;
        System.out.println("count = " + count);
    }
}
  1. count = 0
  2. count = 2
  3. count = 3
  4. count = 4
Show answer & explanation
Answer: (C) count = 3
Explanation: 1. f is a boolean array of size 3, all false initially; count = 0. 2. set(f,0): f[0]=true, count=1. set(f,2): f[2]=true, count=2. 3. First if: f[0] && f[1] | f[2]. f[0]=true, f[1]=false, f[2]=true. (true && false) | true = false | true = true, so count becomes 3. 4. Second if: f[1] && f[(++count - 2)]. f[1] is false, so && short-circuits; ++count is NOT evaluated. 5. count stays 3. Output: count = 3.
Q207.Hard
What will be the output of the program?
class BitShift {
  public static void main(String [] args ) {
    int x = 0x80000000 ;
    System.out.print (x + " and " );
    x = x >>> 31 ;
    System.out.println (x);
  }
}
  1. -2147483648 and 1
  2. 0x80000000 and 0x00000001
  3. -2147483648 and -1
  4. 1 and -2147483648
Show answer & explanation
Answer: (A) -2147483648 and 1
Explanation: 0x80000000 is the int with only the sign bit set, which equals -2147483648 in decimal. The >>> operator shifts right filling left bits with zeros: 1000...0000 becomes 0000...0001, i.e. 1. print() shows integers in base 10, so output is '-2147483648 and 1'.
Q208.Hard
What will be the output of this program?

public class Task implements Runnable {
    private int p;
    private int q;
    public static void main(String[] args) {
        Task obj = new Task();
        (new Thread(obj)).start();
        (new Thread(obj)).start();
    }
    public synchronized void run() {
        for (;;) {
            p++;
            q++;
            System.out.println("p = " + p + " q = " + q);
        }
    }
}
  1. An error at the run() method declaration causes compilation to fail
  2. Errors at the two start() lines cause compilation to fail
  3. The program prints pairs of values for p and q that might not always be the same on the same line (for example, "p=2 q=1")
  4. The program prints pairs of values for p and q that are always the same on the same line (for example, "p=1 q=1"), and each value appears once (for example, "p=1 q=1" followed by "p=2 q=2")
Show answer & explanation
Answer: (D) The program prints pairs of values for p and q that are always the same on the same line (for example, "p=1 q=1"), and each value appears once (for example, "p=1 q=1" followed by "p=2 q=2")
Explanation: 1. Both threads share the same Task object. 2. run() is synchronized, so only one thread can execute it at a time. 3. Inside run() p and q are incremented together, so they always match on a printed line. 4. The infinite loop means the first thread that enters run() never releases the monitor, so only one thread ever executes. 5. Output is p=1 q=1, p=2 q=2, ... with matching values, each appearing once.
Q209.Hard
Which three of the following 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
Show answer & explanation
Answer: (B) 2, 5 and 6
Explanation: 1) wait() always moves the current thread into the object's wait set — guaranteed. 2) sleep(1000) always pauses the current thread for at least the given time — guaranteed. 3) join() on a live thread blocks the calling thread until that thread dies — guaranteed. 4) yield() is only a hint and may not change state. 5) notify()/notifyAll() affect other waiting threads, not the caller. 6) Thread.killThread() does not exist. So the guaranteed three are 2, 5 and 6.
123

Get In Touch

INTMAT EDTECH (OPC) PRIVATE LIMITED
No - 16, Javaraiahna Doddi, Mysore Road,
Bengaluru Urban, BENGALURU,
Karnataka 560098, India

contact@intmatedtech.com

TestMyIndia All India Rank