1. Question: What will be the output of the program?
    class SC2 
    {
        public static void main(String [] args) 
        {
            SC2 s = new SC2();
            s.start();
        }
    
        void start() 
        {
            int a = 3;
            int b = 4;
            System.out.print(" " + 7 + 2 + " ");
            System.out.print(a + b);
            System.out.print(" " + a + b + " ");
            System.out.print(foo() + a + b + " ");
            System.out.println(a + b + foo());
        }
    
        String foo() 
        {
            return "foo";
        }
    }

    A
    9 7 7 foo 7 7foo

    B
    72 34 34 foo34 34foo

    C
    9 7 7 foo34 34foo

    D
    72 7 34 foo34 7foo

    Note: Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.
    1. Report
  2. Question: What will be the output of the program?
    class Test 
    {
        static int s;
        public static void main(String [] args) 
        {
            Test p = new Test();
            p.start();
            System.out.println(s);
        }
    
        void start() 
        {
            int x = 7;
            twice(x);
            System.out.print(x + " ");
        }
    
        void twice(int x) 
        {
            x = x*2;
            s = x;
        }
    }

    A
    7 7

    B
    7 14

    C
    14 0

    D
    14 14

    Note: The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14.
    1. Report
  3. Question: 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;
        }
    }

    A
    null null 42

    B
    0 0 42

    C
    0 42 42

    D
    0 0 0

    Note: In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.
    1. Report
  4. Question: What will be the output of the program?
    class BoolArray 
    {
        boolean [] b = new boolean[3];
        int count = 0;
    
        void set(boolean [] x, int i) 
        {
            x[i] = true;
            ++count;
        }
    
        public static void main(String [] args) 
        {
            BoolArray ba = new BoolArray();
            ba.set(ba.b, 0);
            ba.set(ba.b, 2);
            ba.test();
        }
    
        void test() 
        {
            if ( b[0] && b[1] | b[2] )
                count++;
            if ( b[1] && b[(++count - 2)] )
                count += 7;
            System.out.println("count = " + count);
        }
    }

    A
    count = 0

    B
    count = 2

    C
    count = 3

    D
    count = 4

    Note: The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test.
    1. Report
  5. Question: What will be the output of the program?
    public class Test 
    { 
        public static void leftshift(int i, int j) 
        {
            i <<= j; 
        } 
        public static void main(String args[]) 
        {
            int i = 4, j = 2; 
            leftshift(i, j); 
            System.out.printIn(i); 
        } 
    }

    A
    2

    B
    4

    C
    8

    D
    16

    Note: Java only ever passes arguments to a method by value (i.e. a copy of the variable) and never by reference. Therefore the value of the variable i remains unchanged in the main method. If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you had 16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If you had 128 right shifted by 2 bits then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting to binary to do the left and right bit shifts.
    1. Report
Copyright © 2024. Powered by Intellect Software Ltd