Practice Test(s) Flashcards
Practice Test 01
What is the largest number of comparisons needed to perform a binary search on an array with 42 elements?
2
5
6
41
42
6
Practice Test 01
Which of the following are valid arrays?
I. int[] coolArray = {1, 2, 3};
II. int[] threeThings = {1.0, 2.0, 3.0};
III. int[] = {“1”, “2”, “3”};
I only
II only
III only
I and II
I and III
I only
Practice Test 01
What is the output of the following program?
- hel
- hello
- hellow
- Error
- None of the above

- Error
Correct! Specifically, the <= in the for loop results in an attempt to access data that is out of the array’s bounds.
Practice Test 01
What values will the array contain after the following the code is run?
int[] someArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
for (int i = someArray.length - 2; i >= 0; i-=2)
{
someArray[i] = someArray[i] - 5;
}
- {6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4}
- {1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11}
- {1, 3, 5, 7, 9, 7, 5, 3, 1, -1, -3}
- {-4, 1, -2, 3, -4, 5, -6, 7, -8, 9}
2. {1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11}
Practice Test 01
What is the proper way to compare String values in Java?
- The == operator
- The .equals() String method
- The = operator
- The | operator
- The .equals() String method
Practice Test 01
What result would be printed given these commands:
SomeFavorites mystery = new SomeFavorites();
mystery. listSomething();
mystery. anotherThing();

42
42
Practice Test 01
What are the values of the variables x, y, and z after this code snippet runs?
int x = 0;
int y = 10;
int z = 20;
x = y;
y = y * 2;
z = 30;
- x = 0, y = 10, z = 30
- x = 0 , y = 20 , z = 20
- x = 10, y = 20, z = 30
- x = 10, y = 10, z = 30
- x = 10, y = 20, z = 30
Practice Test 01
Your robot Baymax seems to have a bug in his battery. The problem is Baymax will only stay on if his battery is at 100%. Anything lower than 100% and Baymax shuts off. What is the bug in this code snippet?
Hint : Baymax represents his battery life as a decimal number between 0 and 1.
- There is a syntax error in Baymax’s baymaxIsOn method. The else statement must also have a conditional expression.
- We are trying to represent batteryLife (a decimal number) with an int variable. We should instead be using a double variable.
- Baymax’s chargeBattery and useBattery methods will error. Java does not allow you to add an int and a double.
- Baymax’s baymaxIsOn method can NEVER return True.

- We are trying to represent batteryLife (a decimal number) with an int variable. We should instead be using a double variable.
Practice Test 01
What would be a correct way to instantiate this 2D array?
- int[][] table = {1, 0, 1, 5,
3, 8}; - int[][] table = { {1, 0, 1, 5},
{3, 8} }; - int[][] table = { {1, 0, 1, 5},
{3, 8, 0, 0} };

- int[][] table = { {1, 0, 1, 5},
{3, 8} };
Practice Test 01
Given the following:
double[][] something =
{ {2.5, 6.8, 8.3},
{6.1, 10.2, 1.3, -2.5},
{1.1, 2.3, 5.8, 13.21, 34.55} }
What is the value of something[1][2]?
- 8
- 1
- 3
- 3
The array does not have an element at that location.
1.3
Practice Test 01
What is the proper way to get the number of items in an ArrayList called list?
- list.length
- list.size
- list.length()
- list.size()
5.
- list.size()
Practice Test 01
What is the output of the following lines?
- int x = 24;*
- System.out.println(“The total is “ + x + x);*
- The total is 24
- The total is 2424
- The total is 48
- The total is x + x
- The total is 2424
Practice Test 01
Which expression is true?
- true && !true
- !false || !true
- true && false
- false || false || !true
- !false || !true
Practice Test 01
What will this program print if the value of grade is 80?

C
Practice Test 01
What will the values of x and y be after this code segment runs?

x = 99
y = 199
Practice Test 01
Refer to the following code segment:
double myDouble = 1/4;
System.out.println(“1 / 4 = “ + myDouble);
The output of the code is:
1 / 4 = 0.0
The student wanted the output to be:
1 / 4 = 0.25
Which change to the first line of their code segment would get the student the answer that they wanted?
- int myDouble = 1/4;
- double myDouble = (double) 1/4;
- double myDouble = (int) 1/4;
- double myDouble = (int) (1.0/4.0);
2. double myDouble = (double) 1/4;
Practice Test 01
What will the following code print?

Java
Practice Test 01
What is wrong with the following code?
ArrayList list = new ArrayList();
list.add(1);
System.out.println(list.get(0));
- It will throw an IndexOutOfBoundsException.
- You cannot use int as the type in an ArrayList.
- list is a reserved word, so you can’t call the variable that.
- You need to define the new ArrayList on a new line of code.
- Nothing. The above code is correct.
- You cannot use int as the type in an ArrayList
Practice Test 01
What does this method call output?
public double doubleNumber(double myNumber)
{
return (double) (int) myNumber * 2;
}
doubleNumber(7.8);
16
- 6
- 0
This method is improperly written.
Answered
14.0
Practice Test 01
Consider the following code segment:
String str = “I am”;
str += 10 + 3;
String age = “years old”;
System.out.println(str + age);
What is printed as a result of executing the code segment?
- I am103years old
- I am13years old
- I am 103 years old
- I am 13 years old
- I am13years old
Practice Test 01
What would this method call output?

divideByTen(340)
34
Practice Test 01
What is the output of this method call?
public String wordScramble(String myWord)
{
myWord.substring(2) + myWord.charAt(3) + “dot”;
}
wordScramble(“polkadot”)
- “dotpolka”
- “lkadotkdot”
- “olkadotldot”
- This method call will error.
- This method call will error
Practice Test 01
What kind of error does this method cause?
myMethod(“Frog”);
- Compile Time Error: Missing return value
- Runtime Error: String index out of range
- Compile Time Error: Unexpected return value
- Compile Time Error: Syntax Error in method definition

- Runtime Error: String index out of range
Practice Test 01
What does this method call output?

someMethod(3,1)
10
Practice Test 01
What is wrong with the class definition?
Hint: We expect names and types to be assigned as instance variables in the class constructor.
- Missing void in constructor definition
- Class constructors must be private
- Arrays can not be passed as parameters to a constructor
- Must use this in constructor when the constructor parameters have the same name as instance variables.

- Must use this in constructor when the constructor parameters have the same name as instance variables.
Practice Test 01
What will the Array gameBoard contain after this code runs?
- [“X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”]
- [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
- [“O”, “X”, “X”, “O”, “X”, “X”, “O”, “X”, “X”]
- [“O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”]

- [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
Practice Test 01
Finn just purchased the newest gaming system, the BMO 2600. Unfortunately, when Finn turned on the system, it froze on the loading screen! Help Finn identify the bug in the Java code running his new gaming system.
boolean onLoadingScreen = true;
int loadingTime = 100;
while(onLoadingScreen) {
loadingTime++;
onLoadingScreen = loadingTime != 0;
}
- The != operator can not be used to compare ints.
- The != operator does not return a boolean value.
- The variable loadingTime is increasing instead of decreasing.
- while statements loop infinitely. A for statement should be used instead.
- The variable loadingTime is increasing instead of decreasing
Practice Test 01
Your friend is bored at lunch and is climbing the stairs for fun. He walks up two stairs, walks down one, walks up two stairs, walks down one, and so on. How many iterations of the while loop will it take her to walk up to the 10th stair?

9
Practice Test 01
All of your coins are inside a 3 x 4 matrix. Which piece of java code will properly find the total number of coins that you have?
- int col = 0; col < numCoins[row].length(); col++
- int col = 0; col < numCoins.length + 1; col++
- int col = 0; col < numCoins[row].length; col++
- int col = 0; col < numCoins.length; col++

- int col = 0; col < numCoins[row].length; col++
Practice Test 01
What is the return value of this method call?

findTheMiddle(2110890125)
89
Practice Test 01
Which methods of class Foo do not require an instance of class Foo to be invoked?
- All methods require an instance of Foo to be invoked.
- No methods require an instance of Foo to be invoked.
- bar()
- baz()
- foo()

- foo()
Practice Test 01
The Java Classes Skeleton, Spider, and Zombie all extend the Java Class Monster. The Monster Class is defined below.
What variables and methods do the Skeleton, Spider, and Zombie Classes inherit?
- Everything
- Only the variables name and type
- Only the variables
- Only the methods
- The variables name and type and the method move

- The variables name and type and the method move
Practice Test 01
Given the definitions of class A and class B….
what is the output of this code
B obj = new B();
System.out.println(obj.i + “ “ + obj.j);
- 1 2
- 2 1
- Runtime Error
- Compilation Error

1. 1 2
Practice Test 01
Given the class definitions of Vector2D and Vector3D below, which of the constructors that follow would be valid in the Vector3D class?
Hint: Which Vector3D class definitions will not cause an error?
I, II, and III
I and II
II and III
Only III
Only II

I, II, and III
Practice Test 01
What is the correct pseudocode for Insertion Sort?
- If there are at least two elements in the collection Partition the collection Insertion sort the left collection Insertion sort the right collection
- If there is more than one element in the collection Break the collection into two halves Insertion sort the left half Insertion sort the right half Compare the two halves Merge the two subcollections into a sorted collection
- Start with the first element and sort it with itself While there is a next element Compare the next unsorted element with the sorted elements Insert the element into the correct position in the sorted elements
- While there are unsorted numbers Find the smallest unsorted number Insert the element at the end of the sorted elements
3. Start with the first element and sort it with itself While there is a next element Compare the next unsorted element with the sorted elements Insert the element into the correct position in the sorted elements
Practice Test 01
When the call mystery2() is executed, what are the values of word and num at the point indicated by /* End of method */?

word = peanut num = 7
What will be returned by mystery?
- The minimum value in the array will be returned.
- The maximum value in the array will be returned.
- The length of the array will be returned.
- No value will be returned.
- All values in the array will be returned.

- The minimum value in the array will be returned
Correct! Using the x = arr[0 [0] guarantees that the correct minimum value will be found and returned.
Given the following method declaration and int[] arr = {30, 7, 88, 17}, what is the value in arr[1] after the method is run?
public static int mystery(int[] arr)
{
arr[1]–;
return (arr[1] * 2);
}
6
Refer to the following code snippet:
double result = 17 / 4;
System.out.println(“17 / 4 = “ + result);
The output is
17 / 4 = 4.0
but the programmer intends the output to be
17 / 4 = 4.25
Which of the following replacements for the first line of code will NOT fix the problem?
- double result = (double) (17 / 4);
- double result = (double) 17 / 4;
- double result = 17.0 / 4;
- double result = 17 / (double) 4;
- double result = (double) 17 / 4.0;
- double result = (double) (17 / 4);
Just before the end of execution of this program, what are the values of a, b, and temp, respectively?
- 12, 17, 12
- 17, 12, 12
- 17, 12, 17
- 17, 12, undefined
- 12, 17, undefined

5. 12, 17, undefined
Primitive types are of the actual arguments when passed as parameters. So a and b are copied into the Mystery method as copies and the args remained unchanged. The local vartemp goes out of scope as soon as the Mystery method is done executing and is thus is undefined just before the end of the program execution.
Consider the following method modifyArray. An array is created that contains {1, 2, 4, 9, 5} and is passed to modifyArray.
What are the contents of the array after the modifyArray method executes?
- {1, 1, 2, 5, -4}
- {11, -10, -8, -4, 5}
- {3, 6, 13, 14, 5}
- {21, 20, 18, 14, 5}
- This method results in an IndexOutOfBounds exception.

- {21, 20, 18, 14, 5}
For every iteration of the loop, arr[i] and arr[i - 1] are added together and are assigned to arr[i - 1].
What value is returned as the result of the call mystery(6)?

48
The result from mystery(6) is 6 * mystery(4) which is 4 * mystery(2) which is 2* mystery(0) which is 1, so the answer is 2 * 4 * 6 = 48.
What is the output when the following code is compiled and run?
Flower flower = new Flower(3);
Flower callaLily = new Lily(4);
flower.showWater();
flower.addWater();
flower.showWater();
callaLily.showWater();
callaLily.addWater();
callaLily.showWater();
- 3 4 3 4
- 6 7 6 7
- 3 4 8 9
- The code won’t complile.
- When callaLily.addWater() is executed a runtime error ClassCastException is thrown.

- 3 4 8 9
An object always knows which class that created it, so even though callaLily is declared to be a Flower the constructor that is executed is the one for Lily.
Which of the following is printed as a result of the call mystery(12345)?
- 543210
- 5432112345
- 1551
- 1234554321
- Many digits are printed because this method creates an infinite recursion.

- Many digits are printed because this method creates an infinite recursion
When the recursive call to mystery(1) occurs, x /10 evaluates to 0 because of integer division and the remainder is discarded. So the current 4th call to mystery will be completed as will all of the previous calls to mystery.