1. Question: What will be the output of the program?
    class PassA 
    {
        public static void main(String [] args) 
        {
            PassA p = new PassA();
            p.start();
        }
    
        void start() 
        {
            long [] a1 = {3,4,5};
            long [] a2 = fix(a1);
            System.out.print(a1[0] + a1[1] + a1[2] + " ");
            System.out.println(a2[0] + a2[1] + a2[2]);
        }
    
        long [] fix(long [] a3) 
        {
            a3[1] = 7;
            return a3;
        }
    }

    A
    12 15

    B
    15 15

    C
    3 4 5 3 7 5

    D
    3 7 5 3 7 5

    Note: The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object. So Output: 3+7+5+" "3+7+5 Output: 15 15 Because Numeric values will be added
    1. Report
  2. Question: What will be the output of the program?
    class Test 
    {
        public static void main(String [] args) 
        {
            Test p = new Test();
            p.start();
        }
    
        void start() 
        {
            boolean b1 = false;
            boolean b2 = fix(b1);
            System.out.println(b1 + " " + b2);
        }
    
        boolean fix(boolean b1) 
        {
            b1 = true;
            return b1;
        }
    }

    A
    true true

    B
    false true

    C
    true false

    D
    false false

    Note: The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
    1. Report
  3. Question: What will be the output of the program?
    class PassS 
    {
        public static void main(String [] args) 
        {
            PassS p = new PassS();
            p.start();
        }
    
        void start() 
        {
            String s1 = "slip";
            String s2 = fix(s1);
            System.out.println(s1 + " " + s2);
        }
    
        String fix(String s1) 
        {
            s1 = s1 + "stream";
            System.out.print(s1 + " ");
            return "stream";
        }
    }

    A
    slip stream

    B
    slipstream stream

    C
    stream slip stream

    D
    slipstream slip stream

    Note: When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".
    1. Report
  4. Question: 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);
        }
    }

    A
    -2147483648 and 1

    B
    0x80000000 and 0x00000001

    C
    -2147483648 and -1

    D
    1 and -2147483648

    Note: Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this: Before: 1000 0000 0000 0000 0000 0000 0000 0000 After: 0000 0000 0000 0000 0000 0000 0000 0001 Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown. Option B is incorrect because the output method print() always displays integers in base 10. Option D is incorrect because this is the reverse order of the two output numbers.
    1. Report
  5. Question: What will be the output of the program?
    class Equals 
    {
        public static void main(String [] args) 
        {
            int x = 100;
            double y = 100.1;
            boolean b = (x = y); /* Line 7 */
            System.out.println(b);
        }
    }

    A
    true

    B
    false

    C
    Compilation fails

    D
    An exception is thrown at runtime

    Note: The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables. Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.
    1. Report
  6. Question: 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);
        }
    }

    A
    small

    B
    tiny

    C
    huge

    D
    Compilation fails

    Note: This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.
    1. Report
  7. Question: 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);
        }
    }

    A
    5 2

    B
    5 3

    C
    6 3

    D
    6 4

    Note: In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented.
    1. Report
  8. Question: 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);
        }
    }

    A
    5 3

    B
    8 2

    C
    8 3

    D
    8 5

    Note: The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.
    1. Report
  9. Question: What will be the output of the program?
    class Bitwise 
    {
        public static void main(String [] args) 
        {
            int x = 11 & 9;
            int y = x ^ 3;
            System.out.println( y | 12 );
        }
    }

    A
    0

    B
    7

    C
    8

    D
    14

    Note: The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.
    1. Report
  10. Question: What will be the output of the program?
    class SSBool 
    {
        public static void main(String [] args) 
        {
            boolean b1 = true;
            boolean b2 = false;
            boolean b3 = true;
            if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
                System.out.print("ok ");
            if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
                System.out.println("dokey");
        }
    }

    A
    ok

    B
    dokey

    C
    ok dokey

    D
    No output is produced

    E
    Compilation error

    Note: The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd