cs030 exam 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output of the following code snippet? int i = 1; while (i <= 10) { System.out.println("Inside the while loop"); i = i + 10; }

"Inside the while loop" will be displayed only once.

What is the output of the code below? for (int val = 0; val < 4; val ++) { int sum = val; for (int num = 0; num < val; num++) { sum = sum + num; } System.out.print(sum + " "); }

0 1 3 6

What will be printed by the statements below? for (int ctr = 10; ctr > 5; ctr--) { System.out.print(ctr + " "); }

10 9 8 7 6

How many times does the code snippet below display "Hello"? int i = 0; while (i != 15) { System.out.println("Hello"); i++; }

15 times

What is the output of the following code? double x = 1; double y = 1; int i = 0; do { y = x / 2; x = x + y; i = i + 1; } while (x < 2.5); System.out.print(i + " ");

3

What is the output of the following code snippet? int[] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.print(myarray[3]);

3040

Consider the following code snippet: int count = 0;int[][] numarray = new int[2][3];for (int i = 0; i < 3; i++){for (int j = 0; j < 2; j++){numarray[j][i] = count;count++;}} What is the value of numarray[1][2] after the code snippet is executed?

5

What is the output of the following code snippet? public static int check(ArrayList<Integer> listData) { int sum = 0; for (int i = 0; i < listData.size(); i++) { sum = sum + listData.get(i); } return sum; } public static void main(String[] args) { ArrayList<Integer> vdata = new ArrayList<Integer>(); int rsum; for (int cnt = 0; cnt < 3; cnt++) { vdata.add(cnt + 1); } rsum = check(vdata); System.out.println(rsum); }

6

What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty());

6 true 1 false

What will be printed by the statements below? int[] values = { 4, 5, 6, 7}; values[0] = values[3]; values[3] = values[0]; for (int i = 0; i < values.length; i++) { System.out.print (values[i] + " "); }

7 5 6 7

Which one of the following statements about declaring an array list as a method parameter is true?

An array list value can be modified inside the method.

Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum?

ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } }

What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size());

Error because num is not initialized

What will be the output of the following code snippet? boolean token1 = true; while (token1) { for (int i = 0; i < 10; i++) { System.out.println("Hello"); } token1 = false; }

Hello will be displayed 10 times.

What is the outcome of the following code snippet? boolean val1 = true; boolean val2 = false; while (val1) { if (val1) { System.out.println("Hello"); } val1 = val2; }

Hello will be displayed only once.

What will be the output of the following code snippet? boolean token = false; do { System.out.println("Hello"); } while (token);

Hello will be displayed only once.

Which statements about array algorithms are true? I. The array algorithms are building blocks for many programs that process arrays. II. Java contains ready-made array algorithms for every problem situation. III. It is inefficient to make multiple passes through an array if you can do everything in one pass.

I, III only

Which of the following should be used when you need to ask a user to enter one data item and repeat the prompt if the data is invalid? I. the for loop II. the while loop III. the do loop

III only

What does the following code do? int sum = 0; final double count = 1000; Random generator = new Random(); for (int i = 1; i <= count; i++) { sum = sum + generator.nextInt(101); } System.out.println(sum / count);

It calculates the average of 1000 random numbers between 0 and 100.

Which of the following statements is true about the variable sum in the code snippet below? int i = 0; while (i != 11) { System.out.print(" " + i); int sum = i; i++; sum++; }

It disappears after the loop is done executing.

Assume the method doSomething has been defined as follows: public static void doSomething (int[] values, int p1, int p2) { int temp = values[p1]; values[p1] = values[p2]; values[p2] = values[p1]; } What does the method do?

It overwrites the value stored in the integer array values at position p1 with the value stored in position p2.

Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities? (Assume Random generator = new Random();.)

System.out.println(generator.nextInt(2)+ " " + generator.nextInt(2));

What will be the result of running the following code fragment? int year = 0; double rate = 5; double principal = 10000; double interest = 0; while (year < 10) { interest = (principal * year * rate) / 100; System.out.println("Interest " + interest); }

The code fragment will continue to display the calculated interest forever because the loop will never end.

Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } } Which one of the following descriptions is correct for the given code snippet?

The code snippet compares the values of two array lists and stores the count of total matches found.

Which one of the following statements is correct about the given code snippet? int[] somearray = new int[6]; for (int i = 1; i < 6; i++) { somearray[i] = i + 1; }

The for loop initializes all the elements except the first element.

What is the result of the following definition of an array list? ArrayList<Double> points;

The statement defines an array list reference but does not create an array list.

What (if anything) is wrong with the following code snippet, which searches for the searchedValue variable in the array data? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); }

There is a logic error.

Which statement about using a debugger is correct?

When the program terminates, the debugger stops as well.

What is the name of the special program that lets you find bugs in your code by following its execution?

debugger

What is the data type of the number generated by the Random.nextDouble() method?

double

Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the expressions simulates that random number? (Assume Random generator = new Random();.)

generator.nextDouble() * 1000000

Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList?

int max = aList.get(0);for (int count = 1; count < aList.size(); count++){if (aList.get(count) > max){max = aList.get(count);}}

Which code snippet produces the sum of the first n even numbers?

int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i * 2; }

Which one of the following statements is the correct declaration for a two-dimensional array of 20 rows and 2 columns of type int?

int[][] num = new int[20][2];

What is the output of the code snippet given below? String s = "abcde"; int i = 1; do { if (i > 1) { System.out.print(s.substring(i, i + 1)); } } while (i < 5);

no output (infinite loop)

Suppose you wish to write a method that returns the sum of the elements in partially filled array. Which is the best choice for a method header?

public int sum(int[] values, int currSize)

Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 && num1.size() > 3) { num1.remove(num1.size() - 1); } } System.out.println("size is : " + num1.size()); What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?

size is : 4

Assume the array of integers values has been created. Which condition must be used in the indicated area so the loop below will assign max the largest value in values? int max = values[0]; for (int current = 1; current < values.length; current++) { if (/* Put condition here */) max = values[current]; }

values[current] > max

In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop? int i; int j; for (i = 0; i <= 9; i++) { for (j = 1; j < 5; j++) { System.out.println("Hello"); } }

when the value of j becomes 5

The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list?

x = a.get(4);


संबंधित स्टडी सेट्स

PEDS CHAPTER 28 (PREP U LEVEL 8)

View Set

ECON MID TERM HW QUESTIONS 4(part 2)

View Set

Word Choice and Author's Purpose in Warriors Don't Cry

View Set

Acct 201: Chapter 12. The statement of cash flows

View Set

American Government Chapter 4 - Civil Liberties

View Set