Free Sun Certified Java Programmer MCQ Questions and Answers — Questions and Answers
Question 1: Which of the following initialization strategies for the array "dayhigh" with seven values is correct?
- int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 }; (Correct answer)
- int dayhigh [] = new int[24, 23, 24, 25, 25, 23, 21];
- int dayhigh = { 24, 23, 24, 25, 25, 23, 21 };
- int[] dayhigh = { 24, 23, 24, 25, 25, 23, 21 }; (Correct answer)
Correct answer: int dayhigh[] = { 24, 23, 24, 25, 25, 23, 21 };
In Java, an array can be initialized directly at the time of declaration using an initializer list enclosed in curly braces. The syntax `int dayhigh[]` correctly declares an integer array named `dayhigh`. The `{ 24, 23, 24, 25, 25, 23, 21 }` part then initializes this array with the specified seven integer values, with the compiler inferring the array's size from the list.
Question 2: Which of the following claims regarding garbage collection in Java is accurate?
- The finalize method is always called before an object is garbage collected.
- Any class that includes a finalize method should invoke its superclass' finalize method.
- The garbage collector can be invoked explicitly using a Runtime object.
- All of the above (Correct answer)
Correct answer: All of the above
All the statements regarding Java's garbage collection are accurate. The `finalize()` method is called before an object is garbage collected, though its execution time is not guaranteed. It is also best practice for `finalize()` methods to call their superclass's `finalize()` for proper resource cleanup. Lastly, the garbage collector can be explicitly suggested to run using `Runtime.getRuntime().gc()` or `System.gc()`, although the JVM retains control over its actual execution.
Question 3: Which procedures are necessary to implement the Runnable interface.
- stop()
- wait()
- update()
- run() (Correct answer)
Correct answer: run()
The `Runnable` interface in Java is designed to encapsulate a task that can be executed by a thread. It is a functional interface, meaning it declares a single abstract method. This method is `run()`, which contains the actual code that a thread will execute when it starts.
Question 4: What causes a thread's execution to halt?
- The thread's wait() method was invoked.
- The thread invoked its yield() method.
- A thread with higher priority began execution.
- All of the above (Correct answer)
Correct answer: All of the above
A thread's execution can halt or pause due to several factors. Invoking `wait()` causes the thread to release its lock and enter a waiting state until notified. The `yield()` method suggests to the scheduler that the current thread is willing to give up its CPU time, allowing other threads to run. Additionally, a higher-priority thread becoming runnable can preempt a lower-priority thread, temporarily halting its execution.
Question 5: Which of the following methods can modify a String object, s?
- concat( s )
- equals( s )
- substring( s )
- none of the above will change s (Correct answer)
Correct answer: none of the above will change s
String objects in Java are immutable, meaning their content cannot be changed after they are created. Methods like `concat()`, `equals()`, and `substring()` do not modify the original String object `s`. Instead, `concat()` and `substring()` return new String objects with the modified content, while `equals()` returns a boolean value indicating equality, leaving the original `s` unchanged.
Question 6: Which subclass(es) of Component in the following list cannot be instantiated directly:
- Frame
- Panel
- Dialog
- Container (Correct answer)
Correct answer: Container
`Container` is an abstract class in the AWT hierarchy. Abstract classes cannot be instantiated directly; they must be subclassed, and then an instance of the concrete subclass can be created. `Frame`, `Panel`, and `Dialog` are all concrete subclasses of `Container` and can therefore be instantiated directly.
Question 7: If a variable-width font is used to create the string text for a column, the column's starting size is:
- determined by the number of characters in the string, multiplied by the average width of a character in this font (Correct answer)
- undetermined
- determined by the number of characters in the string, multiplied by the width of a character in this font
- exclusively determined by the number of characters in the string
Correct answer: determined by the number of characters in the string, multiplied by the average width of a character in this font
For variable-width fonts, each character can have a different width. Therefore, simply multiplying by a single character width would not accurately determine the column's size. Instead, the starting size is estimated by considering the total number of characters in the string and multiplying it by the average width of a character in that specific font, providing a more realistic approximation.
Which of the following initialization strategies for the array "dayhigh" with seven values is correct?