Java Mock Tests For SCJP
Java Mock Tests For SCJP
4)
JavaBeat Home SCJP 1.4 Home Objectives Forums Mock Exams Online Mock Exam Resources
Mock Exams
MockQuestions - 1 MockQuestions - 2 MockQuestions - 3 MockQuestions - 4
MockQuestions - 5 MockQuestions - 6 MockQuestions - 7 MockQuestions - 8
MockQuestions - 9 MockQuestions - 10 MockQuestions - 11 MockQuestions - 12
MockQuestions - 13 MockQuestions - 14 MockQuestions - 15 MockQuestions - 16
MockQuestions - 17 MockQuestions - 18 MockQuestions - 19 MockQuestions - 20
Question 1.
if("String".toString() == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 2.
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 3.
Read the code below. Will be the result of attempting to compile and run the code below.
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
Answers
Question 4.
Read the code below. Will be the result of attempting to compile and run the code below.
Answers
Question 5.
Question 6.
Answers
True
False
Question 7.
1. is public
2. is final
3. implements java.util.List
4. is serializable
5. has only One constructor
Question 8.
A String Class
1. is final
2. is public
3. is serializable
4. has a constructor which takes a StingBuffer Object as an Argument
Question 9.
Question 10.
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
Answers
Question 11.
Answers
Question 12.
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.
Question 14.
i. Is public
ii. Extends Throwable
iii. Implements Throwable
iv. Is serializable
Question 15.
if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 16.
Answers
Question 17.
The following lines of code
byte b = 0;
b += 1;
Question 18.
char c = -1;
1. will cause a compiler error as the range of character is between 0 and 2^16 - 1. Will request for an explicit cast.
2. will not cause a compiler error and c will have the value -1;
3. c will not represent any ascii character.
4. c will still be a unicode character.
Question 19.
Question 20.
All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character)
1. are public
2. are serializable
3. are immutatable
4. extend java.lang.Number
5. are final
Question 21.
Answers
Question 22.
if("String".substring(0,6) == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 23.
if("String".replace('t','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 24.
public class A
{
Question 26.
1. XXX should be an interface,something1 and something2 need not, for the expression to be legal
2. XXX should be a class, something1 and something2 must be interfaces for the expression to be legal.
3. XXX, something1 and something2 must be interfaces for the expression to be legal.
4. The above statement is alway illegal in Java as multiple inheritance is not supported.
Question 27.
Question 28.
Question 29.
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
Question 31.
if("String".replace('T','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 32.
System.out.println("String".substring(0,4));
Answers
Question 33.
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
Question 34.
Question 35.
Question 36.
Assuming that the classes are in two seperate files, compilation of the Dervied.java causes
Question 37.
Math.round(Float.MAX_VALUE);
1. Returns Integer.MAX_VALUE.
2. Returns a closest integer to Float.MAX_VALUE;
3. Causes a compilation error.
4. Causes a runtime Exception
Question 38.
import java.awt.*;
public class TestFrame extends Frame
{
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?
Question 39.
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
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?
Question 41.
import java.awt.*;
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();
}
}
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.*;
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.
pack();
setVisible(true);
}
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.
import java.awt.*;
pack();
setVisible(true);
}
Question 45.
Which of the following statements are true about setLayout() method in java.awt.ScrollPane
1. Does nothing.
2. Throws UnsupportedMethodException when called.
3. It is not overriden in java.awt.ScrollPane.
4. Sets the layout to the specified Layout Manager.
Question 46.
Question 47.
1. Is serializable.
2. Is cloneable.
3. belongs to the java.awt package.
4. extends Object.
Question 48.
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();
}
}
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.
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();
}
}
1. will cause a compilation error as the checkbox group contains only one checkbox.
2. will compile successfully but throws a runtime exception because the checkbox group contains only one
checkbox.
3. will compile and run successfully. The checkbox appears as a single radio button which is always true.
4. will compile and run successfull. The checkbox bears its original appearence and does not appear as a radio
button.
Question 50.
Which of the following methods of the java.io.File class throws a checked Exceptions -
1. getCanonicalPath()
2. getCanonicalFile()
3. getAbsolutePath()
4. getAbsoluteFile()
5. createTempFile()
6. createNewFile()
7. mkdir()
8. mkdirs()
9. toURL()
Answers:
1. 1
2. 2
3. 3
4. 1
5. 4
6. FALSE
7. 1,3,4
8. 1,2,3,4
9. 1,3
10. 2
11. 3
12. 3
13. 1
14. 1,2,4
15. 1
16. 1
17. 1
18. 1
19. 1,2
20. 1,2,3,5
21. 1
22. 1
23. 1
24. 1,2,3
25. 2
26. 3
27. 3
28. 3
29. 3
30. 4
31. 1
32. 2
33. 2
34. 2
35. 3
36. 2
37. 1
38. 4
39. 5
40. 1
41. 3,4
42. 2
43. 3
44. 3
45. 1
46. 3
47. 1,2,3,4
48. 4
49. 3
50. 1,2,5,6,9