Você está na página 1de 15

1. What is y after the following switch statement?

int x = 0; int y = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; default: y = -1 }


A) 1

B) -1
C) 0 D) None of the above 2. Which of the following is a constant, according to Java naming conventions?

A) MAX_VALUE
B) Test C) read D) ReadInt 3. Analyze the following code:

double sum = 0; for (double d = 0; d < 10; sum += sum + d) { d += 0.1; }

A) The program has a syntax error because the adjustment statement is incorrect in the for loop. B) The program has a syntax error because the control variable in the for loop cannot be of the double type. C) The program compiles but does not stop because d would always be less than 10.

D) The program compiles and runs fine.

4. Which array initialization is correct? A) int[ ] = new x int = {8, 7, 6, 5, 4};

B) int[ ] x = {8, 7, 6, 5, 4};


C) int x[4] = {8, 7, 6,}; D) const int SIZE = 4; Int x[SIZE]; E) const int SIZE = 4; int x[SIZE-4]; 5. What is k after invoking nPrint("a message", k+1)?

xMethod{ int k=2; nPrint("A message", k+1); } nPrint(String message, int n){ while( n>0){ System.out.print(message); n--; } }
A) -1 B) 0 C) 1

D) 2
6. Which of the following outputs values from a 2-D array?

A) for (row = 0; row < 5; row++){ for (col = 0; col < 4; col++){ JOptionPane.showMessageDialog(null, table[row] [column]); } }
B)

for (row = 5; row < 0; row++){ for (col = 0; col < 4; col++){ JOptionPane.showMessageDialog(null, table[row}[column]); }
C)

for (row = 0; row < 5; row++){ for (col = 0; col < 0; col++) JOptionPane.showMessageDialog(null, table[row}[column]); }
D)

for (row = 0; row < 5; row++) for (col = 0; col < 4; col++) JOptionPane.showMessageDialog(null, table[row}[column]);
7. Given the following statement:

int[ ] list = new int[10]; list.length has the value

A) 10
B) 9 C) The value depends on how many integers are stored in list. D) None of the above 8. To declare a constant PI, you write A) final static PI = 3.14159; B) final float PI = 3.14159; C) static double PI = 3.14159; D)

final double PI = 3.14159;

9. Which of the following is a possible output for 50 * Math.random()? A) 0 B) 50 C) 100

D) a and b.

10. Which one of the following assigns the index value to an array of 10 numbers

A) for (i=0;i<=9;i++) scores[i]=i;


B) for (i=0:i<9;i++) scores[i]=i-1; C) for (i=0;i<0;i++) scores[i]=i; D) for (i=0;i<9;++i) scores[i]=i; 11. Which of the following expression is equivalent to (x > 1)? A) x >= 1

B) !(x <= 1)
C) !(x = 1) D) !(x < 1) 12. Given the array declaration, int[] = new a int[20]; The first element is written as: A) a[1]

B) a[0]
C) a D) a[20] E) a[19] 13. Which one of the following assigns a value to the 4th element of an array of 10 integers A) score[4] = 10; B) score[2] = 10;

C) score[3] = 10;
D) score[2] = 10; 14. Given the array declaration, int[]= new a int[20]; The last (legal) element is written as: A) a[2] B) a[0]

C) a D) a[20]

E) a[19]

15. Which of the following is correct about arraycopy? A) names a copy of the array argument. B) refers to exactly the same array as the calling program

C) copies one array to another


D) refers to the array using a name that is always different from the calling program's argument. 16. Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]? A) 1 B) 2 C) 3

D) None of the above


17. Which of the following is not a valid boolean expression?

A) (1 < x < 100)


B) (x = 1) || (x != 1) C) (x>=90 ? "Yes" : "No") D) a, b, and c are all correct 18. Analyze the following code:

class Test { public static void main(String[] args) { System.out.println(xMethod((double)5)); } public static int xMethod(int n) { System.out.println("int"); return n; }

public static long xMethod(long n) { System.out.println("long"); return n; } }


A) The program displays int followed by 5. B) The program displays long followed by 5. C) The program runs fine but displays things other than given in a and b.

D) The program does not compile.


E) None of the above 19. A variable that is declared inside a method is called ________ variable. A) a static B) an instance

C) a local
D) a global E) a class 20. An array can be used in which of the following ways? A) As a local variable B) As a parameter of a method C) As a return value of a method

D) All of the above


21. Analyze the following two code fragments:

(i) int x =5; if ((0<x) && (x< 100)) System.out.println("x is between 1 and 100"); (ii) int x = 5; if (0 < x && x < 100) System.out.println("x is between 1 and 100");
A) The first fragment has a syntax error.

B) The second fragment has a syntax error.

C) Both fragments produce the same output.


D) none of the above

22. Subscripts are A) superscripts.

B) indices.
C) Titles. D) Array sizes. 23.

Given the following declaration: int[ ][ ] m = new int[5][6];


Which of the following statements is true?

A) The name m represents a two-dimensional array of 30 int values.


B) m[2][4] represents the element stored in the 2nd row and the 4th column of m. C) m.length has the value 6. D) m[0].length has the value 5. 24. Which of the following is a valid array declaration? A) char charArray = new char[26];

B) int[] words = new words[10];


C) char[] charArray = "Computer Science"; D) double[3] nums = {3.5, 35.1, 32.0}; 25.

Which one of the following outputs values from an array of 10 numbers

A) for (int i = 0; i < numbers.length; i++){ JOptionPane.showMessageDialog(null,"numbers["+ i + "]=",numbers[i]); }


B) for (int i = 0; i < numbers.length; i--) { JOptionPane.showMessageDialog(null,"numbers["+ i + "]=",numbers[i]); } C) for (int i = 0; i < numbers.length; --i) { JOptionPane.showMessageDialog(null,"numbers["+ i + "]=",numbers[i]); }

D) for (int i=0; i<=numbers.length; i++){ JOptionPane.showMessageDialog(null, "numbers["+i+"]=", numbers[i}); } 26.

Consider the following code fragment: int[] list = new int[10]; for ( int i = 0; i < list.length; i++) { list[i] = (int)(Math.random() * 10); }
Which of the following statements is true?

A) list.length must be replaced by 10

B) The loop body will execute 10 times, filling up the array with random numbers.
C) The loop body will execute 10 times, filling up the array with zeros. D) The code has a runtime error indicating that the array is out of bound. 27. Given the following statement:

int[ ] list = new int[10];

A) The array variable list contains a memory address that refers to an array of 10 int values.

B) The array variable list contains a memory address that refers to an array of 9 int values. C) The array variable list contains ten values of type int. D) The array variable list contains nine values of type int. 28.

static void nPrint(String message, int n) { while (n > 0) { System.out.print(message); n--; } } What is the printout of the call nPrint("a", 4)?

A) aaaaa

B) aaaa
C) aaa D) invalid call 29. Analyze the following code:

public static void main(String[] args) { for (int i = 1; i < 10; i++) { int k = 3; nPrint("A message", k); } System.out.println("k is " + k); }

A) The code has a syntax error because k is not defined for System.out.println("k is " + k).
B) The code prints k is 0. C) The code prints k is 1. D) The code prints k is 2. 30. Analyze the following code:

public class Test {

public static void main(String[] args) { int n = 2; xMethod(n); System.out.println("n is " + n); } void xMethod(int n) { n++; } }
A) The code has a syntax error because xMethod does not return a value.

B) The code has a syntax error because xMethod is not declared static.
C) The code prints n is 1. D) The code prints n is 2.

31. Which of the following is not an advantage of using methods?

A) Using methods makes program run faster.


B) Using methods makes reusing code easier. C) Using methods makes programs easier to read. D) Using methods hides detailed implementation from the clients. 32. Analyze the following code:

int x = 0; int y = ((x<100) & (x>0)) ? 1: -1;


A) The code has a syntax error because & must be &&. B) y becomes 1 after the code is executed.

C) y becomes -1 after the code is executed.


D) None of the above. 33.

Analyze the following fragment:

double x = 0; double d = 1; switch (d + 4) { case 5: x++; case 6: --x; }


A) The code is incorrect because --x will produce a negative number. B) The code is incorrect because there are no cases for numbers other than 5 and 6. C) The switch control variable cannot contain an operation.

D) The switch control variable cannot be double.


34. Analyze the following code:

int x = 0; if (x > 0); { System.out.println("x"); }

A) The symbol x is always printed.


B) The value of variable x is always printed. C) Nothing is printed because x > 0 is false. D) None of the above. 35. Which of the loop statements always have their body executed at least once? A) The while loop

B) The do-while loop


C) The for loop D) None of the above 36. Analyze the following code:

int x = 1; while (0<x) && (x< 100)

System.out.println(x++);
A) The loop runs for ever. B) The code does not compile because the loop body is not in the braces.

C) The code does not compile because (0 < x) & (x < 100) is not enclosed in a pair of parentheses.
D) The number 1 to 99 are displayed. 37. What is y after the following for loop statement is executed?

int y = 0; for (int i = 0; i < 10; ++i) { y+= 1; }


A) 9

B) 10
C) 11 D) 12

38. What is balance after the following code is executed?

int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; }
A) -1 B) 0 C) 1

D) The loop does not end


39.

What is the value of balance after the following code is executed?

int balance = 10; while (balance >= 1) { if (balance < 9) break; balance = balance - 9; }
A) -1 B) 0

C) 1
D) 2 40. What is x after evaluating?

x = (2 > 3) ? 2 : 3;
A) 2

B) 3
C) 4 D) None of the above

41. To add a to b and store result in b, you write (Note: Java is case-sensitive)

A) b += a;
B) a = b + a; C) b = A + b; D) a += b; 42. To declare an int variable x with initial value 200, you write A) int x = 200L; B) int x = 200l;

C) int x = 200;
D) int x = 200.0; 43.

To assign a double variable d to an int variable x, you write A) x = (long)d

B) x = (int)d;
C) x = d; D) x = (float)d; 44. Which of the boolean expressions below has incorrect logic? A) (true) && (3 > 4) B) !(x > 0) && (x > 0) C) (x > 0) || (x < 0)

D) (x != 0) || (x = 0)
45. Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative? A) 1 < x < 100 && x < 0

B) ((x < 100) && (x > 1)) || (x < 0)


C) ((x < 100) && (x > 1)) && (x < 0) D) (1 > x > 100) || (x < 0)

46. Which of the following shows the char value a?

A) 'a'
B) "a" C) '\000a' D) None of the above. 47. Which of the following assignment statements is illegal? A) float f = 34.0f; B) int t = 23;

C) int y = 14.4;

D) float f = -34f; 48. A Java statement ends with a __________. A) comma (,)

B) semicolon (;)
C) period (.) D) closing brace 49. The assignment operator in Java is __________. A) :=

B) =
C) = = D) <50. In Java array indices, that is subscript values, must be A) An integer type B) A negative number C) Greater than zero D) Less than or equal to the declared size of the array

E) None of these is correct

Você também pode gostar