1. Question: What will be the output of the program?
    1. class PassA
    2. {
    3. public static void main(String [] args)
    4. {
    5. PassA p = new PassA();
    6. p.start();
    7. }
    8.  
    9. void start()
    10. {
    11. long [] a1 = {3,4,5};
    12. long [] a2 = fix(a1);
    13. System.out.print(a1[0] + a1[1] + a1[2] + " ");
    14. System.out.println(a2[0] + a2[1] + a2[2]);
    15. }
    16.  
    17. long [] fix(long [] a3)
    18. {
    19. a3[1] = 7;
    20. return a3;
    21. }
    22. }

    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?
    1. class Test
    2. {
    3. public static void main(String [] args)
    4. {
    5. Test p = new Test();
    6. p.start();
    7. }
    8.  
    9. void start()
    10. {
    11. boolean b1 = false;
    12. boolean b2 = fix(b1);
    13. System.out.println(b1 + " " + b2);
    14. }
    15.  
    16. boolean fix(boolean b1)
    17. {
    18. b1 = true;
    19. return b1;
    20. }
    21. }

    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?
    1. class PassS
    2. {
    3. public static void main(String [] args)
    4. {
    5. PassS p = new PassS();
    6. p.start();
    7. }
    8.  
    9. void start()
    10. {
    11. String s1 = "slip";
    12. String s2 = fix(s1);
    13. System.out.println(s1 + " " + s2);
    14. }
    15.  
    16. String fix(String s1)
    17. {
    18. s1 = s1 + "stream";
    19. System.out.print(s1 + " ");
    20. return "stream";
    21. }
    22. }

    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?
    1. class BitShift
    2. {
    3. public static void main(String [] args)
    4. {
    5. int x = 0x80000000;
    6. System.out.print(x + " and ");
    7. x = x >>> 31;
    8. System.out.println(x);
    9. }
    10. }

    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?
    1. class Equals
    2. {
    3. public static void main(String [] args)
    4. {
    5. int x = 100;
    6. double y = 100.1;
    7. boolean b = (x = y); /* Line 7 */
    8. System.out.println(b);
    9. }
    10. }

    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?
    1. class Test
    2. {
    3. public static void main(String [] args)
    4. {
    5. int x=20;
    6. String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
    7. System.out.println(sup);
    8. }
    9. }

    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?
    1. class Test
    2. {
    3. public static void main(String [] args)
    4. {
    5. int x= 0;
    6. int y= 0;
    7. for (int z = 0; z < 5; z++)
    8. {
    9. if (( ++x > 2 ) && (++y > 2))
    10. {
    11. x++;
    12. }
    13. }
    14. System.out.println(x + " " + y);
    15. }
    16. }

    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?
    1. class Test
    2. {
    3. public static void main(String [] args)
    4. {
    5. int x= 0;
    6. int y= 0;
    7. for (int z = 0; z < 5; z++)
    8. {
    9. if (( ++x > 2 ) || (++y > 2))
    10. {
    11. x++;
    12. }
    13. }
    14. System.out.println(x + " " + y);
    15. }
    16. }

    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?
    1. class Bitwise
    2. {
    3. public static void main(String [] args)
    4. {
    5. int x = 11 & 9;
    6. int y = x ^ 3;
    7. System.out.println( y | 12 );
    8. }
    9. }

    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?
    1. class SSBool
    2. {
    3. public static void main(String [] args)
    4. {
    5. boolean b1 = true;
    6. boolean b2 = false;
    7. boolean b3 = true;
    8. if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
    9. System.out.print("ok ");
    10. if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
    11. System.out.println("dokey");
    12. }
    13. }

    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 © 2025. Powered by Intellect Software Ltd