Multiple Choice Questions 3

  1. Analyze the following code:
    if (x <100) && (x >10)
    System.out.println("x is between 10 and 100");

  2. The statement has syntax errors because (x<100) && (x > 10) must be enclosed inside parentheses.
    The statement has syntax errors because (x<100) && (x > 10) must be enclosed inside parentheses and      the println(…) statement must be put inside a block.
    The statement compiles fine.

  3. Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?
    if (x > 0)
    if (y > 0)
    System.out.println("x > 0 and y > 0");
    else if (z > 0)
    System.out.println("x < 0 and z > 0");

  4. x > 0 and y > 0;
    x < 0 and z > 0;
    x < 0 and z < 0;
    none of the above

  5. What is y after the following switch statement is executed?
    x = 3;
    switch (x+3)
    { case 6: y = 1;
    default: y += 1; }

  6. 1
    2
    3
    4

  7. What is y after the following statement is executed?
    x = 0;
    0) ? 1 : -1;

  8. -1
    0
    1
    2

  9. Analyze the following program fragment:
    int x;
    double d = 1.5;
    switch (d)
    { case 1.0: x = 1;
    case 1.5: x = 2; }

  10. The required break statement is missing in the switch statement.
    The required default case is missing in the switch statement.
    The switch control variable cannot be double.
    None of the above.

  11. Analyze the following statement:
    double sum = 0;
    for (double d = 0; d<10; )
    { d += 0.1;
    sum += sum + d; }

  12. The program has a syntax error because the adjustment is missing in the for loop.
    The program has a syntax error because the control variable in the for loop cannot be of the double type.
    The program compiles but does not stop because d would never be less than 10.
    The program compiles and runs fine.

  13. Do the following two statements result in the same value in sum?
    for (int i=0; i<10; ++i)
    { sum += i; }
    for (int i=0; i<10; i++)
    { sum += i; }

  14. Yes
    No

  15. What is y after the following for loop statement is executed?
    int y = 0;
    for (int i=0; i<10; ++i)
    { y += i; }

  16. 10
    11
    12
    None of the above.

  17. What is i after the following for loop?
    for (int i=0; i<10; ++i)
    { y += i; }

  18. 9
    10
    11
    undefined

  19. Is the following loop correct?
    for (; ; );

  20. Yes
    No

  21. Will the following program terminate?
    int balance = 10;
    while (true)
    { if (balance < 9) continue;
    balance = balance - 9; }

  22. Yes
    No

  23. Will the following program terminate?
    int balance = 10;
    while (true)
    { if (balance < 9) break;
    balance = balance - 9; }

  24. Yes
    No

  25. What is sum after the following loop terminates?
    int sum = 0;
    int item = 0;
    do
    { item++;
    sum += item;
    if (sum > 4) break;
    }
    while (item < 5);

  26. 5
    6
    7
    8

  27. What is sum after the following loop terminates?
    int sum = 0;
    int item = 0;
    do
    { item++;
    sum += item;
    if (sum >= 4) continue;
    }
    while (item < 5);

  28. 5
    6
    7
    8

  29. Analyze the following statement:
    double sum = 0;
    double d = 0;
    while (d == 10.0)
    { d += 0.1;
    sum += sum + d; }

  30. The program does not compile because sum and d are declared double, but assigned with integer value 0.
    The program never stops because d is always 0.1 inside the loop.
    The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating      with floating-point numbers.
    None of the above.

Visited times since 3 Feb 1999.