SCJP Exam - Java Certification Mock Exam
SCJP Exam - Java Certification Mock Exam
SCJP Exam - Java Certification Mock Exam
John Hunt
Email: [email protected]
URL: //www.jttc.demon.co.uk/cert.htm
As with any examination technique is an important aspect of the examination process. In most
situations there are mock examinations that you can use to obtain experience. This is the intention
of this Java Certification Mock Exam.
This mock exam is solely intended to help those intending to take Sun’s Certified Java
Programmer 1.1. examination practice the style of questions used in the examination. It should
not be used as a way of learning Java nor should it be treated as a form of revision.
Note some care has been taken to ensure that the style of questions are the same as those in the
actual exam, but that no questions from the examination are actually included. The style is based
on one persons experience of taking the Java Certification examination and it should therefore not
be assumed that this mock exam is an exact reflection of the actual exam.
This mock examination consists of 65 questions (the actual exam consists of 60 questions).
Just as in the actual examination a mixture of style of questions are included. These range
between questions that require a single answer and those that require 1 or more answers. The
former are indicated by:
Note a question that has the above request may require only one option, it is for you to decide.
However, if you do not identify all the options for that question, then you will score zero for that
question. One free format question is included as an example of that style of questioning.
As in the actual exam you should not attempt to identify any trends relating to As, Bs or Cs. That
is, do not assume that because the answer A has not appeared for a while there is a good chance
that it will soon.
For the actual examination the pass mark is 70% you should therefore aim to achieve at least 46
correct answers in this mock examination.
If you do believe that you have found an error please do email me, I am just as capable of making
a typo or a cut and paste error as the next person!
1
Q. 1
Which colour is used to indicate instance methods in the standard “javadoc” format
documentation:
A. blue
B. red
C. purple
D. orange
Select the most appropriate answer.
Q. 2
What is the correct ordering for the import, class and package declarations when found in a single
file?
A. package, import, class
B. class, import, package
C. import, package, class
D. package, class, import
Select the most appropriate answer.
Q. 3
Which methods can be legally applied to a string object?
A. equals(String)
B. equals(Object)
C. trim()
D. round()
E. toString()
Select all correct answers.
Q. 4
What is the parameter specification for the public static void main method?
A. String args []
B. String [] args
C. Strings args []
D. String args
Select all correct answers.
Q. 5
What does the zeroth element of the string array passed to the public static void main method
contain?
A. The name of the program
B. The number of arguments
C. The first argument if one is present
Select the most appropriate answer.
Q. 6
Which of the following are Java keywords?
A. goto
B. malloc
C. extends
2
D. FALSE
Select all correct answers
Q. 7
What will be the result of compiling the following code:
Q. 8
Which of these is the correct format to use to create the literal char value a?
A. ‘a’
B. “a”
C. new Character(a)
D. \000a
Select the most appropriate answer.
Q. 9
What is the legal range of a byte integral type?
A. 0 - 65, 535
B. (–128) – 127
C. (–32,768) – 32,767
D. (–256) – 255
Select the most appropriate answer.
Q. 10
Which of the following is illegal:
A. int i = 32;
B. float f = 45.0;
C. double d = 45.0;
Select the most appropriate answer.
Q. 11
What will be the result of compiling the following code:
3
System.out.println(“The age is “ + age);
}
}
Q. 12
Which of the following are correct?
A. 128 >> 1 gives 64
B. 128 >>> 1 gives 64
C. 128 >> 1 gives –64
D. 128 >>> 1 gives –64
Select all correct answers
Q. 13
Which of the following return true?
A. "john" == "john"
B. "john".equals("john")
C. "john" = "john"
D. "john".equals(new Button("john"))
Select all correct answers.
Q. 14
Which of the following do not lead to a runtime error?
A. "john" + " was " + " here"
B. "john" + 3
C. 3 + 5
D. 5 + 5.5
Select all correct answers.
Q. 15
Which of the following are so called "short circuit" logical operators?
A. &
B. ||
C. &&
D. |
Select all correct answers.
Q. 16
Which of the following are acceptable?
A. Object o = new Button("A");
B. Boolean flag = true;
C. Panel p = new Frame();
D. Frame f = new Panel();
E. Panel p = new Applet();
Select all correct answers.
4
Q. 17
What is the result of compiling and running the following code:
Q 18
Which of the following is correct:
A. String temp [] = new String {"j" "a" "z"};
B. String temp [] = { "j " " b" "c"};
C. String temp = {"a", "b", "c"};
D. String temp [] = {"a", "b", "c"};
Select the most appropriate answer.
Q. 19
What is the correct declaration of an abstract method that is intended to be public:
A. public abstract void add();
B. public abstract void add() {}
C. public abstract add();
D. public virtual add();
Select the most appropriate answer.
Q. 20
Under what situations do you obtain a default constructor?
A. When you define any class
B. When the class has no other constructors
C. When you define at least one constructor
Select the most appropriate answer.
Q. 21
5
Given the following code:
Which of the following can be used to define a constructor for this class:
A. public void Test() {…}
B. public Test() {…}
C. public static Test() {…}
D. public static void Test() {…}
Select the most appropriate answer.
Q. 22
Which of the following are acceptable to the Java compiler:
A. if (2 == 3) System.out.println("Hi");
B. if (2 = 3) System.out.println("Hi");
C. if (true) System.out.println("Hi");
D. if (2 != 3) System.out.println("Hi");
E. if (aString.equals("hello")) System.out.println("Hi");
Select all correct answers.
Q. 23
Assuming a method contains code which may raise an Exception (but not a RuntimeException),
what is the correct way for a method to indicate that it expects the caller to handle that exception:
A. throw Exception
B. throws Exception
C. new Exception
D. Don't need to specify anything
Select the most appropriate answer.
Q. 24
What is the result of executing the following code, using the parameters 4 and 0:
Q.25
Which of the following is a legal return type of a method overloading the following method:
6
public void add(int a) {…}
A. void
B. int
C. Can be anything
Select the most appropriate answer.
Q.26
Which of the following statements is correct for a method which is overriding the following
method:
Q. 27
Given the following classes defined in separate files:
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}
What will be the effect of compiling and running this class Test?
A. Generates a Compiler error on the statement v= c;
B. Generates runtime error on the statement v= c;
C. Prints out:
7
Vehicle: drive
Car: drive
Car: drive
D. Prints out:
Vehicle: drive
Car: drive
Vehicle: drive
Select the most appropriate answer.
Q. 28
Where in a constructor, can you place a call to a constructor defined in the super class?
A. Anywhere
B. The first statement in the constructor
C. The last statement in the constructor
D. You can't call super in a constructor
Select the most appropriate answer.
Q. 29
Which variables can an inner class access from the class which encapsulates it?
A. All static variables
B. All final variables
C. All instance variables
D. Only final instance variables
E. Only final static variables
Select all correct answers.
Q. 30
What class must an inner class extend:
A. The top level class
B. The Object class
C. Any class or interface
D. It must extend an interface
Select the most appropriate answer.
Q. 31
In the following code, which is the earliest statement, where the object originally held in e, may
be garbage collected:
A. Line 10
8
B. Line 11
C. Line 7
D. Line 8
E. Never
Select the most appropriate answer.
Q. 32
What is the name of the interface that can be used to define a class that can execute within its own
thread?
A. Runnable
B. Run
C. Threadable
D. Thread
E. Executable
Select the most appropriate answer.
Q. 33
What is the name of the method used to schedule a thread for execution?
A. init();
B. start();
C. run();
D. resume();
E. sleep();
Select the most appropriate answer.
Q. 34
Which methods may cause a thread to stop executing?
A. sleep();
B. stop();
C. yield();
D. wait();
E. notify();
F. notifyAll()
G. synchronized()
Select all correct answers.
Q. 35
Write code to create a text field able to display 10 characters (assuming a fixed size font)
displaying the initial string "hello":
:
Q. 36
Which of the following methods are defined on the Graphics class:
A. drawLine(int, int, int, int)
B. drawImage(Image, int, int, ImageObserver)
C. drawString(String, int, int)
D. add(Component);
E. setVisible(boolean);
F. setLayout(Object);
Select all correct answers.
9
Q. 37
Which of the following layout managers honours the preferred size of a component:
A. CardLayout
B. FlowLayout
C. BorderLayout
D. GridLayout
Select all correct answers.
Q. 38
Given the following code what is the effect of a being 5:
Q. 39
What is the effect of issuing a wait() method on an object
A. If a notify() method has already been sent to that object then it has no effect
B. The object issuing the call to wait() will halt until another object sends a notify() or
notifyAll() method
C. An exception will be raised
D. The object issuing the call to wait() will be automatically synchronized with any other objects
using the receiving object.
Select the most appropriate answer.
Q. 40
The layout of a container can be altered using which of the following methods:
A. setLayout(aLayoutManager);
B. addLayout(aLayoutManager);
C. layout(aLayoutManager);
D. setLayoutManager(aLayoutManager);
Select all correct answers.
Q. 41
Using a FlowLayout manager, which is the correct way to add elements to a container:
A. add(component);
10
B. add("Center", component);
C. add(x, y, component);
D. set(component);
Select the most appropriate answer.
Q. 42
Given that a Button can generate an ActionEvent which listener would you expect to have to
implement, in a class which would handle this event?
A. FocusListener
B. ComponentListener
C. WindowListener
D. ActionListener
E. ItemListener
Select the most appropriate answer.
Q. 43
Which of the following, are valid return types, for listener methods:
A. boolean
B. the type of event handled
C. void
D. Component
Select the most appropriate answer.
Q. 44
Assuming we have a class which implements the ActionListener interface, which method should
be used to register this with a Button?
A. addListener(*);
B. addActionListener(*);
C. addButtonListener(*);
D. setListener(*);
Select the most appropriate answer.
Q. 45
In order to cause the paint(Graphics) method to execute, which of the following is the most
appropriate method to call:
A. paint()
B. repaint()
C. paint(Graphics)
D. update(Graphics)
E. None – you should never cause paint(Graphics) to execute
Select the most appropriate answer.
Q. 46
Which of the following illustrates the correct way to pass a parameter into an applet:
A. <applet code=Test.class age=33 width=100 height=100>
B. <param name=age value=33>
C. <applet code=Test.class name=age value=33 width=100 height=100>
D. <applet Test 33>
Select the most appropriate answer.
Q. 47
11
Which of the following correctly illustrate how an InputStreamReader can be created:
A. new InputStreamReader(new FileInputStream("data"));
B. new InputStreamReader(new FileReader("data"));
C. new InputStreamReader(new BufferedReader("data"));
D. new InputStreamReader("data");
E. new InputStreamReader(System.in);
Select all correct answers.
Q. 48
What is the permanent effect on the file system of writing data to a new FileWriter("report"),
given the file report already exists?
A. The data is appended to the file
B. The file is replaced with a new file
C. An exception is raised as the file already exists
D. The data is written to random locations within the file
Select the most appropriate answer.
Q. 49
What is the effect of adding the sixth element to a vector created in the following manner:
Q. 50
What is the result of executing the following code when the value of x is 2:
switch (x) {
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
Q. 51
Consider the following example:
class First {
12
public First (String s) {
System.out.println(s);
}
}
public class Second extends First {
public static void main(String args []) {
new Second();
}
}
Q. 53
Consider the following classes:
13
A. The string Test is printed to the standard out.
B. A runtime exception is raised stating that an object has not been created.
C. Nothing is printed to the standard output.
D. An exception is raised stating that the method test cannot be found.
E. An exception is raised stating that the variable this can only be used within an instance.
F. The class fails to compile stating that the variable this is undefined.
Select all correct answers.
Q. 54
Examine the following class definition:
Q. 55
What is the result of compiling and executing the following Java class:
14
F. Nothing will happen.
Select the most appropriate answer.
Q. 56
Given the following sequence of Java statements
Q. 57
What is the result of executing the following Java class:
import java.awt.*;
A. Nothing happens.
B. Three buttons are displayed across a window.
C. A runtime exception is generated (no layout manager specified).
D. Only the “first” button is displayed.
E. Only the “second” button is displayed.
15
F. Only the “third” button is displayed.
Q. 58
1. CODEBASE
2. ALT
3. NAME
4. CLASS
5. JAVAC
6. HORIZONTALSPACE
7. VERTICALSPACE
8. WIDTH
9. PARAM
10. JAR
Which of the above can be used within the <APPLET> and </APPLET> tags?
A. line 1, 2, 3
B. line 2, 5, 6, 7
C. line 3, 4, 5
D. line 8, 9, 10
E. line 8, 9
Q. 59
Which of the following is a legal way to construct a RandomAccessFile:
A. RandomAccessFile(“data”, “r”);
B. RandomAccessFile(“r”, “data”);
C. RandomAccessFile(“data”, “read”);
D. RandomAccessFile(“read”, “data”);
Q. 60
Carefully examine the following code:
16
st1.print();
StaticTest st2 = new StaticTest();
st2.print();
}
}
Q. 61
Consider the following program:
Q. 62
Examine the following code, it includes an inner class:
17
}
}
Q. 63
Carefully examine the following class:
Q. 64
The following code defines a simple applet:
import java.applet.Applet;
import java.awt.*;
18
}
<html>
<title>Sample Applet</title>
<body>
<applet code="Sample.class" width=200 height=200></applet>
</body>
</html>
Q. 65
Examine the following code:
19
Answers to Java Certification Mock Exam
1. B 2. A 3. A, B, C, E 4. A, B 5. C
6. A, C 7. D 8. A 9. B 10. B
11. B 12. A,B 13. A, B 14. A, B, C, D 15.B, C
16. A, E 17. D 18. D 19. A 20. B
21. B 22. A, C, D, E 23. B 24. A 25. C
26. A 27. C 28. B 29. A, B, C 30. C
31. C 32. A 33. B 34. A, B, C, D 35. new
TextField("hello", 10)
36. A, B, C 37. B 38. D 39. B 40. A
41. A 42. D 43. C 44. B 45. B
46. B 47. A, E 48. B 49. C 50. C
51. F 52. A 53. F 54. C 55. D
56. D, F 57. F 58. A, E 59. A 60. C
61. C 62. A 63. F 64. B 65. C
Further Reading
Some useful references for the Java certification exam provided by Sun Microsystems are:
In addition there are two books available which are aimed specifically at helping you to pass the
Certification exam. These are:
Java 1.1 Certification Study Guide, Simon Roberts and Philip Heller, Sybex, ISBN 0-7821-2069-
5, 1997. On-line information can be found at http://www.sybex.com/cgi-
bin/bookpg.pl?2069back.html.
Java certification for programmers and developers, Barry Boone, McGraw-Hill, 0-0791-3657-5,
1997. On-line information can be found at http://mcgraw-
hill.inforonics.com/cgi/getarec?mgh31641.
John Hunt
9th July 1999
20