Download Practice Test Benchmark FINAL ANSWER KEY

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Practice - Week 7-9 Test For & While Loops / Benchmark
Name_______________________
1. Refer to the following code fragment:
double answer = 13 / 5;
System.out.println(“13/5 = “ + answer);
The output is
13 / 5 = 2.0
The programmer intends the output to be
13 / 5 = 2.6
Which of the following replacements for the first line of code will NOT fix the problem?
a. double answer = (double) (13 / 5);
b. double answer = 13 / 5.0;
c. double answer = (double) 13 / 5;
d. double answer = 13 / (double) 5;
e. double answer = 13.0 / 5;
2. Let x be a variable of type double that is positive. A program contains the boolean expression
(Math.pow(x,0.5) == Math.sqrt(x)). Even though
is mathematically equivalent to
the above expression returns the value false. Which of the following is the most likely reason?
a. The computer stores floating-point numbers with 32-bit words.
b. x was imprecisely calculated in a previous program statement.
c. There is an overflow error in calculating the pow functions.
d. Math.pow returns an int, while Math.sqrt returns a double.
e. There is a round-off error in calculating the pow and sqrt functions.
3. What output will be produced by
System.out.print(“\\*This is not\n a comment *\\”);
a. \*This is not
a comment *\
b. *This is not
a comment *
c. *This is not a comment *
d. \\*This is not
a comment *\\
e. \*This is not a comment *\
4. Which of the following will evaluate to true only if boolean expressions A, B and C are all false?
a. !A && !(B && !C)
b. !(A && B && C)
c. !A || !(B || !C)
d. !A || !B || !C
e. !(A || B || C)
5. Assume that a and b are integers. The boolean expression
!(a <= b) && (a * b > 0)
will always evaluate to true given that
a. a = b b. a > b, and b < 0 c. a < b d. a > b, and b > 0 e. a > b
6. Consider the following method:
public void process(String s)
{
s = s.substring(2,3) + s.substring(1,2) + s.substring(0,1);
}
What is the result of executing the following statements?
String s = “ABCD”;
process(s);
System.out.println(s);
a. CDBCA
b. CBA
c. error
d. CDBCAB
e. ABCD
,
7. For which of the following pairs of values a and b does the expression below evaluate to true?
(a > 20 && a < b) || (a > 10 && a > b)
a. None
b. 15 and 20
c. 5 and 0
d. 15 and 10
e. 5 and 10
8. What output will be produced by this code segment?
for (int i = 5; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
System.out.print(2 * j - 1);
System.out.println();
}
a. 97531
b. 1
c. 97531
d. 13579
e.
9753
13
7531-1
1357
975
135
531-1-3
135
97
1357
31-1-3-5-7
13
9
13579
1-1-3-5-7
1
9. What values are stored in x and y after execution of the following program segment?
int x = 30, y = 40;
if (x >= 0)
{
if ( x <= 100)
{
y = x * 3;
if (y < 50)
x /= 10;
}
else
y = x * 2;
}
else
y = -x;
97531
7531
531
31
1
a. x = 30
b. x = 30
c. x = 30
d. x = 3
e. x = 30
y = -30
y = 90
y = 60
y = -3
y = 40
The following two questions #10-11 refer to the method checkNumber, which checks the validity of its
four-digit integer parameter.
/** @param n a 4-digit integer
* @return true if n is valid, false if otherwise
*/
public boolean checkNumber(int n)
{
int d1,d2,d3,checkDigit,nRemaining,rem;
//strip off digits
checkDigit = n % 10;
nRemaining = n / 10;
d3 = nRemaining % 10;
nRemaining /= 10;
d2 = nRemaining % 10;
nRemaining /= 10;
d1 = nRemaining % 10;
//check validity
rem = (d1 + d2 + d3) % 7;
return rem == checkDigit;
}
A program invokes method checkNumber with the statement
boolean valid = checkNumber(num);
10. What is the purpose of the local variable nRemaining?
a. On exiting, the method, the value of nRemaining may be reused.
b. nRemaining is needed as the left-hand side operand for integer division.
c. nRemaining prevents the parameter num from being altered.
d. nRemaining enhances the readability of the algorithm.
e. It is not possible to separate n into digits without the help of a temporary variable.
11. Which of the following values of num will result in valid having a value of true?
a. 6143
b. 6144
c. 6147
d. 6146
e. 6145
12. 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
a.
b.
c.
d.
e.
count = 1
while (count <= n)
{
System.out.println(count);
count++;
}
I and II are exactly equivalent for all input values n.
I and II are exactly equivalent only when is even.
I and II are exactly equivalent only when
.
I and II are not equivalent for any input values of n.
I and II are exactly equivalent for all input values
, but differ when
.
13. Which of the following statements about developing a Java program is false?
a. Hello.class, a Java class file obtained by compiling Hello.java under a Windows operating
system can be executed on a a Mac computer.
b. The main purpose of testing a Java program is to make sure it doesn’t generate run-time exceptions.
c. Division of an integer value by zero in a Java program will cause a run-time
ArithmeticException.
d. The size of an int variable in Java is four bytes, the same under any operating system or model.
e. At run time, int values are represented in the computer memory in the binary number system.
14. 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
initially, the final value of newNum must be in the range
II There is no initial value of num that will cause an infinite while loop.
III If
initially, newNum will have a final value of 0.
a. II only
b. I, II and III
c. I only
d. II and III
e. III only
.
15. The following fragment intends that a user will enter a list of positive integers at the keyboard and terminate
the list with a sentinel:
int value = 0;
final int SENTINEL = -999;
while (value != SENTINEL)
{
//code to process value
...
value = scanner.nextInt();
}
The fragment is not correct. Which is a true statement?
a. A poor choice of SENTINEL value causes the loop to terminate before all values have
been processed.
b. The last nonsentinel value entered in the list fails to get processed.
c. Entering the SENTINEL value as the first value causes a run-time error.
d. The sentinel gets processed.
e. The code will always process a value that is not passed in by the Scanner class.
16. What is true of the following boolean expression, given that x is a variable of type double?
3.0 == x * (3.0 / x)
a. It will always evaluate to false.
b. It may evaluate to false for some values of x.
c. It will always evaluate to true.
d. It will evaluate to false only when x is very large or very close to zero.
e. It will evaluate to false only when x is zero.
17. Which of the following methods are equivalent (always return the same value for the same input parameters)?
I. public boolean fun(int a, int b, int c)
{
if (a >= b)
if (b >= c)
return true;
else
return false;
else
return false;
}
II. public boolean fun(int a, int b, int c)
{
if (a >= b && b >= c)
return true;
else
return false;
}
III. public boolean fun(int a, int b, int c)
{
return a >= b || b >= c;
}
a. I and III
b. II and III
c. I and II
18. In hexadecimal, a color is represented in the format
the red, green and blue. Using this notation, the color
Which of the following hex codes represents the color
a.
b.
c.
d. None
d.
e. I, II and III
, where
,
, and
are hex values for
would be coded as
.
?
e.
19. What is printed as a result of the following statements?
double x = 2.5, y = 1.99;
System.out.println((int)(x/y) + (int)(x*y));
a. 0
b. 3
c. 5
d. 4.0
e. 4
20. Consider this code segment:
int x = 10, y = 0;
while (x > 5)
{
y = 3;
while (y < x)
{
y *= 2;
if (y % x == 1)
y += x;
}
x -= 3;
}
System.out.println(x + “ “ + y);
What will be the output after execution of this code segment?
a. -3 12
b. 1 6
c. 7 12
d. -3 6
e. 4 12
21. In the following code segment, you may assume that a, b, and n are all type int.
if (a != b && n / (a - b) > 90)
{
/* statement1 */
}
else
{
/* statement2 */
}
/* statement3 */
What will happen if a == b is false?
a. A compile-time error will occur.
b. /* statement2 */ will be executed.
c. /* statement1 */ will be executed.
d. Either /* statement1 */ or /* statement2 */ will be executed.
e. An exception will be thrown.
22. Suppose that base-2 (binary) numbers and base-16 (hexadecimal) numbers can be denoted with subscripts, as
shown below:
Which is equal to
a.
b.
?
c.
d.
e.
23. Consider the following code segment
if (n != 0 && x / n > 100)
statement1;
else
statement2;
If n is of type int and has a value of 0 when the segment is executed, what will happen?
a. An ArithmeticException will be thrown.
b. statement1, but not statement2, will be executed.
c. A syntax error will occur.
d. Neither statement will be executed; control will pass to the first statement below the if-else
block.
e. statement2, but not statement1, will be executed.
24. In Java, a variable of type int is represented internally as a 32-bit signed integer. Suppose that one bit stores
the sign, and the other 31 bits store the magnitude of the number in base 2. In this scheme, what is the largest
value that can be stored as type int?
a.
b.
c.
d.
e.
25. Suppose each pixel in a digital image is represented by a 24-bit color value. How much memory does it take
to store an uncompressed image of 2048 pixels by 1024 pixels?
a. 2 MB
b. 6 GB
c. 2 KB
d. 32 TB
e. 6 MB
26. What is printed when the following statement is executed?
System.out.println(17 / 5 % 3 + 17 * 5 % 3);
a. 9
b. 42.5
c. 42
d. 4
e. 1
27. Given that a, b, and c are integers, consider the boolean expression
(a < b) || !((c == a * b) && (c < a))
Which of the following will guarantee that the expression is true?
a. c < a is true
b. c == a * b is true and c < a is true
c. c == a * b is true
d. c < a is false
e. a < b is false
28. What will the output be for the following poorly formatted program segment, if the value read from the
Scanner object for num is 22?
int num = scanner.nextInt();
if (num > 0)
if (num % 5 == 0)
System.out.println(num);
else System.outprintln(num + “ is a negative”);
a. 2 is negative
b. 4
c. No output
d. 22
e. 22 is negative
29. What is the result when the following code segment is compiled and executed?
int m = 4, n = 5;
double d = Math.sqrt((m + n)/2);
System.out.println(d);
a.
b.
c.
d.
e.
Syntax error “sqrt(double) in java.lang.Math cannot be applied to int”
2.1213203435596424 is displayed
2.0 is displayed
ArithmeticException
1.5 is displayed
30. Which of the following pairs of declarations will cause an error message?
I
double x = 14.7;
int y = x;
II
double x = 14.7;
int y = (int) x;
III int x = 14;
double y = x;
a. I and III
b. II only
c. None
d. III only
e. I only
31. Which of the following program fragments will produce this output?
I
II
III
for (int i = 1; i <= 6; i++)
{
for (int k = 1; k <= 6; k++)
if (k == i)
System.out.print(2 * k);
else
System.out.print(“-“);
System.out.println();
}
for (int i = 1; i <= 6; i++)
{
for (int k = 1; k <= i-1; k++)
System.out.print(“-“);
System.out.print(2 * i);
for (int k = 1; k <= 6-i; k++)
System.out.print(“-“);
System.out.println();
}
for (int i = 1; i <= 6; i++)
{
for (int k = 1; k <= i-1; k++)
System.out.print(“-“);
System.out.print(2 * i);
for (int k = i+1; k <= 6; k++)
System.out.print(“-“);
System.out.println();
}
a. I, II and III
b. I only
c. II only
d. I and II
32. What value is stored in result if
int result = 13 - 3 * 6 / 4 % 3;
a. 13
b. 12
c. 0
d. -1
2-----4-----6-----8-----10-----12
e. III only
e. -5
33. Consider the method reverse:
/** Precondition: n > 0.
* Postcondition:
*
- Returns n with its digits reversed.
*
- Example: If n = 234, method reverse returns 432.
* @param n a positive integer
* @return n with its digits reversed
*/
public int reverse(int n)
{
int rem, revNum = 0;
/* code segment */
return revNum;
}
Which of the following replacements for /* code segment */ would cause the method to work as intended?
I
for (int i = 0; i <= n; i++)
{
rem = n % 10;
revNum = revNum * 10 + rem;
n /= 10;
}
II
while (n != 0)
{
rem = n % 10;
revNum = revNum * 10 + rem;
n /= 10;
}
III
for (int i = n; i != 0; i /= 10)
{
rem = i % 10;
revNum = revNum * 10 + rem;
}
a. I only
b. I and II
c. II and III
d. II only
e. I and III
34. Which of the following expressions is true if and only if all three variables a, b, and c do NOT have the
same value?
a. a >= b && b >= c && c >= a
b. a != b || b != c
c. a > b || b > c || c > a
d. a != b && b != c
e. !(a == b || b == c || a == c)
Short Answer
For the following problems, write a method that prints the given star pattern. Your method should not return any values,
and should be named star1, star2, star3, and star4, one for each problem. Your code may require several loops.
1. **********
*********
********
*******
******
*****
****
***
**
*
2.
*
**
***
****
*****
******
*******
********
*********
**********
3.
*
***
*****
*******
*********
*********
*******
*****
***
*
4. **********
*********
********
*******
******
*****
****
***
**
*
Practice - Week 7-9 Test For Loops and While Loops / Benchmark
Answer Section
MULTIPLE CHOICE
1. ANS: A
2. ANS: E
3. ANS: A
4. ANS: E
5. ANS: D
6. ANS: E
7. ANS: D
8. ANS: E
9. ANS: B
10. ANS: D
11. ANS: B
12. ANS: A
13. ANS: B
14. ANS: D
15. ANS: E
16. ANS: B
17. ANS: C
18. ANS: E
19. ANS: C
20. ANS: A
21. ANS: D
22. ANS: A
23. ANS: E
24. ANS: E
25. ANS: E
26. ANS: E
27. ANS: D
28. ANS: E
29. ANS: C
30. ANS: E
31. ANS: A
32. ANS: B
33. ANS: C
34. ANS: E
SHORT ANSWER
1. ANS:
ha
PTS: 1
2. ANS:
ha
PTS: 1
3. ANS:
ha
PTS: 1
4. ANS:
ha
PTS: 1
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
PTS:
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1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