Reference no: EM132314826
Final AP Review
Question: 1
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 'I}
public void someMethod()
{/*code*/}
}
MyOtherClass.java
public class MyOther.flass extends MyClass
{
public MyOtherClass()
{/*code */}
public void sontOtheMethod()
{/*code */}
}
Which of the following method calls would error if placed inside the /*more code here*/ comment inside the main method.
You may a551,1M e that none of the implementations of the methods will throw a runtime error.
I. myInstance.someMethod();
II. myInstance.sumOtherMethod();
III. myOtherInstance,someMethod();
IV. myOtherInstance,someOtherMethod();
All of them
None of them
III Only
I and IV
Question: 2
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.until.Arraylist;
Question: 3
Given this code segment
public void mymethod(String[] array)
{
for(i= 0; i< array.length; i++)
(
System,out.println(array[1]);
}
}
Identify the bug in the code.
Arrays do riot 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
Question: 4
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);
Question: 5
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
Questior: 6
You have an array of size 7, declared with the fDllovoiric code:
int[] arr= {1, 2, 3, 4, 5, 6, 7};
What do you get when you execute the following code?
Syvtem.out.println(arr[7]);
7
1
ThE code will execute, but nothing will b printed.
A run-time trror
A compile-time error
Question: 7
Assume A e and C are booleans.
WIlich of the follovving expressions are equivalent?
I.(!A || !B)
II.(!A && !B)
III.!(A && B)
I and II
1 and III
II and III
I, II, and III
None is equivalent with another.
Question: 8
What would bie printed to file console with the following expression;
System.out.printin( (5 + 4) + "string" + ( 7 + 7 ) );
9string14
54string77
9string77
54string14
This code win resuit in an error
Question: 9
What is the decimal value of the binary number 1011 ?
3
9
11
Question: 10
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
Question: 11
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]
Question: 12
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 myArray is >0.
Sets the value of myBoolean to true if any value in myArray is o.
Atways sets the value of myBoolean to true
Question: 13
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<Intger> arr = new ArrayListInteger>();
- IV ArrayList<int> arr = new ArrayList<int>();
- V List<Integer> arr = new ArrayListInteger>();
I, IV
II, III, V
II, III
III, V
III, IV
Question: 14
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.
Question: 15
Consider the following method foo . Assume n will always be greater than or equal 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
Question: 16
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 11 !C)
(!A || C || B)
Question: 17
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.
Il - If the Message class has a private variable, none of its children classes can access the variable directly.
III - Each of the 4 classes can have a method carled 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
I, II, and III
I, II, III, and IV
Question: 18
What is wrong with the following interface declaration?
public interface ball
private in airLevel;
public Ball()
{
airievel = 0;
public void bounce();
public void roll();
public void inflate();
public void deflate();
a) airtevel =0; needs to use the this keyword. Like so, this.airLevei = 0;
b) Java interfaces must not include any implementation logic.
c) All of the method declarations should be private.
d) All of the methods must include their implementation logic.
e) Interface methods can not return void.
Question: 19
Given the following method.
// Precondition: myArray is not empty
/ Postcondition: Returns max int value found in myArray
public static it 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
Question: 21
How many comparisons will the selection sort algorithm make on an array of 8 elements?
3
8
24
28
64
Question: 22
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("1alalala", "1") returns "aaaa"
Which of the following blocks of code will successfully implement removeFromString()?
a) private String rtmoveFromString(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.lergth());
old = old.substring(0, index);
index = old,indexOf(frag);
}
}
return old;
}
b) private String recoverfromString(String old, String frag)
{
int i cold,indexOf(frag);
while (i > -1) {
String rest = old.substring(i + frag.length());
old = old. substring(0, 1);
old = old + rest;
i= old,index0f(frag);
}
return old;
}
c) private String removeFromString(String old; String frag)
int index = o1d.index0f(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 = o1d,index0f(Frag);
}
}
return old;
}
d) 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(O, index + 1);
index = old.index0f(frag);
}
}
return old;
}
Question: 23
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,
Question: 24
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 area method.
ll - No instances of the class can be created. This class cannot be instantiated.
III - The area nmethod 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
II, IV, and v
Question: 25
Given the followjng 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 is constructor method?
a)name = name;
breed - breed;
age - age;
b)myName = name;
myereed - breed;
myAge age;
c)this.name - name;
this.breed = breed;
this.age = age;
d)this.name = name;
this.breed = getBreed();
this.age = getAge();
e)return new Dog(name, breed, age);
Question: 26
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
Question: 27
What will the folhowing 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
Question: 28
Of the sorting algorithms we've learned (election 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 fester when the array is sorted.
No, because insertion sort is faster when the array is sorted.
Question: 29
The following method counts the number of times even numbers appear in an ArrayList:
ublic int countEvenrJumbers(ArrayList<Integer> numbers)
{
int count = 0;
//Missing code here
return count;
}
What should replace the comment // Missing code here?
a) for (int i = 0; i < numbers.size(); i++)
{
if (numbers.get(i) % 2 == 0)
{
count++;
}
b) for (int i = 0, i <= numbers.size(); i++)
{
if (numbers.get(i) % 2 == 0)
{
count++;
}
}
c)for (int i = 0; i < numbgrs.size(); i++)
if (numbers.get(i) % 2 ==1)
{
count++;
}
}
d) for (int i w 0; i < numbArs.size(); i++)
if (i % 2 == 0)
count++;
}
}
e) for (int i - 0; i < numbers.size(); I++)
if (isEven(numbers.get(i)))
Count++;
Question: 30
Using the binaiy search algorithm. what is the maximum number o iterations needed to find an eiement in an array containing 256 elements?
128
256
4
8
16
Question: 31
Which of the following algorithms take endvantage 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
Question: 32
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 = " ' \\ ' ";
Ouestion: 33
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
Question: 34
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]
Question: 35
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 String S in the with the String[] with the same value. Which of the following actions would be best?
a) The method should be written or the assumption that there is only one dessert string in the string[][] with the passed value.
b) The method should be written so as to return the index of every occurrence of the passed dessert String value.
c) The specification should be modified to indicate what should be done if there is more than one index of the passed dessert string
C) The method should be written to output a message if more than one larger value is found.
d) The method should be written to delete all subsequent instances of the dessert string from the String[][]
Question: 36
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][fl.equals(arrBfil[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][1].compareTo(arrB[i][1]) != 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 Tv
Question: 37
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 100P7
[60, 50, 40, 3O 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]
Question: 38
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.
Question: 39
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.
Question: 40
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++;