2014-2015 Mid-Term Review AP Java
- If a picture was made up of 64 possible colors, how many bits would be needed to store each pixel of the picture?
- 4
- 5
- 6
- 7
- 8
- How many bits are there in 12 KB?
- 12,000
- 98,304
- 8192
- 9600
- 12,288
- Which of the following is equivalent to 220 x 22 bits?
- 2 KB
- 4 KB
- 2 MB
- 4 MB
- 4 GB
- How many different items can be represented with 11 bits?
- 11
- 22
- 121
- 1100
- 2048
- Which of the following is an example of an analog device?
- mercury thermometer
- computer
- music CD
- digital alarm clock
- vending machine
- Which of the following is not a valid Java identifier?
- Factorial
- anExtremelyLongIdentifierIfYouAskMe
- 2ndLevel
- level2
- highest$
- Which of the following is a valid Java Identifier?
- 14andCounting
- max_value
- 123
- %taxRate
- hook&ladder
- Which of the following pairs of variables are different from each other?
- Total and total
- case and Case
- codeTwo and code2
- oneMore and one_More
- all of the above
- What will be printed by the following statement?
System.out.println(“Use a \”\\\””);
- Use a \”\\\”
- “Use a “\””
- Use a “\”
- Use a \”\””
- Use a “\\”
- Which keyword is used to declare a constant?
- int
- double
- MAX
- constant
- final
- The expression “number” + 6 + 4 * 5 produces which of the following string literals?
- “number645”
- “number105”
- “number50”
- “number620”
- “number26”
- Which of the following is a character literal?
- b
- ‘b’
- “b”
- 2
- 2.0
- What is the result of the operation 30 % 4?
- 2
- 3
- 4
- 7
- 7.5
- The expression 1 / 4 is equal to which of the following?
- 1.0 / 4.0
- (double) 2 / 8
- 0.25
- (int) 1.0 / 4.0
- 1 / (int) 4.0
- Which of the following instantiates a String object?
- String word;
- word = new String(“the”);
- word.length();
- word = name;
- String word = name;
- Assuming g is an instance of the Random class, which statement will generate a random number between 10 and 100 inclusive?
- num = g.nextInt(101);
- num = 10 + g.nextInt(101);
- num = 10 + g.nextInt(91);
- num = 10 + g.nextInt(90);
- num = g.nextInt(110) – 10;
- Which statement would we use to create an object from a class called Thing?
- Thing something;
- Thing something = Thing ();
- Thing something = new Thing;
- Thing something = new Thing ();
- new Thing = something;
- 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?
- doIt();
- something.doIt();
- doIt(something);
- something/doIt;
- something(doIt);
- Which of the following statements increase the value of x by 1?
- x++;
- x = x +1;
- x += 1;
- i only
- ii only
- i and iii
- ii and iii
- i, ii, and iii
- 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”);
- yes
- maybe
- sometimes
- no
- There will be an error because you can’t mix integers and Booleans in the same expression.
- The expression !f || g is the same as which of the following?
- f || !g
- !(f || g)
- !(f & g)
- !(!f & !g)
- !(f & !g)
- 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(“-“);
- 5
- 6
- 10
- 11
- 13
- 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);
- 5
- 8
- 35
- 40
- 64
- 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?
- if ( x < 0 )x = 1;
- if (x < 20)x = 19;
- if (x < 0)x = 19;
- if (x < 20)x = 20;
- x = 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?
- num = 1, max = 1;
- num = 1, max = 2;
- num = 2, max = 2;
- num = 2, max = 1;
- num = 1, max = 3;
- Which for loop is equivalent to this while loop?
int y = 5;
while (y >= 0)
{
System.out.println(y);
y--;
}
- for (int y = 0; y < 5; y ++)
System.out.println(y);
- for (int y = 5; y > 0; y --)
System.out.println(y);
- for (int y = 5; y >= 0; y --)
System.out.println(y);
- for (int y = 0; y > 5; y ++)
System.out.println(y);
- for (int y = 0; y > 5; y --)
System.out.println (y);
- Which expression tests to make sure the grade is between0 and 100 inclusive?
- (grade <= 100) || (grade <= 0)
- (grade <= 100) || (grade >= 0)
- (grade <101) || (grade > -1)
- (grade <= 100) & (grade >= 0)
- (grade >= 100) & (grade <= 0)
- Which values of x, y, a, or b will cause the if statement to be short-circuited?
if ((x > y) & (a || b))
statement;
- x = 1, y = 1
- x = 5, y = 1
- x = 2, y = 1, a = true, b = false
- a = false, b = true
- 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;
}
}
- Which of the following statements creates a point with coordinates (0,0)?
- p = new Point ();
- p = Point (0, 0);
- p = (0,0);
- i only
- ii only
- iii only
- i and ii only
- ii and iii only
- Which of the following statements is true regarding the Point class?
- The class won’t compile because there are two methods name Point.
- Variable myX and myY can be changed from outside of the class.
- Point objects are immutable.
- It’s impossible to create Point objects with coordinates other than (0, 0).
- Giving myX and myY private visibility was a poor design decision.
- 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?
- x = myX;
- myX = x;
- myX = 0;
- x = 0;
- myX = myY;
- Consider a method that will calculate and return the sales tax on an item. Which method signature would be most appropriate?
- int computeTax (double price)
- void computeTax (double price)
- int computeTax()
- double computeTax (double price)
- 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;
}
- What best characterizes the return value of this method?
- Return k – j
- Return j – k
- Returns the smaller of j and k
- Returns the larger of j and k
- Returns j if k and j are equal, k otherwise
- Consider the following code segment.
int p = 5;
int q = 6;
doSomething (p, q);
System.out.println (p + “ “ + q);
What is printed?
- 56
- 55
- 66
- pq
- There is a compile error because the return value of doSomething is not stored in a variable.