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