



























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























































































Question 1. Which of the following statements about the strictfp keyword is correct? A) It forces all arithmetic to use double precision B) It disables floating-point overflow checks C) It ensures that floating-point calculations conform to IEEE 754 D) It makes the method final Answer: C Explanation: strictfp restricts floating-point operations to the IEEE 754 standard, guaranteeing consistent results across platforms. Question 2. What is the default value of an instance variable of type int in a Java class? A) null B) 0 C) "" (empty string) D) -1 Answer: B Explanation: Instance variables are automatically initialized to default values; for numeric primitives the default is zero. Question 3. Which of the following is a legal identifier for a Java class? A) 2Student B) student$ C) class D) int
Answer: B Explanation: Identifiers may contain letters, digits, $, and _ but cannot start with a digit and cannot be a reserved keyword. Question 4. Which of these literals is a valid char literal? A) 'AB' B) '\u0041' C) '\\' D) '\u00G1' Answer: B Explanation: Unicode escape sequences like '\u0041' represent a single character; option C is also a valid char ('\\'), but the question asks for a valid literal and B is the most illustrative example. Question 5. Which primitive type has the smallest range of values? A) byte B) short C) int D) long Answer: A Explanation: byte occupies 8 bits and ranges from –128 to 127, smaller than the other numeric primitives. Question 6. Which statement about array initialization is true? A) int[] a = new int[5]{1,2,3,4,5}; is legal B) int a[] = {1,2,3}; is legal
Question 9. Which of the following is true about an interface in Java 1.2? A) Interface methods may be declared protected. B) Interface variables are implicitly public static final. C) An interface can contain instance fields. D) An interface can extend multiple classes. Answer: B Explanation: All variables declared in an interface are constants (public static final) by definition. Question 10. Which statement about the switch statement in Java 1.2 is correct? A) The selector expression can be a String. B) case labels may be of type long. C) case labels must be compile-time constant integral values. D) switch can be used with enum types. Answer: C Explanation: In Java 1.2, switch accepts only byte, short, char, or int (or their wrappers) and each case must be a constant expression. Question 11. Which of the following loops will always execute its body at least once? A) for loop B) while loop C) do-while loop D) None of the above Answer: C
Explanation: The do-while loop checks its condition after executing the body, guaranteeing one execution. Question 12. What is the effect of a break statement with a label? A) It terminates the innermost loop only. B) It terminates the outermost loop only. C) It terminates the loop or switch block identified by the label. D) It causes a compile-time error. Answer: C Explanation: A labeled break exits the block (loop or switch) whose label is specified. Question 13. Which exception is a checked exception? A) NullPointerException B) ArrayIndexOutOfBoundsException C) IOException D) ArithmeticException Answer: C Explanation: IOException is a subclass of Exception (but not RuntimeException), so the compiler forces it to be caught or declared. Question 14. Which of the following statements about the finally block is correct? A) It executes only if an exception is thrown. B) It executes even if the try block completes normally. C) It cannot contain a return statement.
Question 17. Which statement about casting an object reference is true? A) A cast never throws ClassCastException. B) The cast is checked at compile time only. C) An explicit cast may succeed at runtime even if the compile-time type is unrelated. D) A cast can be used to downcast from a superclass to a subclass. Answer: D Explanation: Downcasting requires an explicit cast and may succeed at runtime if the actual object is an instance of the subclass; otherwise a ClassCastException occurs. Question 18. When a subclass constructor is executed, which of the following occurs first? A) Instance variable initializers of the subclass run. B) The superclass constructor (super()) is invoked. C) Static initializers of the subclass run. D) The subclass constructor body runs. Answer: B Explanation: The first statement in any constructor must be a call to super() (or this()), which executes the superclass constructor before subclass initializations. Question 19. Which of the following statements about static initializers is correct? A) They run each time an object of the class is created. B) They run when the class is first loaded. C) They can access instance variables directly.
D) They must be declared public. Answer: B Explanation: A static initializer block executes once when the class is loaded by the JVM. Question 20. Which of the following is a correct way to declare an anonymous inner class that implements Runnable? A) new Runnable() { public void run() {} }; B) Runnable r = new Runnable { public void run() {} }; C) Runnable r = new Runnable() { void run() {} }; D) Runnable r = new Runnable() { public void run() {} }(); Answer: A Explanation: The syntax new InterfaceName() { ... } creates an anonymous class; the method must be public void run(). Question 21. Which of the following statements about a method-local inner class is true? A) It can be declared static. B) It can access only final (or effectively final) local variables of the enclosing method. C) It can be instantiated outside the method where it is defined. D) It cannot access members of the enclosing class. Answer: B Explanation: Prior to Java 8, a method-local inner class could access only final local variables; after Java 8, they must be effectively final.
D) appendString(char c) Answer: B Explanation: StringBuffer.append(char) adds the character to the end of the mutable sequence. Question 25. Which collection class implements a dynamically resizing array? A) LinkedList B) Vector C) HashSet D) TreeMap Answer: B Explanation: Vector (and later ArrayList) grows its internal array as elements are added. Question 26. Which of the following List implementations guarantees insertion order and allows random access? A) LinkedList B) Vector C) HashSet D) TreeSet Answer: B Explanation: Vector maintains the order in which elements are added and provides constant-time positional access via get(int). Question 27. Which Set implementation orders its elements according to their natural ordering or a supplied Comparator?
A) HashSet B) LinkedHashSet C) TreeSet D) EnumSet Answer: C Explanation: TreeSet is a sorted set that orders elements based on their compareTo method or a provided comparator. Question 28. Which method of the Iterator interface removes the last element returned by next()? A) delete() B) remove() C) clear() D) pop() Answer: B Explanation: Iterator.remove() removes the element returned by the most recent call to next(). Question 29. Which of the following statements about HashMap is true? A) It allows duplicate keys. B) It maintains insertion order. C) It permits one null key and multiple null values. D) It is synchronized by default. Answer: C Explanation: HashMap allows exactly one null key and any number of null values; it does not preserve order and is not thread-safe.
Answer: B Explanation: Marking a field transient excludes it from the default serialization process. Question 33. Which method of ObjectOutputStream writes an object to a stream? A) write(Object obj) B) writeObject(Object obj) C) serialize(Object obj) D) output(Object obj) Answer: B Explanation: ObjectOutputStream.writeObject(Object) serializes the supplied object. Question 34. Which of the following File methods tests whether a pathname denotes an existing directory? A) isFile() B) isDirectory() C) exists() D) isHidden() Answer: B Explanation: File.isDirectory() returns true if the file denotes a directory. Question 35. Which of the following statements about a Thread object is true? A) Calling run() directly starts a new thread. B) The start() method creates a new thread of execution.
C) A thread can be started more than once. D) The sleep() method must be called on the thread object itself. Answer: B Explanation: Thread.start() causes the JVM to invoke the run() method in a separate call stack; calling run() directly executes it in the current thread. Question 36. Which thread state represents a thread that has completed execution? A) RUNNABLE B) BLOCKED C) TERMINATED D) WAITING Answer: C Explanation: After run() finishes, the thread enters the TERMINATED state. Question 37. Which method causes the currently executing thread to pause for at least the specified number of milliseconds? A) yield() B) wait() C) sleep(long millis) D) join() Answer: C Explanation: Thread.sleep(long) suspends the current thread for the given duration, subject to scheduler granularity.
D) All of the above Answer: D Explanation: Container, Frame, and Button all extend Component directly or indirectly; Container extends Component, and Frame and Button extend Container. Question 41. Which layout manager arranges components in five regions: NORTH, SOUTH, EAST, WEST, and CENTER? A) FlowLayout B) BorderLayout C) GridLayout D) CardLayout Answer: B Explanation: BorderLayout defines the five named regions. Question 42. Which listener interface must be implemented to receive action events from a Button? A) MouseListener B) KeyListener C) ActionListener D) WindowListener Answer: C Explanation: Button generates ActionEvents; an ActionListener handles them via actionPerformed. Question 43. Which adapter class provides empty implementations of the WindowListener methods?
A) WindowAdapter B) ComponentAdapter C) EventAdapter D) ListenerAdapter Answer: A Explanation: WindowAdapter implements WindowListener with no-op methods, allowing subclasses to override only needed callbacks. Question 44. Which of the following lifecycle methods is called when an applet becomes visible on the screen? A) init() B) start() C) stop() D) destroy() Answer: B Explanation: After init(), the browser calls start() each time the applet's page is displayed. Question 45. Which of the following statements about the final keyword when applied to a class is true? A) The class can be subclassed but its methods cannot be overridden. B) The class cannot be subclassed. C) All methods of the class become static. D) The class must have a private constructor. Answer: B Explanation: Declaring a class final prevents any other class from extending it.
Answer: B Explanation: volatile ensures visibility of changes across threads by preventing caching of the variable in registers or thread-local storage. Question 49. Which of the following java.io classes is a character stream for reading text? A) FileInputStream B) BufferedInputStream C) FileReader D) DataInputStream Answer: C Explanation: FileReader reads characters using the default charset, whereas the others are byte streams. Question 50. Which of the following statements about DataOutputStream is correct? A) It writes primitive data types in a portable binary format. B) It writes objects directly without serialization. C) It can only write String objects. D) It automatically compresses the data. Answer: A Explanation: DataOutputStream provides methods like writeInt, writeDouble, etc., which encode primitives in a platform-independent binary representation. Question 51. Which of the following statements about the Object.clone() method is false?
A) It performs a shallow copy of the object. B) The class must implement Cloneable to avoid CloneNotSupportedException. C) It creates a deep copy of all referenced objects automatically. D) It returns a reference of type Object. Answer: C Explanation: clone() only copies field values; referenced objects are not recursively cloned unless the class overrides the method. Question 52. Which of the following statements about the java.util.Collections class is true? A) It provides methods to sort a Map. B) It contains static utility methods for collection objects. C) It can be instantiated to create a collection. D) It implements the List interface. Answer: B Explanation: Collections is a utility class with static methods such as sort, binarySearch, unmodifiableList, etc. Question 53. Which of these Map implementations guarantees that the order of iteration is the order in which entries were inserted? A) HashMap B) TreeMap C) LinkedHashMap D) WeakHashMap Answer: C