0% found this document useful (0 votes)
11 views6 pages

Unit 4 Test

The document contains a test on iteration concepts in programming, featuring multiple-choice questions and a free response question. It includes code snippets that require understanding of loops, string manipulation, and conditionals. The free response section asks for a method to simulate rolling dice until a specific outcome is achieved.

Uploaded by

coconutboy303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Unit 4 Test

The document contains a test on iteration concepts in programming, featuring multiple-choice questions and a free response question. It includes code snippets that require understanding of loops, string manipulation, and conditionals. The free response section asks for a method to simulate rolling dice until a specific outcome is achieved.

Uploaded by

coconutboy303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 4: Iteration​ ​ ​ ​ ​ ​ ​ ​ Name: Adhit Natla

Test​

Multiple Choice

__D___ 1) Consider the following code:

int n = 80;
while (n > 40)
{
System.out.print(n % 10 + " ");
n -= 5;
}

What is output?

(A) 0 5 0 5 0 5 0
(B) 0 5 0 5 0 5 0 5
(C) 5 0 5 0 5 0 5 0
(D) 0 5 0 5 0 5 0 5 0
(E) 5 0 5 0 5 0 5

___C__ 2) What are the first and last values printed by:

int n = 5;
while (n < 32)
{
n += 5;
System.out.print(n + " ");
}

(A) 10 35
(B) 10 32
(C) 5 35
(D) 5 30
(E) 5 32
__C___ 3) What is output by the following code?

for (int i = 1; i < 4; i++)


{
for (int j = 0; j < i; j++)
{
System.out.print(i);
}
System.out.println();
}

(A)​ ​ ​ (B)​ ​ ​ (C)​ ​ ​ (D)​ ​ ​ (E)


0​ ​ ​ 11​ ​ ​ 1​ ​ ​ 1​ ​ ​ 0
01​ ​ ​ 222​ ​ ​ 2​ ​ ​ 22​ ​ ​ 11
012​ ​ ​ 3333​ ​ ​ 3​ ​ ​ 333​ ​ ​ 222

__A___ 4) Consider the following code:

String str = "HAMILTON";


int i = 0;
int j = str.length() - 1;
String result = "";
while (i < j)
{
result = str.substring(i, i + 1) + result + str.substring(j, j + 1);
i++;
j--;
}
System.out.println(result);

What is printed as a result?

(A) NOTLIMAH
(B) IMAHNOTL
(C) HNAOMTIL
(D) HNAOMT
(E) IMAOTL
___C__ 5) Assuming that str is a correctly initialized String (that does not start with a space), which of the
following best describes what the algorithm below does?

int index = str.indexOf(" ");


while (index != -1)
{
System.out.print(str.substring(index - 1, index));
str = str.substring(index + 1);
index = str.indexOf(" ");
}

(A) Prints characters from str in an infinite loop.


(B) Prints the number of times a space appears in str.
(C) Prints the character before each space in str.
(D) Prints the character after each space in str.
(E) Prints each character in str followed by a space.

__C___ 6) Which of the following code segments will print all the multiples of 2 between 1 and 50 inclusive?

I.
for (int k = 1; k <= 50; k++)
{
if ((k % 2) == 0)
{
System.out.println(k);
}
}

II.
for (int k = 2; k <= 50; k += 2)
{
System.out.println(k);
}

III.
for (int k = 1; 2 * k <= 50; k++)
{
System.out.println(2 * k);
}

(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I, II and III
__C___ 7) Consider the following code segment.

String str = "iteration";


String result = "";
for (int i = 0; i < str.length(); i++)
{
int index = (i + 2) % str.length();
result += str.substring(index, index + 1);
}

What is stored by the string result after this code segment has been executed?

(A) rationite
(B) teration
(C) eration
(D) erationit
(E) Nothing, an IndexOutOfBoundsException is thrown.

__B___ 8) Consider the following code segment.

int sum = 0;
for (int a = 1; a < 4; a++)
{
for (int b = 1; b <= a; b++)
{
sum += a;
}
}
System.out.println(sum);

What is printed as a result of executing the code segment?

(A) 8​ ​ (B) 6​ ​ (C) 30​ ​ (D) 10​ ​ (E) 14


__B___ 9) Given that n and count are both type int, which statement is true about the following code
segment?

I.
for(int count = 1; count <= n; count++){
System.out.println(count);
}

II. ​
int count = 1;
while(count <= n){
System.out.println(count);
​count++;
}

(A) I and II are exactly equivalent for all input values n.


(B) I and II are exactly equivalent for all input values n >= 1 , but differ when n <= 0
(C) I and II are exactly equivalent only when n = 0.
(D) I and II are exactly equivalent only when n is even.
(E) I and II are not equivalent for any input values of n.

___A__ 10) What is printed as a result of the following code?

int m = 30, n = 80;


while((m < 200) && (n > 20)){
m += 15;
n -= 10;
}
System.out.println(m + " and " + n);

(A) 120 and 20 ​


(B) 30 and 80 ​
(C) 60 and 20 ​
(D) 60 and 10 ​
(E) 45 and 20
Free Response

11) How many times do you have to roll a pair of dice before they come up snake eyes (roll a one on each die)?
Write a method called rollSnakeEyes() that simulates the experiment. The method has no parameters but
should return the number of rolls that it makes before the dice come up snake eyes. Use Math.random() to
simulate rolling a random die.

public static int rollSnakeEyes() {


int rolls = 0;
int die1, die2;

do {
die1 = (int)(Math.random() * 6) + 1;
die2 = (int)(Math.random() * 6) + 1;
rolls++;
} while (die1 != 1 || die2 != 1); // keep rolling until both are 1

return rolls;
}

You might also like