Computer Science: Application Multiple Choice

1. True or false: If an array is already sorted, Linear Search / Sequential Search is more efficient than Binary Search.

True
False

2. What must be true before performing a binary search?

The elements must be sorted.
It can only contain binary values.
The elements must be some sort of number (i.e. int, double, Integer)
There are no necessary conditions.

3. Which of the following functions is a proper implementation of binary search?

public static int binarySearch(int[] array, int key)

{

int n = array.length;

int first = 0;

int last = n – 1;

int middle = (first + last)/2;

while( first <= last )

{

if ( array[middle] < key )

{

first = middle + 1;

}

else if ( array[middle] == key )

{

return (middle + 1);

}

else

{

last = middle – 1;

}

middle = (first + last)/2;

}

return -1;

}

public static int binarySearch(int[] array, int key)

{

int n = array.length;

int first = 0;

int last = n – 1;

int middle = (first + last)/2;

while( first <= last )

{

if ( array[middle] < key )

{

first = middle + 1;

}

else if ( array[middle] == key )

{

return middle;

}

else

{

last = middle – 1;

}

middle = (first + last)/2;

}

return -1;

}

public static int binarySearch(int[] array, int key)

{

n = array.length;

first = 0;

last = n – 1;

middle = (first + last)/2;

while( first <= last )

{

if ( array[middle] < key )

{

first = middle + 1;

}

else if ( array[middle] == key )

{

System.out.println(key + ” found at index ” + middle);

}

else

{

last = middle – 1;

}

middle = (first + last)/2;

}

System.out.println(“Key not found!”)

}

public static int binarySearch(int[] array, int key)

{

int n = array.length;

int first = 0;

int last = n – 1;

int middle = (first + last)/2;

while( first <= last )

{

if ( array[middle] < key )

{

first = middle + 1;

}

else if ( array[middle] == key )

{

int result = middle;

}

else

{

last = middle – 1;

}

middle = (first + last)/2;

}

}

4. Given this array:

1 2 4 5 6 7 8 12 14 21 22 42 53

How many comparisons are required to find 42 using the Binary Search?

3
2
10
5

5. Given this array:

1 2 4 5 6 7 8 12 14 21 22 42 53

How many comparisons are required to find 42 using the Linear Search / Sequential Search?

3
2
12
5

6. BMO the robot is programming a new game called “Open the box!” You give him a number and he tries to open a numbered box. He’s using Binary Search to accomplish this. Unfortunately, the boxes are not sorted. They are in the following order:

1 3 6 9 14 10 21

Which box can NEVER be found using binary search?

9
6
14
10

7. We are searching for an int key in a sorted int array that has n elements. Under what circumstances will Linear Search / Sequential Search be more efficient than Binary Search?

key is the last element in the array
key is in the middle of the array
n is very large
key is the first element in the array
key does not exist in the array

8. What is the largest number of comparisons needed to perform a binary search on an array with 42 elements?

2
5
6
41
42

9. What approach does MergeSort use?

divide and conquer
iterative
search and swap
search and insert

10. Which sorting method is implemented below?

public int[] array;

public int[] tempArr;

public int length;

public void sort(int[] inputArr)

{

array = inputArr;

length = inputArr.length;

tempArr = new int[length];

doSort(0, length – 1);

}

private void doSort(int lowerIndex, int higherIndex)

{

if (lowerIndex < higherIndex)

{

int middle = lowerIndex + (higherIndex – lowerIndex) / 2;

doSort(lowerIndex, middle);

doSort(middle + 1, higherIndex);

doSomething(lowerIndex, middle, higherIndex);

}

}

private void doSomething(int lowerIndex, int middle, int higherIndex)

{

for (int i = lowerIndex; i <= higherIndex; i++)

{

tempArr[i] = array[i];

}

int i = lowerIndex;

int j = middle + 1;

int k = lowerIndex;

while (i <= middle && j <= higherIndex)

{

if (tempArr[i] <= tempArr[j])

{

array[k] = tempArr[i];

i++;

}

else {

array[k] = tempArr[j];

j++;

}

k++;

}

while (i <= middle)

{

array[k] = tempArr[i];

k++;

i++;

}

}

Selection sort
Insertion sort
Mergesort
Binary search
Sequential search / Linear search

11. Given the method implemented above, how many times will the method doSort be called if an array agescontains the values

[23, 12, 9, 46, 34]

and sort(ages) is called?

12
6
9
5
15

12. How are Selection Sort and Insertion Sort different?

I – Selection Sort is always faster than Insertion Sort

II – Insertion Sort is always faster than Selection Sort

III – When Selection Sort places an element into the sorted part of the array, that element is in its final position, whereas Insertion Sort may move the element later if it finds a smaller element. Selection Sort builds up an absolutely sorted array as it goes while Insertion Sort builds up a relatively sorted array as it goes.

IV – Insertion Sort is faster if the array is already sorted or close to sorted

I only
II only
II and IV
II and III
III and IV

13. 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

14. For this array of numbers:

[1, 2, 3, 4, 5]

How many comparisons are made using Selection Sort?

5
10
1
6
11

15. Which of the following sorting algorithms use recursion?

Selection Sort
Insertion Sort
Merge Sort

16. What are the drawbacks of using mergesort?

It has no drawbacks.
It only works on arrays.
There are special cases where it runs very slowly.
It uses more memory.

17. Suppose we have a sorted list with n elements. We decide to perform an insertion sort on this already sorted set. How many comparisons will the sort make?

n – 1
n
n (n – 1)
n + 1

18. What must be true in order to sort objects?

They must be wrapper classes of primitives (Integer, Double, etc.)
They must be Comparable.
At least one instance variable must be a primitive.
They must be Strings
Objects are not sortable, only primitives are sortable.

19. Consider the following recursive function:

public int mystery(int n)

{

if(n == 0)

{

return 2;

}

else

{

return 2 * mystery(n – 1);

}

}

How many times will the function mystery be called if we call mystery(5) (be sure to include the first call mystery(5))

5
6
4
10
The recursion will go on forever because there is no base case.

Question: 1

You are working as a programmer on a new game called Dessert Demolish at the well known game studio Queen. You are asked to write a method that searches a String[][] for a given dessert name. The problem specification does not indicate what your method should do if there are multiple Strings in the String[][]with the same value. Which of the following actions would be best?

The method should be written on the assumption that there is only one dessert String in the String[][] with the passed value.
The method should be written so as to return the index of every occurrence of the passed dessert String value.
The specification should be modified to indicate what should be done if there is more than one index of the passed dessert String.
The method should be written to output a message if more than one larger value is found.
The method should be written to delete all subsequent instances of the dessert String from the String[][]

CHECK

Question: 2

Considering the inheritance hierarchy

public class Message

{

}

public class Text extends Message

{

}

public class Email extends Message

{

}

public class Letter extends Message

{

}

Which of the following statements are true about the above classes?

I – The Text class can have private instance variables that are not in its sibling or parent classes.  II – If the Message class has a private variable, none of it’s children classes can access the variable directly.  III – Each of the 4 classes can have a method called toString, whose code is the exact same throughout all of them.  IV – The Text, Email, and Letter class all inherit the contructors of the Message class

I only
III only
I and II
III, and III
IIIIII, and IV

CHECK

Question: 3

Given the following class:

public class Dog

{

public String name;

private String breed;

private int age;

public Dog(String name, String breed, int age)

{

}

public String getBreed()

{

return breed;

}

public int getAge()

{

return age;

}

}

Which of the following represents the correct implementation of the Dog’s constructor method?

name = name;

breed = breed;

age = age;

myName = name;

myBreed = breed;

myAge = age;

this.name = name;

this.breed = breed;

this.age = age;

this.name = name;

this.breed = getBreed();

this.age = getAge();

return new Dog(name, breed, age);

CHECK

Question: 4

Given the following method.

// Precondition: myArray is not empty

// Postcondition: Returns max int value found in myArray

public static int findMax(int[] myArray)

{

int max = // Some value.

for (int i = 0; i < myArray.length; i++)

{

if (myArray[i] > max)

{

max = myArray[i];

}

}

return max;

}

Which of following are correct starting values for the int max variable.

– I Integer.MIN_VALUE

– II Integer.MAX_VALUE

– III myArray[0]

– IV 9999

I Only
IV Only
I and III
II and IV
II Only

CHECK

Question: 5

What is the largest base-10 integer that can be represented by 8 bits?

8
256
16
15
255

CHECK

Question: 6

public abstract class Quadrilateral

{

private String labels;

public Quadrilateral(String labels)

{

this.labels = labels;

}

public String getLabels()

{

return labels;

}

public abstract int area();

}

Which of the following statements about the Quadrilateral class are true?

I – Subclasses of the class must implement the the area method.  II – No instances of the class can be created. This class cannot be instantiated.  III – The area method is missing an implementation. This code will not compile.  IV – The getLabels method is not abstract.  V – Subclasses of Quadrilateral will not directly inherit the constructor.

IV only
II only
I and III
IIIV, and V
IIIIV, and V

CHECK

Question: 7

When must the super keyword be called in a subclass constructor?

The super keyword must always be called in the subclass constructor.
The super keyword never has to be called in the subclass constructor.
The super keyword must be called in the subclass constructor if the parent class is abstract.
The super keyword must be called in the subclass constructor if the parent class does not have a constructor with no parameters.
The super keyword must be called in the subclass constructor if the parent is the Object class

CHECK

Question: 8

Considering the following code segments:  Main.java

public class Main

{

public void run()

{

MyClass myInstance = new MyClass();

MyOtherClass myOtherInstance = new MyOtherClass();

/* more code here */

}

}

MyClass.java

public class MyClass

{

public MyClass()

{/* code */}

public void someMethod()

{/* code */}

}

MyOtherClass.java

public class MyOtherClass extends MyClass

{

public MyOtherClass()

{/* code */}

public void someOtherMethod()

{/* code */}

}

which of the following method calls would error if placed inside the /* more code here */ comment inside the main method.  You may assume that none of the implementations of the methods will throw a runtime error.

I. myInstance.someMethod();

II. myInstance.someOtherMethod();

III. myOtherInstance.someMethod();

IV. myOtherInstance.someOtherMethod();

All of them
None of them
III Only
I and IV
II Only

CHECK

Question: 9

Which of the following is true about a method that is overriding another method?

A method with the same name as another method but with different types for the parameters
A method with the same name as another method but with a different number of parameters
A method with the same precondition as another method
A method with the same name and parameter list as an inherited method
None of these are true

CHECK

Question: 10

Given this code segment:

public void myMethod(String[] array)

{

for(i = 0; i < array.length; i++)

{

System.out.println(array[i]);

}

}

Identify the bug in the code.

Arrays do not have a length property
There will be an index out of bounds exception
The code results in a null pointer exception
You cannot initialize variables inside a for loop statement
Variable i was not declared

CHECK

Question: 11

What would be printed to the console with the following expression:

System.out.println( 5 + 4 + “string” + 7 + 7 );

9string14
54string77
9string77
54string14
This code will result in an error

CHECK

Question: 12

What would be printed to the console with the following expression:

System.out.println( (5 + 4) + “string” + ( 7 + 7 ) );

9string14
54string77
9string77
54string14
This code will result in an error

CHECK

Question: 13

What is the output of the following code:

ArrayList<Integer> numbers = new ArrayList<Integer>();

numbers.add(1);

numbers.add(0, 2);

numbers.add(1, 3);

numbers.add(4);

numbers.remove(2);

numbers.add(3, 5);

numbers.remove(2);

[2, 3, 4]
[2, 3, 5]
[1, 2, 5]
[1, 2, 4]
[1, 3, 4]

CHECK

Question: 14

Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements?

128
256
4
8
16

CHECK

Question: 15

How many comparisons will the selection sort algorithm make on an array of 8 elements?

3
8
24
28
64

CHECK

Question: 16

The following method counts the number of times even numbers appear in an ArrayList:

public int countEvenNumbers(ArrayList<Integer> numbers)

{

int count = 0;

// Missing code here

return count;

}

What should replace the comment // Missing code here?

for (int i = 0; i < numbers.size(); i++)

{

if (numbers.get(i) % 2 == 0)

{

count++;

}

}

for (int i = 0; i <= numbers.size(); i++)

{

if (numbers.get(i) % 2 == 0)

{

count++;

}

}

for (int i = 0; i < numbers.size(); i++)

{

if (numbers.get(i) % 2 == 1)

{

count++;

}

}

for (int i = 0; i < numbers.size(); i++)

{

if (i % 2 == 0)

{

count++;

}

}

for (int i = 0; i < numbers.size(); i++)

{

if (isEven(numbers.get(i)))

{

count++;

}

}

CHECK

Question: 17

What is the precondition for binary search to work on an array?

The array must contain only integers.
The array must be of even size.
The array must be of odd size.
The element being searched for must be in the array.
The array must be sorted.

CHECK

Question: 18

The following array is to be sorted biggest to smallest using insertion sort.

[10, 40, 50, 30, 20, 60]

What will the array look like after the third pass of the for loop?

[60, 50, 40, 30, 20, 10]
[50, 40, 10, 30, 20, 60]
[50, 40, 30, 10, 20, 60]
[60, 50, 40, 30, 10, 20]
[10, 30, 40, 50, 20, 60]

CHECK

Question: 19

Which of the following algorithms take advantage of the divide and conquer paradigm?

Selection Sort and Insertion Sort
Selection Sort and Merge Sort
Sequential Search and Merge Sort
Binary Search and Merge Sort
Binary Search and Insertion Sort

CHECK

Question: 20

The following method calculates the sum of the integers between a and b, inclusive.

public int sumNumbers(int a, int b) {

int sum = 0;

// Missing code here

return sum;

}

What should be the missing code?

while (a <= b) {

sum += a;

a++;

}

while (a <= b) {

sum += b;

a++;

}

while (a <= b) {

a += sum;

a++;

}

while (a <= b) {

sum += a;

b++;

}

while (a < b) {

sum += a;

a++;

}

CHECK

Question: 21

Of the sorting algorithms we’ve learned (selection sort, insertion sort, and merge sort), is merge sort the fastest in every case? Why?

Yes, because merge sort uses divide and conquer.
Yes, because merge sort doesn’t look at every element in the array.
No, because insertion sort is faster when the array has 1 or 2 elements.
No, because selection sort is faster when the array is sorted.
No, because insertion sort is faster when the array is sorted.

CHECK

Question: 22

The following method is an implementation of sequential search:

public int sequentialSearch(int[] array, int key)

{

int i = 0;

int n = array.length;

while (/* missing conditional statement */)

{

i++;

}

if (i == n) return -1; // value not found

else return i; // value found at index i

}

What code should replace the /* missing conditional statement */ in order to have a functional sequential search?

key != array[i]
i < n && key == array[i]
key != array[i] && i < n
i < n && key != array[i]
i < n || key != array[i]

CHECK

Question: 23

What is the decimal value of the binary number 1011?

3
9
11
13
24

CHECK

Question: 24

Assume A, B and C are booleans.  Which of the following expressions are equivalent?

I. (!A || !B)  II. (!A && !B)  III. !(A && B)

I and II.
I and III
II and III
I, II, and III
None is equivalent with another.

CHECK

Question: 25

Which of the following expressions evaluates to true?  Assume A = true, B = false and C = false.

(A || B) && C
(A && C) || B
(A || C) || B
!(A && B || !C)
(!A || C || B)

CHECK

Question: 26

Which of the following, as the /* body */ of the half, will return the double 3.5 when half(7) is called?

public double half(int x)

{

/* body */

}

return x/2;
return (double)(x/2);
return (double)x/2;
return x//2;
return double(x/2);

CHECK

Question: 27

What is returned as a result of calling mystery(5)?

public int mystery(int b)

{

if (b == 0)

{

return 0;

}

if (b % 2 == 0)

{

return mystery(b – 1) + 2;

}

else

{

return mystery(b – 1) + 1;

}

}

5
7
12
0
This program creates an infinite loop.

CHECK

Question: 28

Consider the following method foo. Assume n will always be greater than or equal to 1:

public int foo(int n)

{

if (n == 1 || n == 2)

{

return 2 * n;

}

else

{

return foo(n – 1) – foo(n – 2);

}

}

When foo(6) is executed, how many calls to foo will be made, including the original call?

7
9
13
15
25

CHECK

Question: 29

What will the following method print to the console?

public void foo()

{

int a = 0;

a++;

if (0 == 1 && a == 1)

{

System.out.println(“foo”);

}

else if (0 == 1 && (a + 1) == 2)

{

System.out.println(“bar”);

}

else

{

System.out.println(“baz”);

}

System.out.print(a);

}

baz

0

baz

1

baz

2

bar

baz

1

bar

baz

2

CHECK

Question: 30

You have an array of size 7, declared with the following code:

int[] arr = {1, 2, 3, 4, 5, 6, 7};

What do you get when you execute the following code?

System.out.println(arr[7]);

7
1
The code will execute, but nothing will be printed.
A run-time error
A compile-time error

CHECK

Question: 31

You’re writing a program that will simulate a waiting line in a doctor’s office. When the user runs the program, she will be able to input either a patient’s name to add them to the waiting line or a number to see who is at that position in the waiting line. What would be the best way to store the waiting line in your program?

An ArrayList
An array
A string
You can use an array or an ArrayList.
None of the above.

CHECK

Question: 32

Which of the following statements will compile with no errors?

– I ArrayList<int> arr = new List<int>();

– II ArrayList<Integer> arr = new List<Integer>();

– III ArrayList<Integer> arr = new ArrayList<Integer>();

– IV ArrayList<int> arr = new ArrayList<int>();

– V List<Integer> arr = new ArrayList<Integer>();

I, IV
II, III, V
II, III
III, V
III, IV

CHECK

Question: 33

The following method

public String removeFromString(String old, String frag)

removes all occurences of the string frag from the string old. For example,

removeFromString(“hello there!”, “he”) returns “llo tre!”

removeFromString(“lalalala”, “l”) returns “aaaa”

Which of the following blocks of code will successfully implement removeFromString()?

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + frag.length());

old = old.substring(0, index);

index = old.indexOf(frag);

}

}

return old;

}

private String removeFromString(String old, String frag)

{

int i = old.indexOf(frag);

while (i > -1) {

String rest = old.substring(i + frag.length());

old = old.substring(0, i);

old = old + rest;

i = old.indexOf(frag);

}

return old;

}

private String removeFromString(String old, String frag)

{

int i = old.indexOf(frag);

while (i > -1) {

String rest = old.substring(i + frag.length());

old = old.substring(0, i+1);

old = old + rest;

i = old.indexOf(frag);

}

return old;

}

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + frag.length());

old = old.substring(0, index + 1);

index = old.indexOf(frag);

}

}

return old;

}

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + 1 + frag.length());

old = old.substring(0, index + 1);

index = old.indexOf(frag);

}

}

return old;

}

CHECK

Question: 34

Your friend has sent you a secret message in the form of 2-D string arrays, but in order to decode it, you’ll have to find the difference between the two arrays. The secret message is the concatenation of all elements in arrA that are different than the corresponding elements in arrB. Given that the two arrays are of the same size, you write the following code:

public static String compareArrays(String[][] arrA, String[][] arrB) {

int rows = arrA.length;

int cols = arrA[0].length;

String secretMsg = “”;

for (int i = 0; i < rows; i++)

{

/* EXPRESSION 1 */

secretMsg = secretMsg + arrA[i][j];

}

}

}

return secretMsg;

}

Which block of code can replace the /* EXPRESSION 1 */ comment to successfully decode the message?

· I

for (int j = 0; j < cols; j++)

{

if (!arrA[i][j].equals(arrB[i][j]))

{

· II

for (int j = 0; j < cols; j++)

{

if (arrA[i][j].compareTo(arrB[j][i]) != 0)

{

· III

for (int j = 0; j < cols; j++)

{

if (arrA[i][j].compareTo(arrB[i][j]) != 0)

{

· IV

for (int j = 0; j < cols; j++)

{

if (!arrA[i][j].equals(arrB[j][i]))

{

Only III
Only IV
Both I and III
Both II and IV
None of the above

CHECK

Question: 35

Which of the following string initializations will result in an error?

String myString = “I’m not sure”;
String myString = ” ‘ ‘ ‘ “;
String myString = “\”Hello World\””;
String myString = “Hello “World””;
String myString = ” ‘ \\ ‘ “;

CHECK

Question: 36

Which code snipped correctly imports the java ArrayList?

import java.util.;
from java.util import ArrayList;
ArrayList = require(‘ArrayList’);
#include<java.util.ArrayList.h>
import java.util.ArrayList;

CHECK

Question: 37

Given the following code snipped:

public class Person

{

String name;

public Person(String name)

{

this.name = name;

}

public static boolean sameName(Person otherPerson)

{

return this.name.equals(otherPerson.name);

}

}

What is the bug in the code?

Strings in java do not have a .equals method.
The this keyword should be replaced with the self keyword.
Static methods do not have access to the this keyword.
The Person class needs to have a default constructor.
The sameName method needs to be private.

CHECK

Question: 38

What is wrong with the following interface declaration?

public interface Ball

{

private int airLevel;

public Ball()

{

airLevel = 0;

}

public void bounce();

public void roll();

public void inflate();

public void deflate();

}

airLevel = 0; needs to use the this keyword. Like so, this.airLevel = 0;.
Java interfaces must not include any implementation logic.
All of the method declarations should be private.
All of the methods must include their implementation logic.
Interface methods can not return void.

CHECK

Question: 39

What does the following code segment do?

boolean myBoolean = false;

for (int i = 0; i < myArray.length; i++)

{

myBoolean = myBoolean && (myArray[i] > 0);

}

Always sets the value of myBoolean to false.
Sets the value of myBoolean to true only if the last value in myArray is < 0.
Sets the value of myBoolean to true only if every value in myArrayis > 0.
Sets the value of myBoolean to true if any value in myArray is > 0.
Always sets the value of myBoolean to true.

CHECK

Question: 40

Which of the following are true about a class that contains an abstract method?

· I. The class must not have fields declared.

· II. The class must be declared with the abstract keyword.

· III. The class can not have constructors.

I and III only
I and II only
I, II and III
I only
II only

CHECK

Continue

Evaluate the mathematical expression: 6 + 12 ÷ 3 × (5 – 6)

-6
-4
2
6

CHECK

Question: 2

If x represents the area of the number line colored orange, which inequality accurately reflects the value of x?  numberline

x < 4
x > 4
x ≥ 4
x ≤ 4

CHECK

Question: 3

Christine will go to the beach if it’s not raining and if it’s between 75°F and 95°F. Otherwise, she’ll go to the movies.  In which of the following cases will Christine go to the beach?

It’s 80°F and raining
It’s not raining and 73°F
It’s raining and 96°F
It’s not raining and 83°F

CHECK

Question: 4

Karel the Dog is instructed to move forward three spaces. Then, if Karel is standing on a ball, Karel will turn left and move forward one space. Otherwise, Karel will turn right and move forward one space. Given the starting point below, where will Karel end up?  Starting Point:

karel

karel
karel
karel
karel

CHECK

Question: 5

Consider the following output.

1 1 1 1 1

2 2 2 2

3 3 3

4 4

5

Which of the following code segments will produce this output?

for (int j = 1; j <= 5; j++)

{

for (int k = 1; k <= 5; k++)

{

System.out.print(j + ” “);

}

System.out.println();

}

for (int j = 1; j <= 5; j++)

{

for (int k = 1; k <= j; k++)

{

System.out.print(j + ” “);

}

System.out.println();

}

for (int j = 1; j <= 5; j++)

{

for (int k = 5; k >= 1; k–)

{

System.out.print(j + ” “);

}

System.out.println();

}

for (int j = 1; j <= 5; j++)

{

for (int k = 5; k >= j; k–)

{

System.out.print(j + ” “);

}

System.out.println();

}

CHECK

Question: 6

Which of the following successfully switches the integer values of the variables x and y? temp may be used as a temporary variable if necessary.

x = y;

y = x;

int temp = x;

y = x;

temp = y;

x = y;

y = temp;

int temp = x;

x = y;

y = temp;

CHECK

Question: 7

A car dealership is writing a program to store information about the cars for sale. You create a class called Car to represent a single car that is for sale.

Which of these would not make sense as an instance variable in the Car class

private double milesPerGallon
private boolean hasAirConditioning
private int numDoors
private int totalNumCars

CHECK

Question: 8

How many times will the loop execute?

int i = 0;

while(i < 50)

{

System.out.println(i);

i += 10;

}

0
5
50
4

CHECK

Question: 9

What is a method?

A procedure that is defined by the user.
A keyword used to loop for a fixed amount of time.
A place where we can store data.
The type that is given to a variable.

CHECK

Question: 10

What does this method call output?

public double doubleOrNothing(double myDouble)

{

return (double) (int) myDouble * 2;

}

doubleOrNothing(9.9);

This method is improperly written.
18.0
18.8
20

CHECK

Question: 11

Given this code snippet,

public class GoldenRetriever

{

public GoldenRetriever(String name)

{

this.name = name;

}

}

what is missing from the class definition?

Need to declare the name instance variable
Missing void in constructor definition
Class constructors must be private
Java does not use the keyword this, instead it uses self
No constructor is defined for the class

CHECK

Question: 12

Given the definitions of class A and class B

class A

{

public int i;

public int j;

public A()

{

i = 1;

j = 2;

}

}

class B extends A

{

int a;

public B()

{

super();

}

}

what is the output of this code

B obj = new B();

System.out.println(obj.i + ” ” + obj.j);

Runtime error
Compilation error
1 2
2 1

CHECK

Question: 13

What is the output of the following program?

public class SayHello

{

public void run()

{

String stringArray[] = {“h”, “e”, “l”, “l”, “o”, “w”};

for(int i=0; i <= stringArray.length; i++)

{

System.out.print(stringArray[i]);

}

}

}

hel
hellow
Error
hello
None of the above

CHECK

Question: 14

Which is the correct way to construct and assign a 2D array, with 12 rows and 17 columns, to the variable pizza?

int[12][17] pizza;
int[12][17] pizza = new int[12][17];
int[][] pizza = new int[12][17];
int[12][17] pizza = new int[][];
int[][] pizza = new int[][](12, 17);

CHECK

Question: 15

Given this array:

1 2 4 5 6 7 8 12 14 21 22 39 53

How many comparisons are required to find 39 using the Binary Search?

2
3
5
10

CHECK