Name:     ID: 
 
Email: 

Compsci_Ap_final_2016-17

Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 1. 

Java’s increment operator is represented by the symbol ____.
a.
+=
b.
++
c.
+
d.
=+
 

 2. 

The condition in an if statement must be a(n) ____ expression.
a.
Integer
b.
String
c.
Arithmetic
d.
Boolean
 

 3. 

The equal-to (comparison) operator is represented by the symbol ____.
a.
=
b.
==
c.
!=
d.
===
 

 4. 

The process of executing all of the contained statements within a loop is called a(n) ____ of the loop.
a.
iteration
b.
implementation
c.
execution
d.
processing
 

 5. 

Which of the following code snippets would display the square roots of 25, 20, 15, and 10?
a.
int number = 25;
while (number > 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number -= 5;
}
b.
int number = 25;
while (number <= 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number--;
}
c.
int number = 25;
while (number >= 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number -= 5;
}
d.
int number = 25;
while (number >= 10){
     System.out.println ("The square root of " +
          number + " is " + Math.sqrt (number));
     number--;
}
 

 6. 

Which of the following represents the common structure for all while loops?
a.
initialize variables
change variables involved in the condition
while (condition){
     perform calculations
}
b.
initialize variables
while (condition){
     perform calculations
     change variables involved in the condition
}
c.
while (condition){
     initialize variables
     perform calculations
     change variables involved in the condition
}
d.
initialize variables
while (condition){
     perform calculations
}
change variables involved in the condition
 

 7. 

Which of the following represents the proper structure of a for loop?
a.
for (initialize counter; update counter) {
    statement;
    statement;
     . . .;
}
b.
for (initialize counter; test counter; update counter) {
    statement;
    statement;
     . . .;
}
c.
for (update counter; initialize counter; test counter ) {
    statement;
    statement;
     . . .;
}
d.
for (test counter; initialize counter; update counter) {
    statement;
    statement;
     . . .;
}
 

 8. 

Consider the following code fragment:

System.out.print("Enter a positive integer: ");
int n = reader.nextInt();

int limit = n / 2;

for (int d = 2; d <= limit; d++){
    if (n % d == 0)
        System.out.print (d + " ");
}

If the user enters the number 12 when prompted, the output displayed to the console will be ____.
a.
2
b.
2 3 4 5 6
c.
2 3 4 6
d.
no output
 

 9. 

Java’s increment operator is represented by the symbol ____.
a.
+=
b.
++
c.
+
d.
=+
 

 10. 

The ____ class contains a number of methods that can be used to perform common mathematical operations, such as calculate a square root.
a.
Mathematical
b.
Arithmetic
c.
Random
d.
Math
 

 11. 

The equal-to (comparison) operator is represented by the symbol ____.
a.
=
b.
==
c.
!=
d.
===
 

 12. 

The process of executing all of the contained statements within a loop is called a(n) ____ of the loop.
a.
iteration
b.
implementation
c.
execution
d.
processing
 

 13. 

Which of the following represents the common structure for all while loops?
a.
initialize variables
change variables involved in the condition
while (condition){
     perform calculations
}
b.
initialize variables
while (condition){
     perform calculations
     change variables involved in the condition
}
c.
while (condition){
     initialize variables
     perform calculations
     change variables involved in the condition
}
d.
initialize variables
while (condition){
     perform calculations
}
change variables involved in the condition
 

 14. 

Consider the following code fragment:

System.out.print("Enter a positive integer: ");
int n = reader.nextInt();

int limit = n / 2;

for (int d = 2; d <= limit; d++){
    if (n % d == 0)
        System.out.print (d + " ");
}

If the user enters the number 12 when prompted, the output displayed to the console will be ____.
a.
2
b.
2 3 4 5 6
c.
2 3 4 6
d.
no output
 

 15. 

Suppose a Student class contains a method setName to set the value of a Student object’s instance variable name, plus a method getName to access the value of name. Consider the following code fragment:

Student s1, s2;
s1 = new Student();
s2 = new Student();
s1.setName(“Bill”);
s2.setName(“Eric”);
s2 = s1;
s2.setName(“Bob”);
System.out.println( s1.getName() );

What would be displayed on the console?
a.
Bob
b.
Eric
c.
Bill
d.
null
 

 16. 

Methods used to initialize a new object are called ____.
a.
initializers
b.
creators
c.
initiators
d.
constructors
 

 17. 

Class definitions usually begin with the keyword ____, indicating that the class is accessible to all potential clients.
a.
accessible
b.
public
c.
private
d.
global
 

 18. 

Which of the following represents the general form of a method?
a.
<visibility modifier> <return type> <method name> (<parameter list>){

    <implementing code>

}
b.
<return type> <visibility modifier> <method name> (<parameter list>){

    <implementing code>

}
c.
<visibility modifier> <method name> <return type> (<parameter list>){

    <implementing code>

}
d.
<return type> <method name> (<parameter list>){

    <implementing code>

}
 

 19. 

The scope of a ____ is all the methods in the defining class.
a.
global variable
b.
local variable
c.
parameter
d.
constant
 

 20. 

Java’s AND operator is represented by the symbol ____.
a.
$$
b.
!
c.
||
d.
&&
 

 21. 

Assuming p and q are Boolean variables, the expression !(p || q) is equivalent to ____.
a.
!p && !q
b.
p && q
c.
!p && q
d.
!p || !q
 

 22. 

Consider the following code fragment:

   if (x > 5 )
        if ( y < 10 )
            System.out.print(“1”);
            System.out.print(“2”);
        else
            System.out.print(“3”);

Assuming that x has a value of 10 and y has a value of 5, the output of this code fragment would be ____.
a.
1
b.
3
c.
12
d.
Nothing. The code would not compile.
 

 23. 

The first index in an array is ____.
a.
-1
b.
0
c.
1
d.
null
 

 24. 

Which of the following would correctly declare an array of 500 integers named abc?
a.
int[] abc = int[500];
b.
int abc = new int[500];
c.
int[] abc = new int[499];
d.
int[] abc = new int[500];
 

 25. 

The [] operator has the same precedence as the ____ operator.
a.
+
b.
.
c.
!
d.
&&
 

 26. 

Given an array of integers named abc, which of the following code fragments can be used to interchange the first two array elements within abc?
a.
abc[0] = abc[1];
abc[1] = abc[0];
b.
int temp = abc[0];
abc[1] = abc[0];
abc[1] = temp;
c.
int temp = abc[0];
abc[1] = abc[0];
abc[0] = temp;
d.
int temp = abc[0];
abc[0] = abc[1];
abc[1] = temp;
 

 27. 

Which of the following statements would declare a two-dimensional array named abc that has two rows and three columns?
a.
int abc = new int[2][3];
b.
int[][] abc = new int[2][3];
c.
int[][] abc = new int[3][2];
d.
int[] abc = new int[2][3];
 

 28. 

Given a two-dimensional array of integers named table, which of the following statements would yield the number of columns in the first row?
a.
table.length[0]
b.
table[0].length
c.
table[1].length
d.
table.length
 

 29. 

Which of the following is the correct syntax for the enhanced for loop?
a.
for (<array object> :<temporary variable declaration> )
   <statement>
b.
for (<temporary variable declaration> : <array object> : <increment step> )
   <statement>
c.
for (<temporary variable declaration> : <increment step> : <array object>)
   <statement>
d.
for (<temporary variable declaration> : <array object>)
   <statement>
 

 30. 

Each of the following methods takes an array of integers named abc and an integer searchValue as arguments. Which of these methods would correctly determine the index of the first occurrence of searchValue in the array and return its index, or return -1 if searchValue does not occur in the array?
a.
int search (int[] a, int searchValue){
   for (int i = 0; i < a.length; i++)
      if (a[i] == searchValue)
         return i;
   return -1;
}
b.
int search (int[] a, int searchValue){
   for (int i = 0; i <= a.length; i++)
      if (a[i] == searchValue)
         return i;
   return -1;
}
c.
int search (int[] a, int searchValue){
   for (int i = 0; i < a.length; i++)
      if (a[i] = searchValue)
         return i;
  return -1;
}
d.
int search (int[] a, int searchValue){
   for (int i = 0; i < a.length; i++)
      if (a[i] == searchValue)
         return i;
      else
         return -1;
}
 

 31. 

The equals method expects a(n) ____ as a parameter.
a.
Boolean
c.
String
b.
Object
d.
int
 

 32. 

The String class’s ____ method returns the character at a specified position in the string.
a.
charIndex
b.
charAt
c.
character
d.
characterAt
 

 33. 

The String class’s ____ method compares the contents of two String objects, but ignores the case of the characters.
a.
equalsCaseSensitive
b.
equals
c.
notEqualsCase
d.
equalsIgnoreCase
 

 34. 

The String class’s ____ method coverts all of the characters in a String object to lowercase.
a.
toLowerCase
b.
lowerCase
c.
smallChars
d.
lower
 

 35. 

Which of the following will return the number of characters in a String object named s1?
a.
s1.length
b.
s1.size
c.
s1.length()
d.
s1.size()
 

 36. 

If there is a String object named s1 with a value of “abcdedf,” the statement s1.indexOf(“xyz”); will return a value of ____.
a.
-1
b.
0
c.
false
d.
null
 

 37. 

Consider the following code:

int search (int[] a, int searchValue){
   for (int i = 0; i < a.length; i++)
      if (a[i] == searchValue)
         return i;
   return -1;
}

This method performs a ____ search on an array.
a.
simplistic
b.
binary
c.
linear
d.
conditional
 

 38. 

Suppose an unsorted array of five integers contains the values {4, 2, 5, 1, 3}. If a selection sort is performed on this array, the values stored in the array after the second pass of the sorting algorithm would be ____.
a.
{4, 2, 5, 1, 3}
b.
{1, 2, 3, 4, 5}
c.
{1, 2, 3, 5, 4}
d.
{1, 2, 5, 4, 3}
 

 39. 

The ArrayList method ____ returns the number of elements currently in the list.
a.
listSize()
b.
numElements()
c.
size()
d.
length()
 

 40. 

Which of the following statements would correctly declare an ArrayList object that only holds references to String objects?
a.
ArrayList[String] list = new ArrayList[String]();
b.
ArrayList<String> list = new ArrayList<String>();
c.
ArrayList list = new ArrayList(<String>);
d.
ArrayList list = new ArrayList();
 

 41. 

If a String object named s1 has a value of “abc,” the statement s1.equals(“abc”); will return a value of ____.
a.
less than 0
b.
0
c.
greater than 0
d.
null
 

 42. 

The basic idea of a binary search is to examine the element at the array’s ____ on each pass through the search loop.
a.
first index
b.
index containing the smallest element
c.
midpoint
d.
last index
 

 43. 

Assuming obj1 and obj2 belong to a class that implements Comparable, the statement obj1.compareTo(obj2); will return a value of ____ if obj1 is less than obj2.
a.
less than 0
b.
greater than 0
c.
0
d.
-1
 

 44. 

      Which of the following expressions gives the last element in the ArrayList alist?
a.
alist.last()
b.
alist.get(alist.last())
c.
alist.get(alist.length())
d.
alist.get(alist.size())
e.
alist.get(alist.size()-1)
 

 45. 

Which of the following statements adds item to the end of the ArrayList alist?
a.
alist.add(item);
b.
alist.set(alist.size()-1, item);
c.
alist[alist.size()-1] = item;
d.
item.addtoEnd(alist);
e.
Both a and b.
 

 46. 

Which of the following correctly declares an ArrayList of Car objects?
a.
ArrayList Car;
b.
ArrayList cars = Car;
c.
ArrayList Car[] cars;
d.
ArrayList<Car> cars;
e.
ArrayList[Car] cars;
 

 47. 

To initialize a String array names to store the three Strings “Huey”, “Duey” and “Louie”, you would do
a.
String names = {“Huey”, “Duey”, “Louie”};
b.
String[ ] names = {“Huey”, “Duey”, “Louie”};
c.
String[ ] names = new String{“Huey”, “Duey”, “Louie”};
d.
String names[3] = {“Huey”, “Duey”, “Louie”};
e.
String names;  names[0] = “Huey”;  names[1] = “Duey”;  names[2] = “Louie”;
 

 48. 

What does the following method do?
                   public int question( )
                  {
int value1 = 0;
int value2 = 0;
for(int j=0; j<12; j++)
      if(candy[j] > value1)
      {
            value1 = candy[j];
                        value2 = j;
      }
return value2;
                   }
a.
It returns the total number of candy bars sold
b.
It returns the total number of children who sold 0 candy bars
c.
It returns the total number of children who sold more than 0 candy bars
d.
It returns the number of candy bars sold by the child who sold the most candy bars
e.
It returns the index of the child who sold the most candy bars
 

 49. 

A variable whose scope is restricted to the method where it was declared is known as a(n)
a.
parameter
b.
global variable
c.
local variable
d.
private instance data
 

 50. 


What will be the result of the following assignment statement?  Assume b = 5 and c = 10.
int a = b * (-c + 2) / 2;
a.
30
b.
–30
c.
20
d.
–20
 

 51. 

If a method does not have a return statement, then
a.
it will produce a syntax error when compiled
b.
it must be a void method
c.
it can not be called from outside the class that defined the method
d.
it must be defined to be a public method
 

 52. 

Assume that BankAccount is a predefined class “Aggregate class” and that the declaration BankAccount[ ] firstEmpireBank; has
already been performed. Then the following instruction reserves memory space for
firstEmpireBank = new BankAccount[1000];
a.
a reference variable to the memory that stores all 1000 BankAccount entries
b.
1000 reference variables, each of which point to a single BankAccount entry
c.
a single BankAccount entry
d.
1000 BankAccount entries
e.
1000 reference variables and 1000 BankAccount entries
 

 53. 

assume an int array, candy, stores the number of candy bars sold by a group of children where
candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.

Which of the following code could be used to compute the total number of bars sold by the children?
a.
for(int j=0; j<12; j++)
sum+= candy[j];
b.
for(int j=0; j<12; j++)
candy[j] = sum;
c.
for(int j=0; j<12; j++)
sum = candy[j];
d.
or(int j=0; j<12; j++) sum += [j];
e.
for(int j=0; j<12; j++)
[j] += sum;
 



 
         Start Over