Anda di halaman 1dari 12

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?
a. 4
b. 5
c. 6
d. 7
e. 8
2. How many bits are there in 12 KB?
a. 12,000
b. 98,304
c. 8192
d. 9600
e. 12,288
3. Which of the following is equivalent to 220 x 22 bits?
a. 2 KB
b. 4 KB
c. 2 MB
d. 4 MB
e. 4 GB
4. How many different items can be represented with 11 bits?
a. 11
b. 22
c. 121
d. 1100
e. 2048
5. Which of the following is an example of an analog device?
a. mercury thermometer
b. computer
c. music CD
d. digital alarm clock
e. vending machine
6. Which of the following is not a valid Java identifier?
a. Factorial
b. anExtremelyLongIdentifierIfYouAskMe
c. 2ndLevel
d. level2
e. highest$
7. Which of the following is a valid Java Identifier?
a. 14andCounting
b. max_value
c. 123
d. %taxRate
e. hook&ladder

8. Which of the following pairs of variables are different from each other?
a. Total and total
b. case and Case
c. codeTwo and code2
d. oneMore and one_More
e. all of the above
9. What will be printed by the following statement?
System.out.println(“Use a \”\\\””);
a. Use a \”\\\”
b. “Use a “\””
c. Use a “\”
d. Use a \”\””
e. Use a “\\”
10. Which keyword is used to declare a constant?
a. int
b. double
c. MAX
d. constant
e. final
11. The expression “number” + 6 + 4 * 5 produces which of the following string
literals?
a. “number645”
b. “number105”
c. “number50”
d. “number620”
e. “number26”
12. Which of the following is a character literal?
a. b
b. ‘b’
c. “b”
d. 2
e. 2.0
13. What is the result of the operation 30 % 4?
a. 2
b. 3
c. 4
d. 7
e. 7.5
14. The expression 1 / 4 is equal to which of the following?
a. 1.0 / 4.0
b. (double) 2 / 8
c. 0.25
d. (int) 1.0 / 4.0
e. 1 / (int) 4.0
15. Which of the following instantiates a String object?
a. String word;
b. word = new String(“the”);
c. word.length();
d. word = name;
e. String word = name;
16. Assuming g is an instance of the Random class, which statement will generate
a random number between 10 and 100 inclusive?
a. num = g.nextInt(101);
b. num = 10 + g.nextInt(101);
c. num = 10 + g.nextInt(91);
d. num = 10 + g.nextInt(90);
e. num = g.nextInt(110) – 10;
17. Which statement would we use to create an object from a class called Thing?
a. Thing something;
b. Thing something = Thing ();
c. Thing something = new Thing;
d. Thing something = new Thing ();
e. new Thing = something;
18. 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?
a. doIt();
b. something.doIt();
c. doIt(something);
d. something/doIt;
e. something(doIt);
19. Which of the following statements increase the value of x by 1?
i. x++;
ii. x = x +1;
iii. x += 1;
a. i only
b. ii only
c. i and iii
d. ii and iii
e. i, ii, and iii
20. 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”);
a. yes
b. maybe
c. sometimes
d. no
e. There will be an error because you can’t mix integers and Booleans in
the same expression.
21. The expression !f || g is the same as which of the following?
a. f || !g
b. !(f || g)
c. !(f && g)
d. !(!f && !g)
e. !(f && !g)
22. 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(“-“);
a. 5
b. 6
c. 10
d. 11
e. 13
23. 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);
a. 5
b. 8
c. 35
d. 40
e. 64
24. 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?
a. if ( x < 0 ) x = 1;
b. if (x < 20) x = 19;
c. if (x < 0) x = 19;
d. if (x < 20) x = 20;
e. x = 1;
25. 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?
a. num = 1, max = 1;
b. num = 1, max = 2;
c. num = 2, max = 2;
d. num = 2, max = 1;
e. num = 1, max = 3;
26. Which for loop is equivalent to this while loop?
int y = 5;
while (y >= 0)
{
System.out.println(y);
y--;
}
a. for (int y = 0; y < 5; y ++)
System.out.println(y);
b. for (int y = 5; y > 0; y --)
System.out.println(y);
c. for (int y = 5; y >= 0; y --)
System.out.println(y);
d. for (int y = 0; y > 5; y ++)
System.out.println(y);
e. for (int y = 0; y > 5; y --)
System.out.println (y);
27. Which expression tests to make sure the grade is between 0 and 100
inclusive?
a. (grade <= 100) || (grade <= 0)
b. (grade <= 100) || (grade >= 0)
c. (grade <101) || (grade > -1)
d. (grade <= 100) && (grade >= 0)
e. (grade >= 100) && (grade <= 0)
28. Which values of x, y, a, or b will cause the if statement to be short-circuited?
if ((x > y) && (a || b))
statement;
a. x = 1, y = 1
b. x = 5, y = 1
c. x = 2, y = 1, a = true, b = false
d. a = false, b = true
e. a = false, b = false
Questions 29-32 refer to the following class.

public class Point


{
private int myX;
private int myY;

public Point ()
{
myX = 0;
myY = 0;
}

public Point (int x, int y)


{
myX = x;
myY = y;
}

public int getX()


{
return myX;
}

public int getY()


{
return myY;
}

29. Which of the following statements creates a point with coordinates (0,0)?
i. p = new Point ();
ii. p = Point (0, 0);
iii. p = (0,0);
a. i only
b. ii only
c. iii only
d. i and ii only
e. ii and iii only
30. Which of the following statements is true regarding the Point class?
a. The class won’t compile because there are two methods name Point.
b. Variable myX and myY can be changed from outside of the class.
c. Point objects are immutable.
d. It’s impossible to create Point objects with coordinates other than (0,
0).
e. Giving myX and myY private visibility was a poor design decision.
31. 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
public void setX (int x)

Which statement should be in the body of the method?


a. x = myX;
b. myX = x;
c. myX = 0;
d. x = 0;
e. myX = myY;
32. Consider a method that will calculate and return the sales tax on an item.
Which method signature would be most appropriate?
a. int computeTax (double price)
b. void computeTax (double price)
c. int computeTax()
d. double computeTax (double price)
e. void computeTax ()
Questions 33 and 34 refer to the following method.
int doSomething (int k, int j)
{
while (k > j)
k--;
if (k < j)
j = k;
else
return j;
return k;

33. What best characterizes the return value of this method?


a. Return k – j
b. Return j – k
c. Returns the smaller of j and k
d. Returns the larger of j and k
e. Returns j if k and j are equal, k otherwise
34. Consider the following code segment.
int p = 5;
int q = 6;
doSomething (p, q);
System.out.println (p + “ “ + q);

What is printed?
a. 5 6
b. 5 5
c. 6 6
d. p q
e. There is a compile error because the return value of doSomething is
not stored in a variable.

Anda mungkin juga menyukai