Java Final Study Bank
What is Math.cell(3.6)?
4.0
What is Math.int(3.6)?
4.0
Put the steps of the selection sort in order: 1.find index of largest value in this subarray 2. consider new subarray of m=m-1 elements by eliminating the last element in the previous array 3. swap values to put largest element at the end of the array 4. consider the n element as a with m=n elements 5.repeat steps 2-4 until m=1
41325
What is the output of the following code? System.out.println(Math.round(8.5)); System.out.println(Math.rint(4.5)); System.out.println(Math.ceil(3.5)); System.out.println(Math.rint(9.5));
9 4.0 4.0 10.0
int[] num = new int[100]; for (int i = 0; i < 50; i++) num[i] = i; num[5] = 10; num[55] = 100; What is the index number of the last component in the array above?
99
What HTML tags begin and end a Web page?
<HTML> </HTML>
The following for loop executes 21 times. (assume all variables are properly declared) for(i=1; i<20; i=i + 1) System.out.println(i);
False
The following for loop executes 21 times. (Assume all variables are properly declared.) for (i = 1; i <= 20; i = i + 1) System.out.println(i); T or F?
False
The methods of a class must be public.
False
The output of the Java code, assuming that all variables are properly declared, is 32. num = 10; while (num <= 32) num = num + 5; System.out.println(num); T or F?
False
The output of the Java code, assuming that all variables are properly declared, is: 2 3 4 5 6. n = 2; while (n >= 6) { System.out.print(n + " "); n++; } System.out.println(); T or F?
False
The output of the java code, assuming that all variables are properly declared is: 2 3 4 5 6 n=2; while(n>=6) { system.out.print(n+" "); n++ } system.out.println();
False
The output of the java code, assuming that all variables are properly declared, is 32 num=10; while(num<=32) num=num + 5 System.out.println(num);
False
The private members of a superclass can be accessed by a subclass.
False
The signature of a method consists of only its formal parameter list.
False
The word (static) is a return type in Java.
False
To use a predefined method, you must know the code in the body of the method.
False
Two methods are said to have different formal parameter lists if both methods have a different number of formal parameters, or if the data types of the formal parameters are the same.
False
Void methods must have at least one parameter.
False
Void methods use the (return) statement to return the value (0).
False
When nesting loops, the variable in the outer loop changes more frequently
False
When nesting loops, the variable in the outer loop changes more frequently. T or F?
False
When no object of the class type is instantiated, static data members of the class fail to exist
False
When no object of the class type is instantiated, static data members of the class fail to exist.
False
When you write your own constructors, you cannot write versions that receive parameters. T or F?
False
Within a method, an identifier used to name a variable in the outer block of the method can be used to name any other variable in an inner block of the method.
False
You can use the (class String) to pass strings as parameters to a method and change the actual parameter.
False
You must always use the reserved word super to use a method from the superclass in the subclass.
False
int number; boolean done = false; do { try { System.out.print("Enter an integer: "); number = console.nextInt(); System.out.println(); done = true; System.out.println("number = " + number); } catch (InputMismatchException imeRef) { str = console.next(); System.out.println("Exception " + imeRef.toString() + " " + str); } } while (!done);
Fix the error and continue
Which of the following is the default layout manager for a Java application?
FlowLayout
Syntax for reading data into an array (Scanner)
Scanner scan=new Scanner(System.in); for(int i=0;i<arrayName.length;i++) { s.o.p("Enter Values"); arrayName[i]=scan.nextDouble(); }
public static double secret(int first, double second) { double temp; if (second > first) temp = first * second; else temp = first - second; return temp; } What is the name of the above method?
Secret
public class Secret {private int x;private static int y; public static int count; public int z; public Secret() {x = 0; z = 1; } public Secret(int a) {x = a; } public Secret(int a, int b) {x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", } count = " + count); public static void incrementY() {y++; } } Based on the class in the accompanying figure, which of the following statements is illegal? a. Secret.incrementY(); b. Secret.count++; c. Secret.z++: d. Secret secret = new Secret(4);
Secret.z++:
comparison- based
Sequential and binary search algorithms are called _______-_______ search algorithms
comparing elements
Sequential and binary search algorithms search the list by ______ _______.
public class Secret {private int x;private static int y; public static int count; public int z; public Secret() {x = 0; z = 1; } public Secret(int a) {x = a; } public Secret(int a, int b) {x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", } count = " + count); public static void incrementY() {y++; } } What does the default constructor do in the class definition in the accompanying figure? a. Sets the value of x to 0 b. Sets the value of z to 1 c. Sets the value of x to 0 and the value of z to 1 d. There is no default constructor.
Sets the value of x to 0 and the value of z to 1
T
T/F: All data members of the superclass are also data members of the subclass. Similarly, the methods of the superclass (unless overridden) are also the methods of the subclass
T
T/F: Computers use stacks to implement method calls.
F
T/F: The subclass cannot override (redefine) the public methods of the superclass; applies only to the objects of the subclass, not objects of the superclass
T
T/F: Using two reference variables, we can simplify the insertion code for a linked list somewhat
sub
The _____class can include additional data and method members.
int[] x = new int[10]; x[0] = 34; x[1] = 88; System.out.println(x[0] + " " + x[1] + " " + x[10]); What is the output of the code fragment above?
This program throws an exception.
What can a method do with a checked exception?
Throw the exception to the method that called this method, or handle the exception in a catch block
What is the main use of inner classes?
To handle events
A class and its members can be described graphically using Unified Modeling Language (UML) notation.
True
A local identifier is an identifier that is declared within a method or block and that is visible only within that method or block.
True
A mutator (setter) method of a class changes the values of the data members of the class.
True
A mutator method of a class changes the values of the data members of the class.
True
A polymorphic reference variable can refer to either an object of their own class or an object of the subclasses inherited from its class.
True
A subclass can directly access protected members of a superclass.
True
Actual and formal parameters have a one-to-one correspondence.
True
An abstract method is a method that has only the heading with no body.
True
An actual parameter is a variable or expression listed in a method call.
True
An alternative to importing a class is to import an entire package of classes. T or F?
True
An application's main() method must have a void return type.
True
An application's main() method must have a void return type. T or F?
True
An identifier can be declared anywhere including within a class, and outside of every method definition (and every block).
True
An interface is a class that contains only abstract methods and/or named constants.
True
Every object has access to a reference to itself.
True
Given the declaration public class MyClass {private int x; public void print() {System.out.println("x = " + x); } private void setX(int y) {x = y; } } MyClass myObject = new MyClass(); The following statement is legal. myObject.print();
True
If a class is declared final, then no other class can be derived from this class.
True
If a formal parameter is a variable of a primitive data type, then after copying the value of the actual parameter, there is no connection between the formal and actual parameter.
True
If the object is created in the definition of a method of the class, then the object can access both the public and private members of the class
True
If the object is created in the definition of a method of the class, then the object can access both thepublic and private members of the class.
True
In Java, (return) is a reserved word.
True
In Java, stream classes are implemented using the inheritance mechanism.
True
In deep copying, each reference variable refers to its own object.
True
In multiple inheritance, the subclass is derived from more than one superclass.
True
In the case of an infinite while loop, the while expression (that is, the loop condition) is always true
True
In the case of an infinite while loop, the while expression (that is, the loop condition) is always true. T or F?
True
It is illegal to declare the same variable name more than once within a block. T or F?
True
Java allows the programmer to declare a variable in the initialization statement of the (for) statement.
True
Methods are also called modules.
True
Modifiers are used to alter the behavior of the class
True
Modifiers are used to alter the behavior of the class.
True
Parentheses in a method declaration contain parameters that are funneled into the method. T or F
True
Reference variables as parameters allow you to return more than one value from a method.
True
Strings assigned to (StringBuffer) variables can be altered.
True
Suppose that the following statement is included in a program. (import static java.lang.Math.*;) After this statement, a static method of the class Math can be called using just the method name.
True
T/F During the sorting phase of insertion sort, the array containing the list is divided into two sublists, sorted and unsorted
True
What does the following statement do?
Vector<Double> thisVector = new Vector<Double>(); It creates an empty vector to create a vector of Double objects.
Suppose that S1, S2, and S3 are three strings given as follows: String S1= "welcome to Java"; String S2= "Programming is fun"; String S3= "welcome to java"; What is the results of the following expressions? S1.Concat(S2)
Welcome to JavaProgramming is fun
base case, recursive case
What are the two cases in which every recursive algorithm involves?
true
When a program executes, the first statement to execute is always the first statement in the main method.
When is a finally block executed?
When is a finally block executed? Always after the execution of a try block, regardless of whether or not an exception is thrown
When does the method finalize execute?
When the class object goes out of scope.
return statements can be used in void methods to return values.
Which of the following is NOT true about return statements?
he class String contains many methods that allow you to change an existing string.
Which of the following statements about strings is NOT true?
The operator new must always be used to allocate space of a specific type, regardless of the type.
Which of the following statements is NOT true?
To handle window events, if the class containing the application program does not extend the definition of another class, you can make that class extend the definition of the class ____.
WindowAdapter
Composition is also called ____
aggregation
Composition is also called ____.
aggregation
What is stored in alpha after the following code executes? int[] alpha = new int[5]; for (int j = 0; j < 5; j++) { alpha[j] = 2 * j; if (j % 2 == 1) alpha[j - 1] = alpha[j] + j;
alpha = {0, 2, 4, 6, 8}
What is stored in alpha after the following code executes? int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) { alpha[j] = j + 5; if (j % 2 == 1) alpha[j - 1] = alpha[j] + 2; }
alpha = {8, 6, 10, 8, 9}
Public method
also known as a service method, tells what an object does
If a negative value is used for an array index, ____.
an ArrayIndexOutOfBoundsException is thrown
With insertion sort, the variable firstOutOfOrder is initialized to ____, assuming n is the length of the list. a. 0 b. 1 c. n - 1 d. n
b. 1
How many members are in the class Rectangle?
b. 10
For a list of length n, selection sort makes ____ item assignments. a. n(n - 1)/2 b. 3(n - 1) c. 3(n) d. 4(n + 1)
b. 3(n - 1)
what is the value of alpha[4] after the following code executes? int[] alpha=new int[5]; for(int j=0;j<5;j++) alpha[j]=2*j-1; a.one b.three c.five d.seven
d
The behavior of quick sort is ___ in the worst case and ___ in the average case. a. O(nlog2n), O(nlog2n) b. O(n^2), O(n) c. O(nlog2n), O(n^2) d. O(n^2), O(nlog2n)
d. O(n^2), O(nlog2n)
When moving array values for insertion sort, to move list[4] into list[2], first ____. a. move list [2] to list [3] b. delete list[2] c. move list [4] to list[3] d. copy list[4] into temp
d. copy list[4] into temp
ADTs illustrate the principle of ____.
information hiding
return type of the size method for an ArrayList
int
int larger (int x, int y, int z)
int larger(int x, int y) Given the method heading in the accompanying figure, which of the following does NOT demonstrate method overloading?
int larger (int a, int y)
int larger(int x, int y) Given the method heading in the accompanying figure, which of the following would be an incorrect demonstration of method overloading?
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } How many base cases are in the code in the accompanying figure?
one
According to the UML class diagram in the accompanying figure, which method is public and doesn't return anything?
print()
Which of the following words indicates an object's reference to itself?
this
Which of the following words indicates an object's reference to itself? a. this b. that c. public d. protected
this
inheritance
"is-a" relationship (superclass/subclass development)
What is Math.floor(-33.6)?
-34.0
int[] num = new int[100]; for (int i = 0; i < 50; i++) num[i] = i; num[5] = 10; num[55] = 100; What is the value of num.length in the array above?
100
A constructor has no type and is therefore a void method
False
A constructor has no type and is therefore a void method.
False
A method body provides information about how other methods can interact with it.
False
A method body provides information about how other methods can interact with it. T or F?
False
All the methods defined in a class must have different names.
False
An accessor (getter) method of a class first accesses the values of the data members of the class and then changes the values of the data members.
False
An accessor method of a class first accesses the values of the data members of the class and then changes the values of the data members
False
Classes that are defined within other classes are called inside classes.
False
Constructors are called like any other method
False
Constructors are called like any other method.
False
Given the declaration public class MyClass {private int x; public void print() {System.out.println("x = " + x); } private void setX(int y) {x = y; } } MyClass myObject = new MyClass(); The following statement is legal. myObject.setX(10);
False
If a member of a class is a private method, you can access it outside the class.
False
In Java, the reference this is used to refer to only the methods, not the instance variables of a class.
False
In Java, the reference thisis used to refer to only the methods, not the instance variables of a class.
False
In dynamic binding the method that gets executed is determined at the compile time not at execution time. On the other hand, in run-time binding the method that gets executed is determined at execution time not at compile time.
False
In order to use the method (isUpperCase) of the (class Character), the package (java.lang) must be explicitly imported.
False
In shallow copying, each reference variable refers to its own object.
False
Java uses late binding for methods that are private but not for methods that are marked final.
False
Just like the nesting of loops, Java allows the nesting of methods.
False
Members of a class are usually classified into three categories: public, private, and static.
False
Not every user-defined class can override the default definition of the method toString because it is provided by Java
False
Suppose that S1, S2 and S3 are strings given as follows: String S1= "Welcome to Java"; String S2= "Programming is fun"; String S3= "Welcome to Java"; What is the results of the following expressions? S2.startswith("wei"); T/F?
False
Suppose that the class Mystery is derived from the class Secret. Consider the following statements: Secret secRef; Mystery mysRef = new Mystery(); secRef = mysRef; The value of the expression secRef instanceof Secret is true
False
T/F During the second iteration of a selection sort, the smallest item is moved to the top of the list
False
The (class Math) is contained in the package (java.math).
False
The (return) statement must be the last line of the method.
False
T/F If n = 1000, then to sort the list, selection sort, the smallest item is moved to the top of the list
False
T/F In a binary search, first, the search item is compared with the last element of the list
False
The abstract data type specifies the logical properties and the implementation details.
False
The components of a class are called fields.
False
The default constructor executes when an object is instantiated and initialized using an existing object
False
(public static char methodHeading(int n, double num)) Based on the method heading in the accompanying figure, what is the return type of the value returned?
(char)
Which modifier is used to specify that a method cannot be used outside a class?
(private)
Which of the following is a member access operator? . + - ->
. (period)
Default Value: byte, short, long, int
0
What is the output of the following java code? int num= 15; while (num > 0) num = num - 3 system.out.println(num);
0
What is the result of the following program? Public class FindMax { ......
0
What is the result? public class FindMax { public static void main(String[] args) { int max = 0; max(1,2,max); System.out.println(max); } public static void max(int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; } }
0
int num = 15; while (num > 0) num = num - 3; System.out.println(num);
0
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What is the output of exampleRecursion(0)?
0
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What is the output of exampleRecursion(0)?
0
int[] array1 = {1, 3, 5, 7} for (int i = 0; i <array.length; i++) if (array1[i] > 5) System.out.println(i + " " + array1[i]); Which indices are in bounds for the array array1, given the declaration above?
0, 1, 2, 3
Default Value: float, double
0.0
double[] as = new double[7]; double[] bs; bs = as; How many objects are present after the code fragment in the accompanying figure is executed?
1
public int mystery(int x, int y) { if (x >= y) return x - y; else return x + y; } Based on the code above, what would be the output of the following statement? System.out.println(mystery(8,7));
1
The following loop displays __________ for(inti=2; i<=10; i++){ system.out.print(i + " "); i ++; }
1 3 5 7 9
How many inches is 12-point font?
1/6
Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? System.out.println(recFunc(10));
10
What is the value of alpha[3] after the following code executes? int[] alpha = new int[5]; int j; for (j = 4; j >= 0; j--) { alpha[j] = j + 5; if (j <= 2) alpha[j + 1] = alpha[j] + 3; }
10
int[] num = new int[100]; for (int i = 0; i < 50; i++) num[i] = i; num[5] = 10; num[55] = 100; What is the value at index 10 of the array above?
10
What is the output of the following Java code? int[] alpha = {2, 4, 6, 8, 10}; int j; for (j = 4; j >= 0; j--) System.out.print(alpha[j] + " "); System.out.println();
10 8 6 4 2
If the string variable contains the string "Now is the time" what is the result of the following expression? string str= "Now is the time"; system.out.println(Str.length() + " " + str.substring(1,3));
15 ow
Suppose the following strings are declared as follows: String city= "Tallahassee"; String state= "Florida"; What is the results of the following expressions? City.concat(state).lastindexof('a')
17
char[][] table = new char[10][5]; How many dimensions are in the array seen in the accompanying figure?
2
What is the maximum number of key comparisons made when searching a list L of length n for an item using a binary search?
2 * log2n + 2
Put the steps of swapping elements a and be in order: 1. assign temp=a 2. define temp variables(temp) 3. assign b=temp 4.assign a=b
2143
If the list in the accompanying figure was sorted, what would be the middle element?
24
Consider the following definition of a recursive method. public static int mystery(int[] list, int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } Given the declaration int[] alpha = {1, 4, 5, 8, 9}; What is the output of the following statement? System.out.println(mystery(alpha, 0, 4));
27
Consider the following definition of a recursive method. public static int mystery(int[] list, int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } Given the declaration int[] alpha = {1, 4, 5, 8, 9}; What is the output of the following statement? System.out.println(mystery(alpha, 0, 4));
27
In the case of an unsuccessful search, it can be shown that for a list of length n, a binary search makes approximately ________ key comparisons
2log2n 2logn
Suppose that S1, S2 and S3 are strings given as follows: String S1= "Tallahassee, FL"; String S2= "Atlanta, GA"; String S3= "Birmingham, AL"; What is the results of the following expressions? S2.substring(3).lastIndexof('a)
3
public class Secret {private int x;private static int y; public static int count; public int z; public Secret() {x = 0; z = 1; } public Secret(int a) {x = a; } public Secret(int a, int b) {x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", } count = " + count); public static void incrementY() {y++; } } How many constructors are present in the class definition in the accompanying figure? a. 0 b. 1 c. 2 d. 3
3
public static int func2(int m, int n) { if (n == 0) return 0; else return m + func2(m, n - 1); } What is the output of func2(2, 3)?
3
int[] array1 = {1, 3, 5, 7} for (int i = 0; i <array.length; i++) if (array1[i] > 5) System.out.println(i + " " + array1[i]); What is the output of the code fragment above?
3 7
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What is the output of exampleRecursion(3)?
36
T/F In a bubble sort, the smaller elements move toward the bottom, and the larger elements move toward the top of the list.
False
What is the output of the following code> int count; int num = 2 for (count = 1; count < 2; count ++) { num=num + 3 system.out.print(num + " ") } system.out.println();
5
What is the output of: System.out.print(("ABCDABD").lastIndexof("BC"));
5
What is the value of alpha[2] after the following code executes? int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) alpha[j] = 2 * j + 1;
5
char[][] table = new char[10][5]; What is the value of table[2].length?
5
int count; int num = 2; for (count = 1; count < 2; count++) { num = num + 3; System.out.print(num + " "); } System.out.println();
5
If the list in the accompanying figure was sorted using selection sort, which two elements would be swapped first?
5 and 16
int[] numList = new int[50]; for (int i = 0; i < 50; i++) numList[i] = 2 * i; num[10] = -20; num[30] = 8; How many components are in the array numList seen in the accompanying figure?
50
Using a recursive algorithm to solve the Tower of Hanoi problem, a computer that can generate one billion moves per second would take ____ years to solve the problem.
500
public static int func2(int m, int n) { if (n == 0) return 0; else return m + func2(m, n - 1); } What is the output of func2(2, 3)?
6
Consider the following definition of a recursive method. public static int strange(int[] list, int first, int last) { if (first == last) return list[first]; else return list[first] + strange(list, first + 1, last); } Given the declaration int[] beta = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25}; What is the output of the following statement? System.out.println(strange(beta, 4, 7));
66
public int mystery(int x, int y) { if (x >= y) return x - y; else return x + y; } Based on the code above, what would be the output of the following statement? System.out.println(mystery(8, mystery(2, 1));
7
Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? System.out.println(recFunc(8));
72?
How many times will the following code print "Welcome to Java"? int count=0; while(count<8){ system.out.println("Welcome to Java"); count ++ }//end of while loop
8
Consider the following list. list = {20, 10, 17, 2, 18, 35, 30, 90, 48, 47}; Suppose that sequential search as discussed in the book is used to determine whether 95 is in list. Exactly how many key comparisons are executed by the sequential search algorithm?
9
Consider the following definition of a recursive method. public static int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } What is the output of the following statement? System.out.println(recFunc(8));
??
A <HTML> <BODY> <OBJECT "WelcomeHomeApplet.class"> </OBJECT> </BODY> </HTML> B <HTML> <BODY> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </OBJECT> </HTML> C <HTML> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </HTML> D <HTML> <BODY> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </OBJECT> </BODY> </HTML> Which of the options in the accompanying figure is a correct Web page for the applet WelcomeHomeApplet?
A
Assume there are four methods A, B, C, and D. If method A calls method B, method B calls method C, method C calls method D, and method D calls method A, which of the following methods is indirectly recursive?
A
What does the return statement do
A Java reserved word that returns a value of it is not used(terminated)
What is the difference between a class and an object?
A class is a blueprint as a house is an object!
nodes
A linked list is a collection of components called ______
true
A local identifier is an identifier that is declared within a method or block and that is visible only within that method or block.
What is a modifier
A modifier is a Java reserved word that names characteristics of a program
recursive case
A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem
int[] hits = {10, 15, 20, 25, 30}; copy(hits); What is being passed into copy in the method call above?
A reference to the array object hits
base case
A simple occurence that can be answered indirectly
LIFO
A stack is also called a(n) ______ data structure.
inheritance
A way to form new classes based on existing classes, taking on their attributes/behavior. - A way to group related classes - A way to share code between two or more classes - One class can extend another, absorbing its data/behavior.
stack
A(n) ________ is a list of homogeneous elements, wherein the addition and deletion of elements occurs only at one end, called the top of the stack.
Which of the following prints "Welcome to Java" 10 times?
AB: A. for (intCount=1; count<10; count++){ system.out.println("welcome to java"); } B. for(intCount= 0; count<10; count++){ system.out.println("welcome to java"); }
polymorphism
Ability for the same code to be used with different types of objects and behave differently with each.
Private method
Also known as the support method, can only be called in that object
false
An identifier can be declared anywhere including within a class, and outside of every method definition (and every block).
Data items you use in a call to a method are called ____.
Arguments
Syntax for instantiating an ArrayList
ArrayList<E> listOfE=new ArrayList<E>();
Syntax of the ArrayList Class
ArrayName<E> arrayListName;
Which limitation of arrays does a vector overcome?
Arrays cannot increase in size; vectors can.
T/F The swap function of quick sort is written differently from the swap function for selection sort.
False
Let f and g be real-valued functions. Assume that f and g are nonnegative, that is, for all real numbers n, f(n) <= 0 and g(n) >= 0. We say that f(n) is ________ of g(n), written f(n) = O(g(n)), if there exist positive constants c and n0 such that f(n) <= cg(n) for all n >= n0
Big-O
Consider the following declaration. int[] list = new int[10]; int j; int sum; Which of the following correctly finds the sum of the elements of list? (i) sum = 0; for (j = 0; j < 10; j++) sum = sum + list[j]; (ii) sum = list[0]; for (j = 1; j < 10; j++) sum = sum + list[j];
Both (i) and (ii)
Which of the following statements describe the base case of a recursive algorithm? (i) F(0) = 0; (ii) F(x) = 2 * F(x - 1); (iii) if (x == 0) F(x) = 5 + x;
Both (i) and (iii)
Which of the following statements describe the base case of a recursive algorithm? (i) F(0) = 0; (ii) F(x) = 2 * F(x - 1); (iii) if (x == 0) F(x) = 5 + x;
Both (i) and (iii)
Suppose you have the following declaration. char[] nameList = new char[100]; Which of the following range is valid for the index of the array nameList. (i) 1 through 100 (ii) 0 through 100 A. Only (ii) B. Both are invalid
Both are invalid
How can a method send a primitive value back to the caller?
By using the return statement.
Private variable
Can only be changed through the class
Which of the following constructors would produce the color white?
Color whiteColor = new Color(255, 255, 255);
System.out.println("Your name is" + yourName); The above statement is an example of _________, which is used to join strings
Concatenation
b
Consider the following class definition. public class Rectangle { private double length; private double width; public Rectangle() { length = 0; width = 0; } public Rectangle(double l, double w) { length = l; width = w; } public void set(double l, double w) { length = l; width = w; } public void print() { System.out.println(length + " " + width); } public double area() { return length * width; } public double perimeter() { return 2 * length + 2 * width; } } Suppose that you have the following declaration. Rectangle bigRect = new Rectangle(10, 4); Which of the following statements are valid in Java? (Assume that console is Scanner object initialized to the standard input device.) (i) bigRect.length = console.nextDouble(); bigRect.width = console.nextDouble(); (ii) double l; double w; l = console.nextDouble(); w = console.nextDouble(); bigRect.set(l, w); a.) Only (i) b.) Only (ii) c.) Both (i) and (ii) d.) None of these
a
Consider the following class definitions. public class BClass { private int x; public void set(int a) { x = a; } public void print(){ } } public class DClass extends BClass { private int y; public void set(int a, int b) { //Postcondition: x = a; y = b; } public void print(){ } } Which of the following is the correct definition of the method set of the class Dclass? (i) public void set(int a, int b) { super.set(a); y = b; } (ii) public void set(int a, int b) { x = a; y = b; } a.) Only (i) b.) Only (ii) c.) Both (i) and (ii) d.) None of these
Goo1 Hoo2 Foo1 Foo2 Goo1 Foo2
Consider the following class definitions: public class Foo { public Foo() { } public void method1() { System.out.println(''Foo 1''); } public void method2() { System.out.println(''Foo 2''); } } public class Goo extends Foo { public Goo() { } public void method1() { System.out.println(''Goo 1''); } } public class Hoo extends Goo { public Hoo() { } public void method2() { System.out.println(''Hoo 2''); } } Given these class definitions, what will be the output of this code fragment? Foo[] elements = new Foo[3]; elements[0] = new Hoo(); elements[1] = new Foo(); elements[2] = new Goo(); for (int i = 0; i < elements.length; i++) { elements[i].method1(); elements[i].method2(); }
a
Consider the following definition of a recursive method. public static int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } Which of the statements represent the base case? a.) Statements in Lines 3 and 4. b.) Statements in Lines 5 and 6. c.) Statements in Lines 3, 4, and 5. d.) None of these
1 2 3 4
Consider the following definition of a recursive method: public static void mystery(int num) { if (num > 0) { mystery(num-1); System.out.print(num+" "); } } What is the output of this program (given num=4)?
A <HTML> <BODY> <OBJECT "WelcomeHomeApplet.class"> </OBJECT> </BODY> </HTML> B <HTML> <BODY> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </OBJECT> </HTML> C <HTML> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </HTML> D <HTML> <BODY> <OBJECT code="WelcomeHomeApplet.class" width = 250 height = 200> </OBJECT> </BODY> </HTML> Which of the options in the accompanying figure is a correct Web page for the applet WelcomeHomeApplet?
D
Which is an infinite loop? a. loopCount = 4; while(loopCount < 3); { System.out.println("Hello"); loopCount = loopCount + 1; } b. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); loopCount = loopCount + 1; } c. loopCount = 5; while(loopCount > 3); { System.out.println("Hello"); loopCount = loopCount - 1; } d. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); }
D. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"; }
What is a formal parameter
Data (values) that is in header of method
The method ____ is the first line of a method
Declaration
The method ____ is the first line of a method.
Declaration
What is the scope variable?
Determined where the variable can be referenced, depends on where it's declared
The program that tests a method is called a ____ program
Driver
return type of the get method of an ArrayList
E
return type of the remove method for an ArrayList
E
return type of the set method of an ArrayList
E
shape/ rectangle/ square
Example of a superclass/ subclass development (class hierarchy) using: - rectangle -shape -square
b, b
Given the following java recursive method, public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What does the code above do? a.) Returns: the cube of the number n. b.) Returns: the sum of the cubes of the numbers, 0 to n. c.) Returns three times the number n. d.) Returns the next number in a Fibonacci sequence. How many base cases are in the code above? a.) 0 b.) 1 c.) 2 d.) 3
When a variable ceases to exist at the end of a method, programmers say the variable ____.
Goes out of scope
In which of the following layout managers do all rows (columns) have the same number of components and all components have the same size?
Grid Layout
Applets are embedded in ____ documents.
HTML
Applets are embedded in ____ documents.
HTML?
What is the output of System.out.println("Happy".substring(0,3));?
Hap
Which of the following statements is true? a. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") throws an IOException. b. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") returns null. c. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") creates a new file. d. If the file "temp.txt" does not exist, new PrintWrite("temp.txt") prints an error message to the console.
If the file "temp.txt" does not exist, new PrintWrite("temp.txt") throws an IOException.
true
In order to use the method isUpperCase of the class Character, the package java.lang must be explicitly imported.
Public variable
Is a variable that can be changed by the user of the code
What is the default definition of the method toString?
It creates a string that is the name of the object's class, followed by the hash code of the object.
What is the default definition of the method toString? a. There is no default definition; you must define the method yourself. b. It creates a string that is the name of the object's class, followed by the hash code of the object. c. It creates a string that is the name of the program. d. It creates a string that is the name of the package, followed by the name of the class, followed by the hash of the object.
It creates a string that is the name of the object's class, followed by the hash code of the object.
Which of the following statements about the reference super is true?
It must be the first statement of the subclass constructor.
What do the start and stop methods of an applet do?
It starts automatically when the applet is active and for the stop automatically when the applet is not on(active)
What class must be extended when you create an applet?
JApplet
Which of the following is NOT included in the Java applet in the accompanying figure?
List
A(n) ____ variable is known only within the boundaries of the method.
Local
sequential search
Locates a target value in an array/ list by examining each element from start to finish
selection sorting
Looks for the smallest element and moves it to the front
The ____ method executes first in an application, regardless of where you physically place it within its class.
Main()
What class can be used to generate a random number?
Math
A(n) ____ is a program module that contains a series of statements that carry out a task.
Method
How is a class associated with itself?
Methods might take objects from the same class as help( a base)
Which of the following statements is NOT true about infinite recursion?
Methods that are indirectly recursive always lead to infinite recursion
Which of the following statements is NOT true about infinite recursion?
Methods that are indirectly recursive always lead to infinite recursion.
What is the first step in the Tower of Hanoi recursive algorithm?
Move the top n - 1 disks from needle 1 to needle 2, using needle 3 as the intermediate needle
Will the following program terminate? int balance= 10; while (true){ if(balance<9) continue; balance=balance - 9; }
No (False)
Consider the following class definition. public class Cylinder { private double baseRadius; private double height; public Cylinder () { baseRadius = 0; height = 0; } public Cylinder (double l, double h) { baseRadius = l; height = h; } public void set(double r, double h) { baseRadius = r; height = h; } public String toString() { return (baseRadius + " " + height); } public double SurfaceArea() { return 2 * 3.14 * baseRadius * height; } public double volume() { return 3.14 * baseRadius * baseRadius * height; } } Suppose that you have the following declaration. Cylinder cyl = new Cylinder(1.5, 10); Which of the following sets of statements are valid in Java?
None of these
The class RuntimeException is the superclass of which of the following classes?
NullPointerException
In general, the selection sort algorithm is good only for small lists because _______ grows rapidly as n grows.
O(n^2)
Any sorting algorithm that sorts a list of n distinct elements by comparison of the keys only, in its worst case, makes at least ______ key comparisons.
O(nlog2n) O(nlogn)
What is an aggregate object?
Object that has objects inside of it
Objects can should be self governing. Explain
Objects should be able to control their own data
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } How many base cases are in the code in the accompanying figure?
One
Suppose that the class Mystery is derived from the class Secret. Consider the following statements. Secret secRef = new Secret(); Mystery mysRef = new Mystery(); Which of the following statements is legal in Java? (i) secRef = mysRef; (ii) mysRef = secRef;
Only (i)
Suppose you have the following declaration. double[] salesData = new double[1000]; Which of the following range is valid for the index of the array salesData. (i) 0 through 999 (ii) 1 through 1000
Only (i)
Which of the following about Java arrays is true? (i) All elements in an array must be of the same type. (ii) The array index and array element must be of the same type. A. Only (i) B. Both (i) and (ii)
Only (i)
Which of the following class definitions is correct in Java?(i) public class Employee { private String name; private double salary; private int id; public Employee() { name = ""; salary = 0.0; id = 0; } public Employee(String n, double s, int i) { name = n; salary = s; id = i; } public void print() { System.out.println(name + " " + id + " " + salary); } } (ii) public class Employee { private String name; private double salary; private int id; public void Employee() { name = ""; salary = 0.0; id = 0; } public void Employee(String n, double s, int i) { name = n; salary = s; id = i; } public void print() { System.out.println(name + " " + id + " " + salary); } }
Only (i)
Consider the following class definition. public class Rectangle { private double length; private double width; public Rectangle() { length = 0; width = 0; } public Rectangle(double l, double w) { length = l; width = w; } public void set(double l, double w) { length = l; width = w; } public void print() { System.out.println(length + " " + width); } public double area() { return length * width; } public double perimeter() { return 2 * length + 2 * width; } } Which of the following statements correctly instantiates the Rectangle object myRectangle? (i) myRectangle = new Rectangle(12.5, 6); (ii) Rectangle myRectangle = new Rectangle(12.5, 6); (iii) class Rectangle myRectangle = new Rectangle(12.5, 6);
Only (ii)
Which of the following about Java arrays is true? (i) Array elements must be of the type double. (ii) The array index must evaluate to an integer.
Only (ii)
____ is NOT an iterative control structure.
Recursion
A(n) ____ causes a value to be sent from a called method back to the calling method.
Return Statement
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What does the code in the accompanying figure do?
Returns the sum of the cubes of the numbers, 0 to n
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What does the code in the accompanying figure do?
Returns the sum of the cubes of the numbers, 0 to n
Consider the following definition of a recursive method. public static int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } Which of the statements represent the base case?
Statements in Lines 3 and 4
true
Strings assigned to StringBuffer variables can be altered.
b
Suppose that the class Mystery is derived from the class Secret. Consider the following statements: Secret secRef = new Secret(); Mystery mysRef = new Mystery(); Which of the following statements is legal in Java? (i) mysRef = secRef; (ii) secRef = mysRef; a.) Only (i) b.) Only (ii) c.) Both d.) None of these
true
Suppose that the following statement is included in a program.
1. super.setData(a, b); z = c; 2. super.print(); System.out.println(z);
Suppose that you have the following class definition: public class One { private int x; private int y; public void print() { System.out.println(x + " " + y); } Protected void setData(int u, int v) { x = u; y = v; } } Consider the following class definition: public class Two extends One { private int z; public void setData(int a, int b, int c) { 1. --------------------------??? } public void print() { 2. --------------------------??? } } Complete the methods setData and print in class Two.
bubble sorting
Swapping adjacent pairs that are out of order
Which of the following is true about a while loop?
The body of the loop may not execute at all
Which of the following is true about a while loop?
The body of the loop may not execute at all.
subclass
The child class that extends the superclass and inherits its behavior; gets a copy of every field and method from superclass.
Which of the following statements about strings is NOT true?
The class String contains many methods that allow you to change an existing String.
Which of the following statements is true?
The class Throwable, which is derived from the class Object, is the superclass of the class Exception
Analyze the following code: { public class TestProg public static void func(int n) { n = 10; } public static void main(String[] args) { int myNum = 0; func(myNum); System.out.println(myNum); } } a. The code compiles, runs fine, and prints 0 to the console. b. The code compiles, runs fine, and prints 10 to the console. c. The code does not compile because the return of func(myNum); is not assigned to a variable. d. The code does not compile because the method func does not contain a return statement.
The code compiles, runs fine, and prints 0 to the console.
Analyze the following code: public class Circle {private double radius; Circle() {radius = 0; } public double getRadius() { return radius; } public void setRadius(double r) {radius = r; } } public class TestProgCircle {public static void func(Circle c) { c.setRadius(10); } public static void main(String[] args) { Circle myCircle = new Circle(); func(myCircle); } System.out.println(myCircle.getRadius()); } a. The code compiles, runs fine, and prints 0 to the console. b. The code compiles, runs fine, and prints 10 to the console. c. The code does not compile because the return of func(myCircle); is not assigned to a variable. d. The code does not compile because the method func does not contain a return statement.
The code compiles, runs fine, and prints 10 to the console.
Consider the following definition of a recursive method. public static int strange(int[] list, int first, int last) { if (first == last) return list[first]; else return list[first] + strange(list, first + 1, last); } Given the declaration int[] beta = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25}; What is the output of the following statement? System.out.println(strange(beta, 4, 7)); 66 Which of the following statements is NOT true about creating your own exceptions?
The exception class that you define extends either the class Throwable or one of its subclasses
the second statement should be - "if (x < 10)". mystery(123)= 1+2+3 = 6; mystery(12345)= 1+2+3+4+5 =15;
The following recursive method some problem. Fix the bug and describe what this method will do once the bug if fixed: public static int mystery(int x) { if (x > 10) { return x; } else { return x % 10 + mystery(x/10); } }
What are constructors used for? How are they defined
They have the same name as the class and are used to set values to an object
Which of the following statements about the method heading in the accompanying figure is NOT true?
The method cannot be used outside the class.
Which of the following statements is NOT true?
The operator new must always be used to allocate space of a specific type, regardless of the type.
superclass
The parent class that is being extended.
superclass
The private members of the superclass are private to the ________
stub
The program that tests a method is called a ____ program.
What happens in a method if there is an exception thrown in a try block but there is no catch block following the try block?
The program throws an exception and proceeds to execute the finally block
directly access
The subclass can ______ ______ the public members of the superclass.
What is an actual parameter?
The value passed into a method by a method call
What is an actual parameter
The values passed into a method
How can you tell overloaded methods apart?
They have many details and the return type is not involved
T/F Suppose that L is a sorted list of size 1024, and we want to determine whether an item x is in L. From the binary search algorithm, it follows that every iteration of he while loop cuts the size of the search list by half
True
T/F The binary search algorithm can be written iteratively or recursively
True
T/F The selection sort algorithm finds the location of the smallest element in the unsorted portion of the list.
True
T/F The sequential search algorithm does not require that the list be sorted.
True
The built-in operation that is valid for classes is the dot operator.
True
The method finalize automatically executes when the class object goes out of scope
True
The non-static methods of a class are called instance methods.
True
The output of the following Java code is: Stoor. int count = 5; System.out.print("Sto"); do { System.out.print('o'); count--; } while (count >= 5); System.out.println('r'); T or F?
True
The output of the following java code is: Stoor
True
The subclass can override public methods of a superclass.
True
To determine whether a reference variable that points to an object is of a particular class type, Java provides the operator instanceof.
True
When a program executes, the first statement to execute is always the first statement in the (main) method.
True
When using the prewritten equals () method, a true or false comparison between two strings is returned, but you do not know how the code looks behind the scenes
True
When you overload a Java method, you write multiple methods with a shared name. T or F?
True
You can identify a class that is an application because it contains a public static void main() method. T or F?
True
You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass.
True
You can write your own constructor methods; but when you don't write a constructor method for a class object, Java writes one for you. T or F?
True
int number; boolean done = false; do { try { System.out.print("Enter an integer: "); number = console.nextInt(); System.out.println(); done = true; System.out.println("number = " + number); } catch (InputMismatchException imeRef) { str = console.next(); System.out.println("Exception " + imeRef.toString() + " " + str); } } while (!done);
Until the user inputs a valid integer
If you are building a mission control system, ____.
Use the most efficient solution
If you want to provide your own code to terminate the program when the window is closed, or if you want the program to take a different action when the window closes, then you use the interface ____.
WindowListener
false
Within a method, an identifier used to name a variable in the outer block of the method can be used to name any other variable in an inner block of the method.
Does an applet compile like any other Java application program?
Yes -- except the Web browser compiles the applet code.??
Does an applet compile like any other Java application program?
Yes -- it is compiled as an application program and produces a .class file.
System gc();
You can issue the statement ______ to run the garbage collector.
What can you do to avoid long complex methods?
You can separate the methods
Syntax to add elements to the end of an ArrayList
add(E element);
given the method heading public static void strange(int a,int b) and the declaration int[] alpha=new int[15]; int[] beta=new int[25]; which of the following is a valid call to the method strange? a. strange(alpha[10], alpha[15]); b. strange(alpha[5], beta); c. strange(alpha[0], beta[25]); d. strange(alpha, beta[20]);
a
Formal parameters of primitive data types provide ____ between actual parameters and formal parameters.
a one way link.
Assuming the following list declaration, which element is at the position 0 after the first iteration of selection sort? int list [ ] = {16, 30, 24, 7, 62, 45, 5, 55} a. 5 b, 7 c. 16 d. 62
a. 5
The behavior of merge sort is ___ in the worst case and ___ in the average case. a. O(nlog2n), O(nlog2n) b. O(n^2), O(n) c. O(nlog2n), O(n^2) d. O(n^2), O(nlog2n)
a. O(nlog2n), O(nlog2n)
Suppose that the class Mystery is derived from the class Secret. Consider the following statements: Secret secRef = new Secret(); Mystery mysRef = new Mystery(); Which of the following statements is legal in Java? (i) secRef = mysRef; (ii) mysRef = secRef;
a. Only (i)
see B class Which of the following is the correct definition of the method set of the class Dclass? (i) public void set(int a, int b) { super.set(a); y = b; } (ii) public void set(int a, int b) { x = a; y = b; }
a. Only (i)
Based on the diagrams above, which of the following is the superclass?
a. Rectangle
Inheritance is an example of what type of relationship?
a. is-a
____ sort requires knowing where the middle element of the list is. a. merge b. bubble c. insertion d. selection
a. merge
With the binary search algorithm, ____ key comparison(s) is/are made in the successful case---the last time through the loop. a. one b. two c. n-2 d. n
a. one
Based on the diagram above, the method setDimension in the class Box ____ the method setDimension in the class Rectangle.
a. overloads
A subclass can directly access ____.
a. public members of a superclass
After the second iteration of bubble sort for a list of length n, the last ____ elements are sorted. a. two b. three c. n-2 d. n
a. two
Syntax for defining a method that returns an array
accessModifier dataType[] methodName(parameterList);
Syntax for defining a method that takes an array as a parameter
accessModifier returnType methodName(dataType[] arrayName);
Syntax for instantiating 2D arrays
arrayName=new dataType[exp1][exp2];
Syntax for instantiating an array
arrayName[]=new dataType[size];
Syntax for accessing 2D array elements
arrayName[exp1][exp2];
Syntax of accessing array elements
arrayName[exp];
Let f be a function of n. By the term ______, we mean the study of the function f as n becomes larger and larger without bond.
asymptotic
suppose you have the following declaration: double[] salesData=new double[500]; Which of the following range is valid for the index of the array salesData? (i) 0 through 500 (ii) 0 through 499 a. only (i) b. only (ii) c. both are invalid d. none of these
b
what is the output of the following Java code? int[] lost={0,5,10,15,20}; for(int j=0;j<5;j++) {s.o.p(list[j]+" ");} s.o.pln(); a.0 1 2 3 4 b.0 5 10 15 20 c. 0, 5, 10, 15, 20 d. 0 5 10 15
b
which of the following declares an array of int name beta? a. int beta; b. int[] beta; c. new int beta[]; d. int beta=int[];
b
See b class Which of the following correctly overrides the method print of DClass? (i) public void print() { System.out.print(x + " " + y); } (ii) public void print() { super.print(); System.out.print(" " + y); }
b. Only (ii)
see class mystery Which of the following statements is legal in Java? (i) mysRef = (Mystery) mySecret; (ii) mysRef = (Mystery) secRef;
b. Only (ii)
If there are three classes: Shape, Circle, and Square, what is the most likely relationship between them?
b. Shape is a superclass, and Circle and Square are subclasses of Shape.
Suppose a class Car and its subclass Honda both have a method called speed as part of the class definition. rentalH refers to an object of the type Honda and the following statements are found in the code: rentalH.cost(); super.speed(); What does the second statement in the description above do?
b. The speed method in Car will be called.
The sequential search algorithm uses a(n) ____ variable to track whether the item is found. a. int b. bool c. char d. double
b. bool
Composition is a(n) ____ relationship.
b. has-a
A sequential search of an n-element list takes ____ key comparisons on average to determine whether the search item is in the list. a. 0 b. n/2 c. n d. n^2
b. n
For a list of length n, insertion sort makes ___ key comparisons, in the worst case. a. n(n-1)/4 b. n(n-1)/2 c. n^2 d. n^3
b. n(n-1)/2
how many components are in the array numList seen in the accompanying figure? int[] numList=new int[50]; for(int -=0;i<50;i++) numList[i]=2*i; num[10]=-20; num[30]=8; a. 0 b. 30 c. 49 d. 50
d
A comparison tree is a(n) __________ tree.
binary
The ______ search algorithm is the optimal worst-case algorithm for solving search problems by using the comparison menthod
binary
Within any class or method, the code between a pair of curly braces is called a(n) ___.
block
return type of the add method for an ArrayList
boolean
Syntax for comparing arrays for equality
boolean isEqual=true; if(arrayName1.length!=arrayName2.length) isEqual=false; else { for(int i=0;i<arrayName1.length&&isEqual;i++) { if(Math.abs(arrayName1[i]-arrayName2[i])>0.001) isEqual=false; } }
The following C++ function implements the ___sort algorithm. template <class elemType> void Sort (elemType list [ ], int length) { for (int iteration = 1; iteration < length; iteration++) { for (int index = 0; index < length - iteration; index++) { if (list [index] > list [index +1]) { elemType temp = list[index]; list [index] = list[index + 1]; list[ index + 1] = temp; } } } }
bubble
Assume that list consists of the following elements. What is the result after bubble sort completes? int list [ ] = {2, 56, 34, 25, 73, 46, 89, 10, 5, 16}; a. 89 73 56 46 25 16 10 5 2 b. 2 56 34 25 5 16 89 46 73 c. 2 5 10 16 25 34 46 56 73 89 d. 2 10 16 25 34 46 56 73 89
c. 2 5 10 16 25 34 46 56 73 89
consider the following method definition public static int strange(int[] list, int listSize, int item) { int count=0; for(int j=0;j<listSize;j++) { if(list[j]==item) count++; } return count; } which of the following statements best describe the behavior of this method? a. this method returns the number of values store in list b. this method returns the sum of all the values of list c. this method returns the number of times item is stored in list d. this method can process an array of doubles
c
given the following method heading public static void mystery(int list[], int size) and the declaration int[] alpha=new int[75]; which of the following is a valid call to the method mystery? a. mystery(alpha[75],50); b. mystery(alpha[], 50); c. mystery(alpha, 40); d. mystery(int[75], alpha);
c
what is the index number of the last component in the array numList seen in the accompanying figure? a. 0 b. 30 c. 49 d. 50
c
The formula to find the index of the middle element of a list is _________. a. (mid + last)/2 b.(first + last) - 2 c. (first + last) / 2 d. (first + mid) *2
c. (first + last) /2
If n = 1000, to sort the list, bubble sort makes about ____ item assignments. a. 10,000 b. 100,000 c. 250,000 d. 500,000
c. 250,000
Consider the following list: int list [ ] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95} When performing a binary search, the target is compared first with _____. a. 4 b. 25 c. 39 d. 95
c. 39
Which of the following statements about the reference super is true?
c. It must be the first statement of the constructor.
An abstract class ____.
c. cannot be instantiated
We can trace the execution of a comparison-based algorithm by using a graph called a _____. a. pivot table b. partition table c. comparison tree d. merge tree
c. comparison tree
Any new class you create from an existing class is called a(n) ____.
c. derived class
An abstract method ____.
c. has no body
Which of the following correctly states the quick sort algorithm? a. if (the list size is greater than 1) { a. Partition the list into four sublists. b. Quick sort sublist1. c. Quick sort sublist2. d. Quick sort sublist3. e. Quick sort sublist4. d. Combine the sorted lists. } b. a. Find the location of the smallest element. b. Move the smallest element to the beginning of the unsorted list c. if(the list size is greater than 1) { a. Partition the list into two sublists, say lowerSublist and upperSublist. b. Quick sort lowerSublist c. Quick sort upperSublist d. Combine the sorted lowerSublist and sorted upperSublist } d. if the list is of a size greater than 1 { a. Divide the list into two sublists b. Merge sort the first sublist c. Merge sort the second sublist. d. Merge the first sublist and the second sublist }
c. if(the list size is greater than 1) { a. Partition the list into two sublists, say lowerSublist and upperSublist. b. Quick sort lowerSublist c. Quick sort upperSublist d. Combine the sorted lowerSublist and sorted upperSublist }
In a bubble sort for list of length n, the first step is to compare elements _____. a. list [0] and list [n] b. list[0] and list [n-1] c. list [0] and list[1] d. list[n-1] and list[n+1]
c. list[0] and list[1]
When working with the unsorted portion of a list, the second step in a selection sort is to _____. a. divide the list into two parts b. move the smallest element to the top of the list (position 0) c. move the smallest element to the beginning of the unsorted list d. find the smallest element
c. move the smallest element to the beginning of the unsorted list
A sequential search of an n-element list takes ______ key comparisons if the item is not in the list. a. 0 b. n/2 c. n d. n^2
c. n
In the average case, sequential search typically searches ____. a. one quarter of the list b. one third of the list c. one half of the list d. the entire list
c. one half of the list
An abstract class ____.
cannot be instantiated
the insertion sort is also known as the...
card sort
An exception that can be recognized by the compiler is a(n) ____.
checked exception
Constructors have the same name as the ____.
class
Constructors have the same name as the ____. 1. data members 2. member methods 3. class 4. package
class
___ variables are variables that are shared by every instantiation of a class.
class
What is the correct syntax for defining a new class Parakeet based on the superclass Bird?
class Parakeet extends Bird{ }
1. //post condition: x=a; y=b; z=c; super(a,b); z=c; 2. //output the value of x,y,z super.print(); System.out.println("z=" + z);
class Test { private int x; private int y; // constructor to initiate the instance variable to 0 public Test() { x = 0; y = 0; } //constructor with parameters to initiate the instance variables to the value //specified by the parameters: Post condition: x=m, y=n public Test(int m, int n) { x = m; y = n; } // print x, y public void print() { System.out.println("x="+ x+ " y=" +y); } } public class TestNew extends Test { private int z; public void Testnew(int a, int b, int c) { 1: ----------------------------------------??? } public void print() { 2: ------------------------------------??? } }
Syntax to remove all elements of an ArrayList
clear();
When writing data to a file, which method should be called to ensure that data is actually written to the file? a. write c. close b. exist d. print
close
when creating a 2D array does the second set of brackets ([exp2]) represent the number of rows or columns?
columns
A variable comes into existence, or ___, when you declare it.
comes into scope
the second loop of the insertion sort...
compares current array element to the element of the sorted array from right to left
To construct a search algorithm of the order less than log2n, it cannot be ______ based
comparison
A(n) ___ method is a method that creates and initializes class objects.
constructor
If a list of eight elements is sorted using selection sort, the unsorted list is ____ after the second iteration. a. list[ 0 ] . . . list[ 1 ] b. list[ 0 ] . . . list[ 6 ] c. list[ 1 ] . . . list[ 7 ] d. list[ 2 ] . . . list[ 7]
d. list[ 2 ] . . . list[ 7]
Consider the following list: int list [ ] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95} When performing a binary search for 75, after the first comparison, the search is restricted to _____. a. list[0]...list[6] b. list[0]...list[7] c. list[5]...list[11] d. list[6]...list[11]
d. list[6]...list[11]
In the bubble sort algorithm, the following code accomplishes swapping values in elements at positions index and index + 1 a. list[index] = list[index + 1] list[index + 1] = list[index] b. list[index + 1] = list[index] list[index ] = list[index + 1] c. list[index] = temp; list[index ] = list[index + 1]; temp = list[index + 1]; d. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp;
d. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp;
Syntax for declaring an array
dataType[] arrayName;
Syntax for assigning initial values to an array
dataType[] arrayName={value0,value1,...};
Syntax for declaring 2D arrays
dataType[][] arrayName;
Syntax for assigning initial values of a 2D array
dataType[][] arrayName={{value0,value1,...},{value0,value1,...},...};
In ____ copying, each reference variable refers to its own object. a. shallow b. deep c. method d. object
deep
Which of the following is a constructor without parameters?
default constructor
Which of the following is a constructor without parameters? a. copy constructor b. default constructor c. finalizer d. modifier
default constructor
Any new class you create from an existing class is called a(n) ____.
derived class
The quick sort algorithm uses the _______ technique to sort a list.
divide and conquer
Syntax for summing all elements in an array
double variable=0.00; for(int i=0;i<arrayName.length;i++) { variable+=arrayName[i]; } s.o.p("The total is "+variable);
Which applet method is called by the Web browser to display the applet to the screen?
drawApplet()???
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } Which of the following is an invalid call to the method in the accompanying figure?
exampleRecursion(-1)
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } Which of the following is an invalid call to the method in the accompanying figure?
exampleRecursion(-1)
the first loop of the insertion sort...
executes n-1
Default Value: boolean
false
In Java, you can automatically make a reference variable of a subclass type point to an object of its superclass.
false
T/F in a linear search the values must be sorted
false
The length ____ contains the number of elements in the array.
field
menuMB.add(fileMenu); menuMB.add(editMenu); menuMB.add(viewMenu); menuMB.add(insertMenu); Which menu will appear first if it is added to the menu bar in the order shown in the accompanying figure?
filemenu
To draw a rectangle filled with the current color, you can use the method ____.
fillRect()
The keyword ___ indicates that a field value is unalterable.
final
Consider the following list. list = {5, 11, 25, 28, 45, 78, 100, 120, 125}; Suppose that binary search as discussed in the book is used to determine whether 110 is in list. What are the values of first and last when the while loop, in the body of the binarySearch method, terminates?
first = 7, last = 6
Syntax of an enhanced for loop
for(className currentObject:arrayListName) { //process current object }
Syntax for aggregating array operations: printing all elements
for(int i=0;i<arrayName.length;i++) { for(int j=0;j<arrayName[i].length;j++) { //process elements of arrayName[i][j] }
Syntax for processing one row at a time
for(int i=0;i<arrayName.length;i++) { for(int j=0;j<arrayName[i].length;j++) { //process elements of arrayName[i][j] } }
Syntax for processing a given column
for(int i=0;i<arrayName.length;i++) { if(j<arrayName[i].length) //process elements of arrayName[i][j] }
Syntax for copying an array
for(int i=0;i<arrayName.length;i++) { newArrayName[i]=arrayName[i]; }
Syntax for printing all elements of an array
for(int i=0;i<arrayName.length;i++) { s.o.p(arrayName[i]); }
Syntax for processing a given row (row i)
for(int j=0;j<arrayName[i].length;j++) { //process elements of arrayName[i][j] }
Syntax for processing one column at a time
for(int j=0;j<maxNumberOfColumns;j++) { for(int i=0;i<arrayName.length;i++) { if(j<arrayName[i].length) { //process elements of arrayName[i][j] } }
___ parameters are variables in a method declaration that accept the values from the actual parameters.
formal
public static int func1(int m, int n) { if (m == n || n == 1) return 1; else return func1(m - 1, n - 1) + n * func1(m - 1, n); } Given the code in the accompanying figure, which of the following method calls would result in the value 1 being returned?
func1(1 ,1)
public static int func2(int m, int n) { if (n == 0) return 0; else return m + func2(m, n - 1); } Which of the following statements about the code in the accompanying is always true?
func2(m, n) = m * n for n >= 0
Given Graphics object g, what method call is used to draw a line from X = 10, Y = 20 to X = 80, Y = 40?
g.drawLine(10, 20, 80, 40);
Syntax to return element at a certain index w/o removing it form the list in an ArrayList
get(int index);
When a variable ceases to exist at the end of a method, programmers say the variable ____.
goes out of scope
Given Graphics object graph, which of the following draws a circle with a radius of 50, at X = 20, Y = 30?
graph.drawOval(20, 30, 50, 50);
If graph holds a Graphics object, which of the following puts "CAT" starting at X = 20, Y = 50?
graph.drawString(20, 50, "CAT");??
Given Graphics object graph, which statement can you use to change the pen color to red?
graph.setColor(Color.red);
Given Graphics object graph, which statement can you use to change the pen color to red?
graph.setPen(Color.red);??
Suppose that sales is a two-dimensional array of 10 rows and 7 columns wherein each component is of the type int. Which of the following correctly finds the sum of the elements of the fifth row of sales?
int sum = 0; for(int j = 0; j < 10; j++) sum = sum + sales[5][j];
Syntax for finding the MIN value in an array
int variable=0; for(int i=0;i<arrayName.length;i++) { if(arrayName[i]<arrayName[variable]) variable=i; } s.o.p("The min is "+variable);
Syntax for finding the MAX value in an array
int variable=0; for(int i=0;i<arrayName.length;i++) { if(arrayName[i]>arrayName[variable]) variable=i; } s.o.p("The max is "+variable);
Which of the following declares an array of int named beta?
int[] beta; new int beta[];
Which of the following declares an array variable of int named hits?
int[] hits;
The ___ package contains frequently used classes and is implicitly imported into java programs.
java.lang
When you type a regular alphanumeric key, the ____ method is executed.
keyPressed??
Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to?
length
Consider the following list. list = {24, 20, 10, 75, 70, 18, 60, 35} Suppose that list is sorted using the selection sort algorithm as discussed in the book. What is the resulting list after two passes of the sorting phase; that is, after two iterations of the outer for loop?
list = {10, 18, 24, 75, 70, 20, 60, 35
Which of the following is used to allocate memory for the instance variables of an object of a class? the operator new Consider the following list. list = {24, 20, 10, 75, 70, 18, 60, 35} Suppose that list is sorted using the selection sort algorithm as discussed in the book. What is the resulting list after two passes of the sorting phase; that is, after two iterations of the outer for loop?
list = {10, 18, 24, 75, 70, 20, 60, 35}
Consider the following list. list = {24, 20, 10, 75, 70, 18, 60, 35} Suppose that list is sorted using the insertion sort algorithm as discussed in the book. What is the resulting list after two passes of the sorting phase; that is, after three iterations of the for loop?
list = {10, 20, 24, 75, 70, 18, 60, 35}
A(n) ____ variable is known only within the boundaries of the method.
local
public static int func1(int m, int n) { if (m == n || n == 1) return 1; else return func1(m - 1, n - 1) + n * func1(m - 1, n); } What precondition must exist in order to prevent the code in the accompanying figure from infinite recursion?
m >= 0 and n >= 1
public static int func1(int m, int n) { if (m == n || n == 1) return 1; else return func1(m - 1, n - 1) + n * func1(m - 1, n); } What precondition must exist in order to prevent the code in the accompanying figure from infinite recursion?
m >= 0 and n >= 1
The ___ method executes first in an application, regardless of where you physically place it within its class.
main()
The ____ method executes first in an application, regardless of where you physically place it within its class.
main()
A locally declared variable always ___ another variable with the same name elsewhere in the class.
masks
Syntax for passing an array as an argument when calling a method
methodName(arrayName);
When they have the same name, variables within ___ of a class override the class's fields.
methods
Which of the following mouse events is handled by the interface MouseMotionListener?
mouseDragged
Given the following method heading public static void mystery(int list[], int size) and the declaration int[] alpha = new int[75]; Which of the following is a valid call to the method mystery?
mystery(alpha, 40); mystery(int[75], alpha)
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What is the limiting condition of the code in the accompanying figure?
n >= 0
public static int exampleRecursion (int n) { if (n == 0) return 0; else return exampleRecursion(n - 1) + n * n * n; } What is the limiting condition of the code in the accompanying figure?
n >= 0
public static int func2(int m, int n) { if (n == 0) return 0; else return m + func2(m, n - 1); } What is the limiting condition of the code in the accompanying figure?
n >= 0
For a list of length n, the bubble sort makes exactly ________ key comparisons
n(n-1)/2
In order to allocate memory for an object, you use the ___ operator.
new
Which of the following statements creates an instance of FileReader for the filename "file.txt"? a. new FileReader(file.txt) c. FileReader("file.txt") b. FileReader(file.txt) d. newFileReader("file.txt")
newFileReader("file.txt")
Given String name = "myfile", which of the following can be used to create an object for writing to a file called "myfile"? a. new PrintWriter(myfile); b. newPrintWriter(name); c. PrintWriter(myfile); d. newPrintWriter("name");
newPrintWriter(name);
Based on the diagram in the accompanying figure, which of the following members will Box NOT have direct access to? length char[][] table = new char[10][5]; 10 import java.util.*; public class ExceptionExample1 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int dividend, divisor, quotient; try { System.out.print( " Enter dividend: " ); dividend = console.nextInt(); System.out.println(); System.out.print( " Enter divisor: " ); divisor = console.nextInt(); System.out.println(); quotient = dividend / divisor; System.out.println( " quotient = " + quotient); } catch (ArithmeticException aeRef) { System.out.println( " Exception " + aeRef.toString()); } catch (InputMismatchException imeRef) { System.out.println( " Exception " + imeRef.toString()); } catch( IOException ioeRef) { System.out.println( " Exception " + ioeRef.toString()); } } } Which method throws the second exception in the code in the accompanying figure?
nextInt
Default Value: other object references (ex: String)
null
Default Value: char
null character
How many finalizers can a class have?
one
Which of the following class definitions is correct in Java? (i) public class Employee {private String name; private double salary; private int id; public Employee() {name = ""; REF: 468 REF: 471 c. pre-defined methods d. methods c. finalizer d. modifier salary = 0.0; id = 0; } public Employee(String n, double s, int i) {name = n; salary = s; id = i; } public void print(){ System.out.println(name+""+id+""+salary); (ii)} }public class Employee {private String name; private double salary; private int id; public void Employee() {name = ""; salary = 0.0; id = 0; } public void Employee(String n, double s, int i) {name = n; salary = s; id = i; } public void print() {System.out.println(name + " " + id + " " + salary); } } a. only i b. only ii c. both i and ii d. neither is correct
only i
Consider the following class definition. public class Rectangle {private double length; private double width; public Rectangle() {length = 0; width = 0; } public Rectangle(double l, double w) {length = l; width = w; } public void set(double l, double w) {length = l; width = w; } public void print() {System.out.println(length + " " + width); } public double area() { return length * width; } public double perimeter() { return 2 * length + 2 * width; } } Which of the following statements correctly instantiates the Rectangle object myRectangle? (i) myRectangle = new Rectangle(12.5, 6);(ii) Rectangle myRectangle = new Rectangle(12.5, 6);(iii) class Rectangle myRectangle = new Rectangle(12.5, 6); a. only i b. only ii c. only iii d. both ii and iii
only ii
Based on the diagram in the accompanying figure, the method setDimension in the class Box ____ the method setDimension in the class Rectangle
overloads
Based on the diagram in the accompanying figure, the method area in the class Box ____ the method area in the class Rectangle
overrides
In a quick sort, all of the sorting work is done by the function ________
partition
A sequence of branches in a comparison tree is called a(n) _______
path
The first step in the quick sort partition algorithm is to determine the _____ and swap it with the first element in the list.
pivot
Class members consist of all of the following EXCEPT ____. a. named constants b. variable declarations c. pre defined methods d. methods
pre defined methods
Class members consist of all of the following EXCEPT ____.
pre-defined methods
Assigning ___ to a field means that no other classes can access the field's values.
private access
If a class implements an interface, it must ____.
provide definitions for each of the methods of the interface
Suppose that Book is class with two instance variables- name of type String and numOfBooks of type int. Which of the following would be appropriate syntax for the heading of a copy constructor for the class called Book?
public Book(Book b)
Suppose that Book is class with two instance variables-name of type String and numOfBooks of type int. Which of the following would be appropriate syntax for the heading of a copy constructor for the class called Book?
public Book(Book b)
Syntax for an array as an instance variable
public Class(double[] array) { arrayName=new double[array.length]; for(int i=0;i<arrayName.length;i++) { arrayName[i]=array[i]; } }
Which of the following would be a default constructor for the class MysteryClass shown in the accompanying figure?
public MysteryClass(){ setData(0, 0.0); }
Which of the following would be a default constructor for the class MysteryClass shown in the accompanying figure?
public MysteryClass(){setData(0, 0.0);}
Syntax for accessors for arrays
public double[] getArrayName() { double[] temp=new double[arrayName.length]; for(int i=0;i<arrayName.length;i++) { temp[i]=arrayName[i]; } return temp; }
Syntax for sequential search
public int indexOf(int key) { for(int i=0;i<arrayName.length;i++) { if(arrayName[i]==key) return i; } return -1 }
A subclass can directly access ____.
public members of a superclass
chat
public static char methodHeading(int n, double num) Based on the method heading in the accompanying figure, what is the return type of the value returned?
It is a value-returning method of type char.
public static char methodHeading(int n, double num) Which of the following statements about the method heading in the accompanying figure is NOT true?
4 3 2 1
public static void mystery(int num) { if (num > 0) { System.out.print(num+" "); mystery(num-1); } } What is the output?
Given the method heading public static void strange(int a, int b) and the declaration int[] alpha = new int[10]; int[] beta = new int[10]; Which of the following is a valid call to the method strange?
strange(alpha[0], alpha[1]);
The graphic in the accompanying figure is an example of ____.
radio buttons
Syntax to remove a certain element at a certain index
remove(int index);
A(n) ___ causes a value to be sent from a called method back to the calling method.
return statement
Which of the following is NOT true about return statements?
return statements can be used in void methods to return values.
Syntax for method to return a 2D array
returnArrayType[][] methodName(parameterList);
Syntax for method to accept 2D array as a parameter
returnType methodName(arrayType[][] arrayParameterName);
The top node of a comparison tree is call the ________ node.
root
when creating a 2D array does the first set of brackets ([exp1]) represent the number of rows or columns?
rows
According to the UML class diagram in the accompanying figure, which of the following is a private member of the class MysteryClass?
second
public static double secret(int first, double second) { double temp; if (second > first) temp = first * second; else temp = first - second; return temp; } Which of the following is a valid call statement to the method given above?
secret (5, 4.8);
Syntax to replace current element at an index and returns replaced element in an ArrayList
set(int index, E element);
Which of the following sets the background color of the applet to white?
setBackground(Color.white);
Which of the following methods does the class JTextArea inherit from the class JTextComponent?
setText()
A mouse can generate ____ different types of events.
seven
The compiler determines which version of a method to call by the method's ___.
signature
Syntax to return the number of elements in an ArrayList
size();
A(n) ____ is an integer contained within square brackets that indicates one of an array's variables.
subscript
What is the code that will display the middle initial 'L' (right justified) with a total of 10 spaces, given the following declaration? String middleName= "Lanette";
system.out.print("Here is the first character of the middle name: %10c", middleName.CharAt(0));
Which of the following is used to allocate memory for the instance variables of an object of a class?
the operator new
Which of the following is used to allocate memory for the instance variables of an object of a class? a. the reserved word public b. the reserved word static c. the operator new d. the operator +
the operator new
If a negative value is used for an array index, ____
the program terminates automatically without throwing an exception the last index of the array is automatically accessed instead an ArrayIndexOutOfBoundsException is thrown
The third Fibonacci number is ____.
the sum of the first two Fibonacci numbers
public class Secret { private int x; private static int y; public static int count; public int z; public Secret() { x = 0; z = 1; } public Secret(int a) { x = a; } public Secret(int a, int b) { x = a; y = b; } public String toString() { return ("x = " + x + ", y = " + y + ", count = " + count); } public static void incrementY() { y++; } } How many constructors are present in the class definition in the accompanying figure?
three
A mouse can generate ____ different types of events.
three??
Which of the following is NOT a method of the class Throwable?
throwMessage
The technique to solve a problem by subdividing into smaller problems is known as divide-and- conquer and ____ design approach.
top-down
Syntax to set capacity to size in an ArrayList
trimToSize();
In single inheritance, the subclass is derived from a single superclass.
true
Inheritance implies an "is-a" relationship
true
T/F Given the declaration: double[] numList=new double[20]; the statement: numList[12]=numList[5]+numList[7]; updates the content of the 13th component of the array numList
true
T/F in an binary search the values must be sorted
true
How many constructors does the class Exception have?
two
In the recursive algorithm for the nth Fibonacci number, there are ____ base case(s)
two
In the recursive algorithm for the nth Fibonacci number, there are ____ base case(s).
two
what java packages does the ArrayList use?
util
return type of the clear method for an ArrayList
void
return type of the trimToSize() method of an ArrayList
void
Which is an infinite loop?
} loopCount = 1; while(loopCount<3); { system.out.println("Hello"); loopCount= loopCount + 1;