Vous êtes sur la page 1sur 3

AP Computer Science

while loop Worksheet


1. Given that n and count are both of type int, which statement is true about the following code segments?
I. for (count = 1; count <= n; count++)
System.out.println(count);
II. 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.
2. Consider this program segment:
int newNum = 0, temp;
int num = k;
//k is some predefined integer value 0
while (num > 10)
{
temp = num % 10;
num /= 10;
newNum = newNum * 10 + temp;
}
System.out.println(newNum);
Which is a true statement about the segment?
I. If 100 num 1000 initially, the final value of newNum must be in the range 10 newNum 100.
II. There is no initial value of num that will cause an infinite while loop.
III. If num 10 initially, newNum will have a final value of 0.
A.
B.
C.
D.
E.

I only
II only
III only
II and III only
I, II, and III

3. Consider the following methods:


public int fun1(int n)
{
int product = 1;
int k;
for(k = 2; k <= n; k++)
{
product *= k;
}
return product;
}

public int fun2(int n)


{
int product = 1;
int k = 2;
while(k <= n)
{
product *= k;
k++;
}
return product;
}
For which integer values of n do fun1(n) and fun2(n) return the same result?
A. Only n > 1
B. Only n < 1
C. Only n == 1
D. Only n >= 1

E. Any integer n
4. Consider the following code segment:
while (x > y)
{
x--;
y++;
}
System.out.println(x - y);
Assume that x and y are int variables and their values satisfy the conditions 0 x 2 and 0 y 2. Which of the following
describes the set of all possible outputs?
A. 0
B. -1, 1
C. -1, -2
D. 0, -1, -2
E. 0, -1, 1, -2, 2
5. Suppose the isPrime method is defined:
//returns true if p is a prime number, false otherwise
//precondition: p >= 2
public static boolean isPrime(int p)
{
< code not shown >
}
Given
int n = 101;
int sum = 0;
Which of the following code segments correctly computes the sum of all prime numbers from 2 to 101?
A. while (n != 2)
{
n--;
if (isPrime(n))
sum += n;
}
B. while (n >= 2)
{
n--;
if (isPrime(n))
sum += n;
}
C. while (n != 2)
{
if (isPrime(n))
sum += n;
n--;
}
D. while (n >= 2)
{
if (isPrime(n))
sum += n;
n--;
}
E. while (n >= 2 && isPrime(n))
{
sum += n;
n--;
}

6. Consider the following two code segments:


Segment I
int x = 0;
while (y > 0)
{
y--;
x++;
}
System.out.println("x = " + x);

Segment 2
int x;
for (x = 0; y > 0; y--)
{
x++;
}
System.out.println("x = " + x);

Assume that y is an initialized int variable. Under which of the following conditions will the output of the two code
segments be different?
A. The output will never be different.
B. The output will always be different.
C. The output will be different if and only if y is zero just before the code segment executes.
D. The output will be different if and only if y is greater than zero just before the code segment executes.
E. The output will be different if and only if y is less than zero just before the code segment executes.
7. Consider the following two methods:
public static void printStuff(int x)
{
int y = 1;
while (y < x)
{
System.out.print(y + " ");
y *= 2;
if (y == x / 2) return;
}
}
public static void mystery()
{
int x = 8;
while (x > 0)
{
printStuff(x);
x /= 2;
}
System.out.println("x=" + x);
}
What will be the output when method mystery is called?
A. 1 2 1 1 1 x=0
B. 1 2 1 1 x=0
C. 1 2 2 x=0
D. 1 2 4 x=0
E. 1 2 x=8

Vous aimerez peut-être aussi