44
Questions on Language Fundamentals 1. Which of these are legal identifiers? Select the three correct answers. A. number_1 B. number_a C. $1234 D. -volatile 2. Which of these are not legal identifiers? Select the four correct answers. A. 1alpha B. _abcd C. xy+abc D. transient E. account-num F. very_long_name 3. Which of the following are keywords in Java. Select the two correct answers. A. friend B. NULL C. implement D. synchronized E. throws 4. Which of the following are Java keywords. Select the four correct answers. A. super B. strictfp C. void D. synchronize E. instanceof 5. Which of these are Java keywords. Select the five correct answers A. TRUE B. volatile C. transient D. native E. interface F. then G. new

Questions on javascjp

Embed Size (px)

Citation preview

Page 1: Questions on javascjp

Questions on Language Fundamentals1. Which of these are legal identifiers? Select the three correct answers.

A. number_1 B. number_a C. $1234 D. -volatile

2. Which of these are not legal identifiers? Select the four correct answers. A. 1alpha B. _abcd C. xy+abc D. transient E. account-num F. very_long_name

3. Which of the following are keywords in Java. Select the two correct answers. A. friend B. NULL C. implement D. synchronized E. throws

4. Which of the following are Java keywords. Select the four correct answers. A. super B. strictfp C. void D. synchronize E. instanceof

5. Which of these are Java keywords. Select the five correct answers A. TRUE B. volatile C. transient D. native E. interface F. then G. new

6. Using up to four characters, write the Java representation of octal literal 6. 7. Using up to four characters, write the Java representation of integer literal 3 in

hexadecimal. 8. Using up to four characters, write the Java representation of integer literal 10 in

hexadecimal. 9. What is the minimum value of char type. Select the one correct answer.

A. 0 B. -215 C. -28 D. -215 - 1 E. -216 F. -216 - 1

Page 2: Questions on javascjp

10. How many bytes are used to represent the primitive data type int in Java. Select the one correct answer.

A. 2 B. 4 C. 8 D. 1 E. The number of bytes to represent an int is compiler dependent.

11. What is the legal range of values for a variable declared as a byte. Select the one correct answer.

A. 0 to 256 B. 0 to 255 C. -128 to 127 D. -128 to 128 E. -127 to 128 F. -215 to 215 - 1

12. The width in bits of double primitive type in Java is --. Select the one correct answer.

A. The width of double is platform dependent B. 64 C. 128 D. 8 E. 4

13.What would happen when the following is compiled and executed. Select the one correct answer.

13.14. public class Compare { 15. public static void main(String args[]) {16. int x = 10, y;17. if(x < 10) 18. y = 1;19. if(x>= 10) y = 2;20. System.out.println("y is " + y);21. }22. }23.

A. The program compiles and prints y is 0 when executed. B. The program compiles and prints y is 1 when executed. C. The program compiles and prints y is 2 when executed. D. The program does not compile complaining about y not being initialized. E. The program throws a runtime exception.

14.What would happen when the following is compiled and executed. Select the one correct answer.

Page 3: Questions on javascjp

24. class example {25. int x;26. int y;27. String name;28. public static void main(String args[]) {29. example pnt = new example();30. System.out.println("pnt is " + pnt.name + 31. " " + pnt.x + " " + pnt.y);32. }33. }34.

A. The program does not compile because x, y and name are not initialized. B. The program throws a runtime exception as x, y, and name are used before

initialization. C. The program prints pnt is 0 0. D. The program prints pnt is null 0 0. E. The program prints pnt is NULL false false

35. The initial value of an instance variable of type String that is not explicitly initialized in the program is --. Select the one correct answer.

A. null B. "" C. NULL D. 0 E. The instance variable must be explicitly assigned.

36. The initial value of a local variable of type String that is not explicitly initialized and which is defined in a member function of a class. Select the one correct answer.

A. null B. "" C. NULL D. 0 E. The local variable must be explicitly assigned.

37. Which of the following are legal Java programs. Select the four correct answers. A. // The comments come before the package

package pkg;import java.awt.*;class C{}

B. package pkg;import java.awt.*;class C{}

C. package pkg1;package pkg2;import java.awt.*;class C{}

D. package pkg;import java.awt.*;

E. import java.awt.*;class C{}

Page 4: Questions on javascjp

F. import java.awt.*;package pkg;class C {}

38. Which of the following statements are correct. Select the four correct answers. A. A Java program must have a package statement. B. A package statement if present must be the first statement of the program

(barring any comments). C. If a Java program defines both a package and import statement, then the

import statement must come before the package statement. D. An empty file is a valid source file. E. A Java file without any class or interface definitions can also be compiled. F. If an import statement is present, it must appear before any class or

interface definitions.

What would be the results of compiling and running the following class. Select the one correct answer.

39.40. class test {41. public static void main() {42. System.out.println("test");43. }44. }45.

A. The program does not compile as there is no main method defined. B. The program compiles and runs generating an output of "test" C. The program compiles and runs but does not generate any output. D. The program compiles but does not run.

46. Which of these are valid declarations for the main method? Select the one correct answer.

A. public void main(); B. public static void main(String args[]); C. static public void main(String); D. public static void main(String ); E. public static int main(String args[]);

47. Which of the following are valid declarations for the main method. Select the three correct answers.

A. public static void main(String args[]); B. public static void main(String []args); C. final static public void main (String args[]); D. public static int main(String args[]); E. public static abstract void main(String args[]);

48. What happens when the following program is compiled and executed with the command - java test. Select the one correct answer.

49.50. class test {51. public static void main(String args[]) {52. if(args.length > 0)53. System.out.println(args.length);

Page 5: Questions on javascjp

54. }55. }56.

A. The program compiles and runs but does not print anything. B. The program compiles and runs and prints 0 C. The program compiles and runs and prints 1 D. The program compiles and runs and prints 2 E. The program does not compile.

57. What is the result of compiling and running this program? Select the one correct answer.

58.59. public class test {60. public static void main(String args[]) {61. int i, j;62. int k = 0;63. j = 2;64. k = j = i = 1;65. System.out.println(k);66. }67. }68.69.

A. The program does not compile as k is being read without being initialized. B. The program does not compile because of the statement k = j = i = 1; C. The program compiles and runs printing 0. D. The program compiles and runs printing 1. E. The program compiles and runs printing 2.

70. What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.

71.

72. public class test {73. public static void main(String args[]) {74. System.out.println(args[0]+" "+args[args.length-1]);75. }76. }77.

A. The program will throw an ArrayIndexOutOfBounds exception. B. The program will print "java test" C. The program will print "java happens"; D. The program will print "test happens" E. The program will print "lets happens"

78. What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.

79.80. public class test {81. public static void main(String args[]) {82. System.out.println(args[0]+" "+args[args.length]);83. }

Page 6: Questions on javascjp

84. }85.

A. The program will throw an ArrayIndexOutOfBounds exception. B. The program will print "java test" C. The program will print "java happens"; D. The program will print "test happens" E. The program will print "lets happens"

86. What all gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the two correct answers.

87.88. public class test {89. public static void main(String args[]) {90. System.out.println(args[0]+" "+args.length);91. }92. }93.

A. java B. test C. lets D. 3 1E. 4 F. 5 G. 6

94. What happens when the following program is compiled and run. Select the one correct answer.

95.96. public class example {97. int i = 0;98. public static void main(String args[]) {99. int i = 1;100. i = change_i(i);101. System.out.println(i);102. }103. public static int change_i(int i) {104. i = 2;105. i *= 2;106. return i;107. }108. }109.

A. The program does not compile. B. The program prints 0. C. The program prints 1. D. The program prints 2. E. The program prints 4.

110. What happens when the following program is compiled and run. Select the one correct answer.

Page 7: Questions on javascjp

111. public class example {112. int i = 0;113. public static void main(String args[]) {114. int i = 1;115. change_i(i);116. System.out.println(i);117. }118. public static void change_i(int i) {119. i = 2;120. i *= 2;121. }122. }

A. The program does not compile. B. The program prints 0. C. The program prints 1. D. The program prints 2. E. The program prints 4.

123. What happens when the following program is compiled and run. Select the one correct answer.

124.125. public class example {126. int i[] = {0};127. public static void main(String args[]) {128. int i[] = {1};129. change_i(i);130. System.out.println(i[0]);131. }132. public static void change_i(int i[]) {133. i[0] = 2;134. i[0] *= 2;135. }136. }137.

A. The program does not compile. B. The program prints 0. C. The program prints 1. D. The program prints 2. E. The program prints 4.

138. What happens when the following program is compiled and run. Select the one correct answer.

139.140. public class example {141. int i[] = {0};142. public static void main(String args[]) {143. int i[] = {1};144. change_i(i);145. System.out.println(i[0]);146. }147. public static void change_i(int i[]) {148. int j[] = {2};149. i = j;150. }

Page 8: Questions on javascjp

151. }152.

A. The program does not compile. B. The program prints 0. C. The program prints 1. D. The program prints 2. E. The program prints 4.

Answers to questions on Language Fundamentals 1. a, b, c 2. a, c, d, e 3. d, e 4. a, b, c, e. Please note that strictfp is a new keyword in Java 2. See Sun's site for

more details. 5. b, c, d, e, g 6. Any of the following are correct answers - 06, 006, or 0006 7. Any of the following are correct answers - 0x03, 0X03, 0X3 or 0x3 8. Any of the following are correct answers - 0x0a, 0X0a, 0Xa, 0xa, 0x0A, 0X0A,

0XA, 0xA 9. a 10. b 11. c 12. b 13. d. The variable y is getting read before being properly initialized. 14. d. Instance variable of type int and String are initialized to 0 and null respectively. 15. a 16. e 17. a, b, d, e 18. b, d, e, f 19. d 20. b 21. a, b, c 22. a 23. d 24. e 25. a 26. c, e 27. e 28. c 29. e 30. c

Page 9: Questions on javascjp

Questions on Operator and Assignments

1. In the following class definition, which is the first line (if any) that causes a compilation error. Select the one correct answer.

2.3. public class test {4. public static void main(String args[]) {5. char c;6. int i;7. c = 'A'; // 18. i = c; //29. c = i + 1; //310. c++; //411. }12. }13.

A. The line labeled 1. B. The line labeled 2. C. The line labeled 3. D. The line labeled 4. E. All the lines are correct and the program compiles.

14. Which of these assignments are valid? Select the four correct answers. A. short s = 28; B. float f = 2.3; C. double d = 2.3; D. int I = '1'; E. byte b = 12;

15. What gets printed when the following program is compiled and run. Select the one correct answer.

16.17. class test {18. public static void main(String args[]) {19. int i,j,k,l=0;20. k = l++;21. j = ++k;22. i = j++;23. System.out.println(i);24. }25. }26.

A. 0 B. 1 C. 2 D. 3

27. Which of these lines will compile? Select the four correct answers. A. short s = 20; B. byte b = 128; C. char c = 32; D. double d = 1.4; E. float f = 1.4;

Page 10: Questions on javascjp

F. byte e = 0; 28. The signed right shift operator in Java is --. Select the one correct answer.

A. <<; B. >> C. >>>; D. None of these.

29. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.

30.31. public class ShortCkt {32. public static void main(String args[]) {33. int i = 0;34. boolean t = true;35. boolean f = false, b;36. b = (t && ((i++) == 0));37. b = (f && ((i+=2) > 0));38. System.out.println(i);39. }40. }41.

A. 0 B. 1 C. 2 D. 3

42. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.

43.44. public class ShortCkt {45. public static void main(String args[]) {46. int i = 0;47. boolean t = true;48. boolean f = false, b;49. b = (t & ((i++) == 0));50. b = (f & ((i+=2) > 0));51. System.out.println(i);52. }53. }54.

A. 0 B. 1 C. 2 D. 3

55. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.

56.57. public class ShortCkt {58. public static void main(String args[]) {59. int i = 0;60. boolean t = true;61. boolean f = false, b;62. b = (t || ((i++) == 0));63. b = (f || ((i+=2) > 0));

Page 11: Questions on javascjp

64. System.out.println(i);65. }66. }67.

A. 0 B. 1 C. 2 D. 3

68. What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.

69.70. public class ShortCkt {71. public static void main(String args[]) {72. int i = 0;73. boolean t = true;74. boolean f = false, b;75. b = (t | ((i++) == 0));76. b = (f | ((i+=2) > 0));77. System.out.println(i);78. }79. }80.

A. 0 B. 1 C. 2 D. 3

81. Which operator is used to perform bitwise inversion in Java. Select the one correct answer.

A. ~ B. ! C. & D. | E. ^

82. What gets printed when the following program is compiled and run. Select the one correct answer.

83.84.85. public class test {86. public static void main(String args[]) {87. byte x = 3;88. x = (byte)~x;89. System.out.println(x);90. }91. }92.93.

A. 3 B. 0 C. 1 D. 11 E. 252

Page 12: Questions on javascjp

F. 214 G. 124 H. -4

94. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

95.96. public class test {97. public static void main(String args[]) {98. int x,y;99. x = 3 & 5;100. y = 3 | 5;101. System.out.println(x + " " + y);102. }103. }104.

A. 7 1 B. 3 7 C. 1 7 D. 3 1 E. 1 3 F. 7 3 G. 7 5

105. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

106.107. public class test {108. public static void main(String args[]) {109. int x,y;110. x = 1 & 7;111. y = 3 ^ 6;112. System.out.println(x + " " + y);113. }114. }115.

A. 1 3 B. 3 5 C. 5 1 D. 3 6 E. 1 7 F. 1 5

116. Which operator is used to perform bitwise exclusive or. A. & B. ^ C. | D. ! E. ~

117. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

118.119. public class test {

Page 13: Questions on javascjp

120. public static void main(String args[]) {121. boolean x = true;122. int a;123. if(x) a = x ? 1: 2;124. else a = x ? 3: 4;125. System.out.println(a);126. }127. }128.

A. 1 B. 2 C. 3 D. 4

129. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

130.131. public class test {132. public static void main(String args[]) {133. boolean x = false;134. int a;135. if(x) a = x ? 1: 2;136. else a = x ? 3: 4;137. System.out.println(a);138. }139. }140.

A. 1 B. 2 C. 3 D. 4

141. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

142.143. public class test {144. public static void main(String args[]) {145. int x, y;146.147. x = 5 >> 2;148. y = x >>> 2;149. System.out.println(y);150. }151. }152.

A. 5 B. 2 C. 80 D. 0 E. 64

153. What gets displayed on the screen when the following program is compiled and run? Select the one correct answer.

154.155. public class test {

Page 14: Questions on javascjp

156. public static void main(String args[]) {157. int x;158.159. x = -3 >> 1;160. x = x >>> 2;161. x = x << 1;162. System.out.println(x);163. }164. }165.

A. 1 B. 0 C. 7 D. 5 E. 23 F. 2147483646

166. Which of the following are correct. Select all correct answers. A. Java provides two operators to do left shift - << and <<<. B. >> is the zero fill right shift operator. C. >>> is the signed right shift operator. D. For positive numbers, results of operators >> and >>> are same.

167. What is the result of compiling and running the following program. Select one correct answer.

168.169. public class test {170. public static void main(String args[]) {171. int i = -1;172. i = i >> 1; 173. System.out.println(i);174. }175. }176.

A. 63 B. -1 C. 0 D. 1 E. 127 F. 128 G. 255

177. What all gets printed when the following gets compiled and run. Select the two correct answers.

178.179. public class example {180. public static void main(String args[]) {181. int x = 0;182. if(x > 0) x = 1;183.184. switch(x) {185. case 1: System.out.println(1);186. case 0: System.out.println(0);187. case 2: System.out.println(2);188. break;

Page 15: Questions on javascjp

189. case 3: System.out.println(3);190. default: System.out.println(4);191. break;192. }193. }194. }195.

A. 0 B. 1 C. 2 D. 3 E. 4

196. What happens when the following class is compiled and run. Select one correct answer.

197.198. public class test {199. public static void main(String args[]) {200. int x = 0, y = 1, z;201. if(x) 202. z = 0;203. else204. z = 1;205.206. if(y) 207. z = 2;208. else209. z = 3;210. System.out.println(z); 211. }212. }213.

A. The program prints 0 B. The program prints 1 C. The program prints 2 D. The program prints 3 E. The program does not compile because of problems in the if statement.

214. Which all lines are part of the output when the following code is compiled and run. Select the nine correct answers.

215.216. public class test {217. public static void main(String args[]) {218. for(int i = 0; i < 3; i++) {219. for(int j = 3; j >= 0; j--) {220. if(i == j) continue;221. System.out.println(i + " " + j);222. }223. }224. }225. }226.

A. 0 0 B. 0 1 C. 0 2

Page 16: Questions on javascjp

D. 0 3 E. 1 0 F. 1 1 G. 1 2 H. 1 3 I. 2 0 J. 2 1 K. 2 2 L. 2 3 M. 3 0 N. 3 1 O. 3 2 P. 3 3 Q. The program does not print anything.

227. Which all lines are part of the output when the following code is compiled and run. Select the one correct answer.

228.229. public class test {230. public static void main(String args[]) {231. for(int i = 0; i < 3; i++) {232. for(int j = 3; j <= 0; j--) {233. if(i == j) continue;234. System.out.println(i + " " + j);235. }236. }237. }238. }239.

A. 0 0 B. 0 1 C. 0 2 D. 0 3 E. 1 0 F. 1 1 G. 1 2 H. 1 3 I. 2 0 J. 2 1 K. 2 2 L. 2 3 M. 3 0 N. 3 1 O. 3 2 P. 3 3 Q. The program does not print anything.

Page 17: Questions on javascjp

240. Which all lines are part of the output when the following code is compiled and run. Select the six correct answers.

241.242. public class test {243. public static void main(String args[]) {244. for(int i = 0; i < 3; i++) {245. for(int j = 3; j >= 0; j--) {246. if(i == j) break;247. System.out.println(i + " " + j);248. }249. }250. }251. }252.

A. 0 0 B. 0 1 C. 0 2 D. 0 3 E. 1 0 F. 1 1 G. 1 2 H. 1 3 I. 2 0 J. 2 1 K. 2 2 L. 2 3 M. 3 0 N. 3 1 O. 3 2 P. 3 3

253. Which all lines are part of the output when the following code is compiled and run. Select the six correct answers.

254.255. public class test {256. public static void main(String args[]) {257. outer: for(int i = 0; i < 3; i++) {258. for(int j = 3; j >= 0; j--) {259. if(i == j) continue outer;260. System.out.println(i + " " + j);261. }262. }263. }264. }265.

A. 0 0 B. 0 1 C. 0 2 D. 0 3 E. 1 0 F. 1 1 G. 1 2

Page 18: Questions on javascjp

H. 1 3 I. 2 0 J. 2 1 K. 2 2 L. 2 3 M. 3 0 N. 3 1 O. 3 2 P. 3 3

266. Which all lines are part of the output when the following code is compiled and run. Select the three correct answers.

267.268. public class test {269. public static void main(String args[]) {270. outer : for(int i = 0; i < 3; i++) {271. for(int j = 3; j >= 0; j--) {272. if(i == j) break outer;273. System.out.println(i + " " + j);274. }275. }276. }277. }278.

A. 0 0 B. 0 1 C. 0 2 D. 0 3 E. 1 0 F. 1 1 G. 1 2 H. 1 3 I. 2 0 J. 2 1 K. 2 2 L. 2 3 M. 3 0 N. 3 1 O. 3 2 P. 3 3

Answers to questions on Operators and Assignments 1. c. It is not possible to assign an integer to a character in this case without a cast. 2. a, c, d, e. 2.3 is of type double. So it cannot be assigned to a float without a cast. 3. b 4. a, c, d, f. If RHS (Right hand side) is an integer within the correct range of LHS

(Left hand side), and if LHS is char, byte, or short, no cast is required. A decimal number is a double by default. Assigning it to float requires a cast.

5. b

Page 19: Questions on javascjp

6. b. In the second assignment to variable b, the expression (i+=2) does not get evaluated.

7. d 8. c 9. d 10. a 11. h 12. c 13. f 14. b 15. a 16. d 17. d 18. f 19. d 20. b 21. a, c 22. e. The expression in the if statement must evaluate to a boolean. 23. b, c, d, e, g, h, i, j, l 24. q 25. b, c, d, g, h, l 26. b, c, d, g, h, l 27. b, c, d

Questions on Declaration and Access Control

1. Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.

A. arr.length B. arr.length - 1 C. arr.size D. arr.size - 1 E. arr.length() F. arr.length() - 1

2. Which of these statements are legal. Select the three correct answers. A. int arr[][] = new int[5][5]; B. int []arr[] = new int[5][5]; C. int[][] arr = new int[5][5]; D. int[] arr = new int[5][]; E. int[] arr = new int[][5];

3. Write the expression to access the number of elements in a one dimensional array arr. The expression should not be assigned to any variable.

Page 20: Questions on javascjp

4. Which of these array declarations and initializations are legal? Select the two correct answers.

A. int arr[4] = new int[4]; B. int[4] arr = new int[4]; C. int arr[] = new int[4]; D. int arr[] = new int[4][4]; E. int[] arr = new int[4];

5. What will the result of compiling and executing the following program. Select the one correct answer.

6.7. class Test {8.9. public static void main(String args[]) {10.11. int arr[] = new int[2];12.13. System.out.println(arr[0]);14.15. }16.17. }18.

A. The program does not compile because arr[0] is being read before being initialized.

B. The program generates a runtime exception because arr[0] is being read before being initialized.

C. The program compiles and prints 0 when executed. D. The program compiles and prints 1 when executed. E. The program compiles and runs but the results are not predictable because

of un-initialized memory being read. 19. Which of the following are legal declaration and definition of a method. Select all

correct answers. A. void method() {}; B. void method(void) {}; C. method() {}; D. method(void) {}; E. void method {};

20. Which of the following are valid constructors within a class Test. Select the two correct answers.

A. test() { } B. Test() { } C. void Test() { } D. private final Test() { } E. abstract Test() { } F. Test(Test t) { } G. Test(void) { }

21. What is the result of compiling and running the following class. Select the one correct answer.

22.

Page 21: Questions on javascjp

23.24.25. class Test {26.27. public void methodA(int i) {28.29. System.out.println(i); 30.31. }32.33. public int methodA(int i) {34.35. System.out.println(i+1); 36.37. return i+1; 38.39. }40.41. public static void main(String args[]) {42.43. Test X = new Test();44.45. X.methodA(5);46.47. }48.49. }50.51.52.

Select the one correct answer.

A. The program compiles and runs printing 5. B. The program compiles and runs printing 6. C. The program gives runtime exception because it does not find the method

Test.methodA(int) D. The program give compilation error because methodA is defined twice in

class Test.

Answers to questions on Declarations 1. a 2. a, b, c 3. arr.length 4. c, e. The size of the array should not be specified when declaring the array. 5. c 6. a 7. b, f. A constructor must have the same name as the class, hence a is not a

constructor. It must not return any value, hence c is not correct. A constructor cannot be declared abstract or final.

8. d

Page 22: Questions on javascjp

Questions on Classes

1. What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

2.3. protected class example {4. public static void main(String args[]) {5. String test = "abc";6. test = test + test;7. System.out.println(test);8. }9. }10.

A. The class does not compile because the top level class cannot be protected. B. The program prints "abc" C. The program prints "abcabc" D. The program does not compile because statement "test = test + test" is

illegal. 11. A top level class may have only the following access modifier. Select the one

correct answer. A. package B. friendly C. private D. protected E. public

12. Write down the modifier of a method that makes the method available to all classes in the same package and to all the subclasses of this class.

13. Select the one most appropriate answer. A top level class without any modifier is accessible to -

A. any class B. any class within the same package C. any class within the same file D. Any subclass of this class.

14. Is this True or False. In Java an abstract class cannot be sub-classed. 15. Is this True or False. In Java a final class must be sub-classed before it can be

used. 16. Which of the following are true? Select the three correct answers.

A. A static method may be invoked before even a single instance of the class is constructed.

B. A static method cannot access non-static methods of the class. C. Abstract modifier can appear before a class or a method but not before a

variable. D. Final modifier can appear before a class or a variable but not before a

method.

Page 23: Questions on javascjp

E. Synchronized modifier may appear before a method or a variable but not F. Before a class.

Answers to questions on classes in Java 1. a 2. e 3. protected 4. b 5. False 6. False 7. a, b, c. final modifier may appear before a method, a variable or before a class.

Questions on Events

This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam. 1. Name the method defined in Event Object class that returns the Object generated

from the event. Select the one correct answer. A. geitEvent() B. getObject() C. getID() D. getSource()

2. What is the return type of the method getID() defined in AWTEvent class. Select the one correct answer.

A. int B. long C. Object D. Component E. short

3. Name the event that gets generated when a button is clicked. Select the one correct answer.

A. Key Event B. Mouse Event C. Item Event D. Action Event

4. Which event is generated when the position of a scrollbar is changed. Select the one correct answer.

A. Key Event B. MouseEvent C. ItemEvent D. ActionEvent E. AdjustmentEvent

5. Which of the following Objects can generate ActionEvent. Select the one correct answer.

A. List

Page 24: Questions on javascjp

B. TextArea C. CheckboxMenuItem D. Choice

6. Which of the following Objects can generate ItemEvent. Select the two correct answer.

A. CheckBox B. Button C. List D. MenuItem

7. Which method identifies the type of an event generated. Select the one correct answer.

A. getSource() B. getType() C. getEventType() D. getID()

8. Which of the following are legal adapter classes in Java. Select the two correct answers.

A. ActionAdapter B. ItemAdapter C. TextAdapter D. MouseAdapter E. MouseMotionAdapter

9. Name the class of the argument of method actionPerformed() defined in the ActionListener interface.

10. Which of these listener classes have corresponding adapter classes. Select the two correct answers.

A. ContainerListener B. TextListener C. ItemListener D. MouseMotionListener

11. Which of these are valid adapter classes. Select the one correct answer. A. ActionAdapter B. AdjustmentAdapter C. KeyAdapter D. TextAdapter

12. Which of these methods are defined in MouseMotionlistener interface. Select the two correct answers.

A. mouseClicked() B. mousePressed() C. mouseEntered() D. mouseDragged() E. mouseMoved()

13. What is the return type of the method getSource() defined in EventObject class. Select the one correct answer.

A. int B. long

Page 25: Questions on javascjp

C. Object D. Component E. short

Answers to questions on Events 1. d 2. a 3. d 4. e 5. a 6. a, c 7. d 8. d, e 9. ActionEvent 10. a, d 11. c 12. d, e 13. c

Questions on AWT

This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam. 1. Which of the following classes are derived from the Container class. Select the

four correct answers. A. Component B. Panel C. java.applet.Applet D. Dialog E. Frame F. MenuComponent

2. Which of the following classes are derived from the Component class. Select the four correct answers.

A. Container B. Window C. List D. MenuItem E. Choice

3. Name the class used to represent a GUI application window, which is optionally resizable and can have a title bar, an icon, and menus. Select the one correct answer.

A. Window B. Panel C. Dialog D. Frame

Page 26: Questions on javascjp

4. Which abstract class is the super class of all menu related classes. 5. Which of these classes can be added to a Frame component. Select the three

correct answers. A. Menu B. Button C. PopupMenu D. Window E. List

6. Which class can be used to represent a checkbox with a textual label that can appear in a menu. Select the one correct answer.

A. MenuBar B. MenuItem C. CheckboxMenuItem D. Menu E. CheckBox

7. Which of these classes can be added to any Container class, using the add method defined in Container class. Select the two correct answers.

A. Button B. CheckboxMenuItem C. Menu D. Canvas

Answers to questions on AWT 1. b, c, d, e 2. a, b, c, e 3. d 4. MenuComponent 5. b, c, e 6. c 7. a, d

Questions on Files and Input/Output

This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam.1. Which abstract class is the super class of all classes used for reading bytes. Select

the one correct answer. A. Reader B. FileReader C. ByteReader D. InputStream E. FileInputStream

2. Which abstract class is the super class of all classes used for writing characters. Select the one correct answer.

A. Writer

Page 27: Questions on javascjp

B. FileWriter C. CharWriter D. OutputStream E. FileOutputStream

3. Which of these are legal ways of accessing a File named "file.tst" for reading. Select the two correct answers.

A. FileReader fr = new FileReader("file.tst"); B. FileInputStream fr = new FileInputStream("file.tst");

InputStreamReader isr = new InputStreamReader(fr, "UTF8"); C. FileReader fr = new FileReader("file.tst", "UTF8"); D. InputStreamReader isr = new InputStreamReader("file.tst");

4. Name the class that allows reading of binary representations of Java primitives from an input byte stream.

5. Which of these classes are abstract. Select the three correct answers. A. FilterWriter B. Reader C. InputStream D. CharArrayReader E. DataInputStream

6. Name the exception thrown by the read method defined in InputStream class

Answers to questions on Files and I/O 1. d 2. a 3. a, b. FileReader class uses the default character encoding, hence c is incorrect.

InputStreamReader character class does not have a constructor that takes file name as an argument. Hence d is incorrect.

4. DataInpiutStream 5. a,b,c 6. IOException

Questions on Collections1. TreeMap class is used to implement which collection interface. Select the one

correct answer. A. Set B. SortedSet C. List D. Tree E. SortedMap

2. Name the Collection interface implemented by the Vector class. 3. Name the Collection interface implemented by the Hashtable class. 4. Name the Collection interface implemented by the HashSet class. 5. Which of these are interfaces in the collection framework. Select the two correct

answers. A. Set B. List

Page 28: Questions on javascjp

C. Array D. Vector E. LinkedList

6. Which of these are interfaces in the collection framework. Select the two correct answers.

A. HashMap B. ArrayList C. Collection D. SortedMap E. TreeMap

7. What is the name of collection interface used to maintain non-unique elements in order.

8. What is the name of collection interface used to maintain unique elements. 9. What is the name of collection interface used to maintain mappings of keys to

values. 10. Is this true or false. Map interface is derived from the Collection interface.

A. True B. False

Answers to questions on Collections 1. e 2. List 3. Map 4. Set 5. a,b 6. c,d 7. List 8. Set 9. Map 10. b

Questions on Layout Managers

Please note that this topic is not included in the SCJP 1.4 exam. It is part of SCJP 1.2 exam though.

1. In Java, which of these classes implement the LayoutManager interface. Select all correct answers.

A. RowLayout B. ColumnLayout C. GridBagLayout D. FlowLayoutManager E. BorderLayoutManager

Page 29: Questions on javascjp

2. Which of the following layout manager is the default Layout Manager for the Applet class in Java. Select the one correct answer.

A. FlowLayout B. BorderLayout C. GridLayout D. CardLayout E. GridBagLayout

3. Which of the following layout manager is the default Layout Manager for the Frame class in Java. Select the one correct answer.

A. FlowLayout B. BorderLayout C. GridLayout D. CardLayout E. GridBagLayout

4. Which of the following layout manager is the default Layout Manager for the Dialog class in Java. Select the one correct answer.

A. FlowLayout B. BorderLayout C. GridLayout D. CardLayout E. GridBagLayout

5. Which Layout Manager follows the following placement policy - "Lays out the components in row-major order in rows growing from left to right, and rows from top to bottom in the container." Select the one correct answer.

A. FlowLayout B. BorderLayout C. CardLayout D. GridBagLayout

6. How will the following program lay out its buttons. Select the one correct answer. 7.8. import java.awt.*;9. public class MyClass {10. public static void main(String args[]) {11. String[] labels = {"A","B","C","D","E","F"};12. Window win = new Frame();13. win.setLayout(new GridLayout(1,0,2,3));14. for(int i=0;i < labels.length;i++)15. win.add(new Button(labels[i]));16. win.pack();17. win.setVisible(true);18. }19. }20.

A. The program will not display any buttons. B. The program will display all buttons in a single row. C. The program will display all buttons in a single column. D. The program will display three rows - A B, C D, and E F. E. The program will display two rows - A B C, and D E F. F. The rows and columns displayed are compiler and platform dependent.

Page 30: Questions on javascjp

21. How will the following program lay out its buttons. Select the one correct answer.

22.23. import java.awt.*;24. public class MyClass {25. public static void main(String args[]) {26. String[] labels = {"A"};27. Window win = new Frame();28. win.setLayout(new FlowLayout());29. for(int i=0;i < labels.length;i++)30. win.add(new Button(labels[i]));31. win.pack();32. win.setVisible(true);33. }34. }35.

A. The button A will appear on the top left corner of the window. B. The button A will appear on the center of first row. C. The button A will appear on the top right corner of the window. D. The button A will appear on the middle row and column, in the center of

the window. 36. Select the one correct answer. The default alignment of buttons in Flow Layout

is ... A. LEFT B. CENTER C. RIGHT D. The alignment must be defined when using the FlowLayout Manager.

37. The default horizontal and vertical gap in FlowLayout is - A. 0 pixel B. 1 pixel C. 5 pixels D. 10 pixels E. The alignment must be defined when using the FlowLayout Manager.

38. The default horizontal and vertical gap in BorderLayout is - A. 0 pixel B. 1 pixel C. 5 pixels D. 10 pixels E. The horizontal and vertical gaps must be defined when using the

BorderLayout Manager. 39. How will the following program lay out its buttons. Select the one correct

answer. 40.41. import java.awt.*;42. public class MyClass {43. public static void main(String args[]) {44. String[] labels = {"A"};45. Window win = new(Frame());46. win.set.layout(new BorderLayout());47. for(i=0;i<labels.length;i++)

Page 31: Questions on javascjp

48. win.add(new Button(labels[i]), "North");49. win.pack();50. win.setVisible(true);51. }52. }53.

A. The button A will appear on the top left corner of the window. B. The button A will appear on the center of first row. C. The button A will appear on the top right corner of the window. D. The button A will appear on the middle row and column, in the center of

the window. E. The button A will occupy the whole window. F. The button A will occupy the complete first row of the window.

54. If the following method is used to add an object to a window using BorderLayout, what would be the placement constraint. Select one correct answer. Component add(Component comp);

A. NORTH B. SOUTH C. EAST D. WEST E. CENTER

55. What is the default filling of GridBagConstraint. A. NONE B. BOTH C. HORIZONTAL D. VERTICAL

56. Which GridBagLayout variable defines the external padding (border) around the component in its display area. Select the one correct answer.

A. gridwidth, gridheight B. gridx, gridy C. anchor D. fill E. ipadx, ipady F. insets

57. Which GridBagLayout variable defines the padding that gets added to each side of the component. Select the one correct answer.

A. gridwidth, gridheight B. gridx, gridy C. anchor D. fill E. ipadx, ipady F. insets

58. Which GridBagLayout variable specifies the number of cells occupied by the component horizontally and vertically in the grid. Select the one correct answer.

A. gridwidth, gridheight B. gridx, gridy C. anchor D. fill

Page 32: Questions on javascjp

E. ipadx, ipady 59. Which GridBagLayout variable specifies where (in which direction) a component

should be placed in its display area. Select the one correct answer. A. gridwidth, gridheight B. gridx, gridy C. anchor D. fill E. ipadx, ipady F. insets

60. What is the default value of GridBagLayout constraint anchor. Select the one correct answer.

A. CENTER B. NORTH C. EAST D. SOUTH E. WEST

Answers to questions on Layout Managers 1. c 2. a 3. b 4. b 5. a 6. b 7. b 8. b 9. c 10. a 11. f 12. e 13. a 14. f 15. e 16. a 17. c 18. a

Questions on Assertions

This topic is part of SCJP 1.4 exam but not SCJP 1.2 exam.

1. What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 3; i++)  for(int j = 3; j > i; j--)     assert i!=j {System.out.println(i); }

Page 33: Questions on javascjp

A. The class compiles and runs, but does not print anything. B. The number 1 gets printed with AssertionError C. The number 2 gets printed with AssertionError D. The number 3 gets printed with AssertionError E. The program generates a compilation error.

2. What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 3; i++)  for(int j = 3; j >= 1; j--)     assert i!=j : i;

A. The class compiles and runs, but does not print anything. B. The number 1 gets printed with AssertionError C. The number 2 gets printed with AssertionError D. The number 3 gets printed with AssertionError E. The program generates a compilation error.

3. What happens when the following code is compiled and run. Select the one correct answer. for(int i = 1; i < 4; i++)  for(int j = 1; j < 4; j++)    if(i < j)       assert i!=j : i;

A. The class compiles and runs, but does not print anything. B. The number 1 gets printed with AssertionError C. The number 2 gets printed with AssertionError D. The number 3 gets printed with AssertionError E. The program generates a compilation error.

4. Which of the following statement is true about the assert statement. Select the one correct answer.

A. If a Java class contains assert statements, then it must be compiled with -1.4 option.

B. When a program having assertions is run, -assertion option must be specified, otherwise the assertions get ignored.

C. A possible syntax of assert statement is  assert logical_expression If logical_expression evaluates to true, the program generates an AssertionError.

D. The program terminates on its first AssertionError.

Page 34: Questions on javascjp

Answers to questions on Assertions 1. e. The condition in assert statement must be followed by a semi-colon. 2. b. When i and j are both 1, assert condition is false, and AssertionError gets

generated. 3. a. When the if condition returns true, the assert statement also returns true. Hence

AssertionError does not get generated. 4. d. The option A is incorrect, as the Java compiler option is -source 1.4 . The

option B is incorrect, as the runtime option is -ea or -enableassertions. If the logical expression evaluates to false, then the program generates an AssertionError, hence C is incorrect.