Practice Test(s) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

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

A

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

I only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Practice Test 01

What is the output of the following program?

  1. hel
  2. hello
  3. hellow
  4. Error
  5. None of the above
A
  1. Error

Correct! Specifically, the <= in the for loop results in an attempt to access data that is out of the array’s bounds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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;

}

  1. {6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4}
  2. {1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11}
  3. {1, 3, 5, 7, 9, 7, 5, 3, 1, -1, -3}
  4. {-4, 1, -2, 3, -4, 5, -6, 7, -8, 9}
A

2. {1, -3, 3, -1, 5, 1, 7, 3, 9, 5, 11}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Practice Test 01

What is the proper way to compare String values in Java?

  1. The == operator
  2. The .equals() String method
  3. The = operator
  4. The | operator
A
  1. The .equals() String method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Practice Test 01

What result would be printed given these commands:

SomeFavorites mystery = new SomeFavorites();

mystery. listSomething();
mystery. anotherThing();

A

42

42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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;

  1. x = 0, y = 10, z = 30
  2. x = 0 , y = 20 , z = 20
  3. x = 10, y = 20, z = 30
  4. x = 10, y = 10, z = 30
A
  1. x = 10, y = 20, z = 30
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

  1. There is a syntax error in Baymax’s baymaxIsOn method. The else statement must also have a conditional expression.
  2. We are trying to represent batteryLife (a decimal number) with an int variable. We should instead be using a double variable.
  3. Baymax’s chargeBattery and useBattery methods will error. Java does not allow you to add an int and a double.
  4. Baymax’s baymaxIsOn method can NEVER return True.
A
  1. We are trying to represent batteryLife (a decimal number) with an int variable. We should instead be using a double variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Practice Test 01

What would be a correct way to instantiate this 2D array?

  1. int[][] table = {1, 0, 1, 5,
    3, 8};
  2. int[][] table = { {1, 0, 1, 5},
    {3, 8} };
  3. int[][] table = { {1, 0, 1, 5},
    {3, 8, 0, 0} };
A
  1. int[][] table = { {1, 0, 1, 5},
    {3, 8} };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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]?

  1. 8
  2. 1
  3. 3
  4. 3

The array does not have an element at that location.

A

1.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Practice Test 01

What is the proper way to get the number of items in an ArrayList called list?

  1. list.length
  2. list.size
  3. list.length()
  4. list.size()
    5.
A
  1. list.size()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Practice Test 01

What is the output of the following lines?

  • int x = 24;*
  • System.out.println(“The total is “ + x + x);*
  1. The total is 24
  2. The total is 2424
  3. The total is 48
  4. The total is x + x
A
  1. The total is 2424
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Practice Test 01

Which expression is true?

  1. true && !true
  2. !false || !true
  3. true && false
  4. false || false || !true
A
  1. !false || !true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Practice Test 01

What will this program print if the value of grade is 80?

A

C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Practice Test 01

What will the values of x and y be after this code segment runs?

A

x = 99

y = 199

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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?

  1. int myDouble = 1/4;
  2. double myDouble = (double) 1/4;
  3. double myDouble = (int) 1/4;
  4. double myDouble = (int) (1.0/4.0);
A

2. double myDouble = (double) 1/4;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Practice Test 01

What will the following code print?

A

Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Practice Test 01

What is wrong with the following code?

ArrayList list = new ArrayList();

list.add(1);

System.out.println(list.get(0));

  1. It will throw an IndexOutOfBoundsException.
  2. You cannot use int as the type in an ArrayList.
  3. list is a reserved word, so you can’t call the variable that.
  4. You need to define the new ArrayList on a new line of code.
  5. Nothing. The above code is correct.
A
  1. You cannot use int as the type in an ArrayList
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Practice Test 01

What does this method call output?

public double doubleNumber(double myNumber)

{

return (double) (int) myNumber * 2;

}

doubleNumber(7.8);

16

  1. 6
  2. 0

This method is improperly written.

Answered

A

14.0

20
Q

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?

  1. I am103years old
  2. I am13years old
  3. I am 103 years old
  4. I am 13 years old
A
  1. I am13years old
21
Q

Practice Test 01

What would this method call output?

divideByTen(340)

A

34

22
Q

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”)

  1. “dotpolka”
  2. “lkadotkdot”
  3. “olkadotldot”
  4. This method call will error.
A
  1. This method call will error
23
Q

Practice Test 01

What kind of error does this method cause?

myMethod(“Frog”);

  1. Compile Time Error: Missing return value
  2. Runtime Error: String index out of range
  3. Compile Time Error: Unexpected return value
  4. Compile Time Error: Syntax Error in method definition
A
  1. Runtime Error: String index out of range
24
Q

Practice Test 01

What does this method call output?

someMethod(3,1)

A

10

25
Q

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.

  1. Missing void in constructor definition
  2. Class constructors must be private
  3. Arrays can not be passed as parameters to a constructor
  4. Must use this in constructor when the constructor parameters have the same name as instance variables.
A
  1. Must use this in constructor when the constructor parameters have the same name as instance variables.
26
Q

Practice Test 01

What will the Array gameBoard contain after this code runs?

  1. [“X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”]
  2. [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
  3. [“O”, “X”, “X”, “O”, “X”, “X”, “O”, “X”, “X”]
  4. [“O”, “X”, “O”, “X”, “O”, “X”, “O”, “X”, “O”]
A
  1. [“X”, “O”, “O”, “X”, “O”, “O”, “X”, “O”, “O”]
27
Q

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;

}

  1. The != operator can not be used to compare ints.
  2. The != operator does not return a boolean value.
  3. The variable loadingTime is increasing instead of decreasing.
  4. while statements loop infinitely. A for statement should be used instead.
A
  1. The variable loadingTime is increasing instead of decreasing
28
Q

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?

A

9

29
Q

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?

  1. int col = 0; col < numCoins[row].length(); col++
  2. int col = 0; col < numCoins.length + 1; col++
  3. int col = 0; col < numCoins[row].length; col++
  4. int col = 0; col < numCoins.length; col++
A
  1. int col = 0; col < numCoins[row].length; col++
30
Q

Practice Test 01

What is the return value of this method call?

findTheMiddle(2110890125)

A

89

31
Q

Practice Test 01

Which methods of class Foo do not require an instance of class Foo to be invoked?

  1. All methods require an instance of Foo to be invoked.
  2. No methods require an instance of Foo to be invoked.
  3. bar()
  4. baz()
  5. foo()
A
  1. foo()
32
Q

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?

  1. Everything
  2. Only the variables name and type
  3. Only the variables
  4. Only the methods
  5. The variables name and type and the method move
A
  1. The variables name and type and the method move
33
Q

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. 1 2
  2. 2 1
  3. Runtime Error
  4. Compilation Error
A

1. 1 2

34
Q

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

A

I, II, and III

35
Q

Practice Test 01

What is the correct pseudocode for Insertion Sort?

  1. If there are at least two elements in the collection Partition the collection Insertion sort the left collection Insertion sort the right collection
  2. 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
  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
  4. While there are unsorted numbers Find the smallest unsorted number Insert the element at the end of the sorted elements
A

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

36
Q

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 */?

A
word = peanut
num = 7
37
Q

What will be returned by mystery?

  1. The minimum value in the array will be returned.
  2. The maximum value in the array will be returned.
  3. The length of the array will be returned.
  4. No value will be returned.
  5. All values in the array will be returned.
A
  1. 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.

38
Q

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);

}

A

6

39
Q

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?

  1. double result = (double) (17 / 4);
  2. double result = (double) 17 / 4;
  3. double result = 17.0 / 4;
  4. double result = 17 / (double) 4;
  5. double result = (double) 17 / 4.0;
A
  1. double result = (double) (17 / 4);
40
Q

Just before the end of execution of this program, what are the values of a, b, and temp, respectively?

  1. 12, 17, 12
  2. 17, 12, 12
  3. 17, 12, 17
  4. 17, 12, undefined
  5. 12, 17, undefined
A

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.

41
Q

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, 1, 2, 5, -4}
  2. {11, -10, -8, -4, 5}
  3. {3, 6, 13, 14, 5}
  4. {21, 20, 18, 14, 5}
  5. This method results in an IndexOutOfBounds exception.
A
  1. {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].

42
Q

What value is returned as the result of the call mystery(6)?

A

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.

43
Q

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();

  1. 3 4 3 4
  2. 6 7 6 7
  3. 3 4 8 9
  4. The code won’t complile.
  5. When callaLily.addWater() is executed a runtime error ClassCastException is thrown.
A
  1. 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.

44
Q

Which of the following is printed as a result of the call mystery(12345)?

  1. 543210
  2. 5432112345
  3. 1551
  4. 1234554321
  5. Many digits are printed because this method creates an infinite recursion.
A
  1. 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.

45
Q
A