



























































































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 correctly declares the entry point of a Java application? A) public void main(String[] args) B) static public void main(String args[]) C) public static void main(String[] args) D) private static void main(String[] args) Answer: C Explanation: The JVM looks for a method with the exact signature public static void main(String[] args) to start execution. Question 2. What is the size, in bits, of the primitive type short in Java? A) 8 B) 16 C) 32 D) 64 Answer: B Explanation: A short occupies 16 bits (2 bytes) of memory. Question 3. Which operator has the highest precedence in the expression a + b * c - d / e? A) + B) - C) * D) / Answer: C Explanation: Multiplication (*) and division (/) have higher precedence than addition and subtraction; they are evaluated first from left to right.
Question 4. What does Math.random() return? A) An integer between 0 and 100 B) A double greater than or equal to 0.0 and less than 1. C) A random boolean value D) A double between - 1.0 and 1. Answer: B Explanation: Math.random() generates a pseudorandom double in the range [0.0, 1.0). Question 5. Which of the following statements will compile without error? A) int x = 5; x = "five"; B) double d = 3 / 2; C) boolean flag = (5 > 3) && (2 < 1); D) char c = 65; Answer: D Explanation: A char can be assigned an integer literal that represents a Unicode code point; 65 corresponds to 'A'. Question 6. Which control structure is most appropriate for executing a block of code exactly once, but allowing early exit based on a condition? A) for loop B) while loop C) do‑while loop D) if statement Answer: D
Explanation: Both the classic case with break and the newer arrow syntax are valid for integer switch. Question 9. In a for loop, which part is executed after each iteration? A) Initialization B) Condition check C) Increment/decrement expression D) Loop body Answer: C Explanation: The increment/decrement expression runs after the loop body and before the next condition check. Question 10. What is the effect of the continue statement inside a for loop? A) Terminates the loop entirely B) Skips the remaining statements in the current iteration and proceeds with the next iteration C) Jumps to the beginning of the loop body without evaluating the condition D) Causes a compile‑time error Answer: B Explanation: continue bypasses the rest of the loop body for the current iteration and proceeds to the loop’s update step. Question 11. Which method signature correctly defines a static method that returns an integer and takes two double parameters? A) public int sum(double a, double b) B) static public int sum(double a, double b) C) public static int sum(double a, double b) D) static int sum(double a, double b)
Answer: C Explanation: The method must be declared static and public (order does not matter), return int, and accept two double arguments. Question 12. Which of the following statements about instance methods versus static methods is true? A) Instance methods can be called without creating an object. B) Static methods can access instance variables directly. C) Instance methods belong to the class, static methods belong to objects. D) Static methods belong to the class and cannot reference this. Answer: D Explanation: Static methods are class‑level; they have no this reference and cannot directly access instance members. Question 13. Which principle encourages breaking a large problem into smaller, reusable methods? A) Encapsulation B) Divide‑and‑conquer C) Inheritance D) Polymorphism Answer: B Explanation: Divide‑and‑conquer involves decomposing a problem into manageable sub‑problems solved by separate methods. Question 14. Which of the following is NOT a step in the Seven‑Step Problem‑Solving approach? A) Define the problem
A) They are mutable; you can change characters directly. B) They are immutable; any modification creates a new object. C) They are stored on the stack. D) They support operator overloading for +. Answer: B Explanation: String objects cannot be altered after creation; operations like concatenation produce new String instances. Question 18. Which method of StringBuilder appends a character to the existing sequence? A) append(char c) B) add(char c) C) insert(char c) D) push(char c) Answer: A Explanation: StringBuilder.append(char) adds the character at the end of the mutable sequence. Question 19. Which Character class method determines whether a given char is a digit? A) isLetter(char) B) isWhitespace(char) C) isDigit(char) D) isUpperCase(char) Answer: C Explanation: Character.isDigit(ch) returns true if ch is a Unicode digit.
Question 20. In a Caesar cipher with a shift of 3, what is the encrypted form of the letter ‘Z’? A) C B) A C) B D) D Answer: A Explanation: Shifting ‘Z’ three positions wraps around to ‘C’. Question 21. Which technique can be used to break a Caesar cipher without knowing the key? A) Brute‑force trying all 26 possible shifts B) Frequency analysis of letters in the ciphertext C) Both A and B D) None of the above Answer: C Explanation: Either exhaustive search or analyzing letter frequencies can reveal the key. Question 22. Which of the following array declarations creates an array capable of holding 10 integers? A) int[] numbers = new int[10]; B) int numbers = new int[10]; C) int[] numbers = new int[9]; D) int numbers[10]; Answer: A Explanation: The correct syntax is new int[10] for a one‑dimensional integer array of length 10.
Question 26. What is the time complexity of adding an element to the end of an ArrayList when it does not need to resize? A) O(1) amortized B) O(n) C) O(log n) D) O(n²) Answer: A Explanation: Adding at the end is constant‑time unless the internal array must grow, which occurs infrequently, giving amortized O(1). Question 27. Which method removes the element at index 3 from an ArrayList<Integer> named numbers? A) numbers.delete(3); B) numbers.remove(3); C) numbers.pop(3); D) numbers.erase(3); Answer: B Explanation: ArrayList.remove(int index) deletes the element at the specified position. Question 28. Which of the following statements about HashMap is true? A) It maintains insertion order. B. It allows duplicate keys. C) It provides average O(1) lookup time. D) It is synchronized by default. Answer: C
Explanation: HashMap offers constant‑time average performance for get/put operations but does not guarantee order or thread safety. Question 29. How do you retrieve a value associated with the key "apple" from a HashMap<String, Integer> named map? A) map.get("apple"); B) map["apple"]; C) map.fetch("apple"); D) map.valueOf("apple"); Answer: A Explanation: The get method returns the value mapped to the specified key, or null if absent. Question 30. Which loop construct can be used to iterate over all entries in a HashMap<String, Double> called prices? A) for (String key : prices) {} B) for (Map.Entry<String, Double> e : prices.entrySet()) {} C) while (prices.hasNext()) {} D) do {} while (prices.iterator().hasNext()); Answer: B Explanation: entrySet() provides a view of key‑value pairs, allowing iteration with an enhanced for loop. Question 31. Which access modifier provides the highest level of encapsulation for class fields? A) public B) protected C) default (package‑private)
public static void main(String[] args) { int x = 10; System.out.println(Test.x); } }
A) 5 B) 10 C) 0 D) Compilation error Answer: A Explanation: `Test.x` explicitly accesses the static field defined in the class, not the local variable. **Question 35.** Which of the following statements about method overloading is correct? A) Overloaded methods must have different return types only. B) Overloaded methods must differ in parameter type, number, or order. C) Overloaded methods cannot be static. D) Overloading is the same as overriding. Answer: B Explanation: Overloading is resolved at compile time based on the method signature (parameter list). **Question 36.** Which keyword is used to prevent a subclass from overriding a method? A) `final` B) `static` C) `abstract` ## Certificate Practice Exam D) `protected` Answer: A Explanation: Declaring a method `final` disallows overriding in subclasses. **Question 37.** Which of the following correctly implements the `Comparable` interface for a class `Box` that should be ordered by its `volume` field? A) `class Box implements Comparable<Box> { public int compareTo(Box b) { return Double.compare(this.volume, b.volume); } }` B) `class Box implements Comparable { public int compareTo(Object o) { return 0; } }` C) `class Box implements Comparable<Box> { public int compare(Box b) { return 0; } }` D) `class Box implements Comparator<Box> { public int compare(Box a, Box b) { return 0; } }` Answer: A Explanation: Proper generic `Comparable<T>` implementation requires a `compareTo(T o)` method returning an int. **Question 38.** Which statement about inheritance in Java is false? A) A class can extend only one superclass. B) Interfaces can be implemented multiple times. C) Subclass constructors automatically call `super()` if not specified. D) Private members of a superclass are directly accessible in the subclass. Answer: D Explanation: Private members are not visible to subclasses; they must be accessed via public/protected methods. **Question 39.** Which of the following is a correct way to handle a checked exception `IOException` when reading a file? A) `try { ... } catch (Exception e) {}` ## Certificate Practice Exam A) `Collections.order(list);` B) `Collections.sort(list);` C) `Collections.arrange(list);` D) `Collections.shuffle(list);` Answer: B Explanation: `Collections.sort(List<T>)` sorts the list according to the natural ordering of its elements. **Question 43.** What will be the result of the following code snippet? ```java String s1 = "Hello"; String s2 = new String("Hello"); System.out.println(s1 == s2); A) true B) false C) Compilation error D) Runtime exception Answer: B Explanation: == compares object references; s1 refers to the interned string, while s2 is a distinct object. Question 44. Which of the following statements correctly creates a thread by extending Thread? A) class MyThread extends Thread { public void run() { } } B) class MyThread implements Thread { public void run() { } }
C) Thread t = new Thread(); t.start(); D) class MyThread implements Runnable { public void run() { } } Answer: A Explanation: Extending Thread and overriding run() is a valid way to define a thread class. Question 45. Which interface must be implemented to define a task that can be executed by an ExecutorService and return a result? A) Runnable B) Callable<V> C) Thread D) Future Answer: B Explanation: Callable<V> defines a call() method that returns a value and can throw exceptions, suitable for ExecutorService. Question 46. What does the volatile keyword guarantee for a variable accessed by multiple threads? A) Atomicity of compound operations B) Visibility of changes across threads C) Mutual exclusion D) None of the above Answer: B Explanation: volatile ensures that reads/writes go directly to main memory, making changes visible to other threads. Question 47. Which of the following statements about the String.format method is correct?
Answer: B Explanation: The sum of 1+2+3+4+5 equals 15. Question 50. Which of the following is a correct way to copy an array src into a new array dest of the same length? A) int[] dest = src.clone(); B) int[] dest = src; C) int[] dest = new int[src.length]; dest = src; D) int[] dest = Arrays.copyOf(src, src.length + 1); Answer: A Explanation: clone() creates a shallow copy of the array with the same length and contents. Question 51. Which statement about the java.util.Optional class is true? A) It can store null values. B) It provides methods to avoid explicit null checks. C) It is a collection that holds multiple values. D) It replaces the need for try‑catch blocks. Answer: B Explanation: Optional encapsulates a value that may be absent, offering methods like orElse to handle missing data. Question 52. In Java, which access level allows members to be accessed from any class in the same package and from subclasses in other packages? A) private B) protected
C) default (package‑private) D) public Answer: B Explanation: protected grants package access and also allows subclass access across packages. Question 53. Which of the following statements correctly creates a lambda expression that implements Runnable? A) Runnable r = () - > System.out.println("Run"); B) Runnable r = () { System.out.println("Run"); }; C) Runnable r = new Runnable() { public void run() { System.out.println("Run"); } }; D) Both A and C are correct. Answer: D Explanation: A lambda expression can replace an anonymous class for functional interfaces like Runnable. Question 54. Which method of java.util.List returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive? A) subList(int fromIndex, int toIndex) B) slice(int from, int to) C) range(int start, int end) D) segment(int a, int b) Answer: A Explanation: subList provides a backed view of the specified range. Question 55. What will be printed by the following code?