Wednesday 25 July 2012

Operators and Assignments - Objective Questions - Part 1

1)

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

Answer: B


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.

2)

import java.awt.Button;
class CompareReference
{
    public static void main(String [] args)
    {
        float f = 42.0f;
        float [] f1 = new float[2];
        float [] f2 = new float[2];
        float [] f3 = f1;
        long x = 42;
        f1[0] = 42.0f;
    }
}

which three statements are true?

   1. f1 == f2
   2. f1 == f3
   3. f2 == f1[1]
   4. x == f1[0]
   5. f == f1[0]

A. 1, 2 and 3
B. 2, 4 and 5
C. 3, 4 and 5
D. 1, 4 and 5

Answer: B

(2) is correct because the reference variables f1  and f3 refer to the same array object.
(4) is correct because it is legal to compare integer and floating-point types.
(5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.

3)

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

Answer: C

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.

4)

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

Answer: D

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.

5)

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

Answer: B

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".

6)

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

Answer: D

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".

7)

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

Answer: D

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.

8)

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

Answer: C

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.

9)

import java.awt.*;
class Ticker extends Component
{    public static void main (String [] args)
    {    Ticker t = new Ticker();
        /* Missing Statements ? */
    }}

which two of the following statements, inserted independently, could legally be inserted
into missing section of this code?

   1. boolean test = (Component instanceof t);
   2. boolean test = (t instanceof Ticker);
   3. boolean test = t.instanceof(Ticker);
   4. boolean test = (t instanceof Component);

A. 1 and 4
B. 2 and 3
C. 1 and 3
D. 2 and 4

Answer: D

(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of
the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component.(1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.

10)

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

Answer: B

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.

No comments:

Post a Comment