Final Exam (part 2) Flashcards

1
Q

What is the value in count after the following loop is executed?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (count++ < 9);
System.out.println(count);

A

10

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

Suppose the input for number is 9. What is the output from running the following program?

import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“Enter an integer: “);
int number = input.nextInt();

  int i;

  boolean isPrime = true;
  for(i = 2; i < number && isPrime; i++) {
        if(number % i == 0) {
              isPrime = false;
        }
  }

  System.out.println("i is " + i);

  if (isPrime)
        System.out.println(number + " is prime");
  else
        System.out.println(number + " is not prime");   } }
A

i is 4 followed by 9 is not prime

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

A break statement can be used only in a loop. T/F

A

false

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

What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4) break;
}
while (item < 5);

A

6

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

Suppose cond1 is a Boolean expression. When will this while condition be true?

while (cond1) …

A

in case cond1 is true

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

Is the following loop correct?

for(; ; );

A

yes

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

How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
count++;
} while (count < 10);

A

10

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

A variable declared in the for loop control can be used after the loop exits. T/F

A

False

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

Each Java class must contain a main method. T/F

A

false

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

You may have a return statement in a void method.

A

True

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

(REPEAT QUESTION):
You may have a return statement in a void method.

A

True

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

The signature of a method consists of _____.

A

method name and parameter list

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

Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

A

a stack

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

Methods can be declared in any order in a class. T/F

A

true

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

(char)(‘a’ + Math.random() * (‘z’ - ‘a’ + 1)) returns a random character _______.

A

between ‘a’ and ‘z’

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

Analyze the following code.

public class Test {
public static void main (String[] args) {
System.out.println(m(2));
}

 public static int m (int num) {
      return num;
 }

 public static void m (int num) {
      System.out.println(num);
 } }
A

The program has a compile error because the two methods m have the same signature.

17
Q

Which of the following is correct to create an array?

int[] m = {1, 2, };
int[] m = {{1, 2}};
int[] m = {{1, 2}, {3, 4}};
int[] m = {1, 2, 3};

A

int[] m = {1, 2, 3};

18
Q

Which correctly creates an array of five empty Strings?

String[] a = new String[5];
String[] a = {“”, “”, “”, “”, “”};
String[5] a;
String[] a = new String[5]; for (int i = 0; i < 5; a[i++] = null);

A

String[] a = {“”, “”, “”, “”, “”};

19
Q

For the binarySearch method in Section 7.10.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[]{1, 4, 6, 8, 10, 15, 20}, 11)?

A

low = 4
high = 26

20
Q

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return?

A

-2

21
Q

What is the output of the following code?

double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length: i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}

System.out.println(indexOfMax);

A

1

22
Q

When you pass an array to a method, the method receives ______.

A

the reference of the array

23
Q

Show the output of the following code:

public class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
increase(x);

      int[] y = {1, 2, 3, 4, 5};
      increase(y[0]);

      System.out.println(x[0] + " " + y[0]);
 }

 public static void increase (int[] x) {
      for (int i = 0; i < x.length; i++)
           x[i]++;
 }

 public static void increase (int y) {
      y++;
 } }
A

2 1

24
Q

Which of the following are valid array declarations?

char[] charArray = new char[26];
int[] words = new words[10];
char[] charArray = “Computer Science”;
double[3] nums = {3.5, 35.1, 32.0};

A

char[] charArray = new char[26];

25
Q

What is the printout of the following code?

public class Test {
public static void main(String[ args) {
int[][][] data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

      System.out.print(data[1][0][0]);
 } }
A

5

26
Q

Given the following declarations:
int[][] m = new int[5][6];

which of the following statements if true?

___The name m represents a two-dimensional array of 30 int values.
___m[2][4] represents the element stored in the 2nd row and the 4th column of m.
___m.length has the value 6.
___m[0].length has the value of 5.

A

The name m represents a two-dimensional array of 30 int values.

27
Q

What is the printout of the following program?

public class Test {
public static void main (String[] args) {
int[][] values = {{3, 4, 5, 1}, {33, 6, 1, 2}};

      int v = values[0][0];
      for (int[] list : values)
           for(int element : list)
                if (v > element)
                     v = element;
      System.out.print(v);
 } }
A

1

28
Q

int[][] matrix = new int[5][5];
for (int column = 0; column < matrix[4].length; column++);
matrix[4][column] = 10;

After the loop, the last column of matrix 10, 10, 10, 10, 10.
After the loop, the last row of matrix 10, 10, 10, 10, 10.
After the loop, the last row of matrix 0, 0, 0, 0, 0.
After the loop, the last row of matrix 0, 0, 0, 0, 0.
A syntax error, because column is not defined.

A

A syntax error, because column is not defined.

29
Q

What is the output of the following code?

public class Test {
public static void main(String[] args) {
int[][] matrix =
{{1, 2, 3, 4},
{4, 5, 6, 7),
{8, 9, 10, 11},
{12, 13, 14, 15}};

      for (int i = 0; i < 4; i++)
           System.out.print(matrix[i][1] + " ");
 } }
A

2 4 9 13

30
Q

Assume int[][][] x = new char[2][5][3], how many elements are in the array?

A

30

31
Q

Write a function public static void parseInt(int x), which prints all the digits of an int type argument x, one digit per line. For example, if int x = 38625, then the output should be in 5 lines as
3
8
6
2
5

A

public static void parseInt(int x) {
String n = Integer.toString(x);
for(int i = 0; i < n.length(); i++) {
System.out.println(n.charAt(i));
}
}

32
Q

Write method public static long factorial(int n) that take a positive integer n as input and return the product of 12…*n

A

public static long factorial(int n) {
int product = 1;
for(int i = 2; i <= n; i++) {
product *= i;
}
return product;
}

33
Q

Write a method public static initializeArray(int[] arr) to initialize a given int array named arr with random numbers between 25 and 150, inclusive.

A

public static initializeArray(int[] arr) {
for(int i = 0; i < arr.length; i++) {
arr[i] = (int)(25 + Math.random() * 150);
}
}

34
Q

Write public static int[] sums(int[][] arr) that returns an integer array, in which ith element is the sum of ith row of table arr.

A

public static int[] sums(int[][] arr) {
int[] intArray = new int[arr.length];
for(int i = 0; i < arr.length; i++) {
int sum = 0;
for(int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
intArray[i] = sum;
}
return intArray;
}

35
Q

Write public static boolean zeroDiagonal(int[][] arr) which returns true if the diagonal elements in table arr are all zeros.

A

public static boolean zeroDiagonal(int[][] arr) {
for(int i = 0; i < arr.length; i++) {
if(arr[i][i] != 0) return false;
}
return true;
}

36
Q

Write method public static int linearSearch(int[] arr, int key) which returns the largest index such that arr[index] matches the key. If there is match, returns -1.

A

public static int linearSearch(int[] arr, int key) {
int bigIndex = -1;
for(int i = 0; i < arr.length; i++) {
if(arr[i] == key) bigIndex = i;
}
return bigIndex;
}

37
Q

Write a while (or for) statement to print all the digits of an int type variable x reversely, one digit per line. For example, if int x = 38625, then the output should be in 5 lines as
5
2
6
8
3

A

while(x > 0) {
System.out.println(x % 10);
x -= x % 10;
x /= 10;
}

38
Q

Write a while (or do-while loop) loop to get user’s input of test scores. Keep track the total score and number of scores entered. When user enter -1, the loop finishes and prints out the average score. Declare and initialize variables as needed.

A

Scanner input = new Scanner(System.in);
int userScore = 0;
int totalScore = 0;
int numberOfScores = 0;
while(userScore != -1) {
userScore = input.nextInt();
if(userScore == -1) break;
totalScore += userScore;
numberOfScores++;
}
double average = ((double)totalScore) / numberOfScores;
System.out.println(average);