












































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This practice exam is designed to help candidates prepare for the Sun Certified Programmer for the Java 2 Platform 1.2 exam. It covers topics such as core Java programming, object-oriented design principles, exception handling, multithreading, and Java APIs. The exam tests the candidate's ability to design, write, and debug Java applications for the Java 2 platform.
Typology: Exams
1 / 84
This page cannot be seen from the preview
Don't miss anything!













































































Question 1. Which of the following is a valid Java identifier? A) 2ndValue B) value_ C) value- 2 D) value 2 Answer: B Explanation: Identifiers can include letters, digits, underscores, and dollar signs, but cannot begin with a digit or include spaces or hyphens. Question 2. Which of the following is NOT a Java keyword? A) transient B) interface C) implements D) include Answer: D Explanation: "include" is not a Java keyword; the others are reserved Java keywords. Question 3. What is the value of the following char literal: '\u0041'? A) 'A' B) 'B' C) 41 D) '\u0041' Answer: A Explanation: '\u0041' is the Unicode escape for the character 'A'.
Question 4. What is the effect of the strictfp keyword in Java? A) Ensures consistent floating-point results across platforms B) Makes a method final C) Restricts access to a method D) Forces integer precision Answer: A Explanation: strictfp enforces strict IEEE 754 compliance for floating-point computations. Question 5. Which primitive data type has the largest range? A) int B) long C) float D) double Answer: D Explanation: double has the largest range among primitives, as it uses 64 bits for floating-point values. Question 6. What is the default value of an instance boolean variable? A) true B) false C) null D) 0 Answer: B Explanation: The default value for a boolean instance variable is false.
Question 10. Which access modifier prevents a class from being subclassed? A) abstract B) protected C) private D) final Answer: D Explanation: The final modifier prevents a class from being subclassed. Question 11. Which of these can be declared as abstract? A) Methods only B) Classes only C) Both classes and methods D) Variables Answer: C Explanation: Both classes and methods can be declared abstract. Question 12. What is true about variables declared in an interface? A) They are public, static, and final by default B) They are private by default C) They must be initialized in the constructor D) They are protected by default Answer: A Explanation: Interface variables are implicitly public, static, and final.
Question 13. What does the following code print? int i = 0; if (i == 0) { System.out.print("A"); } else { System.out.print("B"); } A) A B) B C) AB D) Compilation error Answer: A Explanation: Since i is 0, the condition is true and "A" is printed. Question 14. Which types are legal in a switch statement in Java 1.2? A) int, byte, short, char B) long C) float D) double Answer: A Explanation: Only int, byte, short, and char are legal in switch statements in Java 1.2. Question 15. What is the output of the following loop? int i = 0; while (i < 3) { System.out.print(i);
Explanation: Throwable is the superclass; Exception and Error extend Throwable; RuntimeException extends Exception. Question 18. Which is a checked exception? A) NullPointerException B) IOException C) ArithmeticException D) ArrayIndexOutOfBoundsException Answer: B Explanation: IOException is checked; must be caught or declared. Question 19. What will happen if a finally block is present after try-catch? A) It always executes B) Executes only if exception is thrown C) Executes only if no exception D) May not execute Answer: A Explanation: finally always executes after try-catch, regardless of exceptions. Question 20. Which method signature is valid for an overridden method? A) public void run() throws IOException B) private void run() throws Exception C) protected void run() throws IOException D) public void run() throws Throwable
Answer: C Explanation: Overridden methods cannot throw broader or new checked exceptions. Question 21. Which of the following enables assertions at runtime? A) - ea B) - da C) - enableassertions D) - assert Answer: A Explanation: - ea enables assertions at runtime. Question 22. What is an appropriate use of assertions? A) Checking method parameters from the public API B) Validating internal invariants C) Input validation D) Authentication Answer: B Explanation: Assertions should be used for validating internal invariants, not for public input. Question 23. What is true about an Is-A relationship? A) Achieved using inheritance B) Achieved using composition C) Achieved using interfaces only D) Not possible in Java
Answer: B Explanation: The actual object type determines which overridden method is called at runtime. Question 27. Which cast is necessary and safe? A) Superclass to subclass when object is actually subclass B) Subclass to superclass C) int to double D) double to int Answer: A Explanation: Downcasting is safe only if the object is truly an instance of the subclass. Question 28. What is constructor overloading? A) Multiple constructors with different parameter lists B) Constructors with same parameter list C) Multiple constructors with same name D) Constructors with different return types Answer: A Explanation: Constructor overloading refers to multiple constructors with different parameter lists. Question 29. When does the compiler provide a default constructor? A) When no constructor is defined by the programmer B) When at least one constructor is defined C) Always D) Never
Answer: A Explanation: The compiler provides a default constructor only if the class defines no constructors. Question 30. Which statement about super() in constructors is true? A) It must be the first statement in a constructor B) It can be called from anywhere in the constructor C) It is optional D) It is used to call subclass constructor Answer: A Explanation: super() must be the first statement in a constructor if used. Question 31. What is the order of execution when creating a new object? A) Static initializers, instance initializers, constructor B) Constructor, static initializers, instance initializers C) Instance initializers, static initializers, constructor D) Static initializers, constructor, instance initializers Answer: A Explanation: Static initializers run first, then instance initializers, then the constructor. Question 32. Which is a regular (member) inner class? A) A class declared within another class B) A class declared in a method C) A static class D) An anonymous class
Answer: C Explanation: Anonymous inner classes cannot have constructors. Question 36. What is true about String immutability? A) String objects cannot be changed after creation B) String objects can be changed after creation C) String is a mutable class D) String is an interface Answer: A Explanation: String objects are immutable; modifications create new objects. Question 37. Which method compares two strings for equality, ignoring case? A) equalsIgnoreCase() B) compareTo() C) equals() D) match() Answer: A Explanation: equalsIgnoreCase() compares two strings, ignoring case. Question 38. What does the substring method of String return? A) A new String B) The same String C) An int D) A char array
Answer: A Explanation: substring returns a new String object. Question 39. Which class is used for mutable sequences of characters in Java 1.2? A) StringBuffer B) String C) StringBuilder D) MutableString Answer: A Explanation: StringBuffer is used for mutable character sequences (StringBuilder introduced in Java 5). Question 40. Which collection interface allows duplicate elements? A) Set B) Map C) List D) Collection Answer: C Explanation: List allows duplicate elements. Question 41. Which implementation maintains insertion order? A) HashSet B) TreeSet C) ArrayList D) HashMap
Answer: B Explanation: Iterator is the primary interface for traversing collections. Question 45. Which interface provides the remove() method to delete elements while iterating? A) Iterator B) Collection C) Set D) Map Answer: A Explanation: Iterator provides remove() to delete elements during iteration. Question 46. Which collection does NOT guarantee any order? A) HashSet B) LinkedList C) TreeSet D) ArrayList Answer: A Explanation: HashSet does not guarantee any order of elements. Question 47. What is the primary purpose of wrapper classes? A) To wrap primitive values as objects B) To provide utility methods C) For inheritance D) To store strings
Answer: A Explanation: Wrapper classes wrap primitive types as objects. Question 48. How do you convert a String to an int using a wrapper class? A) Integer.parseInt("123") B) String.parseInt("123") C) parseInt("123") D) Integer.toInt("123") Answer: A Explanation: Integer.parseInt("123") converts a String to int. Question 49. What is true about wrapper class objects? A) They are immutable B) They are mutable C) They are interfaces D) They must be subclassed Answer: A Explanation: Wrapper class objects are immutable. Question 50. Which method starts a thread? A) start() B) run() C) execute() D) begin()
Answer: A Explanation: sleep() pauses the current thread for the specified number of milliseconds. Question 54. What is the purpose of the yield() method? A) Suggests the current thread to pause and let others run B) Terminates the thread C) Suspends the thread D) Restarts the thread Answer: A Explanation: yield() suggests to the thread scheduler to let other threads of equal priority run. Question 55. Which keyword is used for synchronization? A) synchronized B) concurrent C) lock D) threadsafe Answer: A Explanation: The synchronized modifier is used for synchronization in Java. Question 56. What is a race condition? A) When multiple threads access shared data and results depend on timing B) When a thread races to finish C) When threads have same priority D) When only one thread runs
Answer: A Explanation: A race condition occurs when multiple threads manipulate shared data concurrently. Question 57. What does entering a synchronized method ensure? A) The thread owns the object's monitor B) The method runs faster C) The method can be called by any thread D) The thread is paused Answer: A Explanation: Entering a synchronized method ensures the thread owns the object's monitor (lock). Question 58. Which method must be called from within a synchronized context? A) wait() B) notify() C) notifyAll() D) All of the above Answer: D Explanation: wait(), notify(), and notifyAll() must be called from a block synchronized on the object. Question 59. Which of the following is a byte stream class? A) FileInputStream B) FileReader C) BufferedWriter D) PrintWriter