Module 6 Flashcards
Given the following recursive method:
public static int doSum(int n)
{
if (n <= 0)
return 0;
else
return 1 + doSum(n - 1);
}
what is the value of the expression doSum(5)?
A) 0
B) 10
C) 5
D) 15
C)
In the recursive step, 1 is added to the partial sum. The result is 1 + 1 + 1+ 1 + 1 + 0.
Which of the following statements are true regarding arrays in Java?
A) An array is a collection of variables which are accessed individually by using the name of its reference variable and an index.
B) An array is specified by putting a pair of square brackets following a variable name.
C) An array is a collection of variables which all share the same name.
D) An array is collection of primitive variables.
A) C)
An array is not a collection of primitive variables because an array may be based on the definition of a derived data type. Putting square brackets following the variable name simply says that a variable is a reference variable which points to an array. This does not define an array. All other options are true.
Which of the following statements indicate some aspect of the definition of an array in Java?
A) The definition of an array may also include specific values to be stored in elements of the array.
B) The array itself will exist in the Heap.
C) The array definition may not actually create the array.
D) The name of the reference variable for an array may be followed by a pair of square brackets and an equal sign.
A) B) C) D)
All are correct.
Which of the following statements or expressions allow a Java program to access the length of array A?
A) A.length = int A[];
B) A.length
C) int A[] = {1, 2, 3, 4, 5};
D) A = length();
B)
Which of the following are valid declarations of an array in Java?
A) int a{} = new int {10}
B) int a[] = new int [10]
C) double b[] = {1.23, 2.34, 3.45}
D) int a[] = new int {10}
B) C)
Given the array, what is the value of the expression values[1] + values[5]
int[] values = {0, 1, 2, 3, 4};
A) 4
B) 6
C) 7
D) None of the given choices
D)
Given the array, what is the value of the expression values[1] + values[2]?
int[] values = {0, 1, 2, 3, 4};
A) 3
B) 5
C) 7
D) 1
A)
Which statement(s) or expression(s) would allow a Java program to display the number of elements in array Infant_Age, as defined as follows:
int Infant_Age[] = {1, 2, 3, 4}
A) len = Infant_Age.length;
System.out.print(len);
B) for (int i=0; i<4; i++) {
System.out.print (i)}
C) System.out.print (Infant_Age[4])
D) System.out.print (length.Infant_Age)
E) System.out.print (Infant_Age.length)
A) E)
What is the index of the “rst element of an array in Java?
A) First
B) 0
C) 1
D) 1.0
B)
Which of the following statements are true regarding arrays in Java?
A) All elements of an array must be of the same data type.
B) The notation of an array is any type of bracket.
C) The data in an array can either be primitive or derived.
D) Once an array has been fully initialized, its data is fixed for the rest of the program’s execution.
E) Once an array has been fully declared, its length is fixed for the rest of the program’s execution.
A) C) E)
Which of the following are expressions in Java which allocate five members to the double array B?
A ) double B = [5];
B) double B[] = {-1.2, 2.3, 3.4, 4.5, 5.6};
C) double B[];
D) int num = 5;
double B[] = new double[num];
E) double B[] = new double [5];
B) D) E)
The three options that begin with double B[] = will allocate five members to the double array B. The others will declare B as a reference variable to an array of double, and define B as a pointer to double.
Which of the following are terminated by a semi-colon in Java?
A) a comment.
B) a variable declaration statement.
C) a method definition statement.
D) an executable statement.
E) a class definition statement.
F) an assignment statement.
B) D) F)
Executable statements, assignment statements, and variable declaration statements must be terminated with a semi-colon. The other choices are not.
Which of the following is true in a correct implementation of a recursive method definition:
A) A recursive method is usually written to call itself forever.
B) A base condition is optional. The recursive method will terminate anyway.
C) Recursive methods are written using loop statements.
D) A base condition is required so that the recursive method will terminate after a finite number of invocations.
D)
Which of the following java statements would correctly call the multiplyNums method shown below?
public static double multiplyNums(int n1, int n2) {
double result = n1*n2;
return result;
}
A) multiplyNums(50, 64);
B) System.out.println(multiplyNums(50, 64));
D) multiplyNums(6.6, 5.5);
C) double sum = multiplyNums(50,64);
E) int sum = multiplyNums(85, 5);
B) C)
A method returning a value must be called by a statement which uses the returned value, such as an assignment statement or a print statement. Remember, the arguments passed to the method as the parameter values must be compatible data types. An integer can be substituted for a double, but not vice versa.
Which of the following statements are true about the code shown below in the class named Quiz?
public class Quiz {
public static int sumNums(int n) {
int sum = 0;
for(int i = 1; i <= n; i++){
if(i % 2 == 0){
sum += i;
System.out.println(i);
}
return sum;
}
public static void main(String arg[]) {
System.out.println(sumNums(20));
}
}
A) The single line of output of this program is the sum, which would be 110.
B) At the end of execution of the method sumNums, sum = 90.
C) During execution of sumNums, n = 20.
D) The value 20 is the argument in the method call.
E) After execution, the method sumNums will return a value of 110 to the main method.
C) D) E)