














































































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 exam tests the fundamental skills required for Java programming, focusing on Java syntax, object-oriented programming concepts, and core libraries. Ideal for candidates pursuing Java development certifications, it evaluates the knowledge of Java language constructs and the Java API.
Typology: Exams
1 / 86
This page cannot be seen from the preview
Don't miss anything!















































































Question 1. Which of the following is a valid Java identifier? A) 2ndVariable B) myVar C) class D) @name Answer: B Explanation: Identifiers can start with a letter, underscore (), or dollar sign ($), but not a digit or reserved keyword. Question 2. What is the default access modifier for a class declared without any access modifier? A) public B) private C) protected D) package-private (default) Answer: D Explanation: If no access modifier is specified, the class has package-private access, visible only within its package. Question 3. Which non-access modifier is used to prevent a method from being overridden? A) abstract B) static C) final D) synchronized Answer: C
Explanation: The final modifier prevents subclasses from overriding the method. Question 4. What access level do interface variables have by default? A) private B) protected C) public D) package-private Answer: C Explanation: Interface variables are implicitly public, static, and final. Question 5. Which primitive type has the widest range of values? A) int B) long C) double D) float Answer: C Explanation: double has the widest range because it is a 64-bit floating-point type. Question 6. What is the size in bytes of a char in Java? A) 1 B) 2 C) 4 D) 8 Answer: B
Explanation: Static fields belong to the class, not to any specific instance. Question 10. Which is a valid way to define a method using varargs? A) void print(int... numbers) B) void print(int numbers...) C) void print(...int numbers) D) void print(int[]... numbers) Answer: A Explanation: Correct varargs syntax uses type, ellipsis, and variable name: int... numbers. Question 11. Which of the following can be used as a switch statement expression in Java 5.0? A) String B) boolean C) int D) double Answer: C Explanation: In Java 5.0, switch expressions can be byte, short, char, int, or enum types—not String or double. Question 12. Which loop always executes its body at least once? A) for loop B) while loop C) do-while loop D) enhanced for loop
Answer: C Explanation: do-while loops check the condition after the first execution, so the body runs at least once. Question 13. Which statement can be used to exit from the current loop iteration and continue with the next one? A) break B) continue C) return D) exit Answer: B Explanation: continue skips the rest of the current loop iteration and starts the next iteration. Question 14. What is the parent class of all checked exceptions? A) Throwable B) Exception C) RuntimeException D) Error Answer: B Explanation: All checked exceptions inherit from Exception, which itself inherits from Throwable. Question 15. What happens if a catch block catches an exception and then a finally block throws a new exception? A) The original exception is propagated B) Both exceptions are propagated
C) Enabled for classes only D) Enabled for methods only Answer: B Explanation: Assertions are disabled by default and must be enabled with the - ea flag. Question 19. What is the result of the following code? String s1 = "abc"; String s2 = new String("abc"); System.out.println(s1 == s2); A) true B) false C) Compilation error D) Runtime exception Answer: B Explanation: s1 and s2 refer to different objects; == compares references, not content. Question 20. Which method is used to compare the actual content of two String objects? A) == B) equals() C) compareTo() D) match() Answer: B Explanation: equals() compares the contents of the String objects.
Question 21. What is the main difference between StringBuilder and StringBuffer? A) StringBuilder is synchronized B) StringBuffer is faster C) StringBuffer is synchronized D) Both are immutable Answer: C Explanation: StringBuffer is thread-safe (synchronized), while StringBuilder is not. Question 22. Which wrapper class provides a method to parse a String to its primitive type? A) Integer B) Boolean C) Double D) All of the above Answer: D Explanation: All wrapper classes provide parseXXX methods to convert Strings to primitive types. Question 23. What is autoboxing? A) Converting primitives to Strings B) Wrapping primitives in their corresponding wrapper classes automatically C) Unwrapping wrapper classes to primitives D) Boxing objects in arrays Answer: B Explanation: Autoboxing is automatic conversion from a primitive type to its corresponding wrapper class.
Answer: A Explanation: ArrayList provides fast random access due to its underlying array structure. Question 27. Which collection implementation guarantees sorted order? A) HashSet B) TreeSet C) ArrayList D) LinkedList Answer: B Explanation: TreeSet stores elements in a sorted order. Question 28. What does Iterator’s remove() method do? A) Removes the last element in the collection B) Removes the current element during iteration C) Removes all elements in the collection D) Removes a specified element Answer: B Explanation: remove() deletes the element returned by the last call to next(). Question 29. How do you declare a list that only accepts String objects? A) List list = new ArrayList(); B) List<String> list = new ArrayList<String>(); C) List<Object> list = new ArrayList<Object>(); D) ArrayList list = new ArrayList();
Answer: B Explanation: Generics enforce type safety; List<String> accepts only Strings. Question 30. Which interface must be implemented to create a thread by passing an object to a Thread constructor? A) Runnable B) Callable C) Comparable D) Observer Answer: A Explanation: The Runnable interface allows an object to be executed by a Thread. Question 31. What is the default state of a newly created thread in Java? A) New B) Running C) Blocked D) Dead Answer: A Explanation: A new thread starts in the New state until start() is called. Question 32. Which keyword is used to ensure that only one thread executes a block of code at a time? A) final B) static C) synchronized
D) BufferedReader Answer: B Explanation: FileInputStream is designed to read raw bytes from files. Question 36. Which class can wrap another stream to buffer input for efficiency? A) BufferedReader B) PrintWriter C) DataInputStream D) FilterInputStream Answer: A Explanation: BufferedReader wraps a Reader to buffer characters for efficient reading. Question 37. Which interface must a class implement to be eligible for serialization? A) Serializable B) Externalizable C) Cloneable D) Runnable Answer: A Explanation: Implementing Serializable enables object serialization. Question 38. What is the purpose of the transient keyword? A) Makes a field immutable B) Excludes a field from serialization C) Allows thread access
D) Enables synchronization Answer: B Explanation: Transient fields are ignored during serialization. Question 39. What does ObjectInputStream do? A) Writes objects to a stream B) Reads objects from a stream C) Reads characters from a stream D) Buffers input Answer: B Explanation: ObjectInputStream is used to deserialize Java objects from a stream. Question 40. What is the main advantage of generics in Java? A) Faster execution B) Compile-time type safety C) Better memory usage D) Easier debugging Answer: B Explanation: Generics provide compile-time type checking, reducing runtime errors. Question 41. Which wildcard allows a generic method to accept any subclass of a type? A) <? super T> B) <? extends T> C) <T>
D) @Inherited Answer: A Explanation: @Override signals that a method overrides a superclass declaration. Question 45. Which annotation suppresses compiler warnings? A) @Override B) @SuppressWarnings C) @Deprecated D) @Retention Answer: B Explanation: @SuppressWarnings disables specified compiler warnings. Question 46. What does System.out.printf() do? A) Prints formatted output to the console B) Reads input from the console C) Prints errors to the console D) Writes to a file Answer: A Explanation: printf() is used for formatted console output. Question 47. How do you format a floating-point value to two decimal places using String.format()? A) String.format("%d", value) B) String.format("%.2f", value) C) String.format("%s", value)
D) String.format("%2f", value) Answer: B Explanation: "%.2f" formats a float/double to two decimal places. Question 48. Which of the following is a legal enum declaration? A) enum Color {RED, GREEN, BLUE} B) Color enum = {RED, GREEN, BLUE} C) enum {RED, GREEN, BLUE} Color D) Color {RED, GREEN, BLUE} enum Answer: A Explanation: The correct syntax is enum Name {constants}. Question 49. What is the default value of a boolean instance variable? A) true B) false C) null D) 0 Answer: B Explanation: boolean instance variables default to false. Question 50. Which block is always executed regardless of whether an exception occurs? A) try B) catch C) finally
B) Compiler inserts an implicit call to super() C) Constructor is ignored D) The constructor is private Answer: B Explanation: The compiler adds an implicit call to the superclass’s no-argument constructor. Question 54. Which class is the root of the Java class hierarchy? A) Object B) Class C) Base D) Super Answer: A Explanation: Object is the root class of all Java classes. Question 55. Which method from Object class is used for object comparison? A) equals() B) compareTo() C) toString() D) hashCode() Answer: A Explanation: equals() is used to check logical equality between objects. Question 56. Which method provides a string representation of an object? A) equals()
B) toString() C) getClass() D) hashCode() Answer: B Explanation: toString() returns a text representation of the object. Question 57. What is the result of the following code? int x = 5; assert x > 10 : "x is not greater than 10"; A) AssertionError at runtime B) Compilation error C) Prints "x is not greater than 10" D) No output Answer: A Explanation: Assertion fails and throws AssertionError with the detail message. Question 58. Which of the following is NOT a primitive type? A) int B) byte C) String D) char Answer: C Explanation: String is an object, not a primitive type.