Name: ________________________ Class: ___________________ Date: __________

MidTerm Review Chapter Math Multiple Choice Identify the choice that best completes the statement or answers the question. ____

1. What is wrong with the following “skeleton” of a Java program? public class MyClass { public static void main(String args[]) { } }

____

a. public and class on the first line should be interchanged b. MyClass name is wrong c. There should not be a [ ] after args d. Nothing is wrong e. None of these 2. Which line of code will cause Hello to be printed? a. b. c.

____

System.out.println(“Hello”); System.out.println(Hello); System.out.println(“Hello”)

d. e.

All of these None of these

3. What is actually printed with the following code?

System.out.print(“Fire”); System.out.println(“ Ants”); a. b. c. ____

d. e.

Pants on fire None of these

4. What is the syntax for indicating that a line of text is not Java code; rather, it is a remark? a. b. c.

____

FireAnts Fire Ants Fire Ants

rem \\ //

d. e.

comment None of these

5. What are the three variable types we have studied up to this point? a. b. c.

String, float, spinner integer, dblPrec, ace int, double, word

d. e.

1

String, int, double None of these

ID: A

Name: ________________________ ____

ID: A

6. Suppose you have the number 189.24. Which variable type would you use to store this number? a. b. c.

String int double

d. e.

tupperWare None of these

____

7. Which line of code declares k to be an integer? a. k int; d. k integer; b. int k; e. None of these c. integer k; 8. Which of the following is an illegal name for a variable?

____

Num d. flag_stuff, flag-stuff e. More than one of these 12flag 9. Which of the following is the least desirable way to name a variable?

____

a. b. c.

redcolor red_color redColor ____ 10. What is output by the following code? a. b. c.

d. e. f.

RedColor All are equally acceptable Both A and D

d. e.

9 None of these

String s = “Mona Lisa”; System.out.println(s.length( )); a. b. c.

7 illegal code 8

____ 11. What is output by the following code?

String girl = “Heather Jones”; System.out.println(girl.substring(8)); a. b. c.

Heather (one space after the ‘r’) er Jones Jones

d. e.

8Jones None of these

____ 12. What is output by the following code?

String girl = “Heather Jones”; System.out.println(girl.substring(8,11)); a. b. c.

Jon one ones

d. e.

2

ather Jones None of these

Name: ________________________

ID: A

____ 13. What is the index of the ‘L’ in the String “Abraham Lincoln”? a. b. c.

9 8 7

d. e.

0 None of these

____ 14. What is output by the following code?

String s = “Beaver Cleaver”; System.out.println(s.toUpperCase( )); a. b. c.

beaver cleaver BEAVER CLEAVER b

d. e.

eEAVER cLEAVER None of these

d. e.

3.1 None of these

d. e.

p+1 None of these

d. e.

j = j + x; None of these

____ 15. If int j = 3, what will be the value of j++? a. b. c.

4 3 2

____ 16. What is another way to write p = p –1; ? a. b. c.

p++ ++p 1-p

____ 17. Write x += j; another way. a. b. c.

x = x + j; x + j = x; x + x = j;

____ 18. Write code that is equivalent to saying the new value of w is the old value of w plus 6. a. b. c.

w = w + 6; w += 6 w++6

d. e.

Both A and B Both B and C

d. e.

x = ++9; More than one of these

d. e.

More than one of these None of these

____ 19. Which of the following is illegal? a. b. c.

x +=9; x = 9; 9 = x;

____ 20. Which of the following is illegal? a. b. c.

int i = 5/2; double d = 27; int i = 203.932;

3

Name: ________________________

ID: A

____ 21. The following code is illegal. Rewrite the code using an integer cast in the last line so as to make it

legal. double d = 187.2; int j = d; a. b. c.

int j = (cast)d; int j = int (d); int j = int d;

d. e.

int j = (int)d; None of these

d. e.

8.5 None of these

____ 22. What is the output of the following?

System.out.println( 27/5 + 3.1); a. b. c.

Illegal, won’t compile 27/5 + 3.1 8

____ 23. What is the output of the following?

System.out.println( (double)7/2 + 3.1); a. b. c.

6.6 6.1 6

d. e.

Illegal, won’t compile None of these

____ 24. Show how we would calculate and print the square root of 139.46 a. b. c.

Math.sqrt(139.46); println(sqrt(139.46)); System.out.println(Math.sqrt(139.46));

d. e.

(139.46)^(1/2); None of these

____ 25. Show how we would print the value of pi (3.14159...). a. b. c.

System.out.println(PI.math); System.out.println(“pi”); System.out.println(PI);

d. e.

System.out.println(Math.pi); System.out.println(Math.PI);

____ 26. What would the following print if d = 208.4?

System.out.println(Math.ceil(d)); a. b. c.

209.0 209 208.0

d. e.

208 None of these

____ 27. Which line of code will calculate (3.1)4.052 and store the result in double d. a. b. c.

double d = Math.pow(4.052, 3.1); double d = Math.pow(3.1, 4.052); double d = 3.1(pow(4.052));

d. e.

4

double d = 3.1^(4.052) None of these

Name: ________________________

ID: A

____ 28. What are the three data types that we are able to input from the keyboard? a. b. c.

int, data, server String, document, file picture, text, file

d. e.

String, int, double None of these

____ 29. Suppose a Scanner object, kbReader, has already been created. Which line of code uses kbReader to

input a number with “decimal places” from the keyboard and store the result in the variable, fract. a. b. c.

double fract = kbReader.nextDouble(); double fract = kbReader.next( ); double fract = kbReader.nextInt( );

d. e.

double fract = kbReader.nextDbl( ); None of these

____ 30. Suppose a Scanner object, kbReader, has already been created. Which line of code uses kbReader to

input a quantity like “Jo mamma” from the keyboard and store the result in the variable, name. a. b. c.

String name = kbReader.next( ); String name = kbReader.nextLine( ); String name = kbReader.nextString( );

d. e.

String name = kbReader.get( ); More than one of these

____ 31. Suppose a Scanner object, kbReader, has already been created. Which line of code uses kbReader to

input a number with “no decimal places” from the keyboard and store the result in the variable, count. a. b. c.

int count = kbReader.nextInt( ); int count = kbReader.nextInt; int count = kbReader.nextDouble( );

d. e.

int count = kbReader.next( ); More than one of these

____ 32. What are the two possible values for a boolean type variable? a. b. c.

yes, no true, false always, never

d. e.

nein, ya None of these

d. e.

This is a stupid question true

____ 33. If boolean p is false what is !p? a. b. c.

false Illegal Not enough information

____ 34. What is the operator used to indicate Boolean AND? a. b. c.

|| && &

d. e.

&| ^

____ 35. What is the operator used to indicate Boolean OR? a. b. c.

|| && &

d. e.

5

| ^

Name: ________________________

ID: A

____ 36. Which has higher precedence && or || ? a. b. c.

Depends on the context && ||

d. e.

Precedance is not assigned to these They are of equal precedance

____ 37. What is the output of the following code assuming that p is true and q is false?

boolean bb = !p || q; System.out.println(bb); a. b. c.

true false Illegal, won’t compile

d. e.

Need more information None of these

____ 38. Which is the basic skeleton of an if - else structure? a.

if-else {

d.

}

if( {

)

} { } else

b.

{if(

)

e.

} {else } c.

if( {

)

} else { }

6

None of these

Name: ________________________

ID: A

____ 39. In the following code, assume that the portion designated with is a true statement. What will be

the output? if( ) { System.out.print(“Elvis”); } System.out.println(“ Presley”); a. b. c.

Elvis ElvisPresley Elvis Presley

d. e.

Presley (has a leading space) None of these

____ 40. In the following code, assume that the portion designated with is a false statement. What will

be the output? if( ) { System.out.print(“Elvis”); } System.out.println(“ Presley”); a. b. c.

Elvis ElvisPresley Elvis Presley

d. e.

Presley (has a leading space) None of these

____ 41. If you wanted to compare the contents of two Strings, s1 and s2, which of the following would be

appropriate to use? s1.equals(s2) d. All of the preceding answers s2.equals(s1) e. s1 = = s2 s1.equalsIgnoreCase(s2) f. None of these ____ 42. In a switch statement ( switch(???) ) what are the two type of variables that may be used in the area designated by? a. b. c.

a. b. c.

char, int int, double char, double

d. e.

String, int char, String

____ 43. What is the purpose of default in a switch structure? a. b. c. d. e.

Provides for error control provide for anything not specifically covered by one of the cases Provides flow control default is not part of a switch structure None of these

7

Name: ________________________

ID: A

int p = ??; int x = 79; switch(p) { case 1: x = 20; break; case 2: x = 2; case 3: x = 3; default: x ++; } ____ 44. If p = 2 what is the final value of x in the code above? a. b. c.

1 2 3

d. e.

4 None of these

____ 45. If p = 1 what is the final value of x in the code above? a. b. c.

18 20 22

d. e.

80 3

int sum = 0; for(int j = 0; j < 3; j++) { sum = sum + 2; } System.out.println(sum); ____ 46. Identify the control expression of the for-loop in the code above. a. b. c.

int j = 0 j