+ All Categories
Home > Documents > Core Java 1

Core Java 1

Date post: 11-Apr-2015
Category:
Upload: api-3748459
View: 1,057 times
Download: 10 times
Share this document with a friend
200
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 Page 1 of 200
Transcript
Page 1: Core Java 1

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

Page 1 of 165

Page 2: Core Java 1

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

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

Page 2 of 165

Page 3: Core Java 1

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.

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

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.

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

26.27. class example {28. int x;29. int y;30. String name;31. public static void main(String args[]) {32. example pnt = new example();

Page 3 of 165

Page 4: Core Java 1

33. System.out.println("pnt is " + pnt.name + 34. " " + pnt.x + " " + pnt.y);35. }36. }37.

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

38. 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.

39. 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.

40. Which of the following are legal Java programs. Select the four correct answers.

A. // The comments come before the packagepackage pkg;import java.awt.*;class C{}

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

Page 4 of 165

Page 5: Core Java 1

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

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

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

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

41. 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.

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

43.44. class test {45. public static void main() {46. System.out.println("test");47. }48. }49.

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.

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

Page 5 of 165

Page 6: Core Java 1

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[]);

51. 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[]);

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

53.54. class test {55. public static void main(String args[]) {56. if(args.length > 0)57. System.out.println(args.length);58. }59. }60.

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.

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

62.63. public class test {64. public static void main(String args[]) {65. int i, j;66. int k = 0;67. j = 2;68. k = j = i = 1;69. System.out.println(k);

Page 6 of 165

Page 7: Core Java 1

70. }71. }72.73.

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.

74. 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.

75.76. public class test {77. public static void main(String args[]) {78. System.out.println(args[0]+" "+args[args.length-1]);79. }80. }81.

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"

82. 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.

83.84. public class test {85. public static void main(String args[]) {86. System.out.println(args[0]+" "+args[args.length]);87. }88. }89.

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"

Page 7 of 165

Page 8: Core Java 1

E. The program will print "lets happens"

90. 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.

91.92. public class test {93. public static void main(String args[]) {94. System.out.println(args[0]+" "+args.length);95. }96. }97.

A. java

B. test

C. lets

D. 3

E. 4

F. 5

G. 6

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

99.100. public class example {101. int i = 0;102. public static void main(String args[]) {103. int i = 1;104. i = change_i(i);105. System.out.println(i);106. }107. public static int change_i(int i) {108. i = 2;109. i *= 2;110. return i;111. }112. }113.

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.

Page 8 of 165

Page 9: Core Java 1

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

115.116. public class example {117. int i = 0;118. public static void main(String args[]) {119. int i = 1;120. change_i(i);121. System.out.println(i);122. }123. public static void change_i(int i) {124. i = 2;125. i *= 2;126. }127. }128.

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.

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

130.131. public class example {132. int i[] = {0};133. public static void main(String args[]) {134. int i[] = {1};135. change_i(i);136. System.out.println(i[0]);137. }138. public static void change_i(int i[]) {139. i[0] = 2;140. i[0] *= 2;141. }142. }143.

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.

Page 9 of 165

Page 10: Core Java 1

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

145.146. public class example {147. int i[] = {0};148. public static void main(String args[]) {149. int i[] = {1};150. change_i(i);151. System.out.println(i[0]);152. }153. public static void change_i(int i[]) {154. int j[] = {2};155. i = j;156. }157. }158.

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

Page 10 of 165

Page 11: Core Java 1

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

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.

Page 11 of 165

Page 12: Core Java 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 12 of 165

Page 13: Core Java 1

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

Page 13 of 165

Page 14: Core Java 1

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));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. !

Page 14 of 165

Page 15: Core Java 1

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

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

Page 15 of 165

Page 16: Core Java 1

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 {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;

Page 16 of 165

Page 17: Core Java 1

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

Page 17 of 165

Page 18: Core Java 1

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 {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

Page 18 of 165

Page 19: Core Java 1

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;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.

Page 19 of 165

Page 20: Core Java 1

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

D. 0 3

E. 1 0

F. 1 1

G. 1 2

H. 1 3

I. 2 0

J. 2 1

K. 2 2

Page 20 of 165

Page 21: Core Java 1

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

Page 21 of 165

Page 22: Core Java 1

O. 3 2

P. 3 3

Q. The program does not print anything.

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

Page 22 of 165

Page 23: Core Java 1

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

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--) {

Page 23 of 165

Page 24: Core Java 1

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 24 of 165

Page 25: Core Java 1

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

Page 25 of 165

Page 26: Core Java 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.

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.

Page 26 of 165

Page 27: Core Java 1

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.23.24.25. class Test {26.27. public void methodA(int i) {28.29. System.out.println(i); 30.31. }32.

Page 27 of 165

Page 28: Core Java 1

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

Questions on Classes

Page 28 of 165

Page 29: Core Java 1

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.

Page 29 of 165

Page 30: Core Java 1

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.

E. Synchronized modifier may appear before a method or a variable but not 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 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"

Page 30 of 165

Page 31: Core Java 1

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.

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

Answers to questions on classes in Java

Page 31 of 165

Page 32: Core Java 1

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 Collections

1. 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

C. Array

D. Vector

E. LinkedList

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

A. HashMap

Page 32 of 165

Page 33: Core Java 1

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

Page 33 of 165

Page 34: Core Java 1

B. ColumnLayout

C. GridBagLayout

D. FlowLayoutManager

E. BorderLayoutManager

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

Page 34 of 165

Page 35: Core Java 1

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.

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.

Page 35 of 165

Page 36: Core Java 1

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++)48. win.add(new Button(labels[i]), "North");49. win.pack();

Page 36 of 165

Page 37: Core Java 1

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

Page 37 of 165

Page 38: Core Java 1

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

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

Page 38 of 165

Page 39: Core Java 1

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 Files and Input/OutputThis 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

Page 39 of 165

Page 40: Core Java 1

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

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

Page 40 of 165

Page 41: Core Java 1

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 AssertionsThis 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); }

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++)

Page 41 of 165

Page 42: Core Java 1

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.

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.

CHAPTER 4 With the phenomenal growth of networks, today's developers must "think distributed". Applications--even parts of applications--must be able to migrate easily to a wide variety of computer systems, a wide variety of hardware architectures, and a wide variety of operating system architectures. They must operate with a plethora of graphical user interfaces.

Page 42 of 165

Page 43: Core Java 1

Clearly, applications must be able to execute anywhere on the network without prior knowledge of the target hardware and software platform. If application developers are forced to develop for specific target platforms, the binary distribution problem quickly becomes unmanageable. Various and sundry methods have been employed to overcome the problem, such as creating "fat" binaries that adapt to the specific hardware architecture, but such methods are not only clumsy but are still geared to a specific operating system. To solve the binary-distribution problem, software applications and fragments of applications must be architecture neutral and portable.

Reliability is also at a high premium in the distributed world. Code from anywhere on the network should work robustly with low probabilities of creating "crashes" in applications that import fragments of code.

This chapter describes the ways in which Java has addressed the issues of architecture neutrality, portability, and reliability.

4.1 Architecture Neutral The solution that the Java system adopts to solve the binary-distribution problem is a "binary code format" that's independent of hardware architectures, operating system interfaces, and window systems. The format of this system-independent binary code is architecture neutral. If the Java run-time platform is made available for a given hardware and software environment, an application written in Java can then execute in that environment without the need to perform any special porting work for that application.

4.1.1 Byte Codes The Java compiler doesn't generate "machine code" in the sense of native hardware instructions--rather, it generates bytecodes: a high-level, machine-independent code for a hypothetical machine that is implemented by the Java interpreter and run-time system.

One of the early examples of the bytecode approach was the UCSD P-System, which was ported to a variety of eight-bit architectures in the middle 1970s and early 1980s and enjoyed widespread popularity during the heyday of eight-bit machines. Coming up to the present day, current architectures have the power to support the bytecode approach for distributed software. Java bytecodes are designed to be easy to interpret on any machine, or to dynamically translate into native machine code if required by performance demands.

The architecture neutral approach is useful not only for network-based applications, but also for single-system software distribution. In today's software market, application developers have to produce versions of their applications that are compatible with the IBM PC, Apple Macintosh, and fifty-seven flavors of workstation and operating system architectures in the fragmented UNIX® marketplace.

With the PC market (through Windows 95 and Windows NT) diversifying onto many CPU architectures, and Apple moving full steam from the 68000 to the PowerPC, production of software to run on all platforms becomes almost impossible until now. Using Java, coupled with the Abstract Window Toolkit, the same version of your application can run on all platforms.

1. A session bean does not survive server crashes.A is correct! Answer (A/True): Page 14. A session bean does not survive server crashes.2. What happens during data marshaling?

Page 43 of 165

Page 44: Core Java 1

B is incorrect

Answer (C): Page 17. In data marshaling, methods that are called on a remote server object are wrapped with their data and sent to the remote server object. The remote server object unwraps (unmarshals) the methods and data, and calls the enterprise bean. The results of the call to the enterprise bean are wrapped again, passed back to the client through the remote server object, and unmarshaled.

3. If you want a bean to manage its own transactions, you have to configure the Enterprise JavaBeans server with which kind of settings? D is correct! Answer (D): Page 45. An isolation level to specify how exclusive a transaction's access to

shared data is. A transaction attribute to specify how to handle bean-managed or container-managed transactions that continue in another bean. A transaction type to specify whether the transaction is managed by the container or the bean.

4. When would you use an Interoperable Object Reference (IOR)?C is incorrect

Answer (B): Page 63. Using a CORBA name service works for most CORBA applications especially when the object request brokers (ORBs) are supplied by one vendor. However, you might find the name service is not completely compatible among all ORBs, and you could get a frustrating COMM_FAILURE message when the CORBA client tries to connect to the CORBA server. The solution is to use an Interoperable Object Reference (IOR) instead. An IOR is available in ORBs that support the Internet Inter-ORB protocol (IIOP). It contains the information a naming service keeps for each object such as the host and port where the object resides, a unique lookup key for the object on that host, and the IIOP version supported.

5. What is the RMI-IIOP API used for? D is incorrect

Answer: (A) Page 65. RMI over Internet Inter-ORB Protocol (RMI-IIOP) lets existing RMI code reference and look up an object using the CORBA CosNaming service. This gives you greater interoperability between architectures with little change to your existing RMI code.

6. How does RMI prevent potential memory leaks from client references to remote objects when the clients have aborted or the network connection has dropped? Blank X Answer (D): Page 75. To avoid potential memory leaks on the server from clients, RMI uses a

leasing mechanism when giving out references to exported objects. When exporting an object,

the Java virtual machine1 increases the count for the number of references to this object and sets an expiration time, or lease time, for the new reference to this object. When the lease expires, the reference count of this object is decreased and if it reaches 0, the object is set for garbage collection. It is up to the client that maintains this weak reference to the remote object to renew the lease if it needs the object beyond the lease time. A weak reference is a way to refer to an object in memory without keeping it from being garbage collected.

7. What kind of exception is raised if you try to map a java.lang.String to a fixed size or bounded IDL string?C is incorrect

Answer (A): Page 85. If you try to map a java.lang.String to fixed size or bounded IDL string and the java.lang.String is too large, a MARSHAL exception is raised.

8. CORBA does not have a distributed garbage collection mechanism.B is incorrect

Answer (A/True): Page: 95. Unlike RMI, CORBA does not have a distributed garbage collection mechanism. References to an object are local to the client proxy and the server servant. This means each Java virtual machine is free to reclaim that object and garbage collect it if there are no longer references to it. If an object is no longer needed on the server, orb.disconnect(object) needs to be called to allow the object to be garbage collected.

9. When you use the Java Native Interface (JNI) to retrieve a reference to an array residing in your program, what does the Java virtual machine do to prevent the array from being moved by the garbage collector when it compacts heap memory? B is incorrect

Answer (C): Page 143- 144. When retrieving an array, you can specify if this is a copy (JNI_TRUE) or a reference to the array residing in your program (JNI_FALSE). If you use a reference to the array, you want the array to stay where it is in the Java heap and not get moved by the garbage collector when it compacts heap memory. To prevent array references from being moved, the Java virtual machine pins the array into memory. Pinning the array ensures that when the array is released, the correct elements are updated in the Java virtual machine.

10. In JNI, how do you make an object persist across native method calls? B is incorrect

Answer (D): Page 155. By default, JNI uses local references when creating objects inside a native method. This means that when the method returns, the references are eligible to be garbage collected. If you want an object to persist across native method calls, use a global reference instead. A global reference is created from a local reference by calling NewGlobalReference on the local reference.

Page 44 of 165

Page 45: Core Java 1

Page 45 of 165

Page 46: Core Java 1

Mock Exam - 1

Q1 public class Test1{ public static void main(String[] args){ int arr[] = new int[3]; for(int i = 0;i < arr.length;i++){ System.out.println(arr[i]); } } } What will be the output?

A1 0 0 0A2 ArrayIndexoutOfBoundsExceptionA3 NullPointerExceptionA4 null null null

Q2 Which of the following are valid array declarations?A1 int arr[] = new int[];A2 float arr[10] = new flA3 double []arr = new double[10];A4 None Of the Above.

Q3 public class Test3{ public void method(){ System.out.println("Called"); } public static void main(String[] args){ method(); } } What will be the output?

A1 "Called"A2 CompilerA3 Runtime A4 Nothing

Q4 public class Test4{ public static void method(){ System.out.println("Called"); } public static void main(String[] args){ Test4 t4 = null; t4.method(); } } What will be the output?

A1 "Called"A2 CompilerA3 Runtime ExceptionA4 Nothing is printed in screen

Q5 public class Test5{ public void Test5(){ System.out.println("Constructor1"); } public Test5(){ System.out.println("Constructor2"); } public static void main(String[] args){ Test5 t5 = new Test5(); } } What will be the output?

A1 "Constructor1"A2 "Constructor2"A3 "Constructor1""Constructor2"

Page 46 of 165

Page 47: Core Java 1

A4 Compiler Errror

Q6 public class Test6{ public Test6(){ this(4); } public Test6(byte var){ System.out.println(var); } public static void main(String[] args){ Test6 t6 = new Test6(); } } What will be the output?

A1 4A2 4 4A3 Compiler ErrorA4 Compiles and Runs without error

Q7 public class Test7{ public Test7(){} public Test7(Test7 ref){ this (ref,"Hai"); } public Test7(Test7 ref,String str){ ref.Test7(str); System.out.println("Hi"); } public void Test7(String str){ System.out.println(str); } public static void main(String[] args){ Test7 t = new Test7(); Test7 t7 = new Test7(t); } } What will be the output?

A1 HIA2 haiA3 Hai HiA4 Hi Hai

Q8 Which of the following are valid Constructors?A1 public Test8(){}A2 private void Test8(){}A3 protected Test8(int k){}A4 test8(){}

Q9 Which of the following are valid method declarations?A1 abstract method(){}A2 abstrct void method(){}A3 final void method(){}A4 int method(){}9)a A3)final void method(){} A4)int method(){}Q10 Which of the following are valid top-level class declarations?A1 class Test10A2 public Test10A3 final Test10A4 abstract final Test10

Q11 transient keywaord can be used with?A1 method

Page 47 of 165

Page 48: Core Java 1

A2 variableA3 classA4 constructor

Q12 which of the following are valid combinations for class declaration?A1 abstract final class{}A2 abstract static class()A3 final static class()A4 public final strictfp class{}

Q13 which of the following are valid constructor signatures?A1 public void className() A2 public void className() A3 private className() A4 static className()

Q14 Which of the following modifiers can be used with top class declaration? A1 staticA2 privatrA3 publicA4 finalA5 abstract

Q15 Which of the following are valid array declarations? A1 int arr[] = new int[]; A2 int arr[][] = new int [10][10];A3 float arr[][] = new float[][10];A4 float arr[] = new float[10];

Q16 What will be the output of the following program? public class Test1 { static{ System.out.println("Static"); } { System.out.println("Instance"); } public void Test1(){ System.out.println("Constructor"); } public static void main(String[] args) { Test1 t = null; } }

A1 Instance StaticA2 Static InstanceA3 StaticA4 Static Instance Constructor

Q17 What will be the output of the following program? class Sup{ public Sup(String str){ System.out.println("Super class"); } } public class Test2 extends Sup{ public Test2(){ System.out.println("Sub class"); } public

Page 48 of 165

Page 49: Core Java 1

static void main(String[] args) { Test2 t2 = new Test2(); } }

A1 Super class,SubClassA2 Super classA3 Sub classA4 Compiler Error

Q18 What will be the output of the following program? public class Test3 { public static void main(String[] args) { System.out.println("Main Method1"); } public static void main(String args){ System.out.println("Main Method2"); } }

A1 Main Method1A2 Main Method1 Main Method2A3 Main Method2A4 Runtime Exception

Q19 What will be the output of the following program? public class Test4 { public static void main(String args) { System.out.println("Sample Program"); } }

A1 Sample ProgramA2 Compiler ErrorA3 Runtime ExceptionA4 None

Q20 What will be the output of the following program? class Sup1{ public Sup1(){ System.out.println("Hai"); } private Sup1(String str){ System.out.println(str); } } public class Test5 extends Sup1{ private Test5(String str){ System.out.println(str); super(); } public static void main(String[] args) { Test5 t5 = new Test5("HI"); } }

A1 Hai,Hi,HiA2 Hai,HiA3 Hi,HiA4 Compiler Error

Q21 Which of the following are not a wrapper class?A1 String ingA2 IntegerA3 StringBuffer A4 Boolean

Page 49 of 165

Page 50: Core Java 1

Q22 Select the correct syntax for main method :A1 public void main(String args[])A2 public static void main(String args)A3 public static void Main(String args[])A4 None of the Above

Q23 Which of the following are not a valid declarations?A1 float f = 1;A2 float f = 1.2f;A3 float f = 1.2;A4 float f = (float)1.2;

Q24 String s1 = new String("hi"); String s2 = "hi"; System.out.println(s1 ==s2); System.out.println(s1.equals(s2));

A1 false trueA2 true falseA3 true trueA4 None of the above.

Q25 Integer i = new Integer(0); Float f = new Float(0); System.out.println(i==f); System.out.println(i.equals(f));

A1 true falseA2 false trueA3 true trueA4 Compiler errorAnswers 1 A1 is correct Local Array variables are initialized to their default values. 2 A3)double []arr = new double[10]; A1 is not valid because (new int[]) array size

must be specified unless it is annonymous array declaration. A2 is not valid because array declaration must not specify the size of the array.

3 A2) Compiler Error non-static(instance) methods or variables are cannot be referenced from static context.Compiler will give the following error while compiling the above code: Test3.java:6: non-static method method() cannot be referenced from a static context method();

4 A1) "Called" Static methods canbe called only by referance.Because its not binded with objects.So, in this case "null" value doesn't make sense."method" is called without any problem.

5 A2)"Constructor2" Constructors cannot have return types. If its declared with return types then it is consider as methods. So, output wull be "Constructor2".

6 A1)47 A3)Hai Hi8 A1)public Test8(){} A3)protected Test8(int k){}9 A3)final void method(){} A4)int method(){}

Page 50 of 165

Page 51: Core Java 1

10 A1) class Test10 A2) public Test10 A3) final Test1011 b)variable12 c)final static class() d)public final strictfp class{} 13 b)public className() c)private className()14 c)public d)final e)abstract15 b)int arr[][] = new int [10][10]; d)float arr[] = new float[10];16 c)Static17 d)Compiler Error18 a)Main Method119 c)Runtime Exception20 d)Compiler Error21 A1)String A4)String Buffer 22 A4)None of the Above23 A3)float f = 1.2;24 A1) false true25 A4)Compiler error

<img> <img>Mock Exam - 2

Q1 import java.util.*; public class Test1{ public static void main(String a[]){ Set s = new TreeSet(); s.add(new Person(20)); s.add(new Person(10)); System.out.println(s); } } class Person{ Person(int i){} } What is the output?

A1 10 20A2 ClassCastExceptionA3 Compiler ErrorA4 Compiler Error

Q2 import java.util.*; public class Test2{ public static void main(String a[]){ Map s = new Hashtable(); s.put(null,null); System.out.println(s); } } What is the output?

A1 [null = null] A2 NullPointerExceptionA3 nullA4 []

Q3 import java.util.*; public class Test3{ public static void main(String a[]){ Map s = new WeakHashMap(10); s.put(null,null); System.out.println(s); } } What is the output?

A1 [null = null]A2 NullPointerExceptionA3 nullA4 []

Page 51 of 165

Page 52: Core Java 1

Q4 import java.util.*; public class Test4{ public static void main(String a[]){ Map s = new LinkedHashMap(); s.put("1","one"); s.put("3","three"); s.put("2","two"); System.out.println(s); } } What is the output?

A1 [1=one,3=three,2=two]A2 NullPointerExceptionA3 [1=one,2=two,3=three]A4 []

Q5 import java.util.*; public class Test5{ public static void main(String a[]){ Map s = new HashMap(); s.put("1","one"); s.put("3","three"); s.put("2","two"); System.out.println(s); } } What is the output?

A1 [1=one,3=three,2=two]A2 [3=three,2=two,1=one]A3 cannot predict the orderA4 []

Q6 public class Test6{ public static void method(float f){ System.out.println("Float"); } public static void method(double f){ System.out.println("Double"); } public static void main(String a[]){ float f1 = 2.0f; float f2 = 2.0f; method(1.0); method(1.0f); method(1.0f*2.0f); method(1.0f*2.0); method(f1*f2); } } What is the output?

A1 Double Double Double Double DoubleA2 Float Float Float Float FloatA3 Double Float Float Double FloatA4 Double Float Float Double Double

Q7 public class Test7{ public static void method(byte b){ System.out.println("Byte"); } public static void method(int i){ System.out.println("Int"); } public static void main(String a[]){ byte b = 1; method(1); method(128); method((byte)128); method(b); } } What is the output?

A1 Byte Int Int ByteA2 Byte Int Int ByteA3 Byte Int Byte ByteA4 Int Int Byte Byte

Q8 public class Test8{ public static void main(String a[]){ byte b = 1; char c = 2; short

Page 52 of 165

Page 53: Core Java 1

s = 3; int i = 4; c = b; // 1 s = b; // 2 i = b; //3 s = c * b; //4 } } Which of the following are correct?

A1 Error at mark 1A2 Error at mark 2A3 Error at mark 3A4 Error at mark 4

Q9 public class Test9{ public static void main(String a[]){ final byte b = 1; char c = 2; short s = 3; int i = 4; c = b; // 1 s = b; // 2 i = b; //3 s = c * b; //4 } } Which of the following are correct?

A1 Error at mark 1A2 Error at mark 2A3 Error at mark 3A4 Error at mark 4

Q10 public class Test10{ public static void main(String a[]){ String s1 = "Sun"; String s2 = "Java"; s1.concat(s2); System.out.println(s1); } } What is output?

A1 SunA2 JavaA3 SunJavaA4 JavaSun

Q11 public class Test11{ public static void main(String a[]){ Integer i1 = new Integer(127); Integer i2 = new Integer(127); Long l = new Long(127); System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println(i1.equals(l)); } } What is output?

A1 false true trueA2 true true trueA3 false true falseA4 Compiler Error

Q12 public class Test12{ public static void main(String a[]){ byte b = 100; Byte b1= new Byte(100); Byte b2 = new Byte(b); System.out.println(b1 == b2); System.out.println(b1.equals(b2)); } } What is output?

A1 false trueA2 true falseA3 true true

Page 53 of 165

Page 54: Core Java 1

A4 Compiler Error

Q13 public class Test13{ public static void method(String s){ System.out.println("String Version"); } public static void method(StringBuffer sb){ System.out.println("String Buffer Version"); } public static void main(String a[]){ method(null); } } What is output?

A1 String VersionA2 String Buffer VersionA3 Runtime ExceptionA4 Compiler Error

Q14 public class Test14{ static String s ="Instance"; public static void method(String s){ s+="Add"; } public static void main(String a[]){ Test14 t = new Test14(); s = "New Instance"; String s = "Local"; method(s); System.out.println(s); System.out.println(t.s); } } What is output?

A1 Local InstanceA2 Local New InstanceA3 Loca Add New InstanceA4 Compiler Error

Q15 public class Test15{ public static void method(StringBuffer sb){ sb.append(" Added"); sb = new StringBuffer("Hai"); } public static void main(String a[]){ StringBuffer sb = new StringBuffer("String Buffer"); method(sb); System.out.println(sb); } } What is output?

A1 String BufferA2 String Buffer AddedA3 HaiA4 Compiler Error

Q16 public class Test16{ public static void method(StringBuffer sb){ sb = new StringBuffer("Hai"); sb.append(" Added"); } public static void main(String a[]){ StringBuffer sb = new StringBuffer("String Buffer"); method(sb); System.out.println(sb); } } What is output?

A1 String BufferA2 String Buffer AddedA3 Hai

Page 54 of 165

Page 55: Core Java 1

A4 Compiler Error

Q17 import java.util.*; public class Test17{ public static void main(String a[]){ Map m = new Hashtable(10,0.75f); System.out.println(m.size()); } } What is output?

A1 A2 10A3 7A4 cOMPILER eRROR

Q18 What is the default capacity of java.util.Hashtable?A1 10A2 16A3 11A4 20

Q19 What is the default capacity of java.util.HashMap?A1 10A2 16A3 11A4 20

Q20 Which of the following classes has synchronized methods?A1 ArrayListA2 VectorA3 HashTableA4 WeakHashMap

Q21 public class Test21{ public static void main(String a[]){ String s1 = new String("Hai"); String s2 = "Hai"; String s3 = new String("Hai").intern(); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s2 == s3); } } What is output?

A1 false false trueA2 true false trueA3 false false falseA4 false true true

Q22 public class Test22{ public static void main(String a[]){ String s1 = "SunMicroSystems"; System.out.println(s1.substring(0)); System.out.println(s1.substring(1,4)); System.out.println(s1.substring(8)); } } What is output?

A1 SunMicrosystems sun oSystem

Page 55 of 165

Page 56: Core Java 1

A2 SunMicrosystems unM SystemsA3 StringIndexOutOfBoundsExceptionA4 None Of the above

Q23 public class Test23{ public static void main(String a[]){ String s1 = "Sun"; System.out.println(s1.substring(5)); } } What is output?

A1 -1A2 0A3 StringIndexOutOfBoundsExceptionA4 ArrayIndexOutOfBoundsException

Q24 Which of the following are static methods in java.lang.String class?A1 valueOfA2 lengthA3 indexOfA4 All the above.

Q25 public class Test25{ public static void main(String a[]){ StringBuffer sb = new StringBuffer(8); sb.append("TestString"); System.out.println(sb.capacity()); System.out.println(sb.length()); } } What is output?

A1 8 10A2 10 10A3 18 10A4 18 18Answers 1 ClassCastException2 NullPointerException3 [null = null]4 [1=one,3=three,2=two]5 cannot predict the order.6 Double Float Float Double Float7 Int Int Byte Byte8 Error at mark 1 Error at mark 49 Error at mark 410 Sun11 false true false12 Compiler Error13 Compiler Error14 Local New Instance15 String Buffer Added16 String Buffer17 018 11

Page 56 of 165

Page 57: Core Java 1

19 1620 Vector HashTable21 false false true22 SunMicrosystems unM Systems23 StringIndexOutOfBoundsException24 valueOf25 18 10

<img> <img>Mock Exam - 3

Q1 public class Test1{ public static void main(String args[]){ System.out.println(method()); } public static int method(){ try{ return 1; } catch(Exception e){ return 2; } finally{ return 3; } } } What will be the output?

A1 1A2 2A3 3A4 0

Q2 public class Test2{ public static void main(String args[]){ System.out.println(method()); } public static int method(){ try{ throw new Exception(); return 1; } catch(Exception e){ return 2; } finally{ return 3; } } } What will be the output? 1

A1 2A2 3A3 4A4 Compiler error

Q3 public class Test3{ public static void main(String args[]){ System.out.println(method()); } public static int method(){ try{ throw new Exception(); } catch(Exception e){ throw new Exception(); } finally{ return 3; } } } What will be the output?

A1 3A2 0A3 Runtime ExceptionA4 Compiler error

Page 57 of 165

Page 58: Core Java 1

Q4 public class Test4{ public static void main(String args[]){ System.out.println(method()); } public static int method(){ return; } } What will be the output?

A1 nullA2 0A3 Runtime exceptionA4 Compiler error

Q5 import java.io.IOException; public class Test5{ public static void main(String args[]){ try{ throw new IOException(); } catch(Exception e){ System.out.println("Excepion"); } catch(IOException e){ System.out.println("IOExcepion"); } } } What will be the output?

A1 ExceptionA2 IOExceptionA3 Exception IOExceptionA4 Compilers error

Q6 public class Test6{ public static void main(String args[]) throws Exception{ try{ throw new Exception(); } finally{ System.out.println("No Error"); } } } What will be the output?

A1 No Error followed by java.lang.ExceptionA2 java.lang.Exception followed by No ErrorA3 No ErrorA4 Compiler Error

Q7 ublic class Test7{ public static void main(String args[]) throws Exception{ Test7 t = new Test7(); t.method(); System.out.println("Print"); } public void method()throws Exception{ throw new Exception(); } } What will be the output?

A1 PrintA2 Exception thrown at runtimeA3 no outputA4 Compiler Error

Q8 public class Test8{ public static void main(String args[]) throws Exception{ Test8 t = new Test8(); t.method(); System.out.println("Print"); } public void method(){ try{ throw new

Page 58 of 165

Page 59: Core Java 1

Exception(); }catch(Exception e){} } } What will be the output?

A1 PrintA2 Exception thrown at runtimeA3 no outputA4 Compiler Error

Q9 public class Test9 extends A{ public static void main(String args[]) throws Exception{ Test9 t = new Test9(); } } class A{ A() throws Exception{ System.out.println("A Class"); } } What will be the output?

A1 A ClassA2 RuntimxceptionA3 no outputA4 Compiler Error

Q10 public class Test10 extends A{ Test10()throws Exception{ System.out.println("Test10 Class"); } public static void main(String args[]) throws Exception{ Test10 t = new Test10(); } } class A{ A() throws Exception{ System.out.println("A Class"); } } What will be the output?

A1 A Class Test10 ClassA2 RuntimxceptionA3 no outputA4 Compiler Error

Q11 public class Test11 extends A{ Test11()throws Exception{ System.out.println("Test10 Class"); } Test11(int i){} public static void main(String args[]) throws Exception{ Test11 t = new Test11(); } } class A{ A() throws Exception{ System.out.println("A Class"); } } What will be the output?

A1 A Class Test10 ClassA2 RuntimxceptionA3 no outputA4 Compiler Error

Q12 import java.io.IOException; public class Test12 extends A{ public void method() throws Exception{ System.out.println("Subclass"); } public static void main(String args[]) throws Exception{ A a = new A(); a.method(); a = new Test12(); a.method(); } } class A{ public void method() throws

Page 59 of 165

Page 60: Core Java 1

IOException{ System.out.println("Superclass"); } } What will be the output?

A1 Subclass SuperclassA2 RuntimxceptionA3 Superclass SuperclassA4 Compiler Error

Q13 What are the legal arguments types for switch?A1 intA2 byteA3 charA4 All the above.

Q14 Which of the following are valif if constructs?A1 if(2>3){}A2 if(false){}A3 if(false){}A4 if(true)

Q15 public class Test15{ public static void main(String args[]) throws Exception{ for (int i = 0;i < 3;i++){ for (int j = 0;j < 3;j++){ System.out.print(i); System.out.print(j+","); break; } } } } What will be the output?

A1 00,A2 00,10,20,A3 000102A4 None of the above

Q16 public class Test16 extends A{ Test16(){ System.out.println("Sub"); } public static void main(String args[]) { Test16 t = new test16(); } } class A{ A(int i){ System.out.println("Super"); } } What will be the output?

A1 Super SubA2 SuperA3 SubA4 Compiler Error

Q17 public class Test17 extends A{ Test17(int i){ System.out.println(i); super(2); } public static void main(String args[]) { Test17 t = new Test17(5); } } class A{ A(int i){ System.out.println(i); } } What will be the output?

Page 60 of 165

Page 61: Core Java 1

A1 5 2 A2 2 5 A3 5 5A4 Compiler error

Q18 public class Test18 { Test18(){ this(7); } Test18(int i){ this(1.0); Test18(i); } Test18(float f){ System.out.println(f * 2); } Test18(double d){ System.out.println(d * 3); } void Test18(int i){ System.out.println(i); } public static void main(String args[]) { Test18 t = new Test18(); } } What will be the output?

A1 3.0 7A2 2.0 7A3 7 3.0A4 Compiler Error

Q19 public class Test19 { float f; Test19(){ this(f); f = 3; } Test19(float f){ System.out.println(f); } public static void main(String args[]) { Test19 t = new Test19(); } } What will be the output?

A1 0.0A2 0A3 3A4 Compiler error

Q20 public class Test20 extends A{ Test20(){ this("Hi"); } Test20(String str){ System.out.println(str); } public static void main(String args[]) { Test20 t = new Test20(); } } class A{ A(){ System.out.println("Super"); } } What will be the output?

A1 Super HiA2 Hi SuperA3 SuperA4 Compiler Error

Q21 public class Test21{ public static void main(String args[]) { Test21 t; t.method(); } public static void method(){ System.out.println("NullPointerException"); } } What will be the output?

A1 Nothing is printed.

Page 61 of 165

Page 62: Core Java 1

A2 RuntimeExceptionA3 NullPointerExceptionA4 Compiler Error

Q22 public class Test22{ public static void main(String args[]) { Test22 t = null; t.method(); } public static void method(){ System.out.println("NullPointerException"); } } What will be the output?

A1 Nothing is printed.A2 RuntimeExceptionA3 NullPointerExceptionA4 Compiler Error

Q23 Which of the following modifiers are allowed in constructor?A1 privateA2 defaultA3 publicA4 static

Q24 Which of the following modifiers are allowed for top-level classes?A1 privateA2 staticA3 publicA4 strictfp

Q25 which one of the keyword cannot be used with instance variables?A1 transientA2 volatileA3 synchronizedA4 abstractAnswers 1 32 Compiler Error3 Compiler Error4 Compiler Error5 Compiler Error6 No Error followed by java.lang.Exception7 Exception thrown at runtime8 Print9 Compiler Error10 A Class Test10 Class11 Compiler Error12 Compiler Error13 All the above14 if(2>3){} if(false){} if(true){}

Page 62 of 165

Page 63: Core Java 1

15 00,10,20,16 Compiler Error17 Compiler Error18 3.0 719 Compiler Error20 Super Hi21 Compiler Error22 NullPointerException23 private public24 strictfp public25 synchronized abstrct

Mock Exam - 1

Q1 Which of the following are valid array construction?A1 int a[] = new int[];A2 int a[] = new int[10];A3 int a[10] = new int[];A4 int a[] = new a[10];

Q2 Which of the following are valid array initialization?A1 new int[] = {1,2,3,4};A2 new int[10] = {1,2,3,4};A3 new int[] = 1,2,3,4;A4 new int[] = [1,2,3,4];

Q3 public class Test3{ public static void main(String args[]){ int arr[] = new int[10]; int i = 5; arr[i++] = ++i+i++; System.out.print(arr[5]+":"+arr[6]); } } What will be the output?

A1 14:0A2 0:14A3 0:0A4 Compiler Error

Q4 public class Test4{ public static void main(String args[]){ int arr[] = new int[10]; int i = 5; arr[i++] = i+++i++; System.out.print(arr[5]+":"+arr[6]); } } What will be the output?

A1 13:0A2 14:0A3 0:14A4 0:13

Page 63 of 165

Page 64: Core Java 1

Q5 public class Test5{ static Boolean bl[] = new Boolean[5]; public static void main(String args[]){ System.out.println(bl[0]); } } What will be the output?

A1 nullA2 falseA3 trueA4 Compiler Error

Q6 Which of the following are valid array declarations?A1 String[][] a1 = String[20][20];A2 String[][] a2 = new String[20,20];A3 String[][] a3 = new String[20][20];A4 String a4[][] = new String[20][20];A5 String[] a5[] = new String[20][20];

Q7 Which of the following code correctly creates an array of four initialized string objects?.

A1 String players[] = new String[4];A2 String players[] = {"","","",""};A3 String players[]; players = new String[4];A4 None of the above.

Q8 char[] c = new char[100]; what is the value of c[50]? A1 50A2 49A3 '\u0000'A4 '\u0020'

Q9 which of the following are valid arrays?A1 new float[10]={1,2};A2 new float[]={1,2};A3 new float={1,2};A4 none of the above

Q10 public class Test10{ public void method(int arr[]){ arr[0] = 10; } public static void main(String[] args){ int[] small = {1,2,3}; Test10 t= new Test10(); t.method(small); System.out.println(small[0]); } } what will be the output?

A1 0A2 1A3 0A4 none of the aboveAnswers 1 A2)int a[] = new int[10];2 A1)new int[] = {1,2,3,4};

Page 64 of 165

Page 65: Core Java 1

3 A1)14:04 A1)13:05 A1)null6 A3 : String[][] a3 = new String[20][20]; A4 : String a4[][] = new String[20][20];

A5 : String[] a5[] = new String[20][20];7 A2 : String players[] = {"","","",""};8 c) '\u0000'9 b)new float[]={1,2};10 10

Mock Exam - 1

Q1 public class Test1 extends Base{ Test1(long i){} public static void main(String args[]){} } class Base{ Base(int i){} } Which of the following constructors are if present in Base, will the program will compile properly?

A1 Base(){}A2 Base(long l){}A3 Base(float f){}A4 It will compile properly.

Q2 public class Test2 extends Base{ Test2(long i){ super(2); } Test2(){} public static void main(String args[]){ new Test2(2); } } class Base{ Base(int i){ System.out.println(i); } Base(byte i){ System.out.println(i*2); } } what will be the output?

A1 2A2 4A3 Compiler ErrorA4 It will compile if we add super(2) in Test2().

Q3 which of the following keywords can be applied to constructors?A1 privateA2 publicA3 voidA4 All the above

Q4 which of the following are valid constructors?A1 Test4(){}A2 void Test4(){}A3 private Test4(){}A4 All the above.

Q5 which of the following statements are true?

Page 65 of 165

Page 66: Core Java 1

A1 constructors can be overriden.A2 constructors can be overloadedA3 constructors can return a valueA4 constructors can be static.

Q6 public class Test6{ Test6(int i, int j){ System.out.println("Int"); } Test6(short i, short j){ System.out.println("Short"); } Test6(byte i, byte j){ System.out.println("Byte"); } public static void main(String args[]){ Test6 t = new Test6(1,2); } } which of the following statements are true?

A1 IntA2 ByteA3 ShortA4 Compiler error

Q7 public class Test7{ Test7(int i, int j){ System.out.println("Int"); } Test7(short i, short j){ System.out.println("Short"); } Test7(byte i, byte j){ System.out.println("Byte"); } public static void main(String args[]){ byte b1 = 1; byte b2 = 2; Test7 t1 = new Test7(b1,b1); Test7 t2 = new Test7(b1*1,b1*2); Test7 t3 = new Test7(b1*b1,b1*b2); } } what will be the output?

A1 Byte Int IntA2 Byte Byte ByteA3 Byte Int ByteA4 Int Int Int

Q8 public class Test8 extends Base{ Test8(){} public static void main(String args[]){} } class Base{ Base() throws Exception{} } what is the output?

A1 Byte Int IntA2 Byte Byte ByteA3 Byte Int ByteA4 Int Int Int

Q9 public class Test9 extends Base{ Test9()throws Exception{} Test9(int i) {} public static void main(String args[])throws Exception{ new Test9(); } } class Base{ Base() throws Exception{ System.out.println("Base"); } } what is

Page 66 of 165

Page 67: Core Java 1

the output?A1 BaseA2 Compiler Error.A3 Runtime exceptionA4 none of the above

Q10 public class Test10 extends Base{ Test10(){} Test10(int i) {} public static void main(String args[]){ new Test10(); } } class Base{ Base() { System.out.println("Base"); } Base(int i) throws Exception{ System.out.println("No argument constructor."); } } what is the output?

A1 BaseA2 Compiler Error.A3 Runtime exceptionA4 none of the above11 class Test11 extends B { private Test11()

{ super(); } public Test11(String msg) { this(); } public Test11(int i) {} } class A { public A() {} public A(int i) { this(); } } class B extends A { public boolean B(String msg) { return false; } } Select all valid answers:

A1 The code will fail to compile.A2 The constructor in A that takes an int as argument will never be called as a

result of constructing an object of class B or Test11.A3 class Test11 has three constructors.A4 At most one of the constructors of each class is called as a result of constructing

an object of class Test11.12 Which of the following statements are true?A1 The default constructor has a return type of voidA2 The default constructor takes a sungle parameter.A3 The default constructor takes no parametersA4 The default constructor is not created if the class has anyconstructors of its

own.Answers 1 Base(){}2 It will compile if we add super(2) in Test2().3 private public4 Test4(){} private Test4(){}5 constructors can be overloaded.6 Int7 Byte Byte Int8 Byte Byte Int9 Compiler Error.10 Base

Page 67 of 165

Page 68: Core Java 1

11 B) The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11. C)class Test11 has three constructors.

12 3) The default constructor takes no parameters 4) The default constructor is not created if the class has any constructors of its own.

Mock Exam - 1

Q1 public class Test1{ private static int i = 10; static class Inner extends Test1{ int i; public void method(){ System.out.println(i); } } public static void main(String args[]){ new Test10.Inner().method(); } } which of the following statement in //? will print "Inner class"?

A1 10A2 0A3 nothing is printed.A4 Compiler error.

Q2 which of the following modifiers are applied to the nested classes class?A1 privateA2 protectedA3 publicA4 staticA5 All the above.

Q3 which of the following modifiers are applied to the top level classes ?A1 staticA2 privateA3 strictfpA4 abstract A5 All the above.

Q4 which modifer is uesd to stop overridding a method?A1 finalA2 staticA3 abstractA4 None of the above.

Q5 public abstract final class Test5{ public static void main(String args[]){ System.out.println("Modifiers"); } } what will be the output?

A1 Modifiers

Page 68 of 165

Page 69: Core Java 1

A2 Compiler errorA3 exception thrown at runtimeA4 None of the above

Q6 which of the following are valid syntax for a toplevel class?A1 public static class Test6{}A2 public abstrct class Test6{}A3 private final class Test6{}A4 public final class Test6{}

Q7 which of the following are valid syntax for nested classes?A1 private final abstract class Test7{}A2 private final class Test7{}A3 private static class Test7{}A4 protected abstract class Test7{}

Q8 public class Test8{ class Inner{ public void method(){ System.out.println("Inner class"); } } public static void main(String args[]){ // ? } } which of the following statement in //? will print "Inner class"?

A1 new Test8.Inner().method();A2 new Test8().Inner().method();A3 new Test8.Inner.method();A4 Compiler error.

Q9 public class Test9{ static class Inner{ public void method(){ System.out.println("Inner class"); } } public static void main(String args[]){ // ? } } which of the following statement in //? will print "Inner class"?

A1 new Test9.Inner().method();A2 new Test9().Inner().method();A3 new Test9.Inner.method();A4 Compiler error.

Q10 which of the following modifiers are applied to local classes(classses inside method)?.

A1 privateA2 publicA3 protectedA4 None of the above.Answers 1 new Test8.Inner().method();.2 All the above.

Page 69 of 165

Page 70: Core Java 1

3 strictfp abstract4 final5 Compiler error6 public abstrct class Test6{} public final class Test6{}7 private final class Test7{} private static class Test7{} protected abstract class

Test7{}8 Compiler error.9 new Test8.Inner().method();.10 None of the above

Page 70 of 165

Page 71: Core Java 1

1. Which of the following are valid definitions of an application's main( ) method?

a) public static void main();

b) public static void main( String args );

c) public static void main( String args[] );

d) public static void main( Graphics g );

e) public static boolean main( String args[] );

2. If MyProg.java were compiled as an application and then run from the command line as: java MyProg I like tests

what would be the value of args[ 1 ] inside the main( ) method?

a) MyProg

b) "I"

c) "like"

d) 3

e) 4

f) null until a value is assigned

3. Which of the following are Java keywords?

a) array

b) boolean

c) Integer

d) protect

e) super

4. After the declaration:

char[] c = new char[100];

Page 71 of 165

Page 72: Core Java 1

what is the value of c[50]?

a) 50

b) 49

c) '\u0000'

d) '\u0020'

e) " "

f) cannot be determined

g) always null until a value is assigned

5. After the declaration:

int x;

the range of x is:

a) -231 to 231-1

b) -216 to 216 - 1

c) -232 to 232

d) -216 to 216

e) cannot be determined; it depends on the machine

6. Which identifiers are valid?

a) _xpoints

b) r2d2

c) bBb$

d) set-flow

e) thisisCrazy

7. Represent the number 6 as a hexadecimal literal.

Page 72 of 165

Page 73: Core Java 1

8. Which of the following statements assigns "Hello Java" to the String variable s?

a) String s = "Hello Java";

b) String s[] = "Hello Java";

c) new String s = "Hello Java";

d) String s = new String("Hello Java");

9. An integer, x has a binary value (using 1 byte) of 10011100. What is the binary value of z after these statements:

int y = 1 << 7;

int z = x & y;

a) 1000 0001

b) 1000 0000

c) 0000 0001

d) 1001 1101

e) 1001 1100

10. Which statements are accurate:

a) >> performs signed shift while >>> performs an unsigned shift.

b) >>> performs a signed shift while >> performs an unsigned shift.

c) << performs a signed shift while <<< performs an insigned shift.

d) <<< performs a signed shift while << performs an unsigned shift.

11. The statement ...

String s = "Hello" + "Java";

Page 73 of 165

Page 74: Core Java 1

yields the same value for s as ...

String s = "Hello";String s2= "Java";

s.concat( s2 );

True

False

12. If you compile and execute an application with the following code in its main() method:

String s = new String( "Computer" );

if( s == "Computer" )System.out.println( "Equal A" );

if( s.equals( "Computer" ) )System.out.println( "Equal B" );

a) It will not compile because the String class does not support the = = operator.

b) It will compile and run, but nothing is printed.

c) "Equal A" is the only thing that is printed.

d) "Equal B" is the only thing that is printed.

e) Both "Equal A" and "Equal B" are printed.

13. Consider the two statements:1. boolean passingScore = false && grade == 70;2. boolean passingScore = false & grade == 70;

The expression

grade == 70

is evaluated:

a) in both 1 and 2

b) in neither 1 nor 2

c) in 1 but not 2

d) in 2 but not 1

e) invalid because false should be FALSE

Page 74 of 165

Page 75: Core Java 1

14. Given the variable declarations below:

byte myByte;int myInt;long myLong;char myChar;float myFloat;

double myDouble;

Which one of the following assignments would need an explicit cast?

a) myInt = myByte;

b) myInt = myLong;

c) myByte = 3;

d) myInt = myChar;

e) myFloat = myDouble;

f) myFloat = 3;

g) myDouble = 3.0;

15. Consider this class example:class MyPoint { void myMethod() { int x, y; x = 5; y = 3; System.out.print( " ( " + x + ", " + y + " ) " ); switchCoords( x, y ); System.out.print( " ( " + x + ", " + y + " ) " ); } void switchCoords( int x, int y ) { int temp; temp = x; x = y; y = temp; System.out.print( " ( " + x + ", " + y + " ) " ); }}

What is printed to standard output if myMethod() is executed?

a) (5, 3) (5, 3) (5, 3)

Page 75 of 165

Page 76: Core Java 1

b) (5, 3) (3, 5) (3, 5)

c) (5, 3) (3, 5) (5, 3)

16. To declare an array of 31 floating point numbers representing snowfall for each day of March in Gnome, Alaska, which declarations would be valid?

a) double snow[] = new double[31];

b) double snow[31] = new array[31];

c) double snow[31] = new array;

d) double[] snow = new double[31];

17. If arr[] contains only positive integer values, what does this function do?public int guessWhat( int arr[] ){ int x= 0; for( int i = 0; i < arr.length; i++ ) x = x < arr[i] ? arr[i] : x; return x;}

a) Returns the index of the highest element in the array

b) Returns true/false if there are any elements that repeat in the array

c) Returns how many even numbers are in the array

d) Returns the highest element in the array

e) Returns the number of question marks in the array

18. Consider the code below:

arr[0] = new int[4];arr[1] = new int[3];arr[2] = new int[2];arr[3] = new int[1];for( int n = 0; n < 4; n++ )

System.out.println( /* what goes here? */ );

Which statement below, when inserted as the body of the for loop, would print the number of values in each row?

Page 76 of 165

Page 77: Core Java 1

a) arr[n].length();

b) arr.size;

c) arr.size -1;

d) arr[n][size];

e) arr[n].length;

19. If size = 4, triArray looks like:

int[][] makeArray( int size) { int[][] triArray = new int[size] []; int val=1; for( int i = 0; i < triArray.length; i++ ) { triArray[i] = new int[i+1];

for( int j=0; j < triArray[i].length; j++ ) { triArray[i][j] = val++; } } return triArray;}

a) 1 2 3 4 5 6 7 8 9 10

b) 1 4 9 16 c) 1 2 3 4 d) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

e) 1 2 3 4 5 6 7 8 9 10

20. Which of the following are legal declarations of a two-dimensional array of integers?

a) int[5][5]a = new int[][];

b) int a = new int[5,5];

c) int[]a[] = new int[5][5];

d) int[][]a = new[5]int[5];

21. Which of the following are correct methods for initializing the array "dayhigh" with 7 values?

a) int dayhigh = { 24, 23, 24, 25, 25, 23, 21 };

b) int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 };

Page 77 of 165

Page 78: Core Java 1

c) int[] dayhigh = { 24, 23, 24, 25, 25, 23, 21 };

d) int dayhigh [] = new int[24, 23, 24, 25, 25, 23, 21];

e) int dayhigh = new[24, 23, 24, 25, 25, 23, 21];

22. If you want subclasses to access, but not to override a superclass member method, what keyword should precede the name of the superclass method?

23. If you want a member variable to not be accessible outside the current class at all, what keyword should precede the name of the variable when declaring it?

24. Consider the code below:

public static void main( String args[] ){ int a = 5; System.out.println( cube( a ) );}int cube( int theNum ){ return theNum * theNum * theNum;}

What will happen when you attempt to compile and run this code?

a) It will not compile because cube is already defined in the java.lang.Math class.

b) It will not compile because cube is not static.

c) It will compile, but throw an arithmetic exception.

d) It will run perfectly and print "125" to standard output.

25. Given the variables defined below:

int one = 1;int two = 2;

Page 78 of 165

Page 79: Core Java 1

char initial = '2';

boolean flag = true;

Which of the following are valid?

a) if( one ){}

b) if( one = two ){}

c) if( one == two ){}

d) if( flag ){}

e) switch( one ){}

f) switch( flag ){}

g) switch( initial ){}

26. If val = 1 in the code below:

switch( val ) { case 1: System.out.print( "P" ); case 2: case 3: System.out.print( "Q" ); break; case 4: System.out.print( "R" ); default: System.out.print( "S" );}

Which values would be printed?

a) P

b) Q

c) R

d) S

27. Assume that val has been defined as an int for the code below:

if( val > 4 ) { System.out.println( "Test A" );}else if( val > 9 ) { System.out.println( "Test B" );}

Page 79 of 165

Page 80: Core Java 1

else System.out.println( "Test C" );

Which values of val will result in "Test C" being printed:

a) val < 0

b) val between 0 and 4

c) val between 4 and 9

d) val > 9

e) val = 0

f) no values for val will be satisfactory

28. What exception might a wait() method throw?

29. For the code:

m = 0;while( m++ < 2 ) System.out.println( m );

Which of the following are printed to standard output?

a) 0

b) 1

c) 2

d) 3

e) Nothing and an exception is thrown

30. Consider the code fragment below:

outer: for( int i = 1; i <3; i++ ) { inner: for( j = 1; j < 3; j++ ) { if( j==2 ) continue outer; System.out.println( "i = " +i ", j = " + j ); }

Page 80 of 165

Page 81: Core Java 1

}

Which of the following would be printed to standard output?

a) i = 1, j = 1

b) i = 1, j = 2

c) i = 1, j = 3

d) i = 2, j = 1

e) i = 2, j = 2

f) i = 2, j = 3

g) i = 3, j = 1

h) i = 3, j = 2

31. Consider the code below:void myMethod() { try { fragile(); } catch( NullPointerException npex ) { System.out.println( "NullPointerException thrown " ); } catch( Exception ex ) { System.out.println( "Exception thrown " ); } finally { System.out.println( "Done with exceptions " ); } System.out.println( "myMethod is done" );}

What is printed to standard output if fragile() throws an IllegalArgumentException?

a) "NullPointerException thrown"

b) "Exception thrown"

c) "Done with exceptions"

Page 81 of 165

Page 82: Core Java 1

d) "myMethod is done"

e) Nothing is printed

32. Consider the following code sample:class Tree{}class Pine extends Tree{}class Oak extends Tree{}public class Forest { public static void main( String[] args ) { Tree tree = new Pine();

if( tree instanceof Pine ) System.out.println( "Pine" );

if( tree instanceof Tree ) System.out.println( "Tree" );

if( tree instanceof Oak ) System.out.println( "Oak" );

else System.out.println( "Oops" ); }}

Select all choices that will be printed:

a) Pine

b) Tree

c) Forest

d) Oops

e) (nothing printed)

33. Consider the classes defined below:import java.io.*;class Super {

int methodOne( int a, long b ) throws IOException { // code that performs some calculations

}float methodTwo( char a, int b ){ // code that performs other calculations

}}public class Sub extends Super{

Page 82 of 165

Page 83: Core Java 1

}

Which of the following are legal method declarations to add to the class Sub? Assume that each method is the only one being added.

a) public static void main( String args[] ){}

b) float methodTwo(){}

c) long methodOne( int c, long d ){}

d) int methodOne( int c, long d ) throws ArithmeticException{}

e) int methodOne( int c, long d ) throws FileNotFoundException{}

34. Assume that Sub1 and Sub2 are both subclasses of class Super.

Given the declarations:

Super super = new Super();Sub1 sub1 = new Sub1();Sub2 sub2 = new Sub2();

Which statement best describes the result of attempting to compile and execute the following statement:

super = sub1;

a) Compiles and definitely legal at runtime

b) Does not compile

c) Compiles and may be illegal at runtime

35. For the following code:class Super { int index = 5; public void printVal() { System.out.println( "Super" ); }}class Sub extends Super{ int index = 2; public void printVal() { System.out.println( "Sub" ); }}public class Runner

Page 83 of 165

Page 84: Core Java 1

{ public static void main( String argv[] ) { Super sup = new Sub(); System.out.print( sup.index + "," ); sup.printVal(); }}

What will be printed to standard output?

a) The code will not compile.

b) The code compiles and "5, Super" is printed to standard output.

c) The code compiles and "5, Sub" is printed to standard output.

d) The code compiles and "2, Super" is printed to standard output.

e) The code compiles and "2, Sub" is printed to standard output.

f) The code compiles, but throws an exception.

36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;String newName = "Nick";newName = "Jason";name = "Frieda";

String newestName = name;

name = null;//Line A

a) 0

b) 1

c) 2

d) 3

e) 4

37. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.

b) The finalize method is always called before an object is garbage collected.

Page 84 of 165

Page 85: Core Java 1

c) Any class that includes a finalize method should invoke its superclass' finalize method.

d) Garbage collection behavior is very predictable.

38. What line of code would begin execution of a thread named myThread?

39. Which methods are required to implement the interface Runnable.

a) wait()

b) run()

c) stop()

d) update()

e) resume()

40. What class defines the wait() method?

41. For what reasons might a thread stop execution?

a) A thread with higher priority began execution.

b) The thread's wait() method was invoked.

c) The thread invoked its yield() method.

d) The thread's pause() method was invoked.

e) The thread's sleep() method was invoked.

42. Which method below can change a String object, s ?

Page 85 of 165

Page 86: Core Java 1

a) equals( s )

b) substring( s )

c) concat( s )

d) toUpperCase( s )

e) none of the above will change s

43. If s1 is declared as:

String s1 = "phenobarbital";

What will be the value of s2 after the following line of code:

String s2 = s1.substring( 3, 5 );

a) null

b) "eno"

c) "enoba"

d) "no"

44. What method(s) from the java.lang.Math class might method() be if the statement

method( -4.4 ) == -4;

is true.

a) round()

b) min()

c) trunc()

d) abs()

e) floor()

f) ceil()

Page 86 of 165

Page 87: Core Java 1

45. Which methods does java.lang.Math include for trigonometric computations?

a) sin()

b) cos()

c) tan()

d) aSin()

e) aCos()

f) aTan()

g) toDegree()

46. This piece of code:

TextArea ta = new TextArea( 10, 3 );

Produces (select all correct statements):

a) a TextArea with 10 rows and up to 3 columns

b) a TextArea with a variable number of columns not less than 10 and 3 rows

c) a TextArea that may not contain more than 30 characters

d) a TextArea that can be edited

47. In the list below, which subclass(es) of Component cannot be directly instantiated:

a) Panel

b) Dialog

c) Container

d) Frame

48. Of the five Component methods listed below, only one is also a method of

Page 87 of 165

Page 88: Core Java 1

the class MenuItem. Which one?

a) setVisible( boolean b )

b) setEnabled( boolean b )

c) getSize()

d) setForeground( Color c )

e) setBackground( Color c )

49. If a font with variable width is used to construct the string text for a column, the initial size of the column is:

a) determined by the number of characters in the string, multiplied by the width of a character in this font

b) determined by the number of characters in the string, multiplied by the average width of a character in this font

c) exclusively determined by the number of characters in the string

d) undetermined

50. Which of the following methods from the java.awt.Graphics class would be used to draw the outline of a rectangle with a single method call?

a) fillRect()

b) drawRect()

c) fillPolygon()

d) drawPolygon()

e) drawLine()

51. The Container methods add( Component comp ) and add( String name, Component comp ) will throw an IllegalArgumentException if comp is a:

a) button

Page 88 of 165

Page 89: Core Java 1

b) list

c) window

d) textarea

e) container that contains this container

52. Of the following AWT classes, which one(s) are responsible for implementing the components layout?

a) LayoutManager

b) GridBagLayout

c) ActionListener

d) WindowAdapter

e) FlowLayout

53. A component that should resize vertically but not horizontally should be placed in a:

a) BorderLayout in the North or South location

b) FlowLayout as the first component

c) BorderLayout in the East or West location

d) BorderLayout in the Center location

e) GridLayout

54. What type of object is the parameter for all methods of the MouseListener interface?

55. Which of the following statements about event handling in JDK 1.1 and later are true?

Page 89 of 165

Page 90: Core Java 1

a) A class can implement multiple listener interfaces

b) If a class implements a listener interface, it only has to overload the methods it uses

c) All of the MouseMotionAdapter class methods have a void return type.

56. Which of the following describe the sequence of method calls that result in a component being redrawn?

a) invoke paint() directly

b) invoke update which calls paint()

c) invoke repaint() which invokes update(), which in turn invokes paint()

d) invoke repaint() which invokes paint directly

57. Choose all valid forms of the argument list for the FileOutputStream constructor shown below:

a) FileOutputStream( FileDescriptor fd )

b) FileOutputStream( String n, boolean a )

c) FileOutputStream( boolean a )

d) FileOutputStream()

e) FileOutputStream( File f )

58. A "mode" argument such as "r" or "rw" is required in the constructor for the class(es):

a) DataInputStream

b) InputStream

c) RandomAccessFile

d) File

Page 90 of 165

Page 91: Core Java 1

e) None of the above

59. A directory can be created using a method from the class(es):

a) File

b) DataOutput

c) Directory

d) FileDescriptor

e) FileOutputStream

60. If raf is a RandomAccessFile, what is the result of compiling and executing the following code?

raf.seek( raf.length() );

a) The code will not compile.

b) An IOException will be thrown.

c) The file pointer will be positioned immediately before the last character of the file.

d) The file pointer will be positioned immediately after the last character of the file.

Questions

1. Which of the following primitive data types should be used to represent a 16-bit unsigned integer?

A. int B. char C. short D. byte E. long

2. What is the output of the following program?

public class Example { static int c = 3; public static void main(String [] args) { System.out.print("Kristin wants "); try { System.out.print("a bike, "); if(c > 0) { throw new Exception();

Page 91 of 165

Page 92: Core Java 1

} System.out.print("a robot, "); } catch (Exception e) { System.out.print("a dollhouse, "); System.exit(0); } finally { System.out.print("and an American Girl doll."); } } }

A. Kristin wants a bike, a robot, a dollhouse, and an American Girl doll. B. Kristin wants a bike, a robot, a dollhouse, C. Kristin wants a bike, a robot, D. Kristin wants a bike, E. Kristin wants F. Kristin wants a bike, a dollhouse, G. Kristin wants a bike, a dollhouse, and an American Girl doll.

3. Evan has written a memory-intensive Java program. After a particular portion of the program has run, he wants to free/recycle the memory that was in use. How can he accomplish this?

A. Use the code: finalize() B. Use the code: System.runFinalization C. Declare the process as “protected.” D. He cannot control this.

4. Which of the following is the default value for a primitive data type of “float”?

A. False B. 0.0d C. 0.0f D. 0

5. What is the output of the following program?

public class ExampleTwo { public static void main(String [] args) { String y1 = new String("Spencer"); if (y1 != "Spencer") { System.out.println("You will be picked up early on Tuesday"); } else { System.out.println("You will need to stay all day Tuesday"); } } }

A. You will need to stay all day Tuesday. B. You will be picked up early on Tuesday. C. The output is blank. D. The code generates a compilation error.

6. In regard to constructors, how can you prevent a class from being instantiated?

Page 92 of 165

Page 93: Core Java 1

A. Use the private declaration. B. Use anonymous classes. C. Employ overloading. D. Use only static inner classes.

7. Karen recently moved from C language programming to Java. She is currently having difficulty with threads and their states. Which of the following are valid states that a thread can be in? (Choose all that apply.)

A. Dead B. Stale C. Waiting D. Ready E. Set F. Running

8. Within the java.awt package, which interface must be implemented to receive events when the mouse is dragged?

A. MouseDragged B. MouseMoved C. KeyEvent D. MouseMotionListener

Page 93 of 165

Page 94: Core Java 1

Mock Exam 1 Q1.What will be the result of compiling and running the given

program?Select one correct answer.

1 class Q1 2 { 3 public static void main(String arg[]) 4 { 5 int a[]={2,2}; 6 int b=1; 7 a[b]=b=0; 8 System.out.println(a[0]); 9 System.out.println(a[1]); 10 } 11 }

Compile time error at the line no. 5.

2. Run time error at the line no. 5.

3. Program compiles correctly and print 2,0 when executed.

4. Program compiles correctly and print 0,2 when executed. Q2.What will be the result of compiling and running the given

program?Select one correct answer.

1 class Q22 {3 public static void main(String arg[])4 {5 StringBuffer s[]={"A","B"};6 System.out.println(s[0]+","+s[1]);7 }8 }

Program compiles correctly and print A,B when executed.

2. Compile time error.

3. Run time error. Q3.Which of the given choices are true?Select any two.

1 class Q32 {3 public static void main(String arg[])4 {

Page 94 of 165

Page 95: Core Java 1

5 int[] a,c,d[];6 int b[]=new int[0];7 }8 }

All the variables a,b,c,d are arrays.

2. Only a,d,b are arrays.

3. We can declare an array with zero element.

4. We cannot declare an array with zero element. Q4.What will be the result of compiling and running the given

program?Select one correct answer.

1 public class Q42 { 3 public static void main(String[] args)4 {

5 boolean t1 = true, t2 = false, t3 = t1; 6 boolean t = false; 7 t &&= (t1 || ( t2 && t3)); 8 System.out.println(t);

9 } 10 }

Program compiles correctly and print true when executed.

2. Program compiles correctly and print false when executed.

3. Compile time error.

4. Run time error.

Q5.What will be the result of compiling and running the given program?Select any two.

1 public class Q52 {3 public static void main(String rag[])4 {5 byte b=1;6 b++;7 byte b=127+1;8 int i=2147483647+1;9 b=- -b;

Page 95 of 165

Page 96: Core Java 1

10 }11 }

Compile time error at line no. 6.

2. Compile time error at line no. 7.

3. Compile time error at line no. 8.

4. Compile time error at line no. 9. Q6.What will be the result of compiling and running the given

program?Select one correct answer.

1 class AA{}2 class BB extends AA{}3 class Q64 {5 public static void main(String arg[])6 {7 AA a=null;8 BB b=(BB)a;9 System.out.println(b);10 System.out.println(b instanceof BB);11 System.out.println(b instanceof AA);12 }13 }

Program compiles correctly and print null,true,false

2. Program compiles correctly and print null,true,true.

3. Program compiles correctly and print null,false,false

4. Program compiles correctly and print null,false,true.

5. Compile time error at line no.8 as null value cannot be casted.

6. Run time error at line no. 8 as null value cannot be casted. Q7.Which one of the following statements will give you an

Exception when placed at line no.10?Select one correct answer.

1 class AA{}2 class BB extends AA{}3 class casting4 {

Page 96 of 165

Page 97: Core Java 1

5 public static void main(String arg[])6 {7 AA a=new AA();8 BB b=new BB();9 AA c=new BB();10 //Here11 }11 }

b = (BB) a;

2. a = b ;

3. b = (BB) c;

4. c = b; Q8.Which one of the following are valid character contants?Select any two.

char c = '\u000d' ;

2. char c = '\u000a';

3. char c = '\u0000';

4. char c = '\uface' ; Q9.Which one of the following are not valid character contants?

Select any two.

char c = '\u00001' ;

2. char c = '\101';

3. char c = 65;

4. char c = '\1001' ; Q10.Which will be the first line to cause an error in the following code?

Select one correct answer.

1 class Char2 {3 public static void main(String arg[])4 {5 char c1,c2;6 c1='A';

Page 97 of 165

Page 98: Core Java 1

7 c1++;8 c1+=1;9 c1=c1+1;10 c2='B';11 c2--;12 c2=c2-1;13 }14 }

Line no. 7

2. Line no. 8

3. Line no. 9

4. None of above. Q11.What will be the result of compiling and running the given program?

Select one correct answer. 1 public class Child extends Parent

2 {3 public static int test(int i) 4 { 5 return 20;

6 }7 public static void main(String[] args) 8 { 9 Parent c = new Child(); 10 System.out.println(c.test(10)); 11 } 12 }13 class Parent 14 { 15 public static int test(int i) 16 { 17 return 5; 18 } 19 }

Compile time as we can't overide static methods

2. Run time error as we can't overide static methods

3. Program compiles correctly and prints 5 when executed.

4. Program compiles correctly and prints 20 when executed. Q12.What will be the result of compiling and running the given program?

Page 98 of 165

Page 99: Core Java 1

Select one correct answer.

1 public class Q122 { 3 String str;4 public static void main(String[] args) 5 {6 Q12 a = new Q12();7 a.append("B");8 a.append("C"); 9 System.out.println(a.str);10 } 11 void append(String s)12 {13 str+=s;14 }15 }

Compile time error as "append" is a reserved name.

2. Compile time error as str may not be initialized.

3. Run time error as str may not be initialized.

4. Program compiles correctly and prints BC when executed.

5. None of above. Q13.What will be the result of compiling and running the given program?

Select one correct answer.

1 class sample2 {3 sample(String s)4 {5 System.out.println("String");6 }7 sample(Object o)8 {9 System.out.println("Object");10 }11 }12 class constructor13 {14 public static void main(String arg[])15 {16 sample s1=new sample(null);17 }18 }

Page 99 of 165

Page 100: Core Java 1

Compile time error as call to constructor at line no. 16 is ambigious.

2. Run time error as call to constructor at line no. 16 is ambigious.

3. Program compiles correctly and prints "object" when executed.

4. Program compiles correctly and prints "string" when executed.

Q14.What will be the result of compiling and running the given program?

Select one correct answer.

1 class sample2 {3 sample(String s)4 {5 System.out.println("String");6 }7 sample(StringBuffer sb)8 {9 System.out.println("StringBuffer");10 }11 }12 class constructor13 {14 public static void main(String arg[])15 {16 sample s1=new sample(null);17 }18 }

Compile time error as call to constructor at line no. 16 is ambigious.

2. Run time error as call to constructor at line no. 16 is ambigious.

3. Program compiles correctly and prints "StringBuffer" when executed.

4. Program compiles correctly and prints "string" when executed.

Q15.Which one of the following method will return closest integer but not less than

Page 100 of 165

Page 101: Core Java 1

for any value passed as argument d?Select one correct answer.

i = (int)Math.ceil(d);

2. i = (int)Math.abs(d);

3. i = (int)Math.floor(d);

4. i = (int)Math.round(d); Q16.What will be the result of compiling and running the given program?

Select one correct answer.1 class A2 {3 void callme()4 { 5 System.out.println("A");6 }7 int r=10;8 }9 class B extends A 10 {11 public void callme() 12 {13 System.out.println("B");14 }15 int r=20;16 }17 class Q1618 {19 public static void main(String args[]) 20 {21 A a = new B(); 22 a.callme();23 System.out.println(a.r); 24 }25 }

Compile time error

2. Program compiles correctly and prints "A" and 10 when executed.

3. Program compiles correctly and prints "B" and 20 when executed.

4. Program compiles correctly and prints "B" and 10 when executed.

Page 101 of 165

Page 102: Core Java 1

Q17.Which one of the following methods will give you an error when placed at

line no.10?Select one correct answer.1 class A2 {3 public void callme()4 { 5 System.out.println("A");6 } 7 }8 class B extends A 9 {10 //Here11 }

public final void callme(){}

2. void callMe(){}

3. void callme(int a){}

4. void callme(){} Q18.Which will be the first line to cause an error in the following code?

Select one correct answer.

1 class Char2 {3 public static void main(String arg[])4 {5 while(false) 6 {7 System.out.println("Hello");8 }9 while(false)10 {11 }12 do;13 while(false);14 do15 {16 ;17 }18 while(false);19 } 20 }

Line no. 5

Page 102 of 165

Page 103: Core Java 1

2. Line no. 9

3. Line no. 12

4. Line no. 16 Q19.Which of the given choices is true?Select one correct

answer.1 class equal2 { 3 public static void main (String args[]) 4 { 5 StringBuffer sb1 = new StringBuffer("Amit"); 6 StringBuffer sb2= new StringBuffer("Amit"); 7 String ss1 = "Amit";8 System.out.println(sb1==sb2); 9 System.out.println(sb1.equals(sb2)); 10 System.out.println(sb1.equals(ss1)); 11 System.out.println(sb1==ss1);12 }13 }

Error at line no. 10

2. Excepetion at line no. 10

3. Error at line no. 11

4. Excepetion at line no. 11 Q20.Which of the given choices are true?Select any two.1 class Q202 {3 public static void main(String arg[])4 {5 Q20 e=null;6 System.out.println(e.equals(null));7 Q20 e1;8 System.out.println(e1.equals(null));9 }10 }

Error at line no. 6

2. Excepetion at line no. 6

3. Error at line no. 8

Page 103 of 165

Page 104: Core Java 1

4. Excepetion at line no. 8 Q21.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 }9 catch(java.io.IOException t)10 { 11 System.out.println("B"); 12 } 13 System.out.println("C"); 14 } 15 }

Compile time error

2. Program compiles correctly and prints "A" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Run time error Q22.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 return;9 }10 catch(Exception e)11 { 12 System.out.println("B"); 13 } 14 System.out.println("C"); 15 } 16 }

Compile time error in line no. 8 as main() method is declared void.

Page 104 of 165

Page 105: Core Java 1

2. Program compiles correctly and prints "A" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Compile time error at line no.14 due to statement not reached. Q23.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 return;9 }10 catch(Exception e)11 { 12 System.out.println("B"); 13 } 14 finally15 {16 System.out.println("C"); 17 }18 } 19 }

Compile time error in line no. 8 as main() method is declared void.

2. Program compiles correctly and prints "A" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Program compiles correctly and prints "A","B" and "C" when executed.

Q24.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 {

Page 105 of 165

Page 106: Core Java 1

8 System.out.println("B"); 9 System.exit(0);9 }10 catch(Exception e)11 { 12 System.out.println("C"); 13 } 14 finally15 {16 System.out.println("D"); 17 }18 } 19 }

Program compiles correctly and prints "A" when executed.

2. Program compiles correctly and prints "A" and "B" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Program compiles correctly and prints "A","B" and "C" when executed.

Q25.Whether the following code compile or not?Select the most appropriate answer.

1 public class Q252 { 3 final static int fsn;4 final int fn;5 static6 {7 fsn=6;8 }9 {10 fn =8;11 }12 }

It will compile.

2. It will not compile.

3. It will compile if we initialize the two variable at the time of declaration.

4. It will compile if we change the initializer blocks into the contructors.

Page 106 of 165

Page 107: Core Java 1

Q26.What will be the value of str after line no.7?Enter your answer in the given field.

Don't use any extra signs like ("")quotes or any string like "str=".For example,

if your answer is the string "XYZ" you just write XYZ1 class string 2 {3 public static void main(String ds[])4 {5 String str="A";6 str.concat("B");7 str+="C";8 }9 }

(Note:This is the same way of doing entry as it is in real exam, you have to take care of above assumption while doing entry in a text field) Q27.What will be the result of compiling and running the given program?

Select any two.1 import java.util.*;2 import java.sql.*;3 class Q274 {5 Date d;6 public static void main(String arg[])7 {8 } 6 }

Compile time error as Date class is defined in both of the above packages.

2. Run time error as Date class is defined in both of the above packages.

3. It will compile and run without any error.

4. It will compile and run if we use ".Date" instead of ".*" in any one of the two import statements

Q28.Which one of the following is the simplest way to print the value of variable

text at line no. 18?Select one correct answer.

1 class Message2 {3 String text="Hello1";4 }5 class Super6 {7 Message msg=new Message();8 }

Page 107 of 165

Page 108: Core Java 1

9 class inheritance extends Super10 {11 public static void main(String arg[])12 {13 inheritance i=new inheritance();14 i.print();15 }16 public void print()17 {18 //Here19 }20 }

System.out.println(msg.text);

2. System.out.println(super.msg.text);

3. System.out.println(Message.text);

4. System.out.println(text); Q29.Whether the following code compile or not?

Select any two.

1 class Base {}2 class Agg extends Base3 {4 public String getFields()5 {

6 String name = "Agg";7 return name;8 }9 }10 public class Inheritence11 {12 public static void main(String argv[])13 {14 Base a = new Agg();15 System.out.println(a.getFields());16 }17 }

It will compile.

2. It will not compile.

3. It will compile if we cast the variable a for the object of class Agg like ((Agg) a).getFields()

Page 108 of 165

Page 109: Core Java 1

4. We can cast the variable a for the object of class Agg, but it is not required.

Q30.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q302 { 3 static int p=abc(); 4 static public int abc() 5 { 6 int i=123; 7 System.out.println(p);8 return i; 9 } 10 public static void main(String arg[])11 {12 }13 }

Compile time error for forward referencing the variable p at line no. 7.

2. Run time error for forward referencing the variable p at line no. 7.

3. Program compiles correctly and prints 123 when executed.

4. Program compiles correctly and prints 0 when executed. Q31.What will be the result of compiling and running the given program?

Select one correct answer.1 public class initialize2 { 3 public initialize(int m) 4 { 5 System.out.println(i+","+j+","+k+","+x+","+m);6 } 7 public static void main(String[]args) 8 { 9 initialize obj=new initialize(x); 10 } 11 static int i=5; 12 static int x; 13 int j=7; 14 int k; 15 { 16 j=70;x=20; 17 } 18 static 19 { 20 i=50;

Page 109 of 165

Page 110: Core Java 1

21 } 22 }

Compile time error.

2. Program will give output 50,70,0,20,0

3. Program will give output 50,70,0,20,20

4. Program will give output 50,0,0,20,0 Q32.What will be the result of compiling and running the given program?

Select one correct answer.1 class initialize2 {3 int y=0;4 {5 y=1;6 }7 initialize()8 {9 this(10);10 y=3;11 }12 initialize(int s)13 {14 y=2;15 }16 public static void main(String arg[])17 {18 initialize i=new initialize();19 System.out.println(i.y);20 }21 }

Compile time error

2. Program compiles correctly and prints 1 when executed.

3. Program compiles correctly and prints 2 when executed.

4. Program compiles correctly and prints 3 when executed.

5. Program compiles correctly and prints 0 when executed. Q33.Which will be the first line to cause an error in the following code?

Select one correct answer.

Page 110 of 165

Page 111: Core Java 1

1 class Outer2 {3 class Inner 4 {5 final int a=20;6 static final int b=20;7 static int c=20;8 }9 }

Line no. 5 as we can't declare a final variable in an inner class.

2. Line no. 6 as we can't declare a static final variable in an inner class.

3. Line no. 7 as we can't declare a static variable in an inner class.

4. There is no error in this code. Q34.Which of the following will be the correct way to make the object of class one

at line no.11?Select one correct answer.

1 class outer2 {3 class one4 {5 }6 }7 class Q338 {9 public static void main(String a[])10 {11 //Here12 }13 }

outer o=new outer.one();

2. outer o=new one();

3. outer.one o=new outer().new one();

4. one o=new outer().new one(); Q35.What will be the result of compiling and running the given program?

Page 111 of 165

Page 112: Core Java 1

Select one correct answer.1 class instanceOf2 {3 public static void main(String arg[])4 {5 G g=new G();6 H h=new H();7 System.out.println(h instanceof G);8 }9 }10 class G{}11 class H{}

Program compiles correctly and prints true when executed.

2. Program compiles correctly and prints false when executed.

3. Run time error

4. Compile time error. Q36.Which of the following method will not give you any error when placed on

line no.3?Select one correct answer.

1 interface i22 {3 //Here4 }

static void abc();

2. abstract void abc();

3. native void abc();

4. synchronized void abc();

5. final void abc();

6. private void abc();

7. protected void abc(); Q37.Which of the following statement will not give you any error when placed

on line no.3?Select one correct answer.

Page 112 of 165

Page 113: Core Java 1

1 interface i22 {3 //Here4 }

private int a=10;

2. int a;

3. transient int a=0;

4. volatile int a=0;

5. public static final int a=0; Q38.Which two of the following statement will give you same results?Select any two.

76>>>4

2. 76<<4

3. 76>>4

4. 76^2 Q39.Which of the following variable is not accessible at line no.12?

Select one correct answer.

1 class outer2 {3 private int a=0;4 int b=0;5 static int c=0;6 public void xyz(int d)7 {8 final int e=0;9 class local10 {11 {12 //Here13 }14 }15 }16 }

Variable a

Page 113 of 165

Page 114: Core Java 1

2. Variable b

3. Variable c

4. Variable d

5. Variable e Q40.Which two of the following are valid declaration for main() method?

Select any two.

final public static void main(String arg[]){}

2. private static void main(String arg[]){}

3. public static void main(String arg[]){}

4. public static void main(String arg){} Q41.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q412 {3 public static void main(String arg[])4 {5 System.out.println(Math.abs(-2147483648);6 }7 }

Program compiles correctly and prints 2147483648 when executed.

2. Program compiles correctly and prints -2147483648 when executed.

3. Program compiles correctly and prints 2147483647 when executed.

4. The program will not compile. Q42.What will be the result of compiling and running the given program?

Select one correct answer.1 class Bitwise

Page 114 of 165

Page 115: Core Java 1

2 {3 public static void main(String arg[])4 {5 abc();6 }7 public void abc()8 {9 byte x=13|2;10 byte y=~x;11 System.out.println(y);12 } 13 }

Program compiles correctly and prints -16 when executed.

2. Program compiles correctly and prints 15 when executed.

3. Program compiles correctly and prints 16 when executed.

4. Compile time error Q43.After which line the object initially refered by str is eligible for garbage

collection?Select one correct answer.

1 class Garbage2 {3 public static void main(String arg[])4 {5 String str=new String("Hello");6 String str1=str;7 str=new String("Hi");8 str1=new String("Hello Again");9 return;10 }11 }

After line no. 6

2. After line no. 7

3. After line no. 8

4. After line no. 9 Q44.Which of the following classes is not derived from the Component class.

Select one correct answer.

Page 115 of 165

Page 116: Core Java 1

Container

2. Window

3. List

4. MenuItem

5. Choice Q45.Name the method defined in EventObject class that returns the Object generated

from the event.Select the one correct answer.

getEvent()

2. getObject()

3. getID()

4. getSource()

5. getComponent() Q46.Which of the following is not valid event class?Select one correct answer.

MouseEvent

2. PaintEvent

3. WindowEvent

4. MouseMotionEvent Q47.Which of the following is valid adapter class?Select one correct answer.

ActionAdapter

2. AdjustmentAdapter

3. KeyAdapter

Page 116 of 165

Page 117: Core Java 1

4. TextAdapter Q48.How will the following program lay out its buttons.

Select one correct answer.

1 import java.awt.*;2 public class MyClass 3 {4 public static void main(String args[]) 5 {6 String[] labels = {"A"};7 Window win = new Frame();8 win.setLayout(new FlowLayout());9 for(int i=0;i < labels.length;i++)10 win.add(new Button(labels[i]));11 win.pack();12 win.setVisible(true);13 }14 }

The button A will appear on the top left corner of the window.

2. The button A will appear on the center of first row.

3. The button A will appear on the top right corner of the window.

4. The button A will appear on the middle row and column, in the center of the window.

Q49.Which of these are legal ways of accessing a File named "file.txt" for

reading.Select one correct answer.

FileReader fr = new FileReader("file.txt");

2. FileInputStream fr = new FileInputStream("file.txt","UTF8");

3. FileReader fr = new FileReader("file.txt", "UTF8");

4. InputStreamReader isr = new InputStreamReader("file.txt"); Q50.Which of the following classes can be used to specify the character encoding

for input?Select one correct answer.

FileReader

2. InputStreamReader

Page 117 of 165

Page 118: Core Java 1

3. OutputStreamWriter

4. FileInputStream

5. RandomAccessFile Q51.What is the name of collection interface used to maintain non-unique elements

in order?Select one correct answer.

Set

2. Map

3. List

4. ArrayList

5. SortedSet

Q52.What is the name of collection interface used to maintain unique elements?

Select one correct answer.

Set

2. Map

3. List

4. ArrayList

5. SortedSet Q53.Which of the following methods is used to create a thread?

Select one correct answer.

run()

2. yield()

3. start()

Page 118 of 165

Page 119: Core Java 1

4. create() Q54.Which of the following methods must be used in try-catch?

Select any two.

sleep()

2. wait()

3. yield()

4. start() Q55.Which of the following methods can be written at line no.3 to make the program

compile correctly?Select one correct answer.

1 class abc imlements Runnable2 {3 //Here4 }

public void start(){}

2. void run(){}

3. public static void main(){}

4. None of above. Q56.Which of the following methods can be written at line no.3 to make the program

compile correctly?Select one correct answer.

1 class abc extends Thread2 {3 //Here4 }

public void start(){}

2. public void run(){}

3. public static void main(){}

4. Program will compile as it is.

Page 119 of 165

Page 120: Core Java 1

Q57.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q572 {3 public static void main(String arg[])4 {5 int j=0;6 switch(1)7 {8 case 2: j+=1;9 case 4: j+=2;10 default: j+=3;11 case 0: j+=4;12 }13 System.out.println(j); 13 }14 }

Compile time error as default must come after all the cases.

2. Compile time error as we can not use 1 as argument to switch.

3. Program compiles correctly and print 3 when executed.

4. None of above. Q58.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q582 {3 public static void main(String args[]) 4 {5 int arr[] = new int[2];6 System.out.println(arr[0]);7 }8 }

Compile time error saying arr may not be initialized.

2. Run time error saying arr may not be initialized.

3. Program compiles and prints 0 when executed.

4. Program compiles and prints null when executed. Q59.What will be the result of compiling and running the given program?

Select one correct answer.1 class strings

Page 120 of 165

Page 121: Core Java 1

2 {3 public static void main(String arg[])4 {5 if("String".indexOf("S",-10) ==0) 6 {7 System.out.println("Equal"); 8 }9 else 10 {11 System.out.println("Not Equal");12 }13 }14 }

Compile time error as we passed negative value in indexOf() method.

2. Run time error as we passed negative value in indexOf() method.

3. Program compiles and prints Equal when executed.

4. Program compiles and prints Not Equal when executed.

Page 121 of 165

Page 122: Core Java 1

Mock Exam 1 Q1.What will be the result of compiling and running the given

program?Select one correct answer.

1 class Q1 2 { 3 public static void main(String arg[]) 4 { 5 int a[]={2,2}; 6 int b=1; 7 a[b]=b=0; 8 System.out.println(a[0]); 9 System.out.println(a[1]); 10 } 11 }

Compile time error at the line no. 5.

2. Run time error at the line no. 5.

3. Program compiles correctly and print 2,0 when executed.

4. Program compiles correctly and print 0,2 when executed. Q2.What will be the result of compiling and running the given

program?Select one correct answer.

1 class Q22 {3 public static void main(String arg[])4 {5 StringBuffer s[]={"A","B"};6 System.out.println(s[0]+","+s[1]);7 }8 }

Program compiles correctly and print A,B when executed.

2. Compile time error.

3. Run time error. Q3.Which of the given choices are true?Select any two.

1 class Q32 {3 public static void main(String arg[])4 {

Page 122 of 165

Page 123: Core Java 1

5 int[] a,c,d[];6 int b[]=new int[0];7 }8 }

All the variables a,b,c,d are arrays.

2. Only a,d,b are arrays.

3. We can declare an array with zero element.

4. We cannot declare an array with zero element. Q4.What will be the result of compiling and running the given

program?Select one correct answer.

1 public class Q42 { 3 public static void main(String[] args)4 {

5 boolean t1 = true, t2 = false, t3 = t1; 6 boolean t = false; 7 t &&= (t1 || ( t2 && t3)); 8 System.out.println(t);

9 } 10 }

Program compiles correctly and print true when executed.

2. Program compiles correctly and print false when executed.

3. Compile time error.

4. Run time error.

Q5.What will be the result of compiling and running the given program?Select any two.

1 public class Q52 {3 public static void main(String rag[])4 {5 byte b=1;6 b++;7 byte b=127+1;8 int i=2147483647+1;9 b=- -b;

Page 123 of 165

Page 124: Core Java 1

10 }11 }

Compile time error at line no. 6.

2. Compile time error at line no. 7.

3. Compile time error at line no. 8.

4. Compile time error at line no. 9. Q6.What will be the result of compiling and running the given

program?Select one correct answer.

1 class AA{}2 class BB extends AA{}3 class Q64 {5 public static void main(String arg[])6 {7 AA a=null;8 BB b=(BB)a;9 System.out.println(b);10 System.out.println(b instanceof BB);11 System.out.println(b instanceof AA);12 }13 }

Program compiles correctly and print null,true,false

2. Program compiles correctly and print null,true,true.

3. Program compiles correctly and print null,false,false

4. Program compiles correctly and print null,false,true.

5. Compile time error at line no.8 as null value cannot be casted.

6. Run time error at line no. 8 as null value cannot be casted. Q7.Which one of the following statements will give you an

Exception when placed at line no.10?Select one correct answer.

1 class AA{}2 class BB extends AA{}3 class casting4 {

Page 124 of 165

Page 125: Core Java 1

5 public static void main(String arg[])6 {7 AA a=new AA();8 BB b=new BB();9 AA c=new BB();10 //Here11 }11 }

b = (BB) a;

2. a = b ;

3. b = (BB) c;

4. c = b; Q8.Which one of the following are valid character contants?Select any two.

char c = '\u000d' ;

2. char c = '\u000a';

3. char c = '\u0000';

4. char c = '\uface' ; Q9.Which one of the following are not valid character contants?

Select any two.

char c = '\u00001' ;

2. char c = '\101';

3. char c = 65;

4. char c = '\1001' ; Q10.Which will be the first line to cause an error in the following code?

Select one correct answer.

1 class Char2 {3 public static void main(String arg[])4 {5 char c1,c2;6 c1='A';

Page 125 of 165

Page 126: Core Java 1

7 c1++;8 c1+=1;9 c1=c1+1;10 c2='B';11 c2--;12 c2=c2-1;13 }14 }

Line no. 7

2. Line no. 8

3. Line no. 9

4. None of above. Q11.What will be the result of compiling and running the given program?

Select one correct answer. 1 public class Child extends Parent

2 {3 public static int test(int i) 4 { 5 return 20;

6 }7 public static void main(String[] args) 8 { 9 Parent c = new Child(); 10 System.out.println(c.test(10)); 11 } 12 }13 class Parent 14 { 15 public static int test(int i) 16 { 17 return 5; 18 } 19 }

Compile time as we can't overide static methods

2. Run time error as we can't overide static methods

3. Program compiles correctly and prints 5 when executed.

4. Program compiles correctly and prints 20 when executed. Q12.What will be the result of compiling and running the given program?

Page 126 of 165

Page 127: Core Java 1

Select one correct answer.

1 public class Q122 { 3 String str;4 public static void main(String[] args) 5 {6 Q12 a = new Q12();7 a.append("B");8 a.append("C"); 9 System.out.println(a.str);10 } 11 void append(String s)12 {13 str+=s;14 }15 }

Compile time error as "append" is a reserved name.

2. Compile time error as str may not be initialized.

3. Run time error as str may not be initialized.

4. Program compiles correctly and prints BC when executed.

5. None of above. Q13.What will be the result of compiling and running the given program?

Select one correct answer.

1 class sample2 {3 sample(String s)4 {5 System.out.println("String");6 }7 sample(Object o)8 {9 System.out.println("Object");10 }11 }12 class constructor13 {14 public static void main(String arg[])15 {16 sample s1=new sample(null);17 }18 }

Page 127 of 165

Page 128: Core Java 1

Compile time error as call to constructor at line no. 16 is ambigious.

2. Run time error as call to constructor at line no. 16 is ambigious.

3. Program compiles correctly and prints "object" when executed.

4. Program compiles correctly and prints "string" when executed.

Q14.What will be the result of compiling and running the given program?

Select one correct answer.

1 class sample2 {3 sample(String s)4 {5 System.out.println("String");6 }7 sample(StringBuffer sb)8 {9 System.out.println("StringBuffer");10 }11 }12 class constructor13 {14 public static void main(String arg[])15 {16 sample s1=new sample(null);17 }18 }

Compile time error as call to constructor at line no. 16 is ambigious.

2. Run time error as call to constructor at line no. 16 is ambigious.

3. Program compiles correctly and prints "StringBuffer" when executed.

4. Program compiles correctly and prints "string" when executed.

Q15.Which one of the following method will return closest integer but not less than

Page 128 of 165

Page 129: Core Java 1

for any value passed as argument d?Select one correct answer.

i = (int)Math.ceil(d);

2. i = (int)Math.abs(d);

3. i = (int)Math.floor(d);

4. i = (int)Math.round(d); Q16.What will be the result of compiling and running the given program?

Select one correct answer.1 class A2 {3 void callme()4 { 5 System.out.println("A");6 }7 int r=10;8 }9 class B extends A 10 {11 public void callme() 12 {13 System.out.println("B");14 }15 int r=20;16 }17 class Q1618 {19 public static void main(String args[]) 20 {21 A a = new B(); 22 a.callme();23 System.out.println(a.r); 24 }25 }

Compile time error

2. Program compiles correctly and prints "A" and 10 when executed.

3. Program compiles correctly and prints "B" and 20 when executed.

4. Program compiles correctly and prints "B" and 10 when executed.

Page 129 of 165

Page 130: Core Java 1

Q17.Which one of the following methods will give you an error when placed at

line no.10?Select one correct answer.1 class A2 {3 public void callme()4 { 5 System.out.println("A");6 } 7 }8 class B extends A 9 {10 //Here11 }

public final void callme(){}

2. void callMe(){}

3. void callme(int a){}

4. void callme(){} Q18.Which will be the first line to cause an error in the following code?

Select one correct answer.

1 class Char2 {3 public static void main(String arg[])4 {5 while(false) 6 {7 System.out.println("Hello");8 }9 while(false)10 {11 }12 do;13 while(false);14 do15 {16 ;17 }18 while(false);19 } 20 }

Line no. 5

Page 130 of 165

Page 131: Core Java 1

2. Line no. 9

3. Line no. 12

4. Line no. 16 Q19.Which of the given choices is true?Select one correct

answer.1 class equal2 { 3 public static void main (String args[]) 4 { 5 StringBuffer sb1 = new StringBuffer("Amit"); 6 StringBuffer sb2= new StringBuffer("Amit"); 7 String ss1 = "Amit";8 System.out.println(sb1==sb2); 9 System.out.println(sb1.equals(sb2)); 10 System.out.println(sb1.equals(ss1)); 11 System.out.println(sb1==ss1);12 }13 }

Error at line no. 10

2. Excepetion at line no. 10

3. Error at line no. 11

4. Excepetion at line no. 11 Q20.Which of the given choices are true?Select any two.1 class Q202 {3 public static void main(String arg[])4 {5 Q20 e=null;6 System.out.println(e.equals(null));7 Q20 e1;8 System.out.println(e1.equals(null));9 }10 }

Error at line no. 6

2. Excepetion at line no. 6

3. Error at line no. 8

Page 131 of 165

Page 132: Core Java 1

4. Excepetion at line no. 8 Q21.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 }9 catch(java.io.IOException t)10 { 11 System.out.println("B"); 12 } 13 System.out.println("C"); 14 } 15 }

Compile time error

2. Program compiles correctly and prints "A" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Run time error Q22.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 return;9 }10 catch(Exception e)11 { 12 System.out.println("B"); 13 } 14 System.out.println("C"); 15 } 16 }

Compile time error in line no. 8 as main() method is declared void.

Page 132 of 165

Page 133: Core Java 1

2. Program compiles correctly and prints "A" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Compile time error at line no.14 due to statement not reached. Q23.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 { 8 return;9 }10 catch(Exception e)11 { 12 System.out.println("B"); 13 } 14 finally15 {16 System.out.println("C"); 17 }18 } 19 }

Compile time error in line no. 8 as main() method is declared void.

2. Program compiles correctly and prints "A" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Program compiles correctly and prints "A","B" and "C" when executed.

Q24.What will be the result of compiling and running the given program?

Select one correct answer.1 public class exception2 { 3 public static void main(String args[]) 4 { 5 System.out.println("A"); 6 try 7 {

Page 133 of 165

Page 134: Core Java 1

8 System.out.println("B"); 9 System.exit(0);9 }10 catch(Exception e)11 { 12 System.out.println("C"); 13 } 14 finally15 {16 System.out.println("D"); 17 }18 } 19 }

Program compiles correctly and prints "A" when executed.

2. Program compiles correctly and prints "A" and "B" when executed.

3. Program compiles correctly and prints "A" and "C" when executed.

4. Program compiles correctly and prints "A","B" and "C" when executed.

Q25.Whether the following code compile or not?Select the most appropriate answer.

1 public class Q252 { 3 final static int fsn;4 final int fn;5 static6 {7 fsn=6;8 }9 {10 fn =8;11 }12 }

It will compile.

2. It will not compile.

3. It will compile if we initialize the two variable at the time of declaration.

4. It will compile if we change the initializer blocks into the contructors.

Page 134 of 165

Page 135: Core Java 1

Q26.What will be the value of str after line no.7?Enter your answer in the given field.

Don't use any extra signs like ("")quotes or any string like "str=".For example,

if your answer is the string "XYZ" you just write XYZ1 class string 2 {3 public static void main(String ds[])4 {5 String str="A";6 str.concat("B");7 str+="C";8 }9 }

(Note:This is the same way of doing entry as it is in real exam, you have to take care of above assumption while doing entry in a text field) Q27.What will be the result of compiling and running the given program?

Select any two.1 import java.util.*;2 import java.sql.*;3 class Q274 {5 Date d;6 public static void main(String arg[])7 {8 } 6 }

Compile time error as Date class is defined in both of the above packages.

2. Run time error as Date class is defined in both of the above packages.

3. It will compile and run without any error.

4. It will compile and run if we use ".Date" instead of ".*" in any one of the two import statements

Q28.Which one of the following is the simplest way to print the value of variable

text at line no. 18?Select one correct answer.

1 class Message2 {3 String text="Hello1";4 }5 class Super6 {7 Message msg=new Message();8 }

Page 135 of 165

Page 136: Core Java 1

9 class inheritance extends Super10 {11 public static void main(String arg[])12 {13 inheritance i=new inheritance();14 i.print();15 }16 public void print()17 {18 //Here19 }20 }

System.out.println(msg.text);

2. System.out.println(super.msg.text);

3. System.out.println(Message.text);

4. System.out.println(text); Q29.Whether the following code compile or not?

Select any two.

1 class Base {}2 class Agg extends Base3 {4 public String getFields()5 {

6 String name = "Agg";7 return name;8 }9 }10 public class Inheritence11 {12 public static void main(String argv[])13 {14 Base a = new Agg();15 System.out.println(a.getFields());16 }17 }

It will compile.

2. It will not compile.

3. It will compile if we cast the variable a for the object of class Agg like ((Agg) a).getFields()

Page 136 of 165

Page 137: Core Java 1

4. We can cast the variable a for the object of class Agg, but it is not required.

Q30.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q302 { 3 static int p=abc(); 4 static public int abc() 5 { 6 int i=123; 7 System.out.println(p);8 return i; 9 } 10 public static void main(String arg[])11 {12 }13 }

Compile time error for forward referencing the variable p at line no. 7.

2. Run time error for forward referencing the variable p at line no. 7.

3. Program compiles correctly and prints 123 when executed.

4. Program compiles correctly and prints 0 when executed. Q31.What will be the result of compiling and running the given program?

Select one correct answer.1 public class initialize2 { 3 public initialize(int m) 4 { 5 System.out.println(i+","+j+","+k+","+x+","+m);6 } 7 public static void main(String[]args) 8 { 9 initialize obj=new initialize(x); 10 } 11 static int i=5; 12 static int x; 13 int j=7; 14 int k; 15 { 16 j=70;x=20; 17 } 18 static 19 { 20 i=50;

Page 137 of 165

Page 138: Core Java 1

21 } 22 }

Compile time error.

2. Program will give output 50,70,0,20,0

3. Program will give output 50,70,0,20,20

4. Program will give output 50,0,0,20,0 Q32.What will be the result of compiling and running the given program?

Select one correct answer.1 class initialize2 {3 int y=0;4 {5 y=1;6 }7 initialize()8 {9 this(10);10 y=3;11 }12 initialize(int s)13 {14 y=2;15 }16 public static void main(String arg[])17 {18 initialize i=new initialize();19 System.out.println(i.y);20 }21 }

Compile time error

2. Program compiles correctly and prints 1 when executed.

3. Program compiles correctly and prints 2 when executed.

4. Program compiles correctly and prints 3 when executed.

5. Program compiles correctly and prints 0 when executed. Q33.Which will be the first line to cause an error in the following code?

Select one correct answer.

Page 138 of 165

Page 139: Core Java 1

1 class Outer2 {3 class Inner 4 {5 final int a=20;6 static final int b=20;7 static int c=20;8 }9 }

Line no. 5 as we can't declare a final variable in an inner class.

2. Line no. 6 as we can't declare a static final variable in an inner class.

3. Line no. 7 as we can't declare a static variable in an inner class.

4. There is no error in this code. Q34.Which of the following will be the correct way to make the object of class one

at line no.11?Select one correct answer.

1 class outer2 {3 class one4 {5 }6 }7 class Q338 {9 public static void main(String a[])10 {11 //Here12 }13 }

outer o=new outer.one();

2. outer o=new one();

3. outer.one o=new outer().new one();

4. one o=new outer().new one(); Q35.What will be the result of compiling and running the given program?

Page 139 of 165

Page 140: Core Java 1

Select one correct answer.1 class instanceOf2 {3 public static void main(String arg[])4 {5 G g=new G();6 H h=new H();7 System.out.println(h instanceof G);8 }9 }10 class G{}11 class H{}

Program compiles correctly and prints true when executed.

2. Program compiles correctly and prints false when executed.

3. Run time error

4. Compile time error. Q36.Which of the following method will not give you any error when placed on

line no.3?Select one correct answer.

1 interface i22 {3 //Here4 }

static void abc();

2. abstract void abc();

3. native void abc();

4. synchronized void abc();

5. final void abc();

6. private void abc();

7. protected void abc(); Q37.Which of the following statement will not give you any error when placed

on line no.3?Select one correct answer.

Page 140 of 165

Page 141: Core Java 1

1 interface i22 {3 //Here4 }

private int a=10;

2. int a;

3. transient int a=0;

4. volatile int a=0;

5. public static final int a=0; Q38.Which two of the following statement will give you same results?Select any two.

76>>>4

2. 76<<4

3. 76>>4

4. 76^2 Q39.Which of the following variable is not accessible at line no.12?

Select one correct answer.

1 class outer2 {3 private int a=0;4 int b=0;5 static int c=0;6 public void xyz(int d)7 {8 final int e=0;9 class local10 {11 {12 //Here13 }14 }15 }16 }

Variable a

Page 141 of 165

Page 142: Core Java 1

2. Variable b

3. Variable c

4. Variable d

5. Variable e Q40.Which two of the following are valid declaration for main() method?

Select any two.

final public static void main(String arg[]){}

2. private static void main(String arg[]){}

3. public static void main(String arg[]){}

4. public static void main(String arg){} Q41.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q412 {3 public static void main(String arg[])4 {5 System.out.println(Math.abs(-2147483648);6 }7 }

Program compiles correctly and prints 2147483648 when executed.

2. Program compiles correctly and prints -2147483648 when executed.

3. Program compiles correctly and prints 2147483647 when executed.

4. The program will not compile. Q42.What will be the result of compiling and running the given program?

Select one correct answer.1 class Bitwise

Page 142 of 165

Page 143: Core Java 1

2 {3 public static void main(String arg[])4 {5 abc();6 }7 public void abc()8 {9 byte x=13|2;10 byte y=~x;11 System.out.println(y);12 } 13 }

Program compiles correctly and prints -16 when executed.

2. Program compiles correctly and prints 15 when executed.

3. Program compiles correctly and prints 16 when executed.

4. Compile time error Q43.After which line the object initially refered by str is eligible for garbage

collection?Select one correct answer.

1 class Garbage2 {3 public static void main(String arg[])4 {5 String str=new String("Hello");6 String str1=str;7 str=new String("Hi");8 str1=new String("Hello Again");9 return;10 }11 }

After line no. 6

2. After line no. 7

3. After line no. 8

4. After line no. 9 Q44.Which of the following classes is not derived from the Component class.

Select one correct answer.

Page 143 of 165

Page 144: Core Java 1

Container

2. Window

3. List

4. MenuItem

5. Choice Q45.Name the method defined in EventObject class that returns the Object generated

from the event.Select the one correct answer.

getEvent()

2. getObject()

3. getID()

4. getSource()

5. getComponent() Q46.Which of the following is not valid event class?Select one correct answer.

MouseEvent

2. PaintEvent

3. WindowEvent

4. MouseMotionEvent Q47.Which of the following is valid adapter class?Select one correct answer.

ActionAdapter

2. AdjustmentAdapter

3. KeyAdapter

Page 144 of 165

Page 145: Core Java 1

4. TextAdapter Q48.How will the following program lay out its buttons.

Select one correct answer.

1 import java.awt.*;2 public class MyClass 3 {4 public static void main(String args[]) 5 {6 String[] labels = {"A"};7 Window win = new Frame();8 win.setLayout(new FlowLayout());9 for(int i=0;i < labels.length;i++)10 win.add(new Button(labels[i]));11 win.pack();12 win.setVisible(true);13 }14 }

The button A will appear on the top left corner of the window.

2. The button A will appear on the center of first row.

3. The button A will appear on the top right corner of the window.

4. The button A will appear on the middle row and column, in the center of the window.

Q49.Which of these are legal ways of accessing a File named "file.txt" for

reading.Select one correct answer.

FileReader fr = new FileReader("file.txt");

2. FileInputStream fr = new FileInputStream("file.txt","UTF8");

3. FileReader fr = new FileReader("file.txt", "UTF8");

4. InputStreamReader isr = new InputStreamReader("file.txt"); Q50.Which of the following classes can be used to specify the character encoding

for input?Select one correct answer.

FileReader

2. InputStreamReader

Page 145 of 165

Page 146: Core Java 1

3. OutputStreamWriter

4. FileInputStream

5. RandomAccessFile Q51.What is the name of collection interface used to maintain non-unique elements

in order?Select one correct answer.

Set

2. Map

3. List

4. ArrayList

5. SortedSet

Q52.What is the name of collection interface used to maintain unique elements?

Select one correct answer.

Set

2. Map

3. List

4. ArrayList

5. SortedSet Q53.Which of the following methods is used to create a thread?

Select one correct answer.

run()

2. yield()

3. start()

Page 146 of 165

Page 147: Core Java 1

4. create() Q54.Which of the following methods must be used in try-catch?

Select any two.

sleep()

2. wait()

3. yield()

4. start() Q55.Which of the following methods can be written at line no.3 to make the program

compile correctly?Select one correct answer.

1 class abc imlements Runnable2 {3 //Here4 }

public void start(){}

2. void run(){}

3. public static void main(){}

4. None of above. Q56.Which of the following methods can be written at line no.3 to make the program

compile correctly?Select one correct answer.

1 class abc extends Thread2 {3 //Here4 }

public void start(){}

2. public void run(){}

3. public static void main(){}

4. Program will compile as it is.

Page 147 of 165

Page 148: Core Java 1

Q57.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q572 {3 public static void main(String arg[])4 {5 int j=0;6 switch(1)7 {8 case 2: j+=1;9 case 4: j+=2;10 default: j+=3;11 case 0: j+=4;12 }13 System.out.println(j); 13 }14 }

Compile time error as default must come after all the cases.

2. Compile time error as we can not use 1 as argument to switch.

3. Program compiles correctly and print 3 when executed.

4. None of above. Q58.What will be the result of compiling and running the given program?

Select one correct answer.1 class Q582 {3 public static void main(String args[]) 4 {5 int arr[] = new int[2];6 System.out.println(arr[0]);7 }8 }

Compile time error saying arr may not be initialized.

2. Run time error saying arr may not be initialized.

3. Program compiles and prints 0 when executed.

4. Program compiles and prints null when executed. Q59.What will be the result of compiling and running the given program?

Select one correct answer.1 class strings

Page 148 of 165

Page 149: Core Java 1

2 {3 public static void main(String arg[])4 {5 if("String".indexOf("S",-10) ==0) 6 {7 System.out.println("Equal"); 8 }9 else 10 {11 System.out.println("Not Equal");12 }13 }14 }

Compile time error as we passed negative value in indexOf() method.

2. Run time error as we passed negative value in indexOf() method.

3. Program compiles and prints Equal when executed.

4. Program compiles and prints Not Equal when executed.

Page 149 of 165

Page 150: Core Java 1

JAVA and EJB - INTERVIEW QUESTIONS1. What is Entity Bean and Session Bean ? 2. What are the methods of Entity Bean? 3. How does Stateful Session bean store its state ? 4. Why does Stateless Session bean not store its state even though it has ejbActivate and ejbPassivate ? 5. What are the services provided by the container ? 6. Types of transaction ? 7. What is bean managed transaction ? 8. Why does EJB needs two interface( Home and Remote Interface) ? 9. What are transaction attributes ? 10. What is the difference between Container managed persistent bean and Bean managed persistent entity bean ? 11. What is J2EE ? 12. What is JTS ? 13. How many entity beans used and how many tables can u use in EJB project ? 14. What is scalable,portability in J2EE? 15. What is Connection pooling?Is it advantageous? 16. Method and class used for Connection pooling ? 17. How to deploy in J2EE(i.e Jar,War file) ? 18. How is entity bean created using Container managed entity bean ? 19. Sotware architechture of EJB ? 20. In Entity bean will the create method in EJB home and ejbCreate in Entity bean have the same parameters ? 21. What methods do u use in Servlet - Applet communication ? 22. What are the types of Servlet ?

Classic Servlets - service() method

HTTP Servlets - doGet() & doPost()

Visual Servlets - generated by Visual Age

JSP’s - java embedded in HTML templates

23. Difference between HttpServlet and Generic Servlets ? Generic servlet is servlet that can handle whatever response/requests.HttpServlet is a servlet which works above HTTP protocol.So generic servlet is wider than HttpServlet it can handle HTTP as well,but in this case you have to implements lots of service functionavailable by default in HttpServlet. Generic servlets can be usedsay in implementing FTP or NNTP services.

24. Difference between doGet and doPost ?

Get and put are two types of requesting from the web server. The simple difference between the two is that any additional parameters u pass with a GET request is encoded in the URL itself and this is called as a query string. It is of the

Page 150 of 165

Page 151: Core Java 1

typehttp://www.mysite.com/search?q=test&count=10 i.e anything following the ? are additional parameters. This method is not designed to send in large amounts of data and normally a get request is limited to 255 chars.

<p>In the post method the additional parameters are sent as part of the request body itself and hence large amounts of data can be sent to the server.

25. What are the methods in HttpServlet?

HttpServlet

GenericServlet is truly generic because it is applicable to any chat-type protocol; however, web development is mostly about the HTTP protocol. To chat in HTTP, javax.Servlet.http.HttpServlet is best to extend. HttpServlet is a subclass of GenericServlet. Therefore, the init, service, and destroy methods from GenericServlet are available when extending HttpServlet.

HttpServlet defines a method for each of the HTTP methods. These methods are doGet, doPost, doPut, doOptions, doDelete, and doTrace. When Tomcat receives a client request of the GET type, the requested Servlet's doGet() method is invoked to reply to that client. Additionally, HttpServlet has an HTTP-specific version of the service method. The only change in the HttpServlet service method over the GenericServlet's version is that HTTP-specific request and response objects are the parameters (javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse).To write a Servlet by subclassing HttpServlet, implement each HTTP method desired (such as doGet or doPost), or define the service method. The HttpServlet class also has a getlastModified method that you may override if you wish to return a value (milliseconds since the 1970 epoch) representing the last time the Servlet or related data was updated. This information is used by caches.

26. What are the types of SessionTracking?

27. What is Cookie ? Why is Cookie used ? "Cookies" are items of user-specific information generated by a Web server and stored on the user's own computer, which may be accessed by the server whenever you subsequently visit the site. In this way, using a cookie, a web page can record and track information about a specific visitor in order to allow a degree of user-side customisation of Web information.

This may be used to enrich the user's web experience in a variety of ways. For instance, a cookie might be set to record the past pages or options a user has selected on their last visit to a site. Using this information on a repeat visit, the site may be personalised to show only information which is relevant or of interest to that user.

28. If my browser does not support Cookie,and my server sends a cookie instance What will happen ?

Page 151 of 165

Page 152: Core Java 1

29. Why do u use Session Tracking in HttpServlet ?

30. Can u use javaScript in Servlets ?

31. What is the capacity the doGet can send to the server ?

32. What are the type of protocols used in HttpServlet ?

33. Difference between TCP/IP and IP protocol ?

34. Why do you use UniCastRemoteObject in RMI ?

35. How many interfaces are used in RMI?

36. Can Rmi registry be written in the code, without having to write it in the command prompt and if yes where?

37. Why is Socket used ?

38. What are the types of JDBC drivers ? 39. Explain the third driver(Native protocol driver) ? 40. Which among the four driver is pure Java driver ? 41. What are the Isolation level in JDBC transaction ? 42. How do you connect with the database ? 43. How do you connect without the Class.forName (" ") ? 44. What does Class.forName return ? 45. What are the types of statement ? 46. Why is preparedStatement,CallableStatement used for? 47. In real time project which driver did u use ? 48. Difference between AWT and Swing compenents ? 49. Is there any heavy weight component in Swings ? 50. Can the Swing application if you upload in net, be compatible with your browser? 51. What should you do get your browser compatible with swing components? 52. What are the methods in Applet ? 53. When is init(),start() called ? 54. When you navigate from one applet to another what are the methods called ? 55. What is the difference between Trusted and Untrusted Applet ? 56. What is Exception ? 57. What are the ways you can handle exception ? 58. When is try,catch block used ? 59. What is finally method in Exceptions ? 60. What are the types of access modifiers ? 61. What is protected and friendly ? 62. What are the other modifiers ? 63. Is synchronised modifier ?

Page 152 of 165

Page 153: Core Java 1

64. What is meant by polymorphism ? 65. What is inheritance ? 66. What is method Overloading ? What is this in OOPS ? 67. What is method Overriding ? What is it in OOPS ? 68. Does java support multi dimensional arrays ? 69. Is multiple inheritance used in Java ? 70. How do you send a message to the browser in JavaScript ? 71. Does javascript support multidimensional arrays ? 72. Why is XML used mainly? 73. Do use coding for database connectivity in XML? 74. What is DTD ? 75. Is there any tool in java that can create reports ?76. What is the diffrence between an Abstract class and Interface ? 77. What is user defined exception ? 78. What do you know about the garbate collector ? 79. What is the difference between C++ & Java ? 80. Explain RMI Architecture? 81. How do you communicate in between Applets & Servlets ? 82. What is the use of Servlets ? 83. What is JDBC? How do you connect to the Database ? 84. In an HTML form I have a Button which makes us to open another page in 15 seconds. How will do you that ? 85. What is the difference between Process and Threads ? 86. What is the difference between RMI & Corba ? 87. What are the services in RMI ? 88. How will you initialize an Applet ? 89. What is the order of method invocation in an Applet ? 90. When is update method called ? 91. How will you pass values from HTML page to the Servlet ? 92. Have you ever used HashTable and Dictionary ? 93. How will you communicate between two Applets ? 94. What are statements in JAVA ? 95. What is JAR file ? 96. What is JNI ? 97. What is the base class for all swing components ? 98. What is JFC ? 99. What is Difference between AWT and Swing ? 100. Considering notepad/IE or any other thing as process, What will happen if you start notepad or IE 3 times? Where 3 processes are started or 3 threads are started ? 101. How does thread synchronization occurs inside a monitor ? 102. How will you call an Applet using a Java Script function ? 103. Is there any tag in HTML to upload and download files ? 104. Why do you Canvas ? 105. How can you push data from an Applet to Servlet ? 106. What are 4 drivers available in JDBC ? 107. How you can know about drivers and database information ?

Page 153 of 165

Page 154: Core Java 1

108. If you are truncated using JDBC, How can you know ..that how much data is truncated ? 109. And What situation , each of the 4 drivers used ? 110. How will you perform transaction using JDBC ? 111. In RMI, server object first loaded into the memory and then the stub reference is sent to the client ? or whether a stub reference is directly sent to the client ? 112. Suppose server object is not loaded into the memory, and the client request for it , what will happen? 113. What is serialization ? 114. Can you load the server object dynamically? If so, what are the major 3 steps involved in it ? 115. What is difference RMI registry and OSAgent ? 116. To a server method, the client wants to send a value 20, with this value exceeds to 20,. a message should be sent to the client ? What will you do for achieving for this ? 117. What are the benefits of Swing over AWT ? 118. Where the CardLayout is used ? 119. What is the Layout for ToolBar ? 120. What is the difference between Grid and GridbagLayout ? 121. How will you add panel to a Frame ? 122. What is the corresponding Layout for Card in Swing ? 123. What is light weight component ? 124. Can you run the product development on all operating systems ? 125. What is the webserver used for running the Servlets ? 126. What is Servlet API used for conneting database ? 127. What is bean ? Where it can be used ? 128. What is difference in between Java Class and Bean ? 129. Can we send object using Sockets ? 130. What is the RMI and Socket ? 131. How to communicate 2 threads each other ? 132. What are the files generated after using IDL to Java Compilet ? 133. What is the protocol used by server and client ? 134. Can I modify an object in CORBA ? 135. What is the functionality stubs and skeletons ? 136. What is the mapping mechanism used by Java to identify IDL language ? 137. Diff between Application and Applet ? 138. What is serializable Interface ? 139. What is the difference between CGI and Servlet ? 140. What is the use of Interface ? 141. Why Java is not fully objective oriented ? 142. Why does not support multiple Inheritance ? 143. What it the root class for all Java classes ? 144. What is polymorphism ? 145. Suppose If we have variable ' I ' in run method, If I can create one or more thread each thread will occupy a separate copy or same variable will be shared ? 146. In servlets, we are having a web page that is invoking servlets username and password ? which is cheks in the database ? Suppose the second page also If we want to

Page 154 of 165

Page 155: Core Java 1

verify the same information whethe it will connect to the database or it will be used previous information? 147. What are virtual functions ? 148. Write down how will you create a binary Tree ? 149. What are the traverses in Binary Tree ? 150. Write a program for recursive Traverse ? 151. What are session variable in Servlets ? 152. What is client server computing ? 153. What is Constructor and Virtual function? Can we call Virtual funciton in a constructor ? 154. Why we use OOPS concepts? What is its advantage ? 155. What is the middleware ? What is the functionality of Webserver ? 156. Why Java is not 100 % pure OOPS ? ( EcomServer ) 157. When we will use an Interface and Abstract class ? 158. What is an RMI? 159. How will you pass parameters in RMI ? Why u serialize? 160. What is the exact difference in between Unicast and Multicast object ? Where we will use ? 161. What is the main functionality of the Remote Reference Layer ? 162. How do you download stubs from a Remote place ? 163. What is the difference in between C++ and Java ? can u explain in detail ? 164. I want to store more than 10 objects in a remote server ? Which methodology will follow ? 165. What is the main functionality of the Prepared Statement ? 166. What is meant by static query and dynamic query ? 167. What are the Normalization Rules ? Define the Normalization ? 168. What is meant by Servelet? What are the parameters of the service method ? 169. What is meant by Session ? Tell me something about HTTPSession Class ? 170. How do you invoke a Servelt? What is the difference in between doPost and doGet methods ? 171. What is the difference in between the HTTPServlet and Generic Servlet ? Expalin their methods ? Tell me their parameter names also ? 172. Have you used threads in Servelet ? 173. Write a program on RMI and JDBC using StoredProcedure ? 174. How do you sing an Applet ? 175. In a Container there are 5 components. I want to display the all the components names, how will you do that one ? 176. Why there are some null interface in java ? What does it mean ? Give me some null interfaces in JAVA ? 177. Tell me the latest versions in JAVA related areas ? 178. What is meant by class loader ? How many types are there? When will we use them ? 179. How do you load an Image in a Servlet ? 180. What is meant by flickering ? 181. What is meant by distributed Application ? Why we are using that in our applications ?

Page 155 of 165

Page 156: Core Java 1

182. What is the functionality of the stub ? 183. Have you used any version control ? 184. What is the latest version of JDBC ? What are the new features are added in that ? 185. Explain 2 tier and 3 -tier Architecture ? 186. What is the role of the webserver ? 187. How have you done validation of the fileds in your project ? 188. What is the main difficulties that you are faced in your project ? 189. What is meant by cookies ? Explain ? 190. Problem faced in your earlier project 191. How OOPS concept is achieved in Java 192. Features for using Java 193. How does Java 2.0 differ from Java 1.0 194. Public static void main - Explain 195. What are command line arguments 196. Explain about the three-tier model 197. Difference between String & StringBuffer 198. Wrapper class. Is String a Wrapper Class 199. What are the restriction for static method 200. Purpose of the file class 201. Default modifier in Interface 202. Difference between Interface & Abstract class 203. Can abstract be declared as Final 204. Can we declare variables inside a method as Final Variables 205. What is the package concept and use of package 206. How can a dead thread be started 207. Difference between Applet & Application 208. Life cycle of the Applet 209. Can Applet have constructors 210. Differeence between canvas class & graphics class 211. Explain about Superclass & subclass 212. Difference between TCP & UDP 213. What is AppletStub 214. Explain Stream Tokenizer 215. What is the difference between two types of threads 216. Checked & Unchecked exception 217. Use of throws exception 218. What is finally in exception handling 219. Vector class 220. What will happen to the Exception object after exception handling 221. Two types of multi-tasking 222. Two ways to create the thread 223. Synchronization 224. I/O Filter 225. How can you retrieve warnings in JDBC 226. Can applet in different page communicate with each other 227. Four driver Manager

Page 156 of 165

Page 157: Core Java 1

228. Features of JDBC 20 229. Explain about stored procedures 230. Servlet Life cycle 231. Why do you go for servlet rather than CGI 232. How to generate skeleton & Stub classes 233. Explain lazy activation 234. Firewalls in RMI235. What is meant by Java ? 236. What is meant by a class ? 237. What is meant by a method ? 238. What are the OOPS concepts in Java ? 239. What is meant by encapsulation ? Explain with an example 240. What is meant by inheritance ? Explain with an example 241. What is meant by polymorphism ? Explain with an example 242. Is multiple inheritance allowed in Java ? Why ? 243. What is meant by Java interpreter ? 244. What is meant by JVM ? 245. What is a compilation unit ? 246. What is meant by identifiers ? 247. What are the different types of modifiers ? 248. What are the access modifiers in Java ? 249. What are the primitive data types in Java ? 250. What is meant by a wrapper class ? 251. What is meant by static variable and static method ? 252. What is meant by Garbage collection ? 253. What is meant by abstract class 254. What is meant by final class, methods and variables ? 255. What is meant by interface ? 256. What is meant by a resource leak ? 257. What is the difference between interface and abstract class ? 258. What is the difference between public private, protected and static 259. What is meant by method overloading ? 260. What is meant by method overriding ? 261. What is singleton class ? 262. What is the difference between an array and a vector ? 263. What is meant by constructor ? 264. What is meant by casting ? 265. What is the difference between final, finally and finalize ? 266. What is meant by packages ? 267. What are all the packages ? 268. Name 2 calsses you have used ? 269. Name 2 classes that can store arbitrary number of objects ? 270. What is the difference between java.applet.* and java.applet.Applet ? 271. What is a default package ? 272. What is meant by a super class and how can you call a super class ? 273. What is anonymous class ?

Page 157 of 165

Page 158: Core Java 1

274. Name interfaces without a method ? 275. What is the use of an interface ? 276. What is a serializable interface ? 277. How to prevent field from serialization ? 278. What is meant by exception ? 279. How can you avoid the runtime exception ? 280. What is the difference between throw and throws ? 281. What is the use of finally ? 282. Can multiple catch statements be used in exceptions ? 283. Is it possible to write a try within a try statement ? 284. What is the method to find if the object exited or not ? 285. What is meant by a Thread ? 286. What is meant by multi-threading ? 287. What is the 2 way of creating a thread ? Which is the best way and why ? 288. What is the method to find if a thread is active or not ? 289. What are the thread-to-thread communcation ? 290. What is the difference between sleep and suspend ? 291. Can thread become a member of another thread ? 292. What is meant by deadlock ? 293. How can you avoid a deadlock ? 294. What are the three typs of priority ? 295. What is the use of synchronizations ? 296. Garbage collector thread belongs to which priority ? 297. What is meant by time-slicing ? 298. What is the use of ‘this’ ? 299. How can you find the length and capacity of a string buffer ? 300. How to compare two strings ? 301. What are the interfaces defined by Java.lang ? 302. What is the purpose of run-time class and system class 303. What is meant by Stream and Types ? 304. What is the method used to clear the buffer ? 305. What is meant by Stream Tokenizer ? 306. What is serialization and de-serialisation ? 307. What is meant by Applet ? 308. How to find the host from which the Applet has originated ? 309. What is the life cycle of an Applet ? 310. How do you load an HTML page from an Applet ? 311. What is meant by Applet Stub Interface ? 312. What is meant by getCodeBase and getDocumentBase method ? 313. How can you call an applet from a HTML file 314. What is meant by Applet Flickering ? 315. What is the use of parameter tag ? 316. What is audio clip Interface and what are all the methods in it ? 317. What is the difference between getAppletInfo and getParameterInfo ? 318. How to communicate between applet and an applet ? 319. What is meant by event handling ?

Page 158 of 165

Page 159: Core Java 1

320. What are all the listeners in java and explain ? 321. What is meant by an adapter class ? 322. What are the types of mouse event listeners ? 323. What are the types of methods in mouse listeners ? 324. What is the difference between panel and frame ? 325. What is the default layout of the panel and frame ? 326. What is meant by controls and types ? 327. What is the difference between a scroll bar and a scroll panel. 328. What is the difference between list and choice ? 329. How to place a component on Windows ? 330. What are the different types of Layouts ? 331. What is meant by CardLayout ? 332. What is the difference between GridLayout and GridBagLayout 333. What is the difference between menuitem and checkboxmenu item. 334. What is meant by vector class, dictionary class , hash table class,and property class ? 335. Which class has no duplicate elements ? 336. What is resource bundle ? 337. What is an enumeration class ? 338. What is meant by Swing ? 339. What is the difference between AWT and Swing ? 340. What is the difference between an applet and a Japplet 341. What are all the components used in Swing ? 342. What is meant by tab pans ? 343. What is the use of JTree ? 344. How can you add and remove nodes in Jtree. 345. What is the method to expand and collapse nodes in a Jtree ? 346. What is the use of JTable ? 347. What is meant by JFC ? 348. What is the class in Swing to change the appearance of the Frame in Runtime. 349. How to reduce flicking in animation ? 350. What is JDBC ? 351. How do you connect to the database ? What are the steps ? 352. What are the drivers available in JDBC ? Explain 353. How can you load the driver ? 354. What are the different types of statement s ? 355. How can you created JDBC statements ? 356. How will you perform transactions using JDBC ? 357. What are the two drivers for web apllication? 358. What are the different types of 2 tier and 3 tier architecture ? 359. How can you retrieve warning in JDBC ? 360. What is the exception thrown by JDBC ? 361. What is meants by PreparedStatement ? 362. What is difference between PreparedStatement and Statement ? 363. How can you call the stored procedures ? 364. What is meant by a ResultSet ?

Page 159 of 165

Page 160: Core Java 1

365. What is the difference between ExecuteUpdate and ExecuteQuery ? 366. How do you know which driver is connected to a database ? 367. What is DSN and System DSN and differentiate these two ? 368. What is meant by TCP, IP, UDP ? 369. What is the difference between TCP and UDP ? 370. What is a proxy server ? 371. What is meant by URL 372. What is a socket and server sockets ? 373. When MalformedURLException and UnknownHost Exception throws ? 374. What is InetAddress ? 375. What is datagram and datagram packets and datagram sockets ? 376. Write the range of multicast socket IP address ? 377. What is meant by a servlet ? 378. What are the types of servlets ? Explain 379. What is the different between a Servlet and a CGI. 380. What is the difference between 2 types of Servlets ? 381. What is the type of method for sending request from HTTP server ? 382. What are the exceptions thrown by Servlets ? Why ? 383. What is the life cycle of a servlet ? 384. What is meant by cookies ? 385. What is HTTP Session ? 386. What is the difference between GET and POST methods ? 387. How can you run a Servlet Program ? 388. How to commuincate between an applet and a servlet ? 389. What is a Servlet-to-Servlet communcation ? 390. What is Session Tracking ? 391. What are the security issues in Servlets ? 392. What is HTTP Tunneling 393. How do you load an image in a Servlet ? 394. What is Servlet Chaining ? 395. What is URL Rewriting ? 396. What is context switching ? 397. What is meant by RMI ? 398. Explain RMI Architecture ? 399. What is meant by a stub ? 400. What is meant by a skelotn ? 401. What is meant by serialisation and deserialisation ? 402. What is meant by RRL ? 403. What is the use of TL ? 404. What is RMI Registry ? 405. What is rmic ? 406. How will you pass parameter in RMI ? 407. What exceptions are thrown by RMI ? 408. What are the steps involved in RMI ? 409. What is meant by bind(), rebind(), unbind() and lookup() methods 410. What are the advanatages of RMI ?

Page 160 of 165

Page 161: Core Java 1

411. What is JNI ? 412. What is Remote Interface ? 413. What class is used to create Server side object ? 414. What class is used to bind the server object with RMI Registry ? 415. What is the use of getWriter method ? 416. What is meant by Javabeans ? 417. What is JAR file ? 418. What is meant by manifest files ? 419. What is Introspection ? 420. What are the steps involved to create a bean ? 421. Say any two properties in Beans ? 422. What is persistence ? 423. What is the use of beaninfo ? 424. What are the interfaces you used in Beans ? 425. What are the classes you used in Beans ?

Page 161 of 165

Page 162: Core Java 1

Java database interview questions

1. How do you call a Stored Procedure from JDBC? - The first step is to create a CallableStatement object. As with Statement and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure.

2. CallableStatement cs =3. con.prepareCall("{call SHOW_SUPPLIERS}");4. ResultSet rs = cs.executeQuery();

5. Is the JDBC-ODBC Bridge multi-threaded? - No. The JDBC-ODBC Bridge does not support concurrent access from different threads. The JDBC-ODBC Bridge uses synchronized methods to serialize all of the calls that it makes to ODBC. Multi-threaded Java programs may use the Bridge, but they won’t get the advantages of multi-threading.

6. Does the JDBC-ODBC Bridge support multiple concurrent open statements per connection? - No. You can open only one Statement object per connection when you are using the JDBC-ODBC Bridge.

7. What is cold backup, hot backup, warm backup recovery? - Cold backup (All these files must be backed up at the same time, before the databaseis restarted). Hot backup (official name is ‘online backup’) is a backup taken of each tablespace while the database is running and is being accessed by the users.

8. When we will Denormalize data? - Data denormalization is reverse procedure, carried out purely for reasons of improving performance. It maybe efficient for a high-throughput system to replicate data for certain data.

9. What is the advantage of using PreparedStatement? - If we are using PreparedStatement the execution time will be less. The PreparedStatement object contains not just an SQL statement, but the SQL statement that has been precompiled. This means that when the PreparedStatement is executed,the RDBMS can just run the PreparedStatement’s Sql statement without having to compile it first.

10. What is a “dirty read”? - Quite often in database processing, we come across the situation wherein one transaction can change a value, and a second transaction can read this value before the original change has been committed or rolled back. This is known as a dirty read scenario because there is always the possibility that the first transaction may rollback the change, resulting in the second transaction having read an invalid value. While you can easily command a database to disallow dirty reads, this usually degrades the performance of your application due to the increased locking overhead. Disallowing dirty reads also leads to decreased system concurrency.

11. What is Metadata and why should I use it? - Metadata (’data about data’) is information about one of two things: Database information (java.sql.DatabaseMetaData), or Information about a specific ResultSet

Page 162 of 165

Page 163: Core Java 1

(java.sql.ResultSetMetaData). Use DatabaseMetaData to find information about your database, such as its capabilities and structure. Use ResultSetMetaData to find information about the results of an SQL query, such as size and types of columns

12. Different types of Transaction Isolation Levels? - The isolation level describes the degree to which the data being updated is visible to other transactions. This is important when two transactions are trying to read the same row of a table. Imagine two transactions: A and B. Here three types of inconsistencies can occur:

o Dirty-read: A has changed a row, but has not committed the changes. B reads the uncommitted data but his view of the data may be wrong if A rolls back his changes and updates his own changes to the database.

o Non-repeatable read: B performs a read, but A modifies or deletes that data later. If B reads the same row again, he will get different data.

o Phantoms: A does a query on a set of rows to perform an operation. B modifies the table such that a query of A would have given a different result. The table may be inconsistent.

TRANSACTION_READ_UNCOMMITTED : DIRTY READS, NON-REPEATABLE READ AND PHANTOMS CAN OCCUR.TRANSACTION_READ_COMMITTED : DIRTY READS ARE PREVENTED, NON-REPEATABLE READ AND PHANTOMS CAN OCCUR.TRANSACTION_REPEATABLE_READ : DIRTY READS , NON-REPEATABLE READ ARE PREVENTED AND PHANTOMS CAN OCCUR.TRANSACTION_SERIALIZABLE : DIRTY READS, NON-REPEATABLE READ AND PHANTOMS ARE PREVENTED.

13. What is 2 phase commit? - A 2-phase commit is an algorithm used to ensure the integrity of a committing transaction. In Phase 1, the transaction coordinator contacts potential participants in the transaction. The participants all agree to make the results of the transaction permanent but do not do so immediately. The participants log information to disk to ensure they can complete In phase 2 f all the participants agree to commit, the coordinator logs that agreement and the outcome is decided. The recording of this agreement in the log ends in Phase 2, the coordinator informs each participant of the decision, and they permanently update their resources.

14. How do you handle your own transaction ? - Connection Object has a method called setAutocommit(Boolean istrue)- Default is true. Set the Parameter to false , and begin your transaction

15. What is the normal procedure followed by a java client to access the db.? - The database connection is created in 3 steps:

o Find a proper database URL

Page 163 of 165

Page 164: Core Java 1

o Load the database driver

o Ask the Java DriverManager class to open a connection to your database

In java code, the steps are realized in code as follows:

o Create a properly formatted JDBR URL for your database. (See FAQ on JDBC URL for more information). A JDBC URL has the formjdbc:someSubProtocol://myDatabaseServer/theDatabaseName

o Class.forName(”my.database.driver”);

o Connection conn = DriverManager.getConnection(”a.JDBC.URL”, “databaseLogin”,”databasePassword”);

1. What is a data source? - A DataSource class brings another level of abstraction than directly using a connection object. Data source can be referenced by JNDI. Data Source may point to RDBMS, file System , any DBMS etc.

1. What are collection pools? What are the advantages? - A connection pool is a cache of database connections that is maintained in memory, so that the connections may be reused

1. How do you get Column names only for a table (SQL Server)? Write the Query. -

1. select name from syscolumns1. where id=(select id from sysobjects where

name='user_hdr')1. order by colid --user_hdr is the table name

Page 164 of 165

Page 165: Core Java 1

Questions on Language Fundamentals 1Answers 10Questions on Operator and Assignments 11Answers to questions on Operators and Assignments 24Questions on Declaration and Access Control 25Answers to questions on Declarations 28Questions on Classes 29Answers to questions on classes in Java 30Questions on Collections 32Answers to questions on Collections 33Questions on Layout Managers 33Answers to questions on Layout Managers 39Questions on Files and Input/Output 39Answers to questions on Files and I/O 40Questions on Assertions 41Answers to questions on Assertions 42Mock Exam – 1 46Answers 50Mock exam – 2 51Answers 56Mock Exam - 3 56Answers 60Mock Exam – 1 62Answers 63Mock Exam – 1 64Answers 66Mock Exam – 1 66Answers 68Practice Exam 70Practice Exam –II 90Mock Exam 1 93Mock Exam 1 122JAVA and EJB - INTERVIEW QUESTIONS 151Java database interview questions 163

Page 165 of 165


Recommended