Module 6 Flashcards

1
Q

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

A

C)

In the recursive step, 1 is added to the partial sum. The result is 1 + 1 + 1+ 1 + 1 + 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A) B) C) D)

All are correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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();

A

B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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}

A

B) C)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

D)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A) E)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the index of the “rst element of an array in Java?

A) First
B) 0
C) 1
D) 1.0

A

B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A) C) E)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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];

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

B) D) F)

Executable statements, assignment statements, and variable declaration statements must be terminated with a semi-colon. The other choices are not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

D)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

C) D) E)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following java statements would correctly call the printInt method shown below?
public static void printInt(int n) { for(int i = 0; i < n; i++){
System.out.println(i);
}
}

A) printInt(5.5);
B) printInt();
C) System.out.println(printInt(5));
D) printInt(50);
E) int num = 56;
printInt(num);

A

D) E)

17
Q

Which of the following java statements would correctly call the sumInt method shown below?

public static int sumInt(int n) {
int sum = 0;
for(int i = 0; i <= n; i++){
sum += i; }
return sum;
}

A) int sum = sumInt(50);
B) System.out.println(sumInt(50));
C) sumInt(60.5);
D) double sum = sumInt(85.2);
E) sumInt(50);

A

A) B)

18
Q

Which of the following java statements would correctly call the multiplyNums method shown below?
public static void multiplyNums(int n1, int n2) {
double result = n1*n2;
System.out.println(result);
}

A) int num1 = 56, num2 = 64;
multiplyNums(num1, num2);
B) multiplyNums(5.5);
C) System.out.println(multiplyNums(5, 8));
D) multiplyNums(50,33);
E) multiplyNums(5.5, 6.7);

A

A) D)

19
Q

Which of the following statements are true about the code shown below in the class named Quiz?
public class Quiz {
public static void printOdd(int n) {
for(int i = 0; i < n; i++){
if(i % 2 != 0)
System.out.println(i);
else
System.out.println(i+1);
}
}
public static void main(String arg[]) {
printOdd(5);
}
}

A) This method will print a list of all even numbers less than n.
B) This method when executed as called by main will print out 5 lines of output.
C) ) A value of 5 is being returned to main from the printOdd method after execution.
D) A value of 5 will be substituted for the parameter during the execution of the printOdd method.
E) This method when executed as called by main will print out 1 line of output.

A

B) D)

printOdd is a void method thus it doesn’t return a value. The method when executed will print a list of odd numbers less than or equal to n. The for loop will execute 5 times thus 5 lines of output will be generated since each loop iteration produces a line of output. The value of 5 is the argument which will be substituted for the parameter n during method execution.

20
Q

Which of the following statements are true about the code shown below in the class named Quiz?
public class Quiz {
public static int sumIntegers(int n) {
int n1 = 1, n2 = 1, n3 = 1, n4 = 0;
while (n4 <= n) {
n4 = n1+n2+n3;
n1 = n2;
n2 = n3;
n3 = n4;
}
return n2;
}
public static void main(String arg[]) {
System.out.println(sumIntegers(5));
}
}

A) During the sumIntegers execution, the while loop will execute 3 times.
B) The value returned to main from the execution of the method named sumIntegers will be 5.
C) The parameter n in the method sumIntegers is being passed an argument value of 5 during the method call.
D) In the main method’s call of sumIntegers, the integer 5 can be referred to as a parameter.
E) The main method will print out one line of output consisting of only the integer 3.

A

A) B) C)

The single value returned to main by the sumIntegers method call is a 5. That 5 will be the only output to the console during execution of this program. During the method call, an argument value of 5 is being passed to the parameter n as defined by the sumIntegers method header. Since n = 5 during the method execution, the while loop will execute 3 times with a value of n4 equal to 0, 3, and 5.

21
Q

Which of the following are categories of variables dependent on their usages in a Java program?

A) Reference variables
B) Instance variables
C) Local variables
D) Formal parameters
E) Actual parameters

A

A) B) C) D)

22
Q

Which of the following statements or expressions allow a Java program to access the length of array A?

A) int A[] = { 1, 2, 3, 4, 5};
B) A.length
C) A = length();
D) A.length = int A[];

A

B)

23
Q

Given the Java definition of the array B as follows, double B[] = new double [5], what would the expression B.length retrieve?

A) B[5]
B) 7
C) 0
D) 1
E) 5

A

E)

24
Q

From the list below, please select the Java syntax which is required to support recursion.

A) Calls to a function
B) Array declarations and indexes
C) The if statement.
D) The new operator
E) Formal parameters and actual parameters

A

A) C) E)

Although array declarations and indexes, and the new operator are useful within recursive routines, they are not necessary. However, all other options are necessary.

25
Q

Given a void method named sumNumbers that takes two integer values as parameters and prints their sum, which of the following Java statements would correctly call this method?

A) sumNumbers(50, 64);
System.out.println(sumNumbers(50,64));
B) sumNumbers(6.6, 5.5);
C) int sum = sumNumbers(85, 5);
D) int n1=5;
int n2=6;
sumNumbers(n1, n2);
E) sumNumbers(50, 64);

A

D) E)

The method described takes two integer values as parameters, not doubles. Remember doubles can’t be automatically cast as integers. A void method can’t be used in a printLn statement since the method doesn’t return a value to be printed. This is also why a void method call can’t be part of an assignment statement.

26
Q

Given a method named sumNumbers that takes two integer values as parameters and returns their sum as an integer, which of the following Java statements would correctly call this method?

A) int n1=5;
int n2=6;
System.out.println(sumNumbers(n1, n2));
B) int sum = sumNumbers(85, 5);
C) sumNumbers(50, 64);
D) double sum = sumNumbers(50,64);
E) sumNumbers(6.6, 5.5);

A

A) B) D)

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.

27
Q

What is the index of the first element of an array in Java?

A) 0
B) 1.0
C) First
D) 1

A

A)

28
Q

Which of the following statements are true about the code shown below in the class named Quiz?
public class Quiz {
public static void sumIntegers(int n) {
int n1 = 1, n2 = 1, n3 = 0;
while (n3 <= n) {
n3 = n1+n2;
System.out.println(n1+”, “+n2+”, “+n3);
n1 = n2;
n2 = n3;
}
}
public static void main(String arg[]) {
sumIntegers(3);
}
}

A) While the sumIntegers method is executing, n = 3.
B) The loop in sumIntegers will execute 4 times.
C) There is not enough information given to determine how many lines of output will be generated.
D) The main method is calling the sumIntegers method and passing a value of 3 as the argument.
E) Execution of this code starts with the sumIntegers method.

A

A) D)