2014-2015 Mid-Term Review AP Java

  1. If a picture was made up of 64 possible colors, how many bits would be needed to store each pixel of the picture?
  2. 4
  3. 5
  4. 6
  5. 7
  6. 8
  7. How many bits are there in 12 KB?
  8. 12,000
  9. 98,304
  10. 8192
  11. 9600
  12. 12,288
  13. Which of the following is equivalent to 220 x 22 bits?
  14. 2 KB
  15. 4 KB
  16. 2 MB
  17. 4 MB
  18. 4 GB
  19. How many different items can be represented with 11 bits?
  20. 11
  21. 22
  22. 121
  23. 1100
  24. 2048
  1. Which of the following is an example of an analog device?
  2. mercury thermometer
  3. computer
  4. music CD
  5. digital alarm clock
  6. vending machine
  7. Which of the following is not a valid Java identifier?
  8. Factorial
  9. anExtremelyLongIdentifierIfYouAskMe
  10. 2ndLevel
  11. level2
  12. highest$
  13. Which of the following is a valid Java Identifier?
  14. 14andCounting
  15. max_value
  16. 123
  17. %taxRate
  18. hook&ladder
  1. Which of the following pairs of variables are different from each other?
  2. Total and total
  3. case and Case
  4. codeTwo and code2
  5. oneMore and one_More
  6. all of the above
  1. What will be printed by the following statement?

System.out.println(“Use a \”\\\””);

  1. Use a \”\\\”
  2. “Use a “\””
  3. Use a “\”
  4. Use a \”\””
  5. Use a “\\”
  1. Which keyword is used to declare a constant?
  2. int
  3. double
  4. MAX
  5. constant
  6. final
  7. The expression “number” + 6 + 4 * 5 produces which of the following string literals?
  8. “number645”
  9. “number105”
  10. “number50”
  11. “number620”
  12. “number26”
  13. Which of the following is a character literal?
  14. b
  15. ‘b’
  16. “b”
  17. 2
  18. 2.0
  1. What is the result of the operation 30 % 4?
  2. 2
  3. 3
  4. 4
  5. 7
  6. 7.5
  7. The expression 1 / 4 is equal to which of the following?
  8. 1.0 / 4.0
  9. (double) 2 / 8
  10. 0.25
  11. (int) 1.0 / 4.0
  12. 1 / (int) 4.0
  13. Which of the following instantiates a String object?
  14. String word;
  15. word = new String(“the”);
  16. word.length();
  17. word = name;
  18. String word = name;
  19. Assuming g is an instance of the Random class, which statement will generate a random number between 10 and 100 inclusive?
  20. num = g.nextInt(101);
  21. num = 10 + g.nextInt(101);
  22. num = 10 + g.nextInt(91);
  23. num = 10 + g.nextInt(90);
  24. num = g.nextInt(110) – 10;
  25. Which statement would we use to create an object from a class called Thing?
  26. Thing something;
  27. Thing something = Thing ();
  28. Thing something = new Thing;
  29. Thing something = new Thing ();
  30. new Thing = something;
  31. Suppose we have a variable something that is a reference to a Thing object. How would we call the method doIt on our Thing object?
  32. doIt();
  33. something.doIt();
  34. doIt(something);
  35. something/doIt;
  36. something(doIt);
  37. Which of the following statements increase the value of x by 1?
  38. x++;
  39. x = x +1;
  40. x += 1;
  1. i only
  2. ii only
  3. i and iii
  4. ii and iii
  5. i, ii, and iii
  1. What will be printed by the following code segment?

boolean flag = true;

int x = -1;

if (flag & (x > 0))

System.out.println(“yes”);

else if (x == 0)

System.out.println(“maybe”);

else if (!flag)

System.out.println(“sometimes”);

else

System.out.println(“no”);

  1. yes
  2. maybe
  3. sometimes
  4. no
  5. There will be an error because you can’t mix integers and Booleans in the same expression.
  1. The expression !f || g is the same as which of the following?
  2. f || !g
  3. !(f || g)
  4. !(f & g)
  5. !(!f & !g)
  6. !(f & !g)
  1. In the following code, what value should go in the blank so that there are exactly six lines of output?

for (int x = 0; x < ______; x = x +2)

System.out.println(“-“);

  1. 5
  2. 6
  3. 10
  4. 11
  5. 13
  1. What will be the largest value printed by the following code?

for (int x = 5; x > 0; x--)

for(int y = 0; y < 8; y ++)

System.out.println(x * y);

  1. 5
  2. 8
  3. 35
  4. 40
  5. 64
  1. Assume x is an integer and has been initialized to some value. Consider the code:

for (int a = 1; a < 20; a ++)

if (x < 0)

x = a;

Which statement will have the same effect on the value of x?

  1. if ( x < 0 )x = 1;
  2. if (x < 20)x = 19;
  3. if (x < 0)x = 19;
  4. if (x < 20)x = 20;
  5. x = 1;
  1. Assume num and max are integer variables. Consider the code:

while (num < max)

num++;

Which values of num and max will cause the body of the loop to be executed

exactly once?

  1. num = 1, max = 1;
  2. num = 1, max = 2;
  3. num = 2, max = 2;
  4. num = 2, max = 1;
  5. num = 1, max = 3;
  1. Which for loop is equivalent to this while loop?

int y = 5;

while (y >= 0)

{

System.out.println(y);

y--;

}

  1. for (int y = 0; y < 5; y ++)

System.out.println(y);

  1. for (int y = 5; y > 0; y --)

System.out.println(y);

  1. for (int y = 5; y >= 0; y --)

System.out.println(y);

  1. for (int y = 0; y > 5; y ++)

System.out.println(y);

  1. for (int y = 0; y > 5; y --)

System.out.println (y);

  1. Which expression tests to make sure the grade is between0 and 100 inclusive?
  2. (grade <= 100) || (grade <= 0)
  3. (grade <= 100) || (grade >= 0)
  4. (grade <101) || (grade > -1)
  5. (grade <= 100) & (grade >= 0)
  6. (grade >= 100) & (grade <= 0)
  7. Which values of x, y, a, or b will cause the if statement to be short-circuited?

if ((x > y) & (a || b))

statement;

  1. x = 1, y = 1
  2. x = 5, y = 1
  3. x = 2, y = 1, a = true, b = false
  4. a = false, b = true
  5. a = false, b = false

Questions 29-32 refer to the following class.

publicclass Point

{

privateintmyX;

privateintmyY;

public Point ()

{

myX = 0;

myY = 0;

}

public Point (intx, inty)

{

myX = x;

myY = y;

}

publicint getX()

{

returnmyX;

}

publicint getY()

{

returnmyY;

}

}

  1. Which of the following statements creates a point with coordinates (0,0)?
  2. p = new Point ();
  3. p = Point (0, 0);
  4. p = (0,0);
  1. i only
  2. ii only
  3. iii only
  4. i and ii only
  5. ii and iii only
  1. Which of the following statements is true regarding the Point class?
  2. The class won’t compile because there are two methods name Point.
  3. Variable myX and myY can be changed from outside of the class.
  4. Point objects are immutable.
  5. It’s impossible to create Point objects with coordinates other than (0, 0).
  6. Giving myX and myY private visibility was a poor design decision.
  7. Suppose we want to add to the Point class a method with the following signature:

//Sets the x coordinate of the point to the given value

publicvoid setX (int x)

Which statement should be in the body of the method?

  1. x = myX;
  2. myX = x;
  3. myX = 0;
  4. x = 0;
  5. myX = myY;
  1. Consider a method that will calculate and return the sales tax on an item. Which method signature would be most appropriate?
  2. int computeTax (double price)
  3. void computeTax (double price)
  4. int computeTax()
  5. double computeTax (double price)
  6. void computeTax ()

Questions 33 and 34 refer to the following method.

int doSomething (intk, intj)

{

while (kj)

k--;

if (kj)

j = k;

else

returnj;

returnk;

}

  1. What best characterizes the return value of this method?
  2. Return k – j
  3. Return j – k
  4. Returns the smaller of j and k
  5. Returns the larger of j and k
  6. Returns j if k and j are equal, k otherwise
  7. Consider the following code segment.

int p = 5;

int q = 6;

doSomething (p, q);

System.out.println (p + “ “ + q);

What is printed?

  1. 56
  2. 55
  3. 66
  4. pq
  5. There is a compile error because the return value of doSomething is not stored in a variable.