Java Certification - Questions and Answers |
Sun Certified Programmer Practice Exam
Here are the rules:
Allow 2 hours and 15 minutes to complete this exam. You should turn off
the telephone, go someplace you won’t be disturbed, check the time on your
watch, and begin. Don’t bring any books with you, because you won’t have
them during the test. You can take breaks, but don’t look anything up. You
can have a piece of scratch paper and a pen or pencil, but you can’t have
any crib sheets!
If you get 49 questions right, you’ve hit the 70% mark, and you’ve
passed. Good luck! Questions Question 1: Which of the following class definitions defines a legal abstract class? Select all right answers. a)
b)
c)
d)
e)
Question 2: For an object to be a target for a Thread, that object must be of type: Fill in the blank.
Question 3: What is the proper way of defining a class named Key so that it cannot be subclassed? a)
b)
c)
d)
e)
Question 4: What modes are legal for creating a new RandomAccessFile object? Select all valid answers.
Question 5: Given the following code:
Question 6: Which assignments are legal? Select all valid answers.
Question 7: Given this class definitions:
Select the one right answer.
Question 8: Which exception might wait() throw? Fill in the blank. Question 9: Which of the following are not Java keywords:
Select all valid answers.
Question 10: Which of the following represents an octal number? Select all that apply.
Question 11: What will appear in the standard output when you run the Tester class?
Question 12: Write a line of code to use the String’s substring() method to obtain the substring "lip" from a String instance named s that is set to "tulip". Fill in the blank. Question 13: There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?
Question 14: What are the range of values for a variable of type byte? Select the one right answer.
Question 15: What will happen when you try to compile and run the following program?
Question 16: What will happen when you compile and run this program:
Question 17: What is the result of attempting to compile and run the following class?
Question 18: What method name can you use from the applet to read a String passed to an applet via the <param> tag? (Supply the method name only, without parameters.) Fill in the blank. Question 19: Given these class definitions:
Select the one right answer.
Question 20: Given these class definitions:
Question 21: How you can use the escape notation \u to set the variable c, declared as a char, to the Unicode character whose value is hex 0x30A0? Fill in the blank.
Question 22: Which operators are overloaded for String objects?
Question 20: Given these class definitions:
Question 21: How you can use the escape notation \u to set the variable c, declared as a char, to the Unicode character whose value is hex 0x30A0? Fill in the blank. Select all valid answers.
Question 23: How can you change the break statement below so that it breaks out of both the inner and middle loops and continues with the next iteration of the outer loop?
Question 24: Given this code snippet:
Select the one right answer.
Question 25: Given these class definitions:
Question 26: Given these class definitions:
Question 27: To invoke read() from an InputStream subclass, you must handle what type of exception?
Select all valid answers.
Question 29: What is the result of executing the following code:
Question 30: Given this code snippet:
Select all valid answers.
Question 31: In Java 1.0.2, to make a Button object non-responsive to mouse clicks, you can invoke which method? (Only supply the method name, without a parameter list.) Fill in the blank.
Select the one right answer.
Question 33: What methods does Java define in the Math class specifically for trigonometric calculations? Select all valid answers.
Question 34: What String instance method would return true when invoked like this:
Select the one right answer.
Question 35: At the end of these two lines of code:
Select the one right answer.
Question 36: What access control keyword should you use to allow other classes to access a method freely within its package, but to restrict classes outside of the package from accessing that method? Select all valid answers.
Question 37: After these two lines of code:
Select the one right answer.
Question 38: What does the following code do?
Question 39: Which expressions are illegal? Select all valid answers.
Question 40: Which label name(s) are illegal? Select all valid answers.
Question 41: Given this code:
Select the one right answer. a)
Question 42: What keyword, when used in front of a method, must also appear in front of the class? Fill in the blank.
Question 44: What happens if the file "Ran.test" does not yet exist and you attempt to compile and run the following code?
Question 45: If you run the following code on a on a PC from the directory c:\source:
Select the one right answer.
Question 46: If you supply a target object when you create a new Thread, as in:
Select the one right answer.
Question 47: What appears in the standard output when you run the Dots class?
Question 48: When you invoke repaint() for a Component, the AWT package calls which Component method?
Question 49: How you can you test whether an object referenced by ref implements an interface named MyInterface? Replace your test here with this test:
Question 51: Given these code snippets:
Select all valid answers.
Question 52: Which LayoutManager arranges components left to right, then top to bottom, centering each row as it moves to the next? Select the one right answer.
Question 53: A component can be resized horizontally, but not vertically, when it is placed in which region of a BorderLayout? Select the one right answer.
Question 54: How can you place three Components along the bottom of a Container? Select the one right answer.
Question 55: What will happen when you attempt to compile and run the following program by passing the Test class to the Java interpreter?
Question 56: Which of the following is a valid way to embed an applet class named Q56 into a Web page? Select all right answers. a)
Fill in the blank. a)
Question 59: What is the result of invoking main() for the classes D and E?
Question 60: What does the following code do?
Question 61: What does the following code do (if anything)?
Question 62: What Graphics methods will draw the outline of a square? Select all right answers.
Question 63: What method from Java 1.0.2 can you use to remove a Component from a user interface display? Select all right answers.
Question 64: Returning a value of false in Java 1.0.2 from an event handler: Select the one right answer.
Question 65: Which statements about garbage collection are true? Select all valid answers.
Question 66: If you’d like to change the size of a Component, you can use the Java 1.1-specific method: Select the one right answer.
Question 67: The setForeground() and setBackground() methods are defined in class: Select the one right answer.
Question 68: How many bits are used to maintain a char data type? Fill in the blank. Select all valid answers.
Question 70: To place a 1 in the high-bit of an int named ref that’s set to 0x00000001, you can write: Select the one right answer.
Question 1: d. An abstract class is defined using the keyword abstract in front of the class keyword and almost always defines at least one abstract method. An abstract class does not have to define an abstract method. If there are no abstract methods in an abstract class, then any subclasses of the abstract class can be instantiated (as long as they are not, in turn, defined using the abstract keyword). (See chapter 1.) Question 2: "Runnable". Only classes that implement the Runnable interface (and so are of type Runnable) can be targets of threads. Question 3: e. Use the final keyword in front of the class to make the class unable to be subclassed. (See chapter 1.) Question 4: b, d. Only "r" and "rw" are legal modes for a RandomAccessFile. Question 5: c. This code is perfectly fine. (See chapter 1.) Question 6: a, b, d, and e. The other tries to cast a boolean to an int, which is illegal. Quesiton 7: c. Since the superclass is abstract and implements Runnable, but does not supply a run() method, the subclass must supply run() or also be declared abstract. (See chapter 1.) Question 8: "InterruptedException" or "IllegalMonitorException". Question 9: a, c, d, e. superclass, open, close, and integer are not Java keywords. goto is a keyword, though it isn’t used as of Java 1.1. (See chapter 2.) Question 10: c. An octal number in Java is preceded by a 0. Question 11: e. Oh, the tangled web we weave… There are three constructors that come into play. First, the constructor that thakes an int is invoked. This invokes the constructor that takes a String. This invokes the no-args constructor, which displays "good-bye." Then, the constructor that takes a String displays "hello." (See chapter 3.) Question 12: "s.substring(2, 5)" or "s.substring(2)" or "s.substring(2, s.length())"; Question 13: a. A new String is created based on args[0], but args[0] does not have to be nulled out before first can be garbage collected. As soon as the line with the label d is executed, the object that first has referred to is ready to be garbage collected, because there is no way to recover a reference to this object again. (See chapter 4.) Question 14: d. The range of integer types goes from minus 2(number of bits - 1) to 2(number of bits - 1) minus 1. (See chapter 5.) Question 15: c. Even though there is an instance variable named index defined in the Car class, the local or automatic variable named index takes precedence. Since automatic variables do not have a default value, this code will not compile because it is uninitialized when we attempt to access the element in the args array. (See chapter 5.) Question 16: e. There’s nothing wrong with this code. 100 0's will appear in the standard output. (See chapter 5.) Question 17: a, b, c. The elements in arrays are initialized to their default values: 0, 0.0, null, false, or \u0000, depending on the data type. Question 18: "getParameter" Question 19: c. Assigning a subclass type to a superclass type is perfectly legal and will run fine at runtime. Question 20: a. You cannot assign an object to a sibling object reference, even with casting. Question 21: "c = ‘\u30A0’;" You can set a char to a Unicode sequence by matching the template \udddd, where dddd are four hexadecimal digits representing the Unicode character you want. (See chapter 5.) Question 22: b. Only + and += are overloaded for String objects. Question 23: b. Changing the break statement to break middle will break out of the loop named using the label middle and continue with the next iteration of the outer loop. The statement continue outer would also have this effect. (See chapter 7.) Question 24: c. NumberFormatException will be handled in the catch clause for Exception. Then, regardless of the return statements, the finally clause will be executed before control returns to the calling method. (See chapter 8.) Question 25: a. An explicit cast is needed to assign a superclass type to a subclass type. Question 26: b. If the object contained in a is not actually a Subclass1 object, the assignment will cause Java to throw a CastClassException. That would be the case in the code in this example. Question 27: "IOException" or "java.io.IOException" Question 28: c. A method in a subclass cannot add new exception types that it might throw. Since it’s superclass, Second, does not define any exceptions in its test() method, Third can’t either. (See chapter 8.) Question 29: c. The method with all double parameters is actually the only version of test() that the Java Virtual Machine can legally coerce the numbers to. The reason the other versions of test() are not invoked is that at least one of the parameters would have to be automatically coerced from a type with greater accuracy to a type with less accuracy, which is illegal. (See chapter 9.) Question 30: c. The Math method floor() finds the integer closest to but less than the parameter to floor(). The methods round() and ceil() would both result in 91, and min() and max() both require two parameters. (See chapter 10.) Question 31: "disable" Question 32: a. Both ceil() and round() will produce 15 from 14.9. The floor() method yields 14. (See chapter 10.) Question 33: a, b, c, d. The methods Java defines for trig operations include sin(), asin(), cos(), and tan(). (See chapter 10.) Question 34: d. The method equalsIgnoreCase() would return true for the two Strings a and b in the question. (See chapter 10.) Question 35: d. The method substring() starts at the first index, inclusive, with 0 being the first character), and ends at the end index - 1 (that is, exclusive of the end index). (See chapter 10.) Question 36: d. This is the default access control for methods and member variables. Question 37: b. The concat() method appends the characters passed to it to the characters in the String responding to the method call. The concat() method creates a new String, since Strings cannot be changed once they are created. (See chapter 10.) Question 38: a. The first line creates a File object that represents the file. By creating a FileOutputStream, you create the file if it does not yet exist, and open that file for reading and writing. (See chapter 11.) Question 39: d, c, e. You cannot assign an integer to a boolean—not even with casting. Also, the default type for a floating-point literal is double, and you cannot assign a double to a float without casting. Question 40: c, e. this is a reserved word, so it cannot be used as an identifier (such as a label). 2to1odds starts with a number, so it is also invalid as an identifier. Question 41: a. In order to write a primitive data type such as an int, you need to use a FilterInputStream subclass such as DataInputStream. This class defines writeInt(), which is perfect for our needs. (See chapter 11.) Question 42: "abstract" Question 43: c. Only the "c" from finally gets written out. The exception thrown doesn’t match the exception caught, so the catch block is not executed. Control returns to the caller after finally to see if there is a catch block there to handle this unchecked exception. If there is not (as is the case here), execution comes to an end. Question 44: e. This code compiles and runs fine. RandomAccessFile implements the DataOutput interface, so it does implement writeBytes(), among others. RandomAccessFile creates the file if it does not yet exist. (See chapter 11.) Question 45: c. The absolute path includes the drive name and the top-level directories, as well as the file name itself. (See chapter 11.) Question 46: d. The target object for a Thread must implement Runnable, which means it will pass the test: targetObject instanceof Runnable (See chapter 12.) Question 47: a. The thread's with start() method is never invoked. (This is not an applet, so init() is not automatically invoked.) (See chapter 12.) Question 48: b. The AWT invokes update() for the Component, which in invokes paint() in its default behavior. (See chapter 13.) Question 49: "ref instanceof MyInterface" Question 50: e. TextField defines a constructor that takes the number of columns, as shown in the example. TextField objects can have their text updated at any time, including long after they're created. (See chapter 13.) Question 51: b. The first yields false, and the others are not legal Java expressions (this is a wrapper type we’re using here…) Question 52: b. A FlowLayout arranges components in this way. (See chapter 13.) Question 53: a. North and South only can resize a component horizontally, to the width of the Container. (See chapter 13.) Question 54: c. Complicated as it might seem, this is the best way to accomplish this goal. First, you set the Container's LayoutManager to be a BorderLayout. Then, you create an intermediate Container and add each Component to this new Container that uses a FlowLayout. Finally, you add that Container to the "South" of the original Container. (See chapter 13.) Question 55: b. The program will compile fine. However, the Java interpreter specifically looks for a main() method declared as public and static, that returns no value, and that takes an array of String objects as its parameter. Question 56: c. The <applet> tag requires three keywords: code, width, and height. Question 57: "p.setBackground(Color.red);" Question 58: a. Use the getParameter() method, passing it the name of the value you want to retrieve. This method retrieves a String representing that value. (You can convert the String to a primitive data type using a wrapper class if you need to.) Question 59: c. The StringBuffer class does not override equals(). Hence, this class returns false when passed two different objects. (See chapter 6.) Question 60: d. The four parameters are the left, top, width, height, start angle (0 is the 3:00 position), and the arc angle (the arc ends at start angle plus the arc angle), drawn counter-clockwise. Question 61: b. The drawLine() method takes four parameters: the starting point and the ending point of the line to draw. Question 62: a, c. You can use drawRect() to draw a rectangle outline given its upper left point and width and height, and drawPolygon() to draw each of the four points of the square, plus an end point that is the same as the first point. Question 63: b. The hide() method is more-or-less the opposite of show() and removes a Component from the display. Question 64: a. Returning false indicates that method did not handle the event, which means AWT passes the event up the container hierarchy looking for someone who does want to handle it. Question 65: b, c, e. You cannot directly free the memory allocated by an object, though you can set an object reference to null. Also, The garbage collector only runs in low-memory situations, and so does not always reclaim an object’s memory as soon as it becomes a candidate for garbage collection. Question 66: d. setSize() is specific to Java 1.1. Question 67: c. These are Component methods. (The setColor() method is defined in the Graphics class.) Question 68: "16" Question 69: d. The && operator combines two boolean expressions. Question 70: d. The << operator shifts the bits the given number of places to the left.
|