








































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
The Cambridge Certified Java Associate Exam evaluates skills in programming using Java. Topics include object-oriented programming, data structures, algorithms, and Java libraries. Candidates will demonstrate their ability to write, test, and maintain Java applications. This certification is ideal for developers seeking to advance their knowledge and expertise in Java programming.
Typology: Exams
1 / 48
This page cannot be seen from the preview
Don't miss anything!









































1. What is the primary advantage of Java’s “Write Once, Run Anywhere” philosophy? A) It eliminates the need for recompilation on different platforms. B) It requires source code changes for each operating system. C) It compiles code into native machine language for every platform. D) It allows Java code to run without a virtual machine. Answer: A Explanation: Java programs are compiled into bytecode, which runs on any platform that has a compatible JVM, eliminating the need for recompilation. 2. Which component is responsible for executing Java bytecode? A) Java Development Kit (JDK) B) Java Runtime Environment (JRE) C) Java Virtual Machine (JVM) D) Integrated Development Environment (IDE) Answer: C Explanation: The JVM interprets Java bytecode and executes it on the host machine, ensuring platform independence. 3. In Java, which keyword is used to declare a constant? A) static B) final C) const D) immutable Answer: B Explanation: The final keyword is used in Java to declare constants, ensuring that the value cannot be modified after initialization. 4. What is the correct entry point for any Java application? A) public void run() B) static void start() C) public static void main(String[] args) D) void main(String args[]) Answer: C Explanation: The main method with the signature public static void main(String[] args) is the starting point for Java applications. 5. Which of the following is NOT a primitive data type in Java? A) int B) char C) String D) boolean Answer: C Explanation: String is not a primitive type; it is an object in Java.
6. What is the default value of a boolean variable in Java when declared as an instance variable? A) true B) false C) null D) 0 Answer: B Explanation: Instance boolean variables are default initialized to false if not explicitly set. 7. Which operator is used to compare two values for equality in Java? A) = B) == C) != D) equals() Answer: B Explanation: The == operator compares primitive values and object references for equality, though for objects, equals() is typically used. 8. What does the ternary operator in Java do? A) Iterates through an array. B) Executes a block of code multiple times. C) Returns one of two values based on a condition. D) Compares three values simultaneously. Answer: C Explanation: The ternary operator (condition? value1 : value2) returns one value if the condition is true and another if it is false. 9. Which loop is guaranteed to execute its body at least once? A) for loop B) while loop C) do-while loop D) enhanced for loop Answer: C Explanation: A do-while loop executes the body first and then evaluates the condition, ensuring the loop body runs at least once. 10. What is method overloading in Java? A) Declaring multiple methods with the same name but different return types. B) Declaring multiple methods with the same name but different parameter lists. C) Declaring a method inside another method. D) Overriding a method from the parent class. Answer: B Explanation: Method overloading allows multiple methods with the same name to coexist as long as their parameter lists differ. 11. Which keyword is used to refer to the current instance of a class in Java? A) super B) this
Answer: A Explanation: An abstract class cannot be instantiated and is designed to be subclassed; it can include abstract methods without implementations.
17. Which of the following statements about Java interfaces is true? A) Interfaces can have instance fields. B) A class can implement multiple interfaces. C) Interfaces cannot contain default methods. D) Interfaces support constructor overloading. Answer: B Explanation: Java supports multiple interface inheritance, allowing a class to implement more than one interface. 18. How do you declare an array in Java? A) int array[] = new int[10]; B) int[] array = new int[10]; C) Both A and B are correct. D) Neither A nor B is correct. Answer: C Explanation: Both notations are valid for declaring arrays in Java. 19. What is the primary benefit of using the Java Collections Framework? A) It provides a set of standard interfaces and classes for handling groups of objects. B) It enforces a specific data structure for all programs. C) It eliminates the need for error handling. D) It automatically optimizes code execution. Answer: A Explanation: The Collections Framework provides reusable data structures and algorithms to manage groups of objects effectively. 20. Which class is typically used to implement a dynamic array in Java? A) Array B) Vector C) ArrayList D) LinkedList Answer: C Explanation: ArrayList is a commonly used dynamic array implementation in Java that allows resizing as needed. 21. What does the Iterator interface in Java allow you to do? A) Modify the underlying collection structure. B) Traverse a collection one element at a time. C) Sort a collection automatically. D) Convert a collection to an array. Answer: B Explanation: The Iterator interface provides methods to sequentially access elements in a collection without exposing its underlying structure.
22. What is the purpose of generics in Java? A) To provide type safety by allowing classes and methods to operate on a specified type. B) To speed up program execution by compiling code faster. C) To enable multiple inheritance of classes. D) To simplify exception handling. Answer: A Explanation: Generics allow classes and methods to operate on objects of various types while providing compile-time type safety. 23. Which keyword is used to handle exceptions that may occur during program execution? A) catch B) try C) throw D) both B and C Answer: D Explanation: The try block is used to wrap code that may throw an exception, and the throw keyword is used to explicitly throw an exception. 24. What is the difference between checked and unchecked exceptions in Java? A) Checked exceptions are compile-time errors, while unchecked exceptions are runtime errors. B) Checked exceptions are ignored by the compiler, while unchecked exceptions must be declared. C) Both are handled in the same way. D) There is no difference between them. Answer: A Explanation: Checked exceptions are verified at compile time, while unchecked exceptions occur at runtime and are not required to be caught or declared. 25. Which statement is true about the finally block in Java exception handling? A) It executes only if an exception is thrown. B) It always executes regardless of whether an exception occurs. C) It is used to throw exceptions. D) It is optional and rarely used. Answer: B Explanation: The finally block always executes after try and catch blocks, regardless of whether an exception is thrown. 26. Which class is used for reading text from a file in Java? A) FileReader B) BufferedReader C) FileWriter D) Both A and B Answer: D Explanation: FileReader is used for reading character files, and BufferedReader can be used to wrap FileReader for efficient reading. 27. What is serialization in Java? A) The process of converting an object into a byte stream.
Answer: D Explanation: Java provides multiple methods to create and manage threads, including extending Thread, implementing Runnable, and using ExecutorService.
33. What is the purpose of the synchronized keyword in Java? A) To create immutable objects. B) To prevent concurrent access to a block of code. C) To overload methods automatically. D) To declare a method as static. Answer: B Explanation: The synchronized keyword ensures that only one thread can access a block of code or method at a time, thus preventing race conditions. 34. What does the wait() method do in Java thread communication? A) Pauses the thread permanently. B) Suspends the current thread until notify() or notifyAll() is called. C) Terminates the current thread immediately. D) Locks the thread for a specified period. Answer: B Explanation: The wait() method causes the current thread to release the monitor and wait until another thread invokes notify() or notifyAll() on the same object. 35. What is the primary role of the ExecutorService in Java concurrency? A) It schedules tasks for future execution on a separate thread pool. B) It terminates threads forcefully. C) It converts bytecode into machine code. D) It manages memory allocation for threads. Answer: A Explanation: ExecutorService provides a higher-level API for managing a pool of threads and scheduling asynchronous tasks. 36. Which Java collection class is thread-safe by design? A) ArrayList B) HashMap C) CopyOnWriteArrayList D) LinkedList Answer: C Explanation: CopyOnWriteArrayList is designed for concurrent use and is thread-safe, making it suitable for scenarios with more reads than writes. 37. In Java, what is the purpose of the transient keyword? A) To mark a variable as temporary and exclude it from serialization. B) To indicate a variable can change frequently. C) To ensure a variable is synchronized. D) To declare a constant variable. Answer: A
Explanation: The transient keyword prevents a variable from being serialized, which is useful when sensitive or non-essential data should not be stored.
38. Which of the following best describes the concept of inheritance in Java? A) A class can directly include methods of another class without extending it. B) A subclass can inherit fields and methods from its superclass. C) Inheritance is used to overload methods in a class. D) Inheritance allows a class to be instantiated multiple times. Answer: B Explanation: Inheritance allows a subclass to inherit the data members and methods of its superclass, promoting code reuse and logical hierarchy. 39. Which method in the Object class is used to obtain a string representation of an object? A) getString() B) toString() C) stringify() D) objectToString() Answer: B Explanation: The toString() method provides a string representation of an object, which can be overridden for custom behavior. 40. What is method overriding in Java? A) Defining a new method with a different name in a subclass. B) Providing a new implementation for a method inherited from a superclass. C) Declaring a method as final in a subclass. D) Implementing multiple methods with the same name and parameters. Answer: B Explanation: Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. 41. What is the significance of the main method being static in Java? A) It allows the method to be overridden in subclasses. B) It allows the method to be called without creating an instance of the class. C) It prevents the method from being accessed by other classes. D) It indicates that the method can only be used with objects. Answer: B Explanation: A static main method can be invoked by the JVM without instantiating the class, making it the proper entry point for execution. 42. What is the primary difference between a while loop and a do-while loop in Java? A) A while loop always executes at least once. B) A do-while loop evaluates its condition before executing the loop body. C) A do-while loop guarantees execution of the loop body at least once. D) There is no difference between the two. Answer: C Explanation: A do-while loop executes its body first and then checks the condition, ensuring at least one execution.
C) To skip the current iteration and continue with the next. D) To pause the loop execution temporarily. Answer: B Explanation: The break statement terminates the loop immediately, regardless of the loop condition.
49. What does the continue statement do in a loop? A) Terminates the loop entirely. B) Skips the rest of the code in the current iteration and moves to the next iteration. C) Exits the program. D) Pauses the loop for a specified period. Answer: B Explanation: The continue statement skips the remaining code in the current iteration and continues with the next iteration of the loop. 50. What is a constructor in Java? A) A special method used to initialize objects when they are created. B) A method that is used to destroy objects. C) A static method used to create objects. D) A method that can be inherited from the superclass. Answer: A Explanation: Constructors are special methods invoked at the time of object creation to initialize the new object’s state. 51. Which of the following is true about constructor overloading? A) Multiple constructors in the same class can have the same parameter list. B) Constructors cannot be overloaded in Java. C) Multiple constructors can exist with different parameter lists. D) Only one constructor is allowed per class. Answer: C Explanation: Constructor overloading allows a class to have multiple constructors with different parameter lists to provide different ways of initializing an object. 52. In Java, what is the role of the new keyword? A) It declares a new variable. B) It creates a new instance of an object. C) It is used to override methods. D) It is used to call a superclass constructor. Answer: B Explanation: The new keyword is used to instantiate objects by allocating memory for them. 53. Which of the following best describes the term “instance variable”? A) A variable defined inside a method. B) A variable that is shared among all instances of a class. C) A variable that is unique to each object instance. D) A variable that is declared as static. Answer: C
Explanation: Instance variables are defined in a class but outside any method and each object instance has its own copy.
54. Which of the following statements about static methods is true? A) They can be overridden in a subclass. B) They belong to the class rather than any object instance. C) They have access to instance variables directly. D) They cannot call other static methods. Answer: B Explanation: Static methods are associated with the class itself and do not require an object instance to be called. 55. Which of these is a valid Java identifier? A) 2ndVariable B) _variable C) variable- 2 D) variable 2 Answer: B Explanation: Valid Java identifiers cannot start with a digit, contain hyphens, or spaces. An underscore followed by letters is acceptable. 56. What does the keyword “this()” do when used inside a constructor? A) Calls another constructor in the same class. B) Calls the superclass constructor. C) Refers to the current object’s instance variables. D) Terminates the constructor. Answer: A Explanation: Using this() in a constructor calls another constructor of the same class, allowing constructor chaining. 57. Which operator is used to perform bitwise shifting in Java? A) >> B) << C) >>> D) All of the above Answer: D Explanation: Java supports various bitwise shift operators including >> (signed right shift), << (left shift), and >>> (unsigned right shift). 58. What is the function of the enhanced for loop in Java? A) To iterate over arrays and collections easily. B) To perform complex mathematical operations. C) To sort collections automatically. D) To replace recursion in all cases. Answer: A Explanation: The enhanced for loop (for-each loop) simplifies iteration over arrays and collections without using an explicit iterator.
Answer: A Explanation: The addition of numbers occurs first (10+20=30) and then "30" is concatenated, resulting in "3030". However, note that since addition is left-associative, the actual output is "3030". Note: There was an intentional trick; please re-read the code carefully. Answer Correction: A detailed look shows 10+20=30 and then "30" is appended to "30" yielding "3030". Answer: A Explanation: The numeric addition happens first before string concatenation.
65. Which exception is thrown when a program attempts to access an array element out of its bounds? A) NullPointerException B) ArrayIndexOutOfBoundsException C) ClassCastException D) IllegalArgumentException Answer: B Explanation: ArrayIndexOutOfBoundsException is thrown when an invalid index is used to access an array element. 66. Which keyword is used to explicitly throw an exception in Java? A) throws B) throw C) catch D) final Answer: B Explanation: The throw keyword is used to explicitly throw an exception in Java. 67. Which of the following is a valid method signature in Java? A) public void myMethod(int a, String b) B) void public myMethod(String a, int b) C) public myMethod void(int a, String b) D) myMethod public void(String a, int b) Answer: A Explanation: The correct order is access modifier, return type, method name, and parameters. 68. What does the Java keyword “extends” indicate? A) It indicates that a class implements an interface. B) It indicates that a class is a subclass of another class. C) It is used to create an object instance. D) It is used to overload a method. Answer: B Explanation: The extends keyword is used in Java for class inheritance, indicating that a class is a subclass of another. 69. Which of the following is an example of method overloading? A) Two methods with the same name and same parameters in the same class.
B) Two methods with the same name but different parameter types in the same class. C) A subclass method that overrides a superclass method. D) A method that calls itself recursively. Answer: B Explanation: Method overloading occurs when multiple methods in the same class have the same name but different parameter lists.
70. In which scenario would you use the “final” keyword with a class? A) When you want the class to be subclassed. B) When you want the class to be immutable and not extendable. C) When you want to overload methods in the class. D) When you want to implement multiple interfaces. Answer: B Explanation: Declaring a class as final prevents it from being subclassed, ensuring its implementation remains unchanged. 71. What is the output of the following code snippet? int x = 5; x += 10; System.out.println(x); A) 5 B) 10 C) 15 D) 20 Answer: C Explanation: The compound assignment operator += adds 10 to x (5), resulting in 15. 72. Which of the following correctly demonstrates type casting from double to int in Java? A) int num = (int) 9.99; B) int num = 9.99; C) int num = double(9.99); D) int num = 9; Answer: A Explanation: Explicit casting is required when converting a double to an int in Java, as shown by (int) 9.99. 73. What is the purpose of the Java keyword “instanceof”? A) To create new instances of a class. B) To compare an object to a specified type. C) To invoke a method from a parent class. D) To check if an object is null. Answer: B Explanation: The instanceof operator checks whether an object is an instance of a particular class or interface. 74. Which of the following correctly declares a two-dimensional array in Java? A) int[][] matrix = new int[3][3];
D) An exception is thrown Answer: B Explanation: After removing the element at index 0, the element originally at index 1 (value 2) shifts to index 0.
80. Which interface must be implemented to allow objects to be sorted in a natural order? A) Comparator B) Sortable C) Comparable D) Orderable Answer: C Explanation: The Comparable interface defines a natural ordering for objects through the compareTo() method. 81. Which exception is thrown when a null object is accessed in Java? A) IOException B) NullPointerException C) ClassNotFoundException D) NumberFormatException Answer: B Explanation: A NullPointerException is thrown when an application attempts to use a null object reference. 82. What does the “throws” keyword in a method signature indicate? A) That the method handles all exceptions internally. B) That the method may pass the exception to its caller. C) That the method will never throw an exception. D) That the method creates a new exception. Answer: B Explanation: The throws keyword in a method signature indicates that the method may throw certain exceptions, which must be handled by the caller. 83. Which of the following statements correctly describes a checked exception? A) It is a runtime exception that the compiler ignores. B) It must be caught or declared in the method signature. C) It does not need to be handled explicitly. D) It only occurs during runtime and not at compile time. Answer: B Explanation: Checked exceptions are enforced at compile time, and the programmer must handle or declare them. 84. Which method is used to close a stream in Java I/O? A) shutdown() B) exit()
C) close() D) finish() Answer: C Explanation: The close() method is used to release resources associated with a stream in Java I/O.
85. What is the primary function of the java.nio package? A) To provide enhanced performance I/O operations. B) To support network communication exclusively. C) To offer an alternative to the Collections Framework. D) To handle graphical user interface elements. Answer: A Explanation: The java.nio package provides non-blocking I/O operations and improved performance for file and network I/O. 86. In Java 8, what is the purpose of a functional interface? A) To define a class with multiple abstract methods. B) To provide a target for lambda expressions and method references. C) To enforce serialization in objects. D) To create graphical user interfaces. Answer: B Explanation: A functional interface contains exactly one abstract method, making it ideal for lambda expressions and method references. 87. Which of the following is NOT a valid functional interface in Java 8? A) Runnable B) Comparator C) Serializable D) Callable Answer: C Explanation: Serializable is a marker interface and does not define any abstract method, so it is not considered a functional interface. 88. What does the Stream API in Java allow you to do? A) Process collections of objects in a functional style. B) Create multi-threaded applications automatically. C) Convert arrays into primitive data types. D) Serialize objects to a file. Answer: A Explanation: The Stream API facilitates functional-style operations on collections, such as filtering, mapping, and reducing. 89. Which of the following operations is a terminal operation in the Stream API? A) filter() B) map() C) collect() D) peek() Answer: C
Explanation: Multithreading allows a program to perform multiple tasks simultaneously, improving efficiency and responsiveness.
95. Which of the following is a correct way to create a thread by implementing the Runnable interface? A) new Thread().start(); B) new Thread(new Runnable() { public void run() { } }).start(); C) Runnable r = new Runnable() { public void run() { } }; new Thread(r).start(); D) Both B and C Answer: D Explanation: Both options B and C correctly implement and start a thread using the Runnable interface. 96. What does the notify() method do in Java’s thread communication? A) It wakes up all waiting threads on an object’s monitor. B) It wakes up a single thread waiting on an object’s monitor. C) It terminates a thread immediately. D) It blocks the current thread until a condition is met. Answer: B Explanation: The notify() method wakes up one waiting thread, whereas notifyAll() wakes up all waiting threads on the object’s monitor. 97. Which concurrency utility would you use to make one thread wait for one or more threads to complete their tasks? A) CyclicBarrier B) CountDownLatch C) Semaphore D) ExecutorService Answer: B Explanation: CountDownLatch allows one or more threads to wait until a set of operations being performed by other threads completes. 98. What is the purpose of the Callable interface in Java concurrency? A) It defines a task that can return a result and throw a checked exception. B) It is used only for tasks that do not return a result. C) It automatically cancels threads upon completion. D) It replaces the Runnable interface entirely. Answer: A Explanation: The Callable interface is similar to Runnable but can return a result and throw checked exceptions, making it suitable for tasks that require a result. 99. What is the role of the DriverManager class in JDBC? A) To manage database connections and load database drivers. B) To execute SQL queries directly. C) To serialize Java objects for storage. D) To convert SQL results into Java objects automatically. Answer: A
Explanation: DriverManager handles the establishment of database connections and the loading of JDBC drivers.
100. Which of the following is true about a PreparedStatement in JDBC? A) It is used for executing static SQL queries only. B) It helps in preventing SQL injection by precompiling SQL queries. C) It cannot accept parameters. D) It is only used for updating the database. Answer: B Explanation: PreparedStatement precompiles SQL queries and allows parameterized inputs, reducing the risk of SQL injection. 101. What is the function of the commit() method in JDBC? A) To permanently save all changes made during a transaction. B) To cancel the current transaction. C) To open a new database connection. D) To create a new SQL statement. Answer: A Explanation: The commit() method finalizes a transaction, making all changes permanent in the database. 102. What does the term “JRE” stand for in Java? A) Java Runtime Environment B) Java Reusable Engine C) Java Resource Editor D) Java Run-time Executor Answer: A Explanation: JRE stands for Java Runtime Environment, which provides the libraries and JVM needed to run Java applications. 103. Which of the following best describes the use of the main method’s String[] args parameter? A) It allows the program to receive command-line arguments. B) It is used to create an array of integers. C) It initializes the Java Virtual Machine. D) It stores the return values of methods. Answer: A Explanation: The String[] args parameter allows a Java application to accept command-line arguments upon startup. 104. Which of the following is a correct declaration of a variable of type long? A) long l = 1000; B) Long l = 1000L; C) long l = 1000L; D) All of the above Answer: D Explanation: All listed declarations correctly declare a long variable, noting that long literals should ideally be suffixed with an L.