Preview

26 - Informal Code Analysis

 1. What does the following code print when x has been set to 187?
if (x < 0) System.out.println("x is negative");
else if (x == 0) System.out.println("x is zero");
else System.out.println("x is positive");

  x is positive

  x is NULL

  x is zero

  x is negative

 2. When the following code executes and x equals 4 and y equals 3, 'second case' is printed.
if (!(x < 3 || y > 2)) System.out.println("first case");
else System.out.println("second case");

  FALSE

  TRUE

 3. What is the value of grade when the following code executes and score is 80?
if (score >= 90) grade = "A";
if (score >= 80) grade = "B";
if (score >= 70) grade = "C";
if (score >= 60) grade = "D";
else grade = "E";

  B

  D

  C

  A

 4. Which of the following expressions is equivalent to !(c || d) ?

  (c || d)

  !(c && d)

  (!c) && (!d)

  (c && d)

 5. The following method will return true if and only if:
public boolean check(String s) {
   return s.length() >= 2 && (s.charAt(0) ==
      s.charAt(1) || check(s.substring(1)));
}

  s contains two or more of the same character in a row

  s.charAt(0) == s.charAt(1)

   s starts with two or more of the same characters

  s contains two or more of the same characters

 6. Ruth is 5 years older than Jonathan. 3 years from now Ruth?s age will be twice Jonathan?s age. What should be in place of the following condition to solve this problem?
for (int s = 1; s <=100; s++) {
   for (int m = 1; m <= 100; m++) {
      if (condition)
         System.out.println("Ruth is " + s + " and Jonathan is " + m);
   }
}

  s == (m - 5) && (2 * s + 3) == (m + 3)

  (s == m - 5) && (s - 3 == 2 * (m - 3))

   (s == (m + 5)) && ((s + 3) == (2 * m + 3))

  s == m + 5 && s + 3 == 2 * m + 6

 7. Assuming that x and y have been declared as valid integer values, which of the following is equivalent to this statement?
(x > 15 && x < 18) || (x > 10 || y < 20)

   (x < 10 && y > 20) && (x < 15 || x > 18)

  (x > 10)|| (y < 20)

  (y < 20) || (x > 15 && x < 18)

   (x > 15 && x < 18) && (x > 10)

 8. What would the following print?
int x = 3;
int y = 2;
if (x > 2) x++;
if (y > 1) y++;
if (x > 2) System.out.print("first ");
if (y < 3) System.out.print("second ");
System.out.print("third");

  first third

  first second third

  first second

  first

 9. What would the following print?
int x = 3;
int y = 2;
if (y / x > 0)
   System.out.print("first ");
   System.out.print("second ");

  first second

  first

  Nothing will be printed

  second

 10. How many stars are output when the following is output?
for (int i = 0; i < 5; i++) {
   for (int j = 0; j < 5; j++)
      System.out.println("*");
}for (int i = 0; i < 5; i++) {
   for (int j = 0; j < 5; j++)
      System.out.println("*");
}

 11. What are the values of var1 and var2 after the following code segment is executed and the while loop finishes?
class Main {
  public static void main(String[] args) {
    int var1=0;
    int var2=2;
    while ((var2!=0)&&((var1/var2)>=0)) {
      var1=var1+1;
      var2=var2-1;
    }
    System.out.println("var1: "+var1);
    System.out.println("var2: "+var2);
  }
}

  var1 = 0, var2 = 2

  var1 = 2, var2 = 0

  var1 = 3, var2 = -1

  var1 = 1, var2 = 1

 12. What is printed as a result of the following code segment?
for (int k = 0; k < 20; k+=2) {
   if (k % 3 == 1)
      System.out.println(k + " ");
}

  Answer: 0 6 12 18

  Answer: 4 10 16

  Answer: 4 16

  Answer: 0 2 4 6 8 10 12 14 16 18

 13. Writing down the values of the variables and how they change each time the body of the loop executes - this is also called.

  variable loop

  trace code (or a trace table)

  merge code

  maze variables

 14. Getting the start and end conditions wrong on the for loop. This will often result in you getting an ______________.

  syntax error

  out of bounds error

  mainframe error

  logical error

 15. ___________: The type of evaluation used for logical and "&&" and logical or "||" expressions.
Note: If the first condition is false in a complex conditional with a logical and the second condition won?t be evaluated. If the first condition is true is a complex conditional with a logical or the second condition won?t be evaluated.

  variable integer evaluation

  short circuit evaluation

  complex logical evaluation

  mid term evaluation