Java Developers [ SCJP ]

SCJP , java certification , Sun java certification , Java question and answer , SCJP question & answer , Top question and answer for java developers , challenge to java developers, Sun certification , Advance java , core java, java 1.5 question and answer, java 1.5.

Wednesday, July 05, 2006

SCJP Question & Answer

Questions on Language Fundamentals

  1. Which of these are legal identifiers. Select the three correct answers.
    1. number_1
    2. number_a
    3. $1234
    4. -volatile
  2. Which of these are not legal identifiers. Select the four correct answers.
    1. 1alpha
    2. _abcd
    3. xy+abc
    4. transient
    5. account-num
    6. very_long_name
  3. Which of the following are keywords in Java. Select the two correct answers.
    1. friend
    2. NULL
    3. implement
    4. synchronized
    5. throws
  4. Which of the following are Java keywords. Select the four correct answers.
    1. super
    2. strictfp
    3. void
    4. synchronize
    5. instanceof
  5. Which of these are Java keywords. Select the five correct answers
    1. TRUE
    2. volatile
    3. transient
    4. native
    5. interface
    6. then
    7. 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.
    1. 0
    2. -215
    3. -28
    4. -215 - 1
    5. -216
    6. -216 - 1
  10. How many bytes are used to represent the primitive data type int in Java. Select the one correct answer.
    1. 2
    2. 4
    3. 8
    4. 1
    5. 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.
    1. 0 to 256
    2. 0 to 255
    3. -128 to 127
    4. -128 to 128
    5. -127 to 128
    6. -215 to 215 - 1
  12. The width in bits of double primitive type in Java is --. Select the one correct answer.
    1. The width of double is platform dependent
    2. 64
    3. 128
    4. 8
    5. 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 <>
19.                y = 1;
20.             if(x>= 10) y = 2;
21.             System.out.println("y is " + y);
22.          }
23.       }
24.        
    1. The program compiles and prints y is 0 when executed.
    2. The program compiles and prints y is 1 when executed.
    3. The program compiles and prints y is 2 when executed.
    4. The program does not compile complaining about y not being initialized.
    5. The program throws a runtime exception.
  1. 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();
33.             System.out.println("pnt is " + pnt.name + 
34.                " " + pnt.x + " " + pnt.y);          
35.          }
36.       }
37.        
    1. The program does not compile because x, y and name are not initialized.
    2. The program throws a runtime exception as x, y, and name are used before initialization.
    3. The program prints pnt is 0 0.
    4. The program prints pnt is null 0 0.
    5. The program prints pnt is NULL false false
  1. The initial value of an instance variable of type String that is not explicitly initialized in the program is --. Select the one correct answer.
    1. null
    2. ""
    3. NULL
    4. 0
    5. The instance variable must be explicitly assigned.
  2. 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.
    1. null
    2. ""
    3. NULL
    4. 0
    5. The local variable must be explicitly assigned.
  3. Which of the following are legal Java programs. Select the four correct answers.
    1. // The comments come before the package
      package pkg;
      import java.awt.*;
      class C{}
    2. package pkg;
      import java.awt.*;
      class C{}
    3. package pkg1;
      package pkg2;
      import java.awt.*;
      class C{}
    4. package pkg;
      import java.awt.*;
    5. import java.awt.*;
      class C{}
    6. import java.awt.*;
      package pkg;
      class C {}
  4. Which of the following statements are correct. Select the four correct answers.
    1. A Java program must have a package statement.
    2. A package statement if present must be the first statement of the program (barring any comments).
    3. If a Java program defines both a package and import statement, then the import statement must come before the package statement.
    4. An empty file is a valid source file.
    5. A Java file without any class or interface definitions can also be compiled.
    6. If an import statement is present, it must appear before any class or interface definitions.
  5. 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.        
    1. The program does not compile as there is no main method defined.
    2. The program compiles and runs generating an output of "test"
    3. The program compiles and runs but does not generate any output.
    4. The program compiles but does not run.
  1. Which of these are valid declarations for the main method? Select the one correct answer.
    1. public void main();
    2. public static void main(String args[]);
    3. static public void main(String);
    4. public static void main(String );
    5. public static int main(String args[]);
  2. Which of the following are valid declarations for the main method. Select the three correct answers.
    1. public static void main(String args[]);
    2. public static void main(String []args);
    3. final static public void main (String args[]);
    4. public static int main(String args[]);
    5. public static abstract void main(String args[]);
  3. 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.        
    1. The program compiles and runs but does not print anything.
    2. The program compiles and runs and prints 0
    3. The program compiles and runs and prints 1
    4. The program compiles and runs and prints 2
    5. The program does not compile.
  1. 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);          
70.          }
71.       }
72.        
73.        
    1. The program does not compile as k is being read without being initialized.
    2. The program does not compile because of the statement k = j = i = 1;
    3. The program compiles and runs printing 0.
    4. The program compiles and runs printing 1.
    5. The program compiles and runs printing 2.
  1. 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.        
    1. The program will throw an ArrayIndexOutOfBounds exception.
    2. The program will print "java test"
    3. The program will print "java happens";
    4. The program will print "test happens"
    5. The program will print "lets happens"
  1. 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.        
    1. The program will throw an ArrayIndexOutOfBounds exception.
    2. The program will print "java test"
    3. The program will print "java happens";
    4. The program will print "test happens"
    5. The program will print "lets happens"
  1. 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.        
    1. java
    2. test
    3. lets
    4. 3
    5. 4
    6. 5
    7. 6
  1. 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.    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.
  1. 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.    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.
  1. 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.    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.
  1. 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.    
    1. The program does not compile.
    2. The program prints 0.
    3. The program prints 1.
    4. The program prints 2.
    5. The program prints 4.


Answers to questions on Language Fundamentals

  1. a, b, c
  2. a, c, d, e
  3. d, e
  4. a, b, c, e. Please note that strictfp is a new keyword in Java 2. See Sun's site for more details.
  5. b, c, d, e, g
  6. Any of the following are correct answers - 06, 006, or 0006
  7. Any of the following are correct answers - 0x03, 0X03, 0X3 or 0x3
  8. Any of the following are correct answers - 0x0a, 0X0a, 0Xa, 0xa, 0x0A, 0X0A, 0XA, 0xA
  9. a
  10. b
  11. c
  12. b
  13. d. The variable y is getting read before being properly initialized.
  14. d. Instance variable of type int and String are initialized to 0 and null respectively.
  15. a
  16. e
  17. a, b, d, e
  18. b, d, e, f
  19. d
  20. b
  21. a, b, c
  22. a
  23. d
  24. e
  25. a
  26. c, e
  27. e
  28. c
  29. e
  30. c

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';               // 1
8.                          i = c;         //2
9.                          c = i + 1;     //3
10.                      c++;           //4
11.               }
12.       }
13.        
    1. The line labeled 1.
    2. The line labeled 2.
    3. The line labeled 3.
    4. The line labeled 4.
    5. All the lines are correct and the program compiles.
  1. Which of these assignments are valid. Select the four correct answers.
    1. short s = 28;
    2. float f = 2.3;
    3. double d = 2.3;
    4. int I = '1';
    5. byte b = 12;
  2. 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.        
    1. 0
    2. 1
    3. 2
    4. 3
  1. Which of these lines will compile? Select the four correct answers.
    1. short s = 20;
    2. byte b = 128;
    3. char c = 32;
    4. double d = 1.4;;
    5. float f = 1.4;
    6. byte e = 0;
  2. The signed right shift operator in Java is --. Select the one correct answer.
    1. <<;
    2. >>
    3. >>>;
    4. None of these.
  3. 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 &&amp;amp; ((i++) == 0));
37.                      b = (f &&amp;amp; ((i+=2) > 0));
38.                      System.out.println(i);         
39.               }
40.       }
41.        
    1. 0
    2. 1
    3. 2
    4. 3
  1. 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.        
    1. 0
    2. 1
    3. 2
    4. 3
  1. 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.        
    1. 0
    2. 1
    3. 2
    4. 3
  1. 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.        
    1. 0
    2. 1
    3. 2
    4. 3
  1. Which operator is used to perform bitwise inversion in Java. Select the one correct answer.
    1. ~
    2. !
    3. &
    4. |
    5. ^
  2. 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.        
    1. 3
    2. 0
    3. 1
    4. 11
    5. 252
    6. 214
    7. 124
    8. -4
  1. 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.    
    1. 7 1
    2. 3 7
    3. 1 7
    4. 3 1
    5. 1 3
    6. 7 3
    7. 7 5
  1. 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.    
    1. 1 3
    2. 3 5
    3. 5 1
    4. 3 6
    5. 1 7
    6. 1 5
  1. Which operator is used to perform bitwise exclusive or.
    1. &
    2. ^
    3. |
    4. !
    5. ~
  2. 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;
125.                  System.out.println(a);
126.           }
127.   }
128.    
    1. 1
    2. 2
    3. 3
    4. 4
  1. 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.    
    1. 1
    2. 2
    3. 3
    4. 4
  1. 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.    
    1. 5
    2. 2
    3. 80
    4. 0
    5. 64
  1. 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 <<>
162.                  System.out.println(x);
163.           }
164.   }
165.    
    1. 1
    2. 0
    3. 7
    4. 5
    5. 23
    6. 2147483646
  1. Which of the following are correct. Select all correct answers.
    1. Java provides two operators to do left shift - <<>
    2. >> is the zero fill right shift operator.
    3. >>> is the signed right shift operator.
    4. For positive numbers, results of operators >> and >>> are same.
  2. 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.    
    1. 63
    2. -1
    3. 0
    4. 1
    5. 127
    6. 128
    7. 255
  1. 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.    
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  1. 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.                  else
204.                      z = 1;
205.    
206.                  if(y) 
207.                      z = 2;
208.                  else
209.                      z = 3;
210.                  System.out.println(z);                
211.           }
212.   }
213.    
    1. The program prints 0
    2. The program prints 1
    3. The program prints 2
    4. The program prints 3
    5. The program does not compile because of problems in the if statement.
  1. 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 <>
219.                  for(int j = 3; j >= 0; j--) {
220.                      if(i == j) continue;
221.                      System.out.println(i + " " + j);
222.                  }
223.               }
224.           }
225.   }
226.    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
    17. The program does not print anything.
  1. 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 <>
232.                  for(int j = 3; j <= 0; j--) {
233.                      if(i == j) continue;
234.                      System.out.println(i + " " + j);
235.                  }
236.               }
237.           }
238.   }
239.    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
    17. The program does not print anything.
  1. 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 <>
245.                  for(int j = 3; j >= 0; j--) {
246.                      if(i == j) break;
247.                      System.out.println(i + " " + j);
248.                  }
249.               }
250.           }
251.   }
252.    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
  1. 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 <>
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.    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 3 3
  1. 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 <>
271.                  for(int j = 3; j >= 0; j--) {
272.                      if(i == j) break outer;
273.                      System.out.println(i + " " + j);
274.                  }
275.               }
276.           }
277.   }
278.    
    1. 0 0
    2. 0 1
    3. 0 2
    4. 0 3
    5. 1 0
    6. 1 1
    7. 1 2
    8. 1 3
    9. 2 0
    10. 2 1
    11. 2 2
    12. 2 3
    13. 3 0
    14. 3 1
    15. 3 2
    16. 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
  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.
    1. arr.length
    2. arr.length - 1
    3. arr.size
    4. arr.size - 1
    5. arr.length()
    6. arr.length() - 1
  2. Which of these statements are legal. Select the three correct answers.
    1. int arr[][] = new int[5][5];
    2. int []arr[] = new int[5][5];
    3. int[][] arr = new int[5][5];
    4. int[] arr = new int[5][];
    5. 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.
    1. int arr[4] = new int[4];
    2. int[4] arr = new int[4];
    3. int arr[] = new int[4];
    4. int arr[] = new int[4][4];
    5. 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.        
    1. The program does not compile because arr[0] is being read before being initialized.
    2. The program generates a runtime exception because arr[0] is being read before being initialized.
    3. The program compiles and prints 0 when executed.
    4. The program compiles and prints 1 when executed.
    5. The program compiles and runs but the results are not predictable because of un-initialized memory being read.
  1. Which of the following are legal declaration and definition of a method. Select all correct answers.
    1. void method() {};
    2. void method(void) {};
    3. method() {};
    4. method(void) {};
    5. void method {};
  2. Which of the following are valid constructors within a class Test. Select the two correct answers.
    1. test() { }
    2. Test() { }
    3. void Test() { }
    4. private final Test() { }
    5. abstract Test() { }
    6. Test(Test t) { }
    7. Test(void) { }
  3. 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.        
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.

    1. The program compiles and runs printing 5.
    2. The program compiles and runs printing 6.
    3. The program gives runtime exception because it does not find the method Test.methodA(int)
    4. 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

  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.        
    1. The class does not compile because the top level class cannot be protected.
    2. The program prints "abc"
    3. The program prints "abcabc"
    4. The program does not compile because statement "test = test + test" is illegal.
  1. A top level class may have only the following access modifier. Select the one correct answer.
    1. package
    2. friendly
    3. private
    4. protected
    5. public
  2. 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.
  3. Select the one most appropriate answer. A top level class without any modifier is accessible to -
    1. any class
    2. any class within the same package
    3. any class within the same file
    4. any subclass of this class.
  4. Is this True or False. In Java an abstract class cannot be sub-classed.
  5. Is this True or False. In Java a final class must be sub-classed before it can be used.
  6. Which of the following are true. Select the three correct answers.
    1. A static method may be invoked before even a single instance of the class is constructed.
    2. A static method cannot access non-static methods of the class.
    3. Abstract modifier can appear before a class or a method but not before a variable.
    4. final modifier can appear before a class or a variable but not before a method.
    5. 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 AWT

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

  1. Which of the following classes are derived from the Container class. Select the four correct answers.
    1. Component
    2. Panel
    3. java.applet.Applet
    4. Dialog
    5. Frame
    6. MenuComponent
  2. Which of the following classes are derived from the Component class. Select the four correct answers.
    1. Container
    2. Window
    3. List
    4. MenuItem
    5. Choice
  3. Name the class used to represent a GUI application window, which is optionally resizable and can have a title bar, an icon, and menus. Select the one correct answer.
    1. Window
    2. Panel
    3. Dialog
    4. Frame
  4. Which abstract class is the super class of all menu related classes.
  5. Which of these classes can be added to a Frame component. Select the three correct answers.
    1. Menu
    2. Button
    3. PopupMenu
    4. Window
    5. List
  6. Which class can be used to represent a checkbox with a textual label that can appear in a menu. Select the one correct answer.
    1. MenuBar
    2. MenuItem
    3. CheckboxMenuItem
    4. Menu
    5. CheckBox
  7. Which of these classes can be added to any Container class, using the add method defined in Container class. Select the two correct answers.
    1. Button
    2. CheckboxMenuItem
    3. Menu
    4. Canvas

Answers to questions on AWT

  1. b, c, d, e
  2. a, b, c, e
  3. d
  4. MenuComponent
  5. b, c, e
  6. c
  7. a, d

Questions on Events

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

  1. Name the method defined in EventObject class that returns the Object generated from the event. Select the one correct answer.
    1. getEvent()
    2. getObject()
    3. getID()
    4. getSource()
  2. What is the return type of the method getID() defined in AWTEvent class. Select the one correct answer.
    1. int
    2. long
    3. Object
    4. Component
    5. short
  3. Name the event that gets generated when a button is clicked. Select the one correct answer.
    1. KeyEvent
    2. MouseEvent
    3. ItemEvent
    4. ActionEvent
  4. Which event is generated when the position of a scrollbar is changed. Select the one correct answer.
    1. KeyEvent
    2. MouseEvent
    3. ItemEvent
    4. ActionEvent
    5. AdjustmentEvent
  5. Which of the following Objects can generate ActionEvent. Select the one correct answer.
    1. List
    2. TextArea
    3. CheckboxMenuItem
    4. Choice
  6. Which of the following Objects can generate ItemEvent. Select the two correct answer.
    1. CheckBox
    2. Button
    3. List
    4. MenuItem
  7. Which method identifies the type of an event generated. Select the one correct answer.
    1. getSource()
    2. getType()
    3. getEventType()
    4. getID()
  8. Which of the following are legal adapter classes in Java. Select the two correct answers.
    1. ActionAdapter
    2. ItemAdapter
    3. TextAdapter
    4. MouseAdapter
    5. MouseMotionAdapter
  9. Name the class of the argument of method actionPerformed() defined in the ActionListener interface.
  10. Which of these listener classes have corresponding adapter classes. Select the two correct answers.
    1. ContainerListener
    2. TextListener
    3. ItemListener
    4. MouseMotionListener
  11. Which of these are valid adapter classes. Select the one correct answer.
    1. ActionAdapter
    2. AdjustmentAdapter
    3. KeyAdapter
    4. TextAdapter
  12. Which of these methods are defined in MouseMotionlistener interface. Select the two correct answers.
    1. mouseClicked()
    2. mousePressed()
    3. mouseEntered()
    4. mouseDragged()
    5. mouseMoved()
  13. What is the return type of the method getSource() defined in EventObject class. Select the one correct answer.
    1. int
    2. long
    3. Object
    4. Component
    5. short

Answers to questions on Events

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

Questions on Files and Input/Output

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

  1. Which abstract class is the super class of all classes used for reading bytes. Select the one correct answer.
    1. Reader
    2. FileReader
    3. ByteReader
    4. InputStream
    5. FileInputStream
  2. Which abstract class is the super class of all classes used for writing characters. Select the one correct answer.
    1. Writer
    2. FileWriter
    3. CharWriter
    4. OutputStream
    5. FileOutputStream
  3. Which of these are legal ways of accessing a File named "file.tst" for reading. Select the two correct answers.
    1. FileReader fr = new FileReader("file.tst");
    2. FileInputStream fr = new FileInputStream("file.tst");
      InputStreamReader isr = new InputStreamReader(fr, "UTF8");
    3. FileReader fr = new FileReader("file.tst", "UTF8");
    4. 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.
    1. FilterWriter
    2. Reader
    3. InputStream
    4. CharArrayReader
    5. DataInputStream
  6. Name the exception thrown by the read method defined in InputStream class

Answers to questions on Files and I/O

  1. d
  2. a
  3. a, b. FileReader class uses the default character encoding, hence c is incorrect. InputStreamReader character class does not have a constructor that takes file name as an argument. Hence d is incorrect.
  4. DataInpiutStream
  5. a,b,c
  6. IOException

Questions on Collections

  1. TreeMap class is used to implement which collection interface. Select the one correct answer.
    1. Set
    2. SortedSet
    3. List
    4. Tree
    5. 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.
    1. Set
    2. List
    3. Array
    4. Vector
    5. LinkedList
  6. Which of these are interfaces in the collection framework. Select the two correct answers.
    1. HashMap
    2. ArrayList
    3. Collection
    4. SortedMap
    5. 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.
    1. True
    2. 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.
    1. RowLayout
    2. ColumnLayout
    3. GridBagLayout
    4. FlowLayoutManager
    5. BorderLayoutManager
  2. Which of the following layout manager is the default Layout Manager for the Applet class in Java. Select the one correct answer.
    1. FlowLayout
    2. BorderLayout
    3. GridLayout
    4. CardLayout
    5. GridBagLayout
  3. Which of the following layout manager is the default Layout Manager for the Frame class in Java. Select the one correct answer.
    1. FlowLayout
    2. BorderLayout
    3. GridLayout
    4. CardLayout
    5. GridBagLayout
  4. Which of the following layout manager is the default Layout Manager for the Dialog class in Java. Select the one correct answer.
    1. FlowLayout
    2. BorderLayout
    3. GridLayout
    4. CardLayout
    5. 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.
    1. FlowLayout
    2. BorderLayout
    3. CardLayout
    4. 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 <>
15.                win.add(new Button(labels[i]));
16.             win.pack();      
17.             win.setVisible(true);
18.          }
19.       }
20.        
    1. The program will not display any buttons.
    2. The program will display all buttons in a single row.
    3. The program will display all buttons in a single column.
    4. The program will display three rows - A B, C D, and E F.
    5. The program will display two rows - A B C, and D E F.
    6. The rows and columns displayed are compiler and platform dependent.
  1. 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 <>
30.                win.add(new Button(labels[i]));
31.             win.pack();
32.             win.setVisible(true);
33.          }
34.       }
35.        
    1. 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.
  1. Select the one correct answer. The default alignment of buttons in Flow Layout is ...
    1. LEFT
    2. CENTER
    3. RIGHT
    4. The alignment must be defined when using the FlowLayout Manager.
  2. The default horizontal and vertical gap in FlowLayout is -
    1. 0 pixel
    2. 1 pixel
    3. 5 pixels
    4. 10 pixels
    5. The alignment must be defined when using the FlowLayout Manager.
  3. The default horizontal and vertical gap in BorderLayout is -
    1. 0 pixel
    2. 1 pixel
    3. 5 pixels
    4. 10 pixels
    5. The horizontal and vertical gaps must be defined when using the BorderLayout Manager.
  4. 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
48.                   win.add(new Button(labels[i]), "North");
49.               win.pack();
50.               win.setVisible(true);
51.           }
52.       }
53.        
    1. 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.
    5. The button A will occupy the whole window.
    6. The button A will occupy the complete first row of the window.
  1. 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);
    1. NORTH
    2. SOUTH
    3. EAST
    4. WEST
    5. CENTER
  2. What is the default filling of GridBagConstraint.
    1. NONE
    2. BOTH
    3. HORIZONTAL
    4. VERTICAL
  3. Which GridBagLayout variable defines the external padding (border) around the component in its display area. Select the one correct answer.
    1. gridwidth, gridheight
    2. gridx, gridy
    3. anchor
    4. fill
    5. ipadx, ipady
    6. insets
  4. Which GridBagLayout variable defines the padding that gets added to each side of the component. Select the one correct answer.
    1. gridwidth, gridheight
    2. gridx, gridy
    3. anchor
    4. fill
    5. ipadx, ipady
    6. insets
  5. Which GridBagLayout variable specifies the number of cells occupied by the component horizontally and vertically in the grid. Select the one correct answer.
    1. gridwidth, gridheight
    2. gridx, gridy
    3. anchor
    4. fill
    5. ipadx, ipady
  6. Which GridBagLayout variable specifies where (in which direction) a component should be placed in its display area. Select the one correct answer.
    1. gridwidth, gridheight
    2. gridx, gridy
    3. anchor
    4. fill
    5. ipadx, ipady
    6. insets
  7. What is the default value of GridBagLayout constraint anchor. Select the one correct answer.
    1. CENTER
    2. NORTH
    3. EAST
    4. SOUTH
    5. WEST

Answers to questions on Layout Managers

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

Questions on Assertions

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

  1. What happens when the following code is compiled and run. Select the one correct answer.


for(int i = 1; i <>
for(int j = 3; j > i; j--)
assert i!=j {System.out.println(i); }

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  1. What happens when the following code is compiled and run. Select the one correct answer.


for(int i = 1; i <>
for(int j = 3; j >= 1; j--)
assert i!=j : i;

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  1. What happens when the following code is compiled and run. Select the one correct answer.


for(int i = 1; i <>
for(int j = 1; j <>
if(i <>
assert i!=j : i;

    1. The class compiles and runs, but does not print anything.
    2. The number 1 gets printed with AssertionError
    3. The number 2 gets printed with AssertionError
    4. The number 3 gets printed with AssertionError
    5. The program generates a compilation error.

  1. Which of the following statement is true about the assert statement. Select the one correct answer.
    1. If a Java class contains assert statements, then it must be compiled with -1.4 option.
    2. When a program having assertions is run, -assertion option must be specified, otherwise the assertions get ignored.
    3. A possible syntax of assert statement is
      assert logical_expression If logical_expression evaluates to true, the program generates an AssertionError.
    4. 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.

pinpoint several places in exam:
1. switch( ?what type? ){ default: ?what order? } - several questions
2. threads synch., volatile variables - must understand, not very difficult
3. File(), java.io.* , streams constructors
4. AWT - simply must know, nothing difficult
5. inner classes (static, private, protected, final variables)
6. incapsulation, polymorphism, inheritance - must know these terms
7. arrays : Object o = new int [] {1,2,3,4}; //correct
8. int i = new Object(); //incorrect
9. String.intern( str ) - must understand
10. Function parameters are NEW variables that are passed by VALUE but not by reference.

Believe me, certification exam is completely different from a real project experience. While working in a project, you don't think through the things like, Can I override a "Final" method, Constructors get inherited or not, Can you >> (right shift) short, char, byte,etc. The certification exam is a way to help the new candidates to learn the JAVA language specifics rules and definitions. Passing the exam doesn't mean you know and can do everything in JAVA. There is a lot to the Java language and the best place to start is to get certified.

  1. Try to execute as many programs as possible. This will also help you in understanding difference between compile time & run time errors.
  2. Loops, continue and use of break statement in combination of increment/decrement operators can become very complex, so try out various examples of these.
  3. Make yourself so much acquainted with Java that any error in the code should give you a signal that there is some problem.
  4. Strong command on operators is very necessary because they change their behavior at different places & different situations.
  5. Overloading/overriding rules of exceptions that can be thrown and how access modifiers can be used with them is very important. In overriding, an overriding method cannot make the access more restricted. For example private amethod() cannot override public amethod().
  6. With respect to classes, methods & variables the access modifiers that are valid. For example a variable or a class cannot be native, synchronized. A variable cannot be abstract. etc.
  7. Inner classes & anonymous inner classes concepts are not present in most of the books. Try to check lots of information about them (deep knowledge is required).
  8. In multithreading understand issues like what will be the result of initiating the same thread twice.
  9. Which all methods are valid for string & string buffer.
  10. In lang & util package deep knowledge of each & every method is not required.
  11. IO streams are complex, their relations with each other, and how they are used is required.
  12. Try to execute each & every possible program with different layout managers & deep knowledge of constraints that make them behave differently is required.
  13. Event Handling and AWT are not that complex. The difficulty level of AWT and Event handling has not been changed. Studing from the books gives you enough knowledge.
  14. Now a general tip - try to be innovative, if your make any code alter it, execute it, try to make your brain work like java compiler & interpreter.
  15. Make a target of 90% or above, never think of just getting through.


For more questions on Assertions, you may want to visit Khalid Mughal's sample chapter on Assertions.

Mock exam 1

The sample test is modeled on the Sun Certification for JavaTM 2 Programmer exam. The test has 59 questions and needs to be executed in 2 hours. The real exam may be a little tougher than this. You need to score 36 correct answers to clear the real exam. The first 50 questions of the mock exam are valid for both 1.2 and 1.4 versions of the exam. The remaining 9 questions are only useful if you are taking the SCJP 1.2 exam. Please let me know if you find any issues with the test. The site also offers another mock exam.

  1. Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer.
    1. public static int main(char args[])
    2. public static void main(String args[])
    3. public static void MAIN(String args[])
    4. public static void main(String args)
    5. public static void main(char args[])
  2. What all gets printed when the following code is compiled and run? Select the three correct answers.
 
public class xyz {
   public static void main(String args[]) {
      for(int i = 0; i <>
         for(int j = 2; j>= 0; j--) {
            if(i == j) break;
            System.out.println("i=" + i + " j="+j);
         }
      }
   }
}
 
    1. i=0 j=0
    2. i=0 j=1
    3. i=0 j=2
    4. i=1 j=0
    5. i=1 j=1
    6. i=1 j=2
    7. i=2 j=0
    8. i=2 j=1
    9. i=2 j=2
  1. What gets printed when the following code is compiled and run with the following command -
    java test 2
    Select the one correct answer.
 
public class test {
   public static void main(String args[]) { 
      Integer intObj=Integer.valueOf(args[args.length-1]);
      int i = intObj.intValue();
 
      if(args.length > 1) 
         System.out.println(i);
      if(args.length > 0)
         System.out.println(i - 1);
      else 
         System.out.println(i - 2);
   }
}
        
 
    1. test
    2. test -1
    3. 0
    4. 1
    5. 2
  1. In Java technology what expression can be used to represent number of elements in an array named arr ?
  2. How would the number 5 be represented in hex using up-to four characters.
  3. Which of the following is a Java keyword. Select the four correct answers.
    1. extern
    2. synchronized
    3. volatile
    4. friend
    5. friendly
    6. transient
    7. this
    8. then
  4. Is the following statement true or false. The constructor of a class must not have a return type.
    1. true
    2. false
  5. What is the number of bytes used by Java primitive long. Select the one correct answer.
    1. The number of bytes is compiler dependent.
    2. 2
    3. 4
    4. 8
    5. 64
  6. What is returned when the method substring(2, 4) is invoked on the string "example"? Include the answer in quotes as the result is of type String.
  7. Which of the following is correct? Select the two correct answers.
    1. The native keyword indicates that the method is implemented in another language like C/C++.
    2. The only statements that can appear before an import statement in a Java file are comments.
    3. The method definitions inside interfaces are public and abstract. They cannot be private or protected.
    4. A class constructor may have public or protected keyword before them, nothing else.
  8. What is the result of evaluating the expression 14 ^ 23. Select the one correct answer.
    1. 25
    2. 37
    3. 6
    4. 31
    5. 17
    6. 9
    7. 24
  9. Which of the following are true. Select the one correct answers.
    1. && operator is used for short-circuited logical AND.
    2. ~ operator is the bit-wise XOR operator.
    3. | operator is used to perform bitwise OR and also short-circuited logical OR.
    4. The unsigned right shift operator in Java is >>.
  10. Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class.
  11. Which of the following is true. Select the two correct answers.
    1. A class that is abstract may not be instantiated.
    2. The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.
    3. A static variable indicates there is only one copy of that variable.
    4. A method defined as private indicates that it is accessible to all other classes in the same package.
  12. What all gets printed when the following program is compiled and run. Select the two correct answers.

 
public class test {
   public static void main(String args[]) { 
      int i, j=1;
      i = (j>1)?2:1;
      switch(i) {
        case 0: System.out.println(0); break;
        case 1: System.out.println(1);
        case 2: System.out.println(2); break;
        case 3: System.out.println(3); break;
      }
   }
}
        
 
    1. 0
    2. 1
    3. 2
    4. 3
  1. What all gets printed when the following program is compiled and run. Select the one correct answer.

 
public class test {
   public static void main(String args[]) { 
      int i=0, j=2;
      do {
         i=++i;
         j--;
      } while(j>0);
      System.out.println(i);
   }
}
        
 
    1. 0
    2. 1
    3. 2
    4. The program does not compile because of statement "i=++i;"
  1. What all gets printed when the following gets compiled and run. Select the three correct answers.
18.        
19.       public class test {
20.           public static void main(String args[]) { 
21.               int i=1, j=1;
22.               try {
23.                   i++; 
24.                   j--;
25.                   if(i/j > 1)
26.                       i++;
27.               }
28.               catch(ArithmeticException e) {
29.                   System.out.println(0);
30.               }
31.               catch(ArrayIndexOutOfBoundsException e) {
32.                   System.out.println(1);
33.               }
34.               catch(Exception e) {
35.                   System.out.println(2);
36.               }
37.               finally {
38.                   System.out.println(3);
39.               }
40.               System.out.println(4);
41.            }
42.       }
43.               
44.        
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  1. What all gets printed when the following gets compiled and run. Select the two correct answers.

 
public class test {
    public static void main(String args[]) { 
        int i=1, j=1;
        try {
            i++; 
            j--;
            if(i == j)
                i++;
        }
        catch(ArithmeticException e) {
            System.out.println(0);
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println(1);
        }
        catch(Exception e) {
            System.out.println(2);
        }
        finally {
            System.out.println(3);
        }
        System.out.println(4);
     }
}
        
 
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
  1. What all gets printed when the following gets compiled and run. Select the two correct answers.

 
public class test {
    public static void main(String args[]) { 
    String s1 = "abc";
    String s2 = "abc";
    if(s1 == s2)
        System.out.println(1);
    else
        System.out.println(2);
    if(s1.equals(s2))
        System.out.println(3);
    else
        System.out.println(4);
    }
}
        
 
    1. 1
    2. 2
    3. 3
    4. 4
  1. What all gets printed when the following gets compiled and run. Select the two correct answers.

 
public class test {
    public static void main(String args[]) { 
    String s1 = "abc";
    String s2 = new String("abc");
 
    if(s1 == s2)
        System.out.println(1);
    else
        System.out.println(2);
    if(s1.equals(s2))
        System.out.println(3);
    else
        System.out.println(4);
    }
}
        
 
    1. 1
    2. 2
    3. 3
    4. 4
  1. Which of the following are legal array declarations. Select the three correct answers.
    1. int i[5][];
    2. int i[][];
    3. int []i[];
    4. int i[5][5];
    5. int[][] a;
  2. What is the range of values that can be specified for an int. Select the one correct answer.
    1. The range of values is compiler dependent.
    2. -231 to 231 - 1
    3. -231-1 to 231
    4. -215 to 215 - 1
    5. -215-1 to 215
  3. How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
    1. By invoking the free method on the object.
    2. By calling system.gc() method.
    3. By setting all references to the object to new values (say null).
    4. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.
  4. What gets printed when the following code is compiled and run. Select the one correct answer.

 
public class test {
    public static void main(String args[]) { 
    int i = 1;
    do {
        i--;
    } while (i > 2);
    System.out.println(i);
    }
}
        
 
    1. 0
    2. 1
    3. 2
    4. -1
  1. Which of these is a legal definition of a method named m assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct answer.
    1. void m() throws IOException{}
    2. void m() throw IOException{}
    3. void m(void) throws IOException{}
    4. m() throws IOException{}
    5. void m() {} throws IOException
  2. Which of the following are legal identifier names in Java. Select the two correct answers.
    1. %abcd
    2. $abcd
    3. 1abcd
    4. package
    5. _a_long_name
  3. At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

 
void method X()  { 
    String r = new String("abc");
    String s = new String("abc");
    r = r+1; //1
    r = null; //2
    s = s + r; //3
} //4
        
 
    1. Before statement labeled 1
    2. Before statement labeled 2
    3. Before statement labeled 3
    4. Before statement labeled 4
    5. Never.
  1. String s = new String("xyz");
    Assuming the above declaration, which of the following statements would compile. Select the one correct answer.
    1. s = 2 * s;
    2. int i = s[0];
    3. s = s + s;
    4. s = s >> 2;
    5. None of the above.
  2. Which of the following statements related to Garbage Collection are correct. Select the two correct answers.
    1. It is possible for a program to free memory at a given time.
    2. Garbage Collection feature of Java ensures that the program never runs out of memory.
    3. It is possible for a program to make an object available for Garbage Collection.
    4. The finalize method of an object is invoked before garbage collection is performed on the object.
  3. If a base class has a method defined as
    void method() { }
    Which of the following are legal prototypes in a derived class of this class. Select the two correct answers.
    1. void method() { }
    2. int method() { return 0;}
    3. void method(int i) { }
    4. private void method() { }
  4. In which all cases does an exception gets generated. Select the two correct answers.


int i = 0, j = 1;

    1. if((i == 0) || (j/i == 1))
    2. if((i == 0) | (j/i == 1))
    3. if((i != 0) && (j/i == 1))
    4. if((i != 0) & (j/i == 1))
  1. Which of the following statements are true. Select the two correct answers.
    1. The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.
    2. The wait(), notify(), and notifyAll() methods must be executed in synchronized code.
    3. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.
    4. The Thread class is an abstract class.
  2. Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer.
    1. transient
    2. volatile
    3. synchronized
    4. native
    5. static
    6. final
  3. What is the name of the Collection interface used to represent elements in a sequence (in a particular order). Select the one correct answer.
    1. Collection
    2. Set
    3. List
    4. Map
  4. Which of these classes implement the Collection interface SortedMap. Select the one correct answers.
    1. HashMap
    2. Hashtable
    3. TreeMap
    4. HashSet
    5. TreeSet
    6. Vector
  5. Which of the following are true about interfaces. Select the two correct answers.
    1. Methods declared in interfaces are implicitly private.
    2. Variables declared in interfaces are implicitly public, static, and final.
    3. An interface can extend any number of interfaces.
    4. The keyword implements indicate that an interface inherits from another.
  6. Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.
    1. test();
    2. super.test();
    3. super.super.test();
    4. ::test();
    5. C.test();
    6. It is not possible to invoke test() method defined in C from a method in A.
  7. What is the return type of method round(double d) defined in Math class.
  8. What gets written on the screen when the following program is compiled and run. Select the one right answer.
 
public class test {
   public static void main(String args[]) { 
   int i;
   float  f = 2.3f;
   double d = 2.7;
   i = ((int)Math.ceil(f)) * ((int)Math.round(d));
 
   System.out.println(i);
   }
}
 
    1. 4
    2. 5
    3. 6
    4. 6.1
    5. 9
  1. Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object.
    1. true
    2. false
  2. Which of these classes defined in java.io and used for file-handling are abstract. Select the two correct answers.
    1. InputStream
    2. PrintStream
    3. Reader
    4. FileInputStream
    5. FileWriter
  3. Name the collection interface used to represent collections that maintain unique elements.
  4. What is the result of compiling and running the following program.
 
public class test {
   public static void main(String args[]) { 
      String str1="abc";
      String str2="def";
      String str3=str1.concat(str2);
 
      str1.concat(str2);
      System.out.println(str1);
   }
}
 
    1. abc
    2. def
    3. abcabc
    4. abcdef
    5. defabc
    6. abcdefdef
  1. Select the one correct answer. The number of characters in an object of a class String is given by
    1. The member variable called size
    2. The member variable called length
    3. The method size() returns the number of characters.
    4. The method length() returns the number of characters.
  2. Select the one correct answer. Which method defined in Integer class can be used to convert an Integer object to primitive int type.
    1. valueOf
    2. intValue
    3. getInt
    4. getInteger
  3. Name the return type of method hashCode() defined in Object class, which is used to get the unique hash value of an Object.
  4. Which of the following are correct. Select the one correct answer.
    1. An import statement, if defined, must always be the first non-comment statement of the file.
    2. private members are accessible to all classes in the same package.
    3. An abstract class can be declared as final.
    4. Local variables cannot be declared as static.
  5. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class. Select the one correct answer.
    1. static
    2. final
    3. abstract
    4. native
    5. volatile
    6. transient
  6. Which of these are core interfaces in the collection framework. Select the one correct answer.
    1. Tree
    2. Stack
    3. Queue
    4. Array
    5. LinkedList
    6. Map
  7. Which of these statements are true. Select the two correct answers.
    1. For each try block there must be at least one catch block defined.
    2. A try block may be followed by any number of finally blocks.
    3. A try block must be followed by at least one finally or catch block.
    4. If both catch and finally blocks are defined, catch block must precede the finally block.

The remaining questions are related to AWT, event classes, and layout managers. These topics are not included in 1.4 version of the exam.

  1. The default layout manager for a Frame is ...
    1. FlowLayout
    2. BorderLayout
    3. GridLayout
    4. GridBagLayout
    5. CardLayout
  2. Which of the following are valid adapter classes in Java. Select the two correct answers.
    1. ComponentAdapter
    2. ActionAdapter
    3. AdjustmentAdapter
    4. ItemAdapter
    5. FocusAdapter
  3. Which method defined in the EventObject class returns the Object that generated an event. The method should be given in the format - return_type method_name();
  4. Which of the following object receives ActionEvent. Select the four correct answers.
    1. List
    2. Button
    3. Choice
    4. CheckBox
    5. TextField
    6. MenuItem
  5. Name the class that may be used to create submenus in pull-down menus.
  6. In which class is the wait() method defined. Select the one correct answer.
    1. Applet
    2. Runnable
    3. Thread
    4. Object
  7. Which is the only layout manager that always honors the size of a component. Select the one correct answer.
    1. FlowLayout
    2. GridLayout
    3. BorderLayout
    4. CardLayout
    5. GridBagLayout
  8. Which of these are valid Event Listener interfaces. Select the two correct answers.
    1. MouseMotionListener
    2. WindowListener
    3. DialogListener
    4. PaintListener
  9. Which abstract class is the super class of all menu-related classes.


Answers to Sample Test 1

  1. b
  2. b, c, f
  3. d. Note that the program gets one command line argument - 2. args.length will get set to 1. So the condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true.
  4. arr.length
  5. Any of these is correct - 0x5, 0x05, 0X05, 0X5
  6. b, c, f, g
  7. a
  8. d
  9. "am"
  10. a, c. Please note that b is not correct. A package statement may appear before an import statement. A class constructor may be declared private also. Hence d is incorrect.
  11. a
  12. a
  13. protected
  14. a, c
  15. b, c
  16. c
  17. a, d, e
  18. d, e
  19. a, c
  20. b, c
  21. b, c, e
  22. b
  23. d
  24. a
  25. a
  26. b, e . The option c is incorrect because a Java identifier name cannot begin with a digit.
  27. d
  28. c
  29. c, d
  30. a, c
  31. b, d
  32. b, c
  33. c
  34. c
  35. c
  36. b, c
  37. f
  38. long
  39. e
  40. a
  41. a, c
  42. Set
  43. a
  44. d
  45. b
  46. int
  47. d
  48. a
  49. f
  50. c, d
  51. b
  52. a, e
  53. Object getSource();
  54. a, b, e, f
  55. Menu
  56. d
  57. a
  58. a, b
  59. MenuComponent

Mock Exam 2

The mock exam is modeled on the Sun Certification for Java 2 Programmer exam. The exam has 38 questions. The real exam may be a little tougher than this. You need to score 61% correct answers to clear the real exam. Please let me know if you find any issues with this exam. Some differences between this and the real exam are given below.

  1. The real exam will be tougher than this.
  2. The real exam will have 59 questions. This one has only 38
  3. The real exam would have more programmatic questions. Questions of the type - "what will happen when this code is compiled".
  4. The real exam will have more (and tougher) questions on threads and IO (for SCJP 1,2).

The first 29 questions are valid for both SCJP 1.2 and SCJP 1.4 . The last nine questions are related to User interface, and are valid only for the SCJP 1.2 exam.

  1. Which of the following are Java keywords? Select the three correct answers.
    1. external
    2. implement
    3. throw
    4. void
    5. integer
    6. private
    7. synchronize
    8. unsigned

  1. Which of the following are legal definitions of the main method that can be used to execute a class. Select the one correct answer.
    1. public void main(String args)
    2. public static int main(String args[])
    3. public static void main(String args[])
    4. static public void MAIN(String args[])
    5. public static void main(string args[])
    6. public static void main(String *args)

  1. Which of these are legal array declarations or definitions? Select the two correct answers.
    1. int[] []x[];
    2. int *x;
    3. int x[5];
    4. int[] x = {1,2,3};

  1. Name the collection interface used to represent a sequence of numbers in a fixed order.

  1. The class Hashtable is used to implement which collection interface. Select the one correct answer.
    1. Table
    2. List
    3. Set
    4. SortedSet
    5. Map

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

 
 
 
class test {
 
    public static void main(String args[]) {
 
        int i;
 
        do {
 
            i++;
 
        }
 
        while(i <>
 
        System.out.println(i);
 
    }
 
}
 
 
 
    1. The program does not compile as i is not initialized.
    2. The program compiles but does not run.
    3. The program compiles and runs but does not print anything.
    4. The program prints 0.
    5. The program prints 1.

  1. What gets printed when the following program is compiled and run? Select the one correct answer.
8.            
9.            
10.        
11.       class xyz {
12.        
13.           static int i;
14.        
15.           public static void main(String args[]) {
16.        
17.        
18.        
19.               while (i <>
20.        
21.                   i--;
22.        
23.               }
24.        
25.               System.out.println(i);
26.        
27.           }
28.        
29.       }
30.        
31.        
32.        
    1. The program does not compile as i is not initialized.
    2. The program compiles but does not run.
    3. The program compiles and runs but does not print anything.
    4. The program prints 0.
    5. The program prints 1.

  1. What gets printed when the following program is compiled and run? Select the one correct answer.
 
 
 
class xyz {
 
 
 
    public static void main(String args[]) {
 
        int i,j,k;
 
        for (i = 0; i <>
 
            for(j=1; j <>
 
                for(k=2; k<5;>
 
                    if((i == j)   && (j==k))
 
                        System.out.println(i);
 
                }                
 
            }
 
        }
 
    }
 
}
 
 
 
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4

  1. Using up to four characters what is the Java representation of the number 23 in hex?

  1. What gets printed when the following program is compiled and run? Select the one correct answer.
 
 
 
class test {
 
    static boolean check;
 
    public static void main(String args[]) {
 
        int i;
 
        if(check == true)
 
            i=1;
 
        else
 
            i=2;
 
 
 
        if(i=2) i=i+2;
 
        else i = i + 4;
 
        System.out.println(i);
 
     }
 
}
 
 
 
    1. 3
    2. 4
    3. 5
    4. 6
    5. The program does not compile because of the statement if(i=2)

  1. Select the one correct answer. The smallest number that can be represented using short primitive type in Java is -
    1. 0
    2. -127
    3. -128
    4. -16384
    5. -32768
    6. The smallest number is compiler dependent.

  1. Given the following declarations, which of the assignments given in the options below would compile. Select the two correct answers.
38.        
39.        
40.        
41.       int i = 5;
42.        
43.       boolean t = true;
44.        
45.       float f = 2.3F;
46.        
47.       double d = 2.3;
48.        
49.        
50.        
    1. t = (boolean) i;
    2. f = d;
    3. d = i;
    4. i = 5;
    5. f = 2.8;

  1. What gets printed when the following program is compiled and run. Select the one correct answer.
52.        
53.        
54.        
55.       public class incr {
56.        
57.           public static void main(String args[]) {
58.        
59.               int i , j;
60.        
61.               i = j = 3;
62.        
63.               int n = 2 * ++i;
64.        
65.               int m = 2 * j++;
66.        
67.               System.out.println(i + " " + j + " " + n + " " + m);
68.        
69.           }
70.        
71.       }
72.        
73.        
74.        
    1. 4 4 8 6
    2. 4 4 8 8
    3. 4 4 6 6
    4. 4 3 8 6
    5. 4 3 8 8
    6. 4 4 6 8

  1. Given two non-negative integers a and b and a String str, what is the number of characters in the expression str.substring(a,b) . Select the one correct answer.
    1. a + b
    2. a - b
    3. b - a - 1
    4. b - a + 1
    5. b - a
    6. b

  1. What is the result of compiling and running the following program. Select the one correct answer.
77.        
78.        
79.        
80.       class test {
81.        
82.           public static void main(String args[]) {
83.        
84.               char ch;
85.        
86.               String test2 = "abcd";
87.        
88.               String test = new String("abcd");
89.        
90.               if(test.equals(test2)) {
91.        
92.                   if(test == test2)
93.        
94.                       ch = test.charAt(0);
95.        
96.                   else
97.        
98.                       ch = test.charAt(1);             
99.        
100.           }
101.    
102.           else {
103.    
104.               if(test == test2)
105.    
106.                   ch = test.charAt(2);
107.    
108.               else                
109.    
110.                   ch = test.charAt(3);
111.    
112.           }
113.    
114.           System.out.println(ch);
115.    
116.       }
117.    
118.   }
119.    
120.    
121.    
    1. 'a'
    2. 'b'
    3. 'c'
    4. 'd'

  1. What is the result of compiling and running the following program. Select the one correct answer.
123.    
124.    
125.    
126.   class test {
127.    
128.       public static void main(String args[]) {
129.    
130.        int i,j=0;
131.    
132.        for(i=10;i<0;i--)>
133.    
134.        switch(j) {
135.    
136.        case (0) :
137.    
138.            j=j+1;
139.    
140.        case(1):
141.    
142.            j=j+2;
143.    
144.            break;
145.    
146.        case (2) :
147.    
148.            j=j+3;
149.    
150.            break;
151.    
152.        
153.    
154.        case (10) :
155.    
156.            j=j+10;
157.    
158.            break;
159.    
160.        default :
161.    
162.            break;
163.    
164.        }
165.    
166.      System.out.println(j);
167.    
168.      }
169.    
170.   }
171.    
172.    
173.    
    1. 0
    2. 1
    3. 2
    4. 3
    5. 10
    6. 20

  1. What is the number displayed when the following program is compiled and run.
 
 
 
class test {
 
    public static void main(String args[]) {
 
        test test1 = new test();
 
            System.out.println(test1.xyz(100));    
 
    }
 
    public int xyz(int num) {
 
        if(num == 1) return 1;
 
        else return(xyz(num-1) + num);
 
    }
 
}
 
 
 

  1. Which of the following statements are true. Select the one correct answer.
    1. Arrays in Java are essentially objects.
    2. It is not possible to assign one array to another. Individual elements of array can however be assigned.
    3. Array elements are indexed from 1 to size of array.
    4. If a method tries to access an array element beyond its range, a compile warning is generated.

  1. Which expression can be used to access the last element of an array. Select the one correct answer.
    1. array[array.length()]
    2. array[array.length() - 1]
    3. array[array.length]
    4. array[array.length - 1]

  1. What is the result of compiling and running the following program. Select the one correct answer.
 
 
 
class test {
 
    public static void main(String args[]) {
 
        int[] arr = {1,2,3,4};
 
        call_array(arr[0], arr);
 
        System.out.println(arr[0] + "," + arr[1]);        
 
    }
 
    static void call_array(int i, int arr[]) {
 
        arr[i] = 6;
 
        i = 5;
 
    }    
 
}
 
 
 
    1. 1,2
    2. 5,2
    3. 1,6
    4. 5,6

  1. Which of the following statements are correct. Select the one correct answer.
    1. Each Java file must have exactly one package statement to specify where the class is stored.
    2. If a Java file has both import and package statement, the import statement must come before package statement.
    3. A Java file has at least one class defined.
    4. If a Java file has a package statement, it must be the first statement (except comments).

  1. What happens when the following program is compiled and then the command "java check it out" is executed. Select the one correct answer.
 
 
 
class check {
 
    public static void main(String args[]) {
 
        System.out.println(args[args.length-2]);
 
    }
 
}
 
 
 
    1. The program does not compile.
    2. The program compiles but generates ArrayIndexOutOfBoundsException exception.
    3. The program prints java
    4. The program prints check
    5. The program prints it
    6. The program prints out

  1. What all gets printed when the following code is compiled and run. Select the three correct answers.
 
 
 
class test {
 
    public static void main(String args[]) {
 
        int i[] = {0,1};
 
        try {
 
            i[2] = i[0] + i[1];
 
        }
 
        catch(ArrayIndexOutOfBoundsException e1) {
 
            System.out.println("1");
 
        }
 
        catch(Exception e2) {
 
            System.out.println("2");
 
        }
 
        finally {
 
            System.out.println(3);
 
        }
 
        System.out.println("4");  
 
     }
 
}
 
 
 
    1. 1
    2. 2
    3. 3
    4. 4

  1. A program needs to store the name, salary, and age of employees in years. Which of the following data types should be used to create the Employee class. Select the three correct answers.
    1. char
    2. boolean
    3. Boolean
    4. String
    5. int
    6. double

  1. To make a variable defined in a class accessible only to methods defined in the classes in same package, which of the following keyword should be used. Select the one correct answer.
    1. By using the keyword package before the variable.
    2. By using the keyword private before the variable.
    3. By using the keyword protected before the variable.
    4. By using the keyword public before the variable.
    5. The variable should not be preceded by any of the above mentioned keywords.

  1. In implementing two classes Employee and Manager, such that each Manager is an Employee, what should be the relationship between these classes. Select the one correct answer.
    1. Employee should be the base class of Manager class.
    2. Manager should be the base class of Employee class.
    3. Manager class should include the Employee class as a data member.
    4. Employee class should include Manager class as a data member.
    5. The Manager and Employee should not have any relationship.

  1. Select the one most appropriate answer. What is the purpose of method parseInt defined in Integer class.
    1. The method converts an integer to a String.
    2. The method is used to convert String to an integer, assuming that the String represents an integer.
    3. The method is used to convert String to Integer class, assuming that the String represents an integer.
    4. The method converts the Integer object to a String.

  1. What should be done to invoke the run() method on a thread for an object derived from the Thread class. Select the one correct answer.
    1. The run() method should be directly invoked on the Object.
    2. The start() method should be directly invoked on the Object.
    3. The init() method should be directly invoked on the Object.
    4. The creation of the object using the new operator would create a new thread and invoke its run() method.

  1. What is the default priority of a newly created thread.
    1. MIN_PRIORITY (which is defined as 1 in the Thread class.)
    2. NORM_PRIORITY (which is defined as 5 in the Thread class.)
    3. MAX_PRIORITY (which is defined as 10 in the Thread class.)
    4. A thread inherits the priority of its parent thread.

The remaining questions are from AWT and related topics, and are not relevant for SCJP 1.4 .

  1. Which of following correctly describes the functionality of the method drawRect(int a, int b, int c, int d) defined in jawa.awt.Graphics class. Select the one correct option.
    1. Draws the outline of a rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the x,y co-ordinates of the bottom right corner.
    2. Draws the outline of a rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the width and height of the rectangle.
    3. Draws a filled rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the x,y co-ordinates of the bottom right corner.
    4. Draws a filled rectangle with a, b being the x,y co-ordinates of top left corner, and c,d being the width and height of the rectangle.

  1. Which Listener interface must be implemented by a class responsible for handling mouse clicks on buttons?

  1. The getSource method defined in the EventObject class returns the source of an event. What is the return type of this getSource method?
    1. EventObject
    2. Event
    3. Object
    4. Component
    5. Button

  1. The focusLost method is defined in FocusListener interface and is executed when a control loses focus. What is the argument of focusLost method?

  1. Which of the following is the super class of these classes - ContainterEvent, FocusEvent, InputEvent, PaintEvent, WindowEvent. Select the one correct answer.
    1. ActionEvent
    2. AdjustmentEvent
    3. ComponentEvent
    4. ItemEvent
    5. TextEvent
    6. Event

  1. Which of these are adapter classes. Select the three correct answers.
    1. ComponentAdapter
    2. ItemAdapter
    3. ActionAdapter
    4. KeyAdapter
    5. ContainerAdapter

  1. Which of the following statements about layout managers is true. Select the one correct answer.
    1. FlowLayout places components left-aligned in a row (by default) and when there is no space in a row, another row is started.
    2. FlowLayout provides a constructor Flowlayout(int align, int x, int y), where x and y are the coordinates of the first component being added.
    3. The FlowLayout is the default layout manager for Window class
    4. Default horizontal and vertical gaps of components placed using FlowLayout is 5 pixels.

  1. Which of the following is true about BorderLayout. Select the two correct answers.
    1. The default layout manager for Applet class is BorderLayout.
    2. BorderLayout places components in North, South, East and West first and then the remaining space is occupied by the Center component.
    3. When a component is added in BorderLayout using the add method, it is placed in the center by default.
    4. The BorderLayout always honors the size of components provided by the program.

  1. Which of these is true about the GridBagLayout. Select the one correct answer.
    1. The weightx and weighty fields of GridBagConstraints specify how many column and rows each component occupies.
    2. The gridwidth and gridheight constraints of GridBagConstraints specify the width and height in pixels of each cell.
    3. GridBagLayout is the default layout manager of the Frame class.
    4. The gridx and gridy parameters of GridBagConstraints define the column and row position of the upper left corner of the component.

Answers to Sample Test 2

  1. c, d, f
  2. c. The main method must be static and return void. Hence a and b are incorrect. It must take an array of String as argument. Hence e and f are incorrect. As Java is case sensitive, d is incorrect.
  3. a, d
  4. List
  5. e. The collection interface Map has two implementation HashMap and Hashtable.
  6. a. Local variables are not initialized by default. They must be initialized before they are used.
  7. d. The variable i gets initialized to zero. The while loop does not get executed.
  8. c. During various iterations of three loops, the only time i, j and k have same values are when all of them are set to 2.
  9. 0x17 or 0X17.
  10. e. The statement "i=2" evaluates to 2. The expression within the if block must evaluate to a boolean.
  11. e. The range of short primitive type is -32768 to 32767.
  12. c,d. Java does not allow casts between boolean values and any numeric types. Hence a is incorrect. Assigning double to a float requires an explicit cast. Hence b and e are incorrect.
  13. a
  14. e
  15. b. Both Strings test and test2 contain "abcd" . They are however located at different memory addresses. Hence test == test2 returns false, and test.equals(test2) returns true.
  16. d. The for loop does not get executed even once as the condition (i < j =" j">
  17. 5050. The recursive function xyz essentially sums up numbers 1 to num. This evaluates to (num * (num + 1))/2.
  18. a. Java supports assignment of one array to another. Hence b is incorrect. Array elements are indexed from 0. Hence c is incorrect. A method that accesses array elements out of its range does not generate a compilation error. Hence d is incorrect.
  19. d. array.length gives the number of elements in the array. As indexes in Java start from 0, d is the correct answer.
  20. c. In the invocation of call_array, the first element is invoked using call-by-value, and the second using call-by-reference.
  21. d. import statement, package statement and class definitions are all optional in a file. Hence a and c are incorrect. If both import and package statements are present in a file, then package statement must appear before the import statement. Hence b is incorrect.
  22. e. The args array consists of two elements "it" and "out". args.length is set to two.
  23. a,c,d. The exception ArrayIndexOutOfBoundsException is generated as the main method tries to access i[2]. Hence 1 gets printed. After this finally block gets excuted, before the program exits.
  24. d,e,f
  25. e. A data member that does not have public/protected/private is accessible to all methods in the same package.
  26. a. The Manager and Employee share as "is a" relationship - A Manager is an Employee. This is captured by making Employee the base class of Manager.
  27. b. The method int parseInt(Sting s) returns the integer value corresponding to input String, assuming that the input string represents an integer in base 10.
  28. b. The start() method invokes the run() method when the thread is ready to execute.
  29. d
  30. b. drawRect method draws the outline of a rectangle. The last two arguments are width and height of the rectangle.
  31. ActionListener
  32. c. The getSource method returns a reference to the object where the event initially occurred.
  33. FocusEvent. A class implementing FocusListener interface must implement the following method -
    public void focusLost(FocusEvent)
    public void focusGained(FocusEvent)
  34. c
  35. a,d,e. There are no adapter classes corresponding to the following interfaces - ActionListener, ItemListener, AdjustmentListener, and TextListener.
  36. d. The default alignment for FlowLayout is CENTER. Hence a is incorrect. The default Layout Manager for Window class is BorderLayout. Hence c is incorrect. X and y in option b indicate horizontal and vertical gaps between components.
  37. b,c. The default Layout Manager for Applet class is FlowLayout. Hence a is incorrect. BorderLayout grows all components to fill the space available. Hence d is incorrect.
  38. d. The weightx and weighty specify how the size of a cell should change when the container exceeds the preferred size of component. Hence a is not correct. gridwidth and gridheight specify how many columns and rows the component specifies. So b is incorrect. BorderLayout is the default layout manager for Frame class.

0 Comments:

Post a Comment

<< Home