Sun Certified Java Programmer(SCJP 1.4) JavaBeat Home
SCJP 1.4 Home
Objectives
Forums
Mock Exams
Online Mock Exam
Resources
Mock Exams MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions
-
1 5 9 13 17
MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions
-
2 6 10 14 18
MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions
-
3 7 11 15 19
MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions
Question 1. Read this piece of code carefully if("String".toString() == "String") System.out.println("Equal"); else System.out.println("Not Equal");
Answers
1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error.
Question 2. Read this piece of code carefully
if(" String ".trim() == "String") System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 3. Read the code below. Will be the result of attempting to compile and run the code below.
-
4 8 12 16 20
public class AQuestion { public void method(Object o) { System.out.println("Object Verion"); } public void method(String s) { System.out.println("String Version"); } public static void main(String args[]) { AQuestion question = new AQuestion(); question.method(null); } } Answers 1. 2. 3. 4.
The The The The
code code code code
does not compile. compiles cleanly and shows "Object Version". compiles cleanly and shows "String Version" throws an Exception at Runtime.
Question 4. Read the code below. Will be the result of attempting to compile and run the code below.
public class AQuestion { public void method(StringBuffer sb) { System.out.println("StringBuffer Verion"); } public void method(String s) { System.out.println("String Version"); } public static void main(String args[]) { AQuestion question = new AQuestion(); question.method(null); } } Answers 1. The code does not compile. 2. The code compiles cleanly and shows "StringBuffer Version". 3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime.
Question 5. Read the following code below. public interface AQuestion { public abstract void someMethod() throws Exception; } A Class implementing this interface should
1. Necessarily be an abstract class. 2. Should have the method public abstract void someMethod(); 3. Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception. 4. Should have the method public void someMethod() which need not throw an Exception.
Question 6. An Interface can never be private or protected. Answers True False
Question 7. A Vector class in jdk 1.2 1. 2. 3. 4. 5.
is public is final implements java.util.List is serializable has only One constructor
Question 8. A String Class 1. 2. 3. 4.
is final is public is serializable has a constructor which takes a StingBuffer Object as an Argument
Question 9.
public interface AQuestion { void someMethod(); } The class which implements AQuestion 1. 2. 3. 4.
Should have Should have Should have Should have defination
someMethod someMethod someMethod someMethod
which which which which
must necessarily be public. could be "friendly" or public should not throw any checked exceptions. cannot be sychronized as sychronized is not in the signature of the interface
Question 10.
public class AQuestion { private int i = j; private int j = 10; public static void main(String args[]) { System.out.println((new AQuestion()).i); } }
Answers 1. 2. 3. 4.
Compiler error complaining about access restriction of private variables of AQuestion. Compiler error complaining about forward referencing. No error - The output is 0; No error - The output is 10;
Question 11. public class AQuestion { private int i = giveMeJ(); private int j = 10; private int giveMeJ() { return j; } public static void main(String args[]) { System.out.println((new AQuestion()).i);
} } Answers
1. 2. 3. 4.
Compiler error complaining about access restriction of private variables of AQuestion. Compiler error complaining about forward referencing. No Compilation error - The output is 0; No Compilation error - The output is 10;
Question 12. public class AQuestion { public static void main(String args[]) { System.out.println("Before Try"); try { } catch(Throwable t) { System.out.println("Inside Catch"); } System.out.println("At the End"); } }
1. Compiler error complaining about the catch block, where no Throwable object can ever be thrown. 2. Compiler error - Throwable Object can not be caught, only Exceptions must be caught. 3. No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
Question 13.
public class AQuestion { public static void main(String args[]) { System.out.println("Before Try"); try { } catch(java.io.IOException t) { System.out.println("Inside Catch"); } System.out.println("At the End"); } }
1. Compiler error complaining about the catch block where no IOException object can ever be thrown. 2. Compiler error - IOException not found. It must be imported in the first line of the code. 3. No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
Question 14. The class java.lang.Exception i. ii. iii. iv.
Is public Extends Throwable Implements Throwable Is serializable
Question 15. Read this piece of code carefully if("String".trim() == "String".trim()) System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 16. Read this piece of code carefully
if( "STRING".toUpperCase() == "STRING") System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 17.
The following lines of code byte b = 0; b += 1;
1. results in b having the value 1. 2. causes a compiler error. 3. will require a cast (byte) before 1.
Question 18. The following express char c = -1;
1. 2. 3. 4.
will cause a compiler error as the range of character is between 0 and 2^16 - 1. Will request for an explicit cast. will not cause a compiler error and c will have the value -1; c will not represent any ascii character. c will still be a unicode character.
Question 19. Which of the following statements are true?
1. A method can throw an Exception 2. A method can return an Exception
Question 20. All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character)
1. 2. 3. 4. 5.
are public are serializable are immutatable extend java.lang.Number are final
Question 21. Read this piece of code carefully
if("String".substring(0) == "String") System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 22. Read this piece of code carefully if("String".substring(0,6) == "String") System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 23. Read this piece of code carefully if("String".replace('t','t') == "String") System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 24. An Anonymous Inner class
1. 2. 3. 4.
Does not have a constructor Can implement an interface Can extend a non-final Class Can implement an interface and extend a non-final class (at the same time).
Question 25.
public class A { private void method1() throws Exception { throw new RuntimeException(); } public void method2() { try { method1(); } catch(RuntimeException e) { System.out.println("Caught Runtime Exception"); } catch(Exception e) { System.out.println("Caught Exception"); } } public static void main(String args[]) { A a = new A(); a.method2(); } } The above lines of code -
1. 2. 3. 4.
will will will will
not compile. compile and show - "Caught Runtime Exception". compile and show - "Caught Exception". compile and show both the messages one after another in the order they appear.
Question 26. public XXXX extends something1, something2
1. 2. 3. 4.
XXX should be an interface,something1 and something2 need not, for the expression to be legal XXX should be a class, something1 and something2 must be interfaces for the expression to be legal. XXX, something1 and something2 must be interfaces for the expression to be legal. The above statement is alway illegal in Java as multiple inheritance is not supported.
Question 27. public class ADirtyOne { char a = '\u000A'; } An attempt to compile the above class 1. 2. 3. 4.
will will will will
complete successfully. not compile as 0x000A is out of range for unicode charaters. complain about illegal character assignment compile but will cause a runtime error in accessing the variable.
Question 28. public class ADirtyOne { //char a = '\u000A'; } An attempt to compile the above class 1. 2. 3.
will complete successfully. will compile sucessfully but with a warning message. will not compile - complains on an invalid expression.
Question 29.
public class AnotherDirtyOne { private final int i =10; private byte k = i; }
An attempt to compile and run the above code will 1. Cause a compilation error due to invalid assignment ( int to byte) and will request for an explicit cast to be done on i [ k=(byte) i ]. 2. Compilation occurs with a warning message - suggesting that the accuracy of k is questionable 3. Compilation will occur cleanly without any warning message. 4. Runtime error occurs when accessing k.
Question 30. interface One { public void someMethod(); }
public class One_impl implements One { public native void someMethod(); }
Assuming that the native method is not provided in any local library, an attempt to compile and run the above lines of code will cause
1. 2. 3. 4.
Compilation error - implimentations can never be native. Compilation error - method not found in local libraries. Runtime Exception - method not found in local libraries. Compilation successfull but runtime error is thrown if and only if the method someMethod of class One_impl is called.
Question 31. Read this piece of code carefully if("String".replace('T','t') == "String") System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 32. Read this piece of code carefully System.out.println("String".substring(0,4));
Answers 1. the code will print "Strin" on the screen. 2. the code will print "Stri" on the screen. 3. the code will cause a compiler error.
Question 33. Read this piece of code carefully if("String".replace('g','G') == "String".replace('g','G')) System.out.println("Equal"); else System.out.println("Not Equal");
Answers 1. the code will compile an print "Equal". 2. the code will compile an print "Not Equal". 3. the code will cause a compiler error
Question 34.
public class ADirtyOne { public static void main(String args[]) { System.out.println(Math.abs(Integer.MIN_VALUE)); } }
an attempt to compile and run the above class will 1. 2. 3. 4.
Cause a compiler error. Cause no error and the value printed on the screen is less than zero. Cause no error and the value printed on the screen is one more than Integer.MAX_VALUE Will throw a runtime exception due to overflow - Integer.MAX_VALUE is less in magnitue than Integer.MIN_VALUE.
Question 35. public class ADirtyOne { public static void main(String args[]) { System.out.println(Math.min(0.0,-0.0)); } }
An attempt to compile and run the above class will
1. Cause a compiler Error. 2. Cause no error and print the value 0.0 on the screen. 3. Cause no error and prints the value -0.0 on the screen.
Question 36.
public class Base { public void aMethod() throws ClassNotFoundException { } }
public class Derived extends Base { public void aMethod() throws RuntimeException { } } Assuming that the classes are in two seperate files, compilation of the Dervied.java causes
1. A compiler error because RuntimeException is not a subclass if ClassNotFoundException. 2. No compiler error.
Question 37. Math.round(Float.MAX_VALUE);
1. 2. 3. 4.
Returns Integer.MAX_VALUE. Returns a closest integer to Float.MAX_VALUE; Causes a compilation error. Causes a runtime Exception
Question 38. Read the code below carefully
import java.awt.*; public class TestFrame extends Frame { Button Button Button Button Button
bNorth = new Button("North"); bSouth = new Button("South"); bEast = new Button("East"); bWest = new Button("West"); bCenter = new Button("Center");
public TestFrame() { setLayout(new BorderLayout()); add(bSouth,BorderLayout.SOUTH); add(bWest,BorderLayout.WEST); add(bEast,BorderLayout.EAST); add(bNorth,BorderLayout.NORTH); add(bCenter); setLayout(new FlowLayout()); validate(); pack(); setVisible(true); }
public static void main(String args[]) { TestFrame tf = new TestFrame(); } }
What will be the effect trying compile and run the above class?
1. Compilation error - a Layout cannot be set twice for a component. 2. Compilation error - One button is added without specifing the position in the borderLayout 3. No Compilation Error. The Buttons are arranged in a line in the order (From left to right) "North","South","West","East" and "Center". 4. No Compilation Error. The Buttons are arranged in a line in the order (From left to right) "South","West","East","North" and "Center". 5. No Compilation Error. The Buttons are arranged in the north , south, west, east and center regions, as in a borderlayout. Any further additions will follow the rules of FlowLayout manager.
Question 39.
import java.awt.*; public class TestFrame extends Frame { Button Button Button Button Button
bNorth = new Button("North"); bSouth = new Button("South"); bEast = new Button("East"); bWest = new Button("West"); bCenter = new Button("Center");
public TestFrame() { setLayout(new FlowLayout()); add(bNorth); add(bSouth); add(bWest); add(bEast); add(bCenter); setLayout(new BorderLayout()); validate(); setSize(300,300); setVisible(true); } public static void main(String args[]) { TestFrame tf = new TestFrame(); } }
Attemping to compile and run the above code
1. Will cause a compilation error - a Layout cannot be set after a component has been added with a preset Layout Manager. 2. Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset Layout Manager. 3. Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and occupies the whole screen. 4. Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager. 5. Will compile and run cleanly, but no component is visible. 6. Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
Button Label Center North South East West
Position Center North South East West
Question 40. A frame uses BorderLayout Management and has components added to all the regions. One resizing the Frame Some space becomes available. The space is alloted to the regions, in which Order of preference?
1. 2. 3. 4.
North , South, West, East and then Center. North , West, South, Center and then Center. Center, East, West, South and then North. West, Center, South, North and then East.
Question 41. Read the following piece of code carefully.
import java.awt.*; public class TestFrame extends Frame { Button firstOne = new Button("One"); Button secondOne = new Button("Two"); public TestFrame() { add(firstOne,BorderLayout.NORTH); add(secondOne,BorderLayout.NORTH); setSize(400,400); setVisible(true); } public static void main(String args[]) { TestFrame tf = new TestFrame(); }
}
An Attempt to compile and run the above piece of code
1. Causes compilation error - a component cannot be added to region which is already occupied by another component. 2. Causes Runtime Exception - a component cannot be added to region which is already occupied by another component. 3. Neither i or ii. The Frame comes up and only the button with label "Two" occupies the entire North region of the Frame. 4. Addition of secondOne causes firstOne to be removed from the container. 5. Addition of the secondOne causes the firstOne to be hidden in the container.
Question 42.
import java.awt.*; public class TestFrame extends Frame { public TestFrame() { Button one = new Button("One"); Button two = new Button("Two"); Button three = new Button("Three"); setLayout(new FlowLayout()); add(one); add(two); add(three); two.setVisible(false); setSize(1000,1000); setVisible(true); validate(); } public static void main(String args[]) { TestFrame tf = new TestFrame(); } } 1. If the above code runs, the buttons - one and three are laid out in a single row from left to right with a gap in between . 2. If the above code runs, the buttons - one and three are laid out in a single row from left to right with no gap in between. 3. Code does not compile - a component can not be hidden after being added to a container. 4. Code gets compiled successfully but throws runtime Exception - a component can not be hidden after being added to a container.
Question 43. Read the code below carefully.
import java.awt.*; public class TestFrame extends Frame { public TestFrame() { setLayout(new GridLayout(2,1)); for(int i = 1 ; i <= 4 ;++i) { add(new Button(Integer.toString(i))); } pack(); setVisible(true); } public static void main(String args[]) { TestFrame tf = new TestFrame(); } } 1. The code above will not compile - The number of components added is more than the magnitude of row * columns of the Grid Layout Manager. 2. The code will throw a runtime Exception - The number of components added is more than the magnitude of row * columns of the Grid Layout Manager. 3. The code will compile cleanly and when run Four buttons are visible in 2 rows and 2 columns. 4. The code will compile and when run, Four buttons are seen in a single Column.
Question 44. Read the code below carefully.
import java.awt.*; public class TestFrame extends Frame { public TestFrame() { setLayout(new GridLayout()); for(int i = 1 ; i <= 4 ;++i) { add(new Button(Integer.toString(i))); } pack(); setVisible(true); } public static void main(String args[]) { TestFrame tf = new TestFrame(); } }
1. 2. 3. 4.
The code The code The code The code visible. 5. The code
will not compile - the grid layout does not have a no-argument constructor.. compiles and when run all the buttons are seen in a single column. compiles and when run all the buttons are seen in a singe row. compiles and when run all button are added one on top or another and only the last one added is compiles , but throws a runtime Exception when components are added.
Question 45. Which of the following statements are true about setLayout() method in java.awt.ScrollPane
1. 2. 3. 4.
Does nothing. Throws UnsupportedMethodException when called. It is not overriden in java.awt.ScrollPane. Sets the layout to the specified Layout Manager.
Question 46. A class which has all its constructors declared as private 1. 2. 3. 4.
Cannot be instantiated by any other class. Cannot be extended. Both i and ii. has to be declared final.
Question 47. The GridBagConstraints Class 1. 2. 3. 4.
Is serializable. Is cloneable. belongs to the java.awt package. extends Object.
Question 48. Read the following code carefully
import java.awt.*; public class TestFrame extends Frame { public TestFrame() { CheckboxGroup chg = null; Checkbox ch = new Checkbox("Test",true,chg); setLayout(new FlowLayout()); add(ch); pack(); setVisible(true);
} public static void main(String args[]) { TestFrame tf = new TestFrame(); } }
An Attempt to compile and run the above code 1. will cause a compilation error as the checkbox group is null in the constructor. 2. will compile successfully but throws a runtime exception because the checkbox group is null in the constructor of the check box 3. will compile and run successfully. The checkbox appears as a single radio button which is always true. 4. will compile and run successfully. The checkbox bears its original appearence and does not appear as a radio button.
Question 49. Read the following code carefully
import java.awt.*; public class TestFrame extends Frame { public TestFrame() { CheckboxGroup chg = new CheckboxGroup(); Checkbox ch = new Checkbox("Test",true,chg); setLayout(new FlowLayout()); add(ch); pack(); setVisible(true); } public static void main(String args[]) { TestFrame tf = new TestFrame(); } }
An Attempt to compile and run the above code 1. will cause a 2. will compile checkbox. 3. will compile 4. will compile button.
compilation error as the checkbox group contains only one checkbox. successfully but throws a runtime exception because the checkbox group contains only one and run successfully. The checkbox appears as a single radio button which is always true. and run successfull. The checkbox bears its original appearence and does not appear as a radio
Question 50. Which of the following methods of the java.io.File class throws a checked Exceptions -
1. 2. 3. 4. 5. 6. 7. 8. 9.
getCanonicalPath() getCanonicalFile() getAbsolutePath() getAbsoluteFile() createTempFile() createNewFile() mkdir() mkdirs() toURL()
Answers: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49.
1 2 3 1 4 FALSE 1,3,4 1,2,3,4 1,3 2 3 3 1 1,2,4 1 1 1 1 1,2 1,2,3,5 1 1 1 1,2,3 2 3 3 3 3 4 1 2 2 2 3 2 1 4 5 1 3,4 2 3 3 1 3 1,2,3,4 4 3
50. 1,2,5,6,9
JavaBeat 2005, India (www.javabeat.net) Submit a Site - Directory - Submit Articles