computer science chapter 6&7 test

Ace your homework & exams now with Quizwiz!

What is the output of the code snippet given below? int i = 0; while (i != 9) { System.out.print(i + " "); i = i + 2; }

0 2 4 6 8 10 12 14 .... (infinite loop)

What is the valid range of index values for an array of size 10?

0 to 9

What is the last output line of the code snippet given below? int i = 5; while (i >= 1) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "**"); num = num * 2; } System.out.println(); i = i - 1; }

1**

How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; }

10

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 following code fragment display "Hi"? int i = 10; while (i >= 0) { System.out.println("Hi"); i--; }

11 times

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 code snippet given below? String s = "12345"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i+=2; }

24

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

What is the output of the following loop? int s = 1; int n = 1; do { s = s + n; n++; } while (s < 4 * n); System.out.println(s);

37

What is the last output line of the code snippet given below? int i = 0; while (i < 10) { for (int j = i; j > 1; j--) { System.out.print(j + " "); } System.out.println("***"); i++; }

9 8 7 6 5 4 3 2 ***

Consider the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; Which statement sorts the data array in ascending order?

Arrays.sort(data);

Consider the following line of code: int[] somearray = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?

System.out.println(somearray[27]);

What is displayed after executing the given code snippet? int[] mymarks = new int[10]; int total = 0; Scanner in = new Scanner(System.in); for (int cnt = 1; cnt <= 10; cnt++) { System.out.print("Enter the marks: "); mymarks[cnt] = in.nextInt(); total = total + mymarks[cnt]; } System.out.println(total);

The code snippet causes a bounds error.

Which statement about this code snippet is accurate? int years = 50; double balance = 10000; double targetBalance = 20000; double rate = 3; for (int i = 1; i <= years; i++) { if (balance >= targetBalance) { i = years + 1; } else { double interest = balance * rate / 100; balance = balance + interest; } }

The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance.

How many times does the following loop execute? double d; Random generator = new Random(); double x = generator.nextDouble() * 100; do { d = Math.sqrt(x) * Math.sqrt(x) - x; System.out.println(d); x = generator.nextDouble() * 10001; } while (d != 0);

can't be determined

Which statement, inserted at the indicated blank line, will correctly terminate this loop when the end of input is reached (detected when the user enters the letter Q)? ArrayList<Double> data = new ArrayList<Double>(); boolean done = false; while (!done) { String input = in.next(); if (input.equalsIgnoreCase("Q")) { __________ } else { double x = Double.parseDouble(input); data.add(x); } }

done = true;

If currLength is an integer variable that gives the number of elements currently in the array myArray, which code snippet prints out the elements in the partially filled array of integers?

for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); }

Which of the following loops executes 8 times?

for(i = 0; i <= 7; i++)

What are the values of i and j after the following code fragment runs? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i=" + i + ", j=" + j);

i = 1951, j = 0

What are the values of i and j after the following code snippet is run? int i = 10; int j = 20; int count = 0; while (count < 5) { i = 2*i; i = i + 1; j = j - 1; count++; } System.out.println("i = " + i + ", j = " + j);

i = 351, j = 15

Which of the following code snippets displays the output exactly 10 times?

int i = 0; while (i < 10) { System.out.println("This is example 2."); i++; }

Identify the correct statement for defining an integer array named numarray of ten elements.

int[] numarray = new int[10];

Which one of the following statements is a valid initialization of an array named somearray of ten elements?

int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

In a partially filled array, the number of slots in the array that are not currently used is the

length of the array minus the number of elements currently in the array.

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

no output (infinite loop)

How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); }

only one time

When an array reading and storing input runs out of space

the array could be "grown" using the new command and the copyOf method.

What does the following loop compute? Scanner in = new Scanner(System.in); int sum = 0; int count = 0; while (in.hasNextInt()) { int value = in.nextInt(); if (value > 0) { sum += value; count++; } } double result = (sum * 1.0)/count;

the average of all the positive integers in the input

This code snippet is intended to calculate the balance of an account after twenty years of gaining interest. Select the statement that correctly completes the loop. int years = 20; double balance = 10000; while (years > 0) { __________ double interest = balance * rate / 100; balance = balance + interest; }

years--;


Related study sets

COMST 313 Exam 2: Contingency Theory

View Set

Psychology Chapter 7 Classical Conditioning

View Set

505-Chapter 11 & 12 Book quizzes

View Set

BCOR 199 Chapter 8. Strutting Organizations for Today's Challenges

View Set

which one of the following is not one of the four risk management principles

View Set

RN Mental Health Theories & Therapies Assessment - Mental Health

View Set

NJN History Topic 3 – Ancient Egypt and Kush Study Guide

View Set

Catcher in the Rye Chapters 22-26 Vocabulary

View Set