2010-03-02 054850 Java Questions
2010-03-02 054850 Java Questions
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:
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.
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:
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)
D) a and b.
10. Which one of the following assigns the index value to an array of 10 numbers
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
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; }
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
(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) indices.
C) Titles. D) Array sizes. 23.
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?
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:
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 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.
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?
B) 10
C) 11 D) 12
int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; }
A) -1 B) 0 C) 1
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.
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
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