5.15. Midterm Test¶
The following 20 questions are similar to what you might see on the AP CSA exam for Units 1 - 5. You may only take this test once while logged in. There are no time limits, but it will keep track of how much time you take. Click on the finish button after you have answered all the questions, and the number correct and feedback on the answers will be displayed.
We estimate that a score of about 50% on this test would correspond to the passing grade of 3 on the AP exam, a score of 65% to a 4, and a score of 80% and above to a 5 on the AP exam. These are just estimates and may not correspond to individual scores.
- I only
- Correct! This will loop with i changing from 1 to 5 and then for each i, j will loop from i to 0 printing the value of i and then a new line.
- I and II only
- II will loop i from 0 to 4 and j from 0 to i, neglecting to ouput 5.
- III only
- III will loop with i changing from 1 to 4 and j from i to 0.
- IV and V only
- IV will loop with i changing from 1 to 5 and j from 0 to i but it will print each value on a different line.
- V only
- V will loop with i changing from 0 to 4 and j from 0 to i.
- A
- num2 is negative
- AC
- Only one letter will be printed.
- C
- Correct because num2 is negative and an or is used.
- BD
- Only one letter will be printed.
- E
- One of the other conditions is true.
- I only
- Loop I will produce this output, but it is not the only loop that will output these values.
- II only
- Loop II will produce this output, but it is not the only loop that will output these values.
- II and III only
- Loop II is correct, but loop III will produce the reverse output, 43210.
- I and II only
- Correct! Both of these loops will produce the correct output.
- I, II, and III
- While loop I and II will produce the correct output, loop III will actually produce the reverse of the correct output.
- 25
- This would be the correct answer if there were only two loops nested, but there are three. Try again!
- 15
- Take a look at how many times each inner loop will execute every time the outer loop runs.
- 125
- Correct!
- 64
- Try again - check the difference between <= and < in each loop.
- 625
- If you got this value you probably made one extra call to the each of the loops, notice that the loops start at 1 and not 0.
- (x < 10) && (x > 5)
- Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (!A && !B).
- (x > 10) && (x <=5)
- Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (A && B).
- (x <= 10) && (x > 5)
- Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (!A && !B). The AND should be changed to an OR.
- (x <= 10) || (x > 5)
- Correct!
- (x > 10) || (x <= 5)
- Use A and B to represent the expressions -- A becomes (x > 10), B becomes (x <= 5). ! (A && B) is NOT equivalent to (A || B). Both A and B should also be negated.
- s="rainbow"; b=8;
- Strings are immutable so changing str doesn't affect the string that s refers to.
- s="rain"; b=8;
- Nothing done in the method test affects the value of b.
- s="rainbow"; b=4;
- Strings are immutable so changing str doesn't affect the string that s refers to.
- s="rain"; b=4;
- Correct!
- s="bow"; b=4;
- All changes to string s result in a new string object.
- The ``getAge()`` method should be declared as private.
- The method should be public so it can be accessed outside of the class.
- The return type of the ``getAge()`` method should be void.
- The method's return type should be int.
- The ``getAge()`` method should have at least one parameter.
- The getAge method should not take any parameters.
- The variable ``age`` is not declared inside the ``getAge()`` method.
- This is an instance variable and should be declared outside of the method.
- The instance variable ``age`` should be returned instead of a, which is local to the constructor.
- Correct! The accessor method getAge should return the instance variable age.
Local variables can be declared in the body of constructors and methods.
Local variables may only be used within the constructor or method and cannot be declared to be public or private.
When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable.
- I only
- It's true that the local variables can be declared in the body of constructors and methods, but there are other options that are also true about local variables.
- I and II only
- Both I and II are true but III is also true regarding local variables.
- I and III only
- Both I and III are true but II is also true regarding local variables.
- I, II, and III
- Correct! All of the above are true.
- II and III only
- Both of these are true but I is also true.
Static methods and variables include the keyword static before their name in the header or declaration and can be public or private.
Static methods can access or change the values of instance variables.
Static methods are associated with the class, not objects of the class.
- I and II only
- Static methods cannot acccess instance variables. They can only access static variables.
- I, II, and III
- Static methods cannot acccess instance variables. They can only access static variables.
- I and III only
- Correct! I and III are true, but static methods cannot acccess instance variables. They can only access static variables.
- I only
- I is true, but there is another option that is true too.
- III only
- III is true, but there is another option that is true too.
- var1 = 0, var2 = 2
- This would be true if the body of the while loop never executed. This would have happened if the while check was if var1 != 0 instead of var2 != 0
- var1 = 1, var2 = 1
- This would be true if the body of the while loop only execued one time, but it executes twice.
- var1 = 3, var2 = -1
- This would be true if the body of the while loop executed three times, but it executes twice.
- var1 = 2, var2 = 0
- Correct!
- The loop won't finish executing because of a division by zero.
- 0/2 won't cause a division by zero. The result is just zero.
- 5 4 3 2 1
- x is initialized (set) to -5 to start and incremented (x++) before the print statement executes.
- -5 -4 -3 -2 -1
- x is incremented (x++) from -5 before the print statement executes.
- -4 -3 -2 -1 0
- Correct!
- -5 -4 -3 -2 -1 0
- x is incremented (x++) from -5 before the print statement executes.
- -4 -3 -2 -1
- 0 is printed out the last time through the loop when x is -1 and is incremented.
- 0 3 6 9 12
- It would also print 15.
- 0 1 2 3 4 5
- The conditional would only match multiples of three.
- 1 4 7 10 13
- The conditional would only match multiples of three.
- 0 3 6 9 12 15
- Yes, the multiples of 3 from 0 to 15.
- This code will not print anything.
- This code would print the multiples of 3.
- I only
- This implementation of ``addMinutes`` does not account for values of additionMinutes that push the minute count above 60.
- II only
- Implementation II works, but implementation III also works.
- IV only
- Implementation IV does not work for situations where additionMinutes + minutes does not go above 60.
- II and III
- Correct!
- I, II, and III
- Implementations II and III are correct, but implementation I is not. Implementation I does not account for values of additionMinutes that push the minute account above 60.
- !(a && b)
- This would be true in any case where a and b weren't both true
- !a && b
- If b was false, this option would be false.
- !a && !b
- Correct!
- a && b
- This will only be true only when both a and b are true.
- a || !b
- This will only be true if a is true, or b is false.
- Prints the string in reverse order
- Correct! This method prints the reversed string.
- Deletes the second half of the string
- Incorrect, this method prints the parameter reversed.
- Prints string normally
- Incorrect, this method prints the parameter reversed.
- Compile-time error occurs
- Incorrect, this method prints the parameter reversed.
- Prints alternating characters from beginning and end of the string.
- Incorrect, this method prints the parameter reversed.
- "Hello World!"
- The variable holds all characters that were stored at even indices for the original phrase.
- "Hello "
- The variable holds all characters that were stored at even indices for the original phrase.
- "He"
- The variable holds all characters that were stored at even indices for the original phrase.
- "HloWrd"
- Correct! The variable holds all characters that were stored at even indices for the original phrase.
- "el ol!"
- The variable holds all characters that were stored at even indices for the original phrase.
- I only
- I contains incorrect syntax. Try again!
- I and II
- I contains incorrect syntax. Try again!
- II only
- Correct! II is the only correct option.
- II and III
- III is incorrect due to a problem with the constructor argument. Try again!
- I, II, and III
- Two of these options are incorrect. Take a closer look at the syntax of I and parameters of III.
- a = 6 and b = 7
- This would be true if the loop stopped when i was equal to 6. Try again!
- a = 6 and b = 13
- Take another look at how a and b change in each iteration of the loop.
- a = 13 and b = 0
- Correct!
- a = 6 and b = 0
- Almost there! b = 0, but take another look at how a changes in each iteration of the loop.
- a = 0 and b = 13
- Take another look at how a and b change within each iteration of the loop. You are close!
- hi there
- Strings are immutable and so any change to a string returns a new string.
- HI THERE
- Strings are immutable and so any change to a string returns a new string.
- Hi There
- Correct!
- null
- Strings are immutable and so any changes to a string returns a new string.
- hI tHERE
- Strings are immutable and so any changes to a string returns a new string.
- a = 6.7
- Check the data type of a.
- b = 87.7
- Check the data type of b.
- 12 = c * b
- Assignment statements must have a variable on the left.
- c = a - b
- Correct!
5-15-1: Which of the following code segments will produce the displayed output?
/* Output:
1
22
333
4444
55555
*/
//Loop I
for (int i = 1; i <= 5; i++)
{
for (int j = i; j > 0; j--)
{
System.out.print(i);
}
System.out.println();
}
//Loop II
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(i);
}
System.out.println();
}
//Loop III
for (int i = 1; i < 5; i++)
{
for (int j = i; j > 0; j--)
{
System.out.print(i);
}
System.out.println();
}
//Loop IV
for (int i = 1; i < 6; i++)
{
for (int j = 0; j < i; j++)
{
System.out.println(i);
}
}
//Loop V
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print(i+1);
}
System.out.println();
}
5-15-2: Consider the following method. What is the output from conditionTest(3,-2);
?
public static void conditionTest(int num1, int num2)
{
if ((num1 > 0) && (num2 > 0))
{
if (num1 > num2)
System.out.println("A");
else
System.out.println("B");
}
else if ((num2 < 0) || (num1 < 0))
{
System.out.println("C");
}
else if (num2 < 0)
{
System.out.println("D");
}
else
{
System.out.println("E");
}
}
5-15-3: Which of these loops will output 01234
?
int max = 5;
//Loop I
for (int i = 0; i < max; i++)
{
System.out.print(i);
}
//Loop II
int j = 0;
while (j < max)
{
System.out.print(j);
j++;
}
//Loop III
int k = 0;
for (int i = max; i > 0; i--)
{
System.out.print(i);
}
5-15-4: Consider the following block of code. What value is returned from solution(5)
?
public int solution(int limit)
{
int s = 0;
for (int outside = 1; outside <= limit; outside++)
{
for (int middle = 1; middle <= limit; middle++)
{
for (int inside = 1; inside <= limit; inside++)
{
s++;
}
}
}
return s;
}
5-15-5: Which of the following is equivalent to !((x > 10) && (x <= 5)) ?
5-15-6: Consider the following class with the method test
. What is the output after the main method is executed calling test(s,b)
?
public class Test1
{
public static void test(String str, int y)
{
str = str + "bow";
y = y * 2;
}
public static void main(String[] args)
{
String s = "rain";
int b = 4;
test(s, b);
System.out.println("s=" + s + "; b=" + b);
}
}
5-15-7: Consider the following Cat
class that has an age
attribute of type int. The getAge
method is intended to allow methods in other classes to access a Cat object’s age value; however, it does not work as intended. Which of the following best explains why the getAge
method does NOT work as intended?
public class Cat
{
private int age;
public Cat(int a)
{
age = a;
}
public int getAge()
{
return a;
}
}
5-15-8: Which of the following statements are TRUE about local variables?
5-15-9: Which of the following statements are TRUE about static methods?
5-15-10: What are the values of var1
and var2
after the following code segment is executed and the while loop finishes?
int var1 = 0;
int var2 = 2;
while ((var2 != 0) && ((var1 / var2) >= 0))
{
var1 = var1 + 1;
var2 = var2 - 1;
}
5-15-11: What does the following code print?
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
5-15-12: What will be printed after this code is executed?
for (int i = 0; i <= 15; i++)
{
if (i % 3 == 0)
{
System.out.print(i + " ");
}
}
5-15-13: Consider the following declaration for a class that will be used to represent points in time. Which of these options correctly implement addMinutes()
?
public class Timer
{
private int hours; // number of hours
private int minutes; // 0 <= minutes < 60
void addHours(int addition)
{
hours = hours + addition;
}
/** addMinutes adds the given argument to the time stored in hours and minutes.
The argument additionMinutes is between 0 and 119. **/
void addMinutes(int additionMinutes)
{
// implementation not shown
}
// ... other methods not shown
}
//Proposed Implementations:
I. public void addMinutes(int additionMinutes)
{
minutes = minutes + additionMinutes;
}
II. public void addMinutes(int additionMinutes)
{
minutes += additionMinutes;
if (minutes >= 60)
{
hours += minutes / 60;
minutes = minutes % 60;
}
}
III. public void addMinutes(int additionMinutes)
{
minutes += additionMinutes;
while (minutes >= 60)
{
hours++;
minutes -= 60;
}
}
IV. public void addMinutes(int additionMinutes)
{
if (additionMinutes + minutes >= 60)
{
minutes = additionMinutes + minutes - 60;
hours += 1;
}
}
5-15-14: Which option will evaluate to true, if and only if both a and b are false?
5-15-15: What does the method mystery
do?
public void mystery(String tester)
{
for (int i = tester.length() - 1; i >= 0; i--)
{
System.out.print(tester.substring(i,i+1));
}
System.out.println("");
}
5-15-16: After the following code is executed, what does the variable mystery
hold?
public class Mysterious
{
public static void main(String[] args)
{
String mystery = "";
String starter = "Hello World!";
for (int i = 0; i < starter.length(); i++)
{
if (i % 2 == 0)
{
mystery += starter.substring(i, i + 1);
}
}
}
}
5-15-17: Which of the following code segments correctly creates an instance of a new Party
object?
public class Party
{
private int numInvited;
private boolean partyCancelled;
public Party()
{
numInvited = 1;
partyCancelled = false;
}
public Party(int invites)
{
numInvited = invites;
partyCancelled = false;
}
}
I. Party myParty;
II. int classSize = 20;
Party ourParty = new Party(classSize);
III. int numOfFriends = 6;
Party yourParty = new Party(numOfFriends + 3.0);
5-15-18: What are the values of a
and b
after the for
loop finishes?
int a = 10, b = 3, t = 0;
for (int i = 1; i <= 6; i++)
{
t = a;
a = i + b;
b = t - i;
}
5-15-19: Consider the following code. What string is referenced by s1
after the code executes?
String s1 = "Hi There";
String s2 = s1;
String s3 = s2;
String s4 = s1;
s2 = s2.toLowerCase();
s3 = s3.toUpperCase();
s4 = null;
5-15-20: Given following code, which of the following statements is a valid assignment statement using these variables?
int a = 5;
int b = 3;
int c = 4;