Original created by
Sreenivasa Kumar Majji (all credit to him)
(has long as it lasts the orginal can be found @ http://www.oocities.org/SiliconValley/Cable/1025/exam1.html)
What will happen if you compile/run this code? 1: public class Q1 extends Thread 2: { 3: public void run() 4: { 5: System.out.println("Before start method"); 6: this.stop(); 7: System.out.println("After stop method"); 8: } 9: 10: public static void main(String[] args) 11: { 12: Q1 a = new Q1(); 13: a.start(); 14: } 15: } A) Compilation error at line 7. B) Runtime exception at line 7. C) Prints "Before start method" and "After stop method". D) Prints "Before start method" only.
Answer
Question 2
What will happen if you compile/run the following code? 1: class Test 2: { 3: static void show() 4: { 5: System.out.println("Show method in Test class"); 6: } 7:} 8: 9: public class Q2 extends Test 10: { 11: static void show() 12: { 13: System.out.println("Show method in Q2 class"); 14: } 15: public static void main(String[] args) 16: { 17: Test t = new Test(); 18: t.show(); 19: Q2 q = new Q2(); 20: q.show(); 21: 22: t = q; 23: t.show(); 24: 25: q = t; 26: q.show(); 27: } 28: } A) prints "Show method in Test class" "Show method in Q2 class" "Show method in Q2 class" "Show method in Q2 class" B) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Test class" C) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Q2 class" D) Compilation error.
Answer
The following code will give
1: class Test 2: { 3: void show() 4: { 5: System.out.println("non-static method in Test"); 6: } 7: } 8: public class Q3 extends Test 9: { 10: static void show() 11: { 12: System.out.println("Overridden non-static method in Q3"); 13: } 14: 15: public static void main(String[] args) 16: { 17: Q3 a = new Q3(); 18: } 19: } A) Compilation error at line 3. B) Compilation error at line 10. C) No compilation error, but runtime exception at line 3. D) No compilation error, but runtime exception at line 10. Answer
The following code will give
1: class Test 2: { 3: static void show() 4: { 5: System.out.println("Static method in Test"); 6: } 7: } 8: public class Q4 extends Test 9: { 10: void show() 11: { 12: System.out.println("Overridden static method in Q4"); 13: } 14: public static void main(String[] args) 15: { 16: } 17: } A) Compilation error at line 3. B) Compilation error at line 10. C) No compilation error, but runtime exception at line 3. D) No compilation error, but runtime exception at line 10. Answer
The following code will print 1: int i = 1; 2: i <<= 31; 3: i >>= 31; 4: i >>= 1; 5: 6: int j = 1; 7: j <<= 31; 8: j >>= 31; 9: 10: System.out.println("i = " +i ); 11: System.out.println("j = " +j); A) i = 1 j = 1 B) i = -1 j = 1 C) i = 1 j = -1 D) i = -1 j = -1 Answer
The following code will print 1: Double a = new Double(Double.NaN); 2: Double b = new Double(Double.NaN); 3: 4: if( Double.NaN == Double.NaN ) 5: System.out.println("True"); 6: else 7: System.out.println("False"); 8: 9: if( a.equals(b) ) 10: System.out.println("True"); 11: else 12: System.out.println("False"); A) True True B) True False C) False True D) False False Answer
1: if( new Boolean("true") == new Boolean("true")) 2: System.out.println("True"); 3: else 4: System.out.println("False"); A) Compilation error. B) No compilation error, but runtime exception. C) Prints "True". D) Prints "False". Answer
1: public class Q8 2: { 3: int i = 20; 4: static 5: { 6: int i = 10; 7: 8: } 9: public static void main(String[] args) 10: { 11: Q8 a = new Q8(); 12: System.out.println(a.i); 13: } 14: } A) Compilation error, variable "i" declared twice. B) Compilation error, static initializers for initialization purpose only. C) Prints 10. D) Prints 20. Answer
The following code will give 1: Byte b1 = new Byte("127"); 2: 3: if(b1.toString() == b1.toString()) 4: System.out.println("True"); 5: else 6: System.out.println("False"); A) Compilation error, toString() is not avialable for Byte. B) Prints "True". C) Prints "False". Answer
What will happen if you compile/run this code? 1: public class Q10 2: { 3: public static void main(String[] args) 4: { 5: int i = 10; 6: int j = 10; 7: boolean b = false; 8: 9: if( b = i == j) 10: System.out.println("True"); 11: else 12: System.out.println("False"); 13: } 14: } A) Compilation error at line 9 . B) Runtime error exception at line 9. C) Prints "True". D) Prints "Fasle". Answer
What will happen if you compile/run the following code? 1: public class Q11 2: { 3: static String str1 = "main method with String[] args"; 4: static String str2 = "main method with int[] args"; 5: 6: public static void main(String[] args) 7: { 8: System.out.println(str1); 9: } 10: 11: public static void main(int[] args) 12: { 13: System.out.println(str2); 14: } 15: } A) Duplicate method main(), compilation error at line 6. B) Duplicate method main(), compilation error at line 11. C) Prints "main method with main String[] args". D) Prints "main method with main int[] args". Answer
What is the output of the following code? 1: class Test 2: { 3: Test(int i) 4: { 5: System.out.println("Test(" +i +")"); 6: } 7: } 8: 9: public class Q12 10: { 11: static Test t1 = new Test(1); 12: 13: Test t2 = new Test(2); 14: 15: static Test t3 = new Test(3); 16: 17: public static void main(String[] args) 18: { 19: Q12 Q = new Q12(); 20: } 21: } A) Test(1) Test(2) Test(3) B) Test(3) Test(2) Test(1) C) Test(2) Test(1) Test(3) D) Test(1) Test(3) Test(2) Answer
What is the output of the following code? 1: int i = 16; 2: int j = 17; 3: 4: System.out.println("i >> 1 = " + (i >> 1)); 5: System.out.println("j >> 1 = " + (j >> 1)); A) Prints "i >> 1 = 8" "j >> 1 = 8" B) Prints "i >> 1 = 7" "j >> 1 = 7" C) Prints "i >> 1 = 8" "j >> 1 = 9" D) Prints "i >> 1 = 7" "j >> 1 = 8" Answer
What is the output of the following code? 1: int i = 45678; 2: int j = ~i; 3: 4: System.out.println(j); A) Compilation error at line 2. ~ operator applicable to boolean values only. B) Prints 45677. C) Prints -45677. D) Prints -45679. Answer
What will happen when you invoke the following method? 1: void infiniteLoop() 2: { 3: byte b = 1; 4: 5: while ( ++b > 0 ) 6: ; 7: System.out.println("Welcome to Java"); 8: } A) The loop never ends(infiniteLoop). B) Prints "Welcome to Java". C) Compilation error at line 5. ++ operator should not be used for byte type variables. D) Prints nothing. Answer
In the following applet, how many Buttons will be displayed? 1: import java.applet.*; 2: import java.awt.*; 3: 4: public class Q16 extends Applet 5: { 6: Button okButton = new Button("Ok"); 7: 8: public void init() 9: { 10: add(okButton); 11: add(okButton); 12: add(okButton); 13: add(okButton); 14: 15: add(new Button("Cancel")); 16: add(new Button("Cancel")); 17: add(new Button("Cancel")); 18: add(new Button("Cancel")); 19: 20: setSize(300,300); 21: } 22: } A) 1 Button with label "Ok" and 1 Button with label "Cancel" . B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" . C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" . D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" . Answer
In the following, which is correct Container-Default layout combination? A) Applet - FlowLayout B) Applet - BorderLayout C) Applet - CardLayout D) Frame - Flowlayout E) Frame - BorderLayout F) Frame - CardLayout G) Panel - FlowLayout H) Panel - BorderLayout. Answer
What is the output of the following code? 1: String str = "Welcome"; 2: 3: str.concat(" to Java!"); 4: 5: System.out.println(str); A) Strings are immutable, compilation error at line 3. B) Strings are immutable, runtime exception at line 3. C) Prints "Welcome". D) Prints "Welcome to Java!". Answer
What is the output of the following code? 1: class MyClass 2: { 3: static int maxElements; 4: 5: MyClass(int maxElements) 6: { 7: this.maxElements = maxElements; 8: } 9: 10: } 11: 12: public class Q19 13: { 14: public static void main(String[] args) 15: { 16: 17: MyClass a = new MyClass(100); 18: MyClass b = new MyClass(100); 19: 20: if(a.equals(b)) 21: System.out.println("Objects have the same values"); 22: else 23: System.out.println("Objects have different values"); 24: } 25: } A) Compilation error at line 20. equals() method was not defined. B) Compiles fine, runtime exception at line 20. C) Prints "Objects have the same values". D) Prints "Objects have different values"; Answer
1: import java.applet.*; 2: import java.awt.*; 3: 4: public class Q20 extends Applet 5: { 6: Button okButton = new Button("Ok"); 7: 8: public void init() 9: { 10: setLayout(new BorderLayout()); 11: 12: add("South", okButton); 13: add("North", okButton); 14: add("East", okButton); 15: add("West", okButton); 16: add("Center", okButon); 17: 18: setSize(300,300); 19: } 20: } The above Applet will display A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet. B) Only one Button with label "Ok" at the Top of the Applet. C) Only one Button with label "Ok" at the Bottom of the applet. D) Only one Button with label "Ok" at the Center of the Applet. Answer
What will happen if you compile/run the following code? 1: public class Q21 2: { 3: int maxElements; 4: 5: void Q21() 6: { 7: maxElements = 100; 8: System.out.println(maxElements); 9: } 10: 11: Q21(int i) 12: { 13: maxElements = i; 14: System.out.println(maxElements); 15: } 16: 17: public static void main(String[] args) 18: { 19: Q21 a = new Q21(); 20: Q21 b = new Q21(999); 21: } 22: } A) Prints 100 and 999. B) Prints 999 and 100. C) Compilation error at line 3, variable maxElements was not initialized. D) Compillation error at line 19.
Answer
What will happen if run the following code? 1: Boolean[] b1 = new Boolean[10]; 2: 3: boolean[] b2 = new boolean[10]; 4: 6: System.out.println("The value of b1[1] = " +b1[1]); 7: System.out.println("The value of b2[1] = " +b2[1]); A) Prints "The value of b1[1] = false" "The value of b2[1] = false". B) Prints "The value of b1[1] = null" "The value of b2[1] = null". C) Prints "The value of b1[1] = null" "The value of b2[1] = false". D) Prints "The value of b1[1] = false" "The value of b2[1] = null".
Answer
Which of the following are valid array declarations/definitions?
1: int iArray1[10]; 2: int iArray2[]; 3: int iArray3[] = new int[10]; 4: int iArray4[10] = new int[10]; 5: int []iArray5 = new int[10]; 6: int iArray6[] = new int[]; 7: int iArray7[] = null;
A) 1. B) 2. C) 3. D) 4. E) 5. F) 6. G) 7.
Answer
What is the output for the following lines of code?
1: System.out.println(" " +2 + 3); 2: System.out.println(2 + 3); 3: System.out.println(2 + 3 +""); 4: System.out.println(2 + "" +3);
A) Compilation error at line 3 B) Prints 23, 5, 5 and 23. C) Prints 5, 5, 5 and 23. D) Prints 23, 5, 23 and 23.
Answer
The following declaration(as a member variable) is legal.
static final transient int maxElements = 100;
A) True. B) False.
Answer
What will happen if you compile/run the following lines of code?
1: int[] iArray = new int[10]; 2: 3: iArray.length = 15; 4: 5: System.out.println(iArray.length);
A) Prints 10. B) Prints 15. C) Compilation error, you can't change the length of an array. D) Runtime exception at line 3.
Answer
What will happen if you compile/run the folowing lines of code?
1: Vector a = new Vector(); 2: 3: a.addElement(10); 4: 5: System.out.println(a.elementAt(0));
A) Prints 10. B) Prints 11. C) Compilation error at line 3. D) Prints some garbage.
Answer
What will happen if you invoke the following method?
1: public void check() 2: { 3: System.out.println(Math.min(-0.0,+0.0)); 4: System.out.println(Math.max(-0.0,+0.0)); 5: System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0)); 6: } A) prints -0.0, +0.0 and false. B) prints -0.0, +0.0 and true. C) prints 0.0, 0.0 and false. D) prints 0.0, 0.0 and true.
Answer
What will happen if you compile/run this code?
1: int i = 012; 2: int j = 034; 3: int k = 056; 4: int l = 078; 5: 6: System.out.println(i); 7: System.out.println(j); 8: System.out.println(k);
A) Prints 12,34 and 56. B) Prints 24,68 and 112. C) Prints 10, 28 and 46. D) Compilation error.
Answer
When executed the following line of code will print
System.out.println(-1 * Double.NEGATIVE_INFINITY); A) -Infinity B) Infinity C) NaN D) -NaN Answer
D. After the execution of stop() method, thread won't execute any more statements. Back to Question 1
D. Explicit casting is required at line 25. Back to Question 2
B. You cann't override an non-static method with static method. Back to Question 3
B. You cann't override a static method with non-static method. Back to Question 4
D. Back to Question 5
C. Back to Question 6
D. Back to Question 7
D. Here the variable '"i" defined in static initializer is local to that block only. The statements in the static initializers will be executed (only once) when the class is first created. Back to Question 8
C. Back to Question 9
C. Conditional operators have high precedence than assignment operators. Back to Question 10
C. Here the main method was overloaded, so it won't give compilation error.
Back to Question 11
D. No matter where they declared, static variables will be intitialized before non-static variables.
Back to Question 12
A. 16 >> 1 is 8 and 17 >> 1 also 8.
Back to Question 13
D. Java allows you to use ~ operator for integer type variables. The simple way to calculate is ~i = (- i) - 1.
Back to Question 14
B. Here the variable 'b' will go upto 127. After that overflow will occur, so 'b' will be set to -ve value, the loop ends and prints "Welcome to Java"
Back to Question 15
B.
Back to Question 16
A, E and G. For Applets and Panels FlowLayout is the default one, BorderLayout is default for Window and Frames.
Back to Question 17
C. Strings are immutable. So str.concat("to Java!") will not append anything to str. Infact it will create another string "Welcome to Java!" and leaves it.
Back to Question 18
D. equals() method was available in base class Object. So it won't give any compilation error. Here MyClass is a user-defined class, so the user has to implement equals() method according to his requirments.
Back to Question 19
D.
Back to Question 20
D. Constructors should not return any value. Java won't allow to indicate with void. In this case void Q21() is an ordinary method which has the same name of the Class.
Back to Question 21
C. By default objects will be initialized to null and primitives to their corresponding default vaulues. The same rule applies to array of objects and primitves.
Back to Question 22
B,C,E and G. You can't specify the array dimension in type specification(left hand side), so A and D are invalid. In line 6 the array dimension is missing(right hand side) so F is invalid. You can intialize an array with null. so G is valid.
Back to Question 23
B.
Back to Question 24
A.
Back to Question 25
C. Once array is created then it is not possible to change the length of the array.
Back to Question 26
C. You can't add primitives to Vector. Here 10 is int type primitive.
Back to Question 27
B. The order of floating/double values is -Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity.
Back to Question 28
D. Here integers are assinged by octal values. Octal numbers will contain digits from 0 to 7. 8 is illegal digit for an octal value, so you get compilation error.
Back to Question 29
B. Compile and see the result.
Back to Question 30
{loading}{scripting-1}{scripting}{submenus}
![]() |
DISCLAIMER
|
![]() |
|
More is added to the list, but what I realy need is your help to keep this information up-to-date and may be it needs correcting in one way or the other. Please feel free to help me complement these question and answers drop us an email at this address. (C) 1998-2001, BMC88 - Personal WebSite of Java Certification Exams |
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|||
![]() |
![]() |
![]() |
![]() |
|
![]() |
![]() |