




























































































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
A foundational Java exam focusing on core syntax, OOP principles, exception handling, collections, basic file operations, and simple class design. Learners interact with coding problems that reinforce understanding of objects, inheritance, polymorphism, and program structure. Perfect for those beginning their journey in Java development.
Typology: Exams
1 / 118
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which component of the Java platform is responsible for converting .java source files into platform‑independent bytecode? A) JVM B) JRE C) JDK D) JIT Answer: C Explanation: The Java Development Kit (JDK) includes the Java compiler (javac) that translates .java files into .class bytecode files. Question 2. In which package are the core language classes such as String, System, and Math located? A) java.util B) java.io C) java.lang D) java.net Answer: C Explanation: The java.lang package is automatically imported and contains fundamental classes like String, System, and Math. Question 3. What is the default value of a boolean instance variable that is not explicitly initialized? A) true B) false C) null D) 0
Answer: B Explanation: Instance variables of type boolean default to false if not assigned a value. Question 4. Which of the following statements correctly declares a constant named MAX_SIZE of type int with a value of 100? A) const int MAX_SIZE = 100; B) static final int MAX_SIZE = 100; C) final int MAX_SIZE = 100; D) int final MAX_SIZE = 100; Answer: C Explanation: The final keyword makes a variable a constant; static is not required unless the constant is class‑level. Question 5. Which operator has the highest precedence in the expression a + b * c - d / e? A) + B) - C) * and / (same level) D) = Answer: C Explanation: Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). **Question 6. What will be the output of the following code?
int x = 5; System.out.println(x++ + ++x); ## Foundation Java Practice Exam Explanation: `new double[10]` allocates space for ten double elements, each automatically initialized to 0.0. **Question 9. Which access modifier allows a member to be visible only within its own package and to subclasses in other packages?** A) public B) private C) protected D) default Answer: C Explanation: `protected` provides package‑level access plus visibility to subclasses, even if they are in different packages. **Question 10. What is the result of the expression `"Hello".equals(new String("Hello"))`?** A) true B) false C) Compilation error D) Runtime exception Answer: A Explanation: `String.equals` compares the character sequences, not the references, so the two strings are equal. **Question 11. Which keyword is used to invoke a superclass constructor from a subclass?** A) super() B) this() C) extends ## Foundation Java Practice Exam D) parent Answer: A Explanation: `super()` calls a constructor of the immediate superclass. **Question 12. Which of the following statements about interfaces in Java 8 is true?** A) Interfaces can contain instance fields. B) Interfaces can have static methods with bodies. C) All methods in an interface must be abstract. D) Interfaces cannot extend other interfaces. Answer: B Explanation: Since Java 8, interfaces may contain static methods with implementations, as well as default methods. **Question 13. In Java, which of the following is a checked exception?** A) NullPointerException B) ArrayIndexOutOfBoundsException C) IOException D) ArithmeticException Answer: C Explanation: `IOException` is a subclass of `Exception` that is not a `RuntimeException`; therefore, it must be declared or caught. **Question 14. Which of the following statements correctly creates a multi‑line comment?** A) `// This is a comment` B) `/* This is a comment */` ## Foundation Java Practice Exam A) for loop B) while loop C) do‑while loop D) foreach loop Answer: C Explanation: The condition of a `do‑while` loop is evaluated after the loop body, ensuring one execution. **Question 18. What will be printed by the following code? ```java for(int i = 0; i < 3; i++) { if(i == 1) continue; System.out.print(i); } ```** A) 012 B) 01 C) 02 D) 0 2 Answer: C Explanation: When `i` equals 1, `continue` skips the `print` statement, so only 0 and 2 are printed without spaces. **Question 19. Which statement correctly demonstrates method overloading?** A) Two methods with the same name and identical parameter types. ## Foundation Java Practice Exam B) Two methods with different names but same parameter list. C) Two methods with the same name but different parameter lists. D) Two methods with the same name, same parameters, but different return types. Answer: C Explanation: Overloading requires the same method name with a different parameter list (type, number, or order). **Question 20. Which of the following is true about the `StringBuilder` class?** A) It is immutable. B) It is thread‑safe. C) It provides better performance for frequent string modifications than `String`. D) It cannot be converted to a `String`. Answer: C Explanation: `StringBuilder` is mutable and not synchronized, making it faster for many modifications; `toString()` converts it to a `String`. **Question 21. What is the output of the following code? ```java int a = 10; int b = ++a + a++; System.out.println(b); ```** A) 21 B) 22 C) 23 ## Foundation Java Practice Exam C) String D) boolean Answer: C Explanation: `String` is a reference type (class), not a primitive. **Question 25. Which of the following correctly creates a package named `com.example.utils`?** A) `package com.example.utils;` placed at the top of the source file. B) `import com.example.utils;` placed at the top of the source file. C) `package com.example.utils` without a semicolon. D) `package utils;` Answer: A Explanation: The `package` declaration must be the first statement (excluding comments) and end with a semicolon. **Question 26. Which of the following statements about the `Math.random()` method is true?** A) It returns an integer between 0 and 1. B) It returns a double value in the range [0.0, 1.0). C) It can be seeded for reproducible results. D) It throws a checked exception. Answer: B Explanation: `Math.random()` generates a double greater than or equal to 0.0 and less than 1.0. **Question 27. Which of the following best describes polymorphism in Java?** A) The ability of a class to inherit from multiple superclasses. ## Foundation Java Practice Exam B) The ability to define multiple methods with the same name but different signatures. C) The ability of an object to take many forms, such as a subclass instance being referenced by a superclass type. D) The ability to overload operators. Answer: C Explanation: Polymorphism allows a subclass object to be treated as an instance of its superclass, enabling dynamic method dispatch. **Question 28. Which of the following statements about the `final` keyword applied to a class is correct?** A) The class can be subclassed but its methods cannot be overridden. B) The class cannot be subclassed. C) The class can be subclassed only within the same package. D) The class can be instantiated only once. Answer: B Explanation: Declaring a class `final` prevents any other class from extending it. **Question 29. What will be the result of the expression `5 / 2` in Java?** A) 2. B) 2 C) 3 D) Compilation error Answer: B Explanation: Both operands are integers, so integer division truncates the fractional part, yielding 2. ## Foundation Java Practice Exam **Question 33. Which of the following is the correct way to catch multiple exception types in a single catch block (Java 7+)?** A) `catch (IOException | NullPointerException e) {}` B) `catch (IOException, NullPointerException e) {}` C) `catch (Exception e) {}` D) `catch (IOException & NullPointerException e) {}` Answer: A Explanation: The multi‑catch syntax uses the pipe `|` to separate exception types. **Question 34. Which of the following statements about the `java.util.Collections` class is correct?** A) It provides methods to sort arrays directly. B) It contains static utility methods for collection manipulation, such as `shuffle` and `binarySearch`. C) It can be instantiated to hold collection objects. D) It implements the `List` interface. Answer: B Explanation: `Collections` is a utility class with static methods for operating on collections. **Question 35. What does the `instanceof` operator test?** A) Whether two objects have the same reference. B) Whether an object is an instance of a specific class or interface. C) Whether a class implements a particular method. D) Whether a variable is declared as final. Answer: B ## Foundation Java Practice Exam Explanation: `instanceof` returns true if the left‑hand operand is an instance of the type on the right. **Question 36. Which of the following statements about the `HashMap` class is FALSE?** A) It allows one null key and multiple null values. B) It maintains insertion order. C) It provides O(1) average time for get and put operations. D) It is not synchronized. Answer: B Explanation: `HashMap` does **not** guarantee any ordering; `LinkedHashMap` preserves insertion order. **Question 37. Which of the following access modifiers provides the most restrictive access?** A) public B) protected C) default (package‑private) D) private Answer: D Explanation: `private` restricts visibility to the declaring class only. **Question 38. What is the output of the following snippet? ```java int[] arr = {1,2,3,4,5}; System.out.println(arr.length); ```** ## Foundation Java Practice Exam ## B) ```java int fact(int n){ return n * fact(n-1); } C)
int fact(int n){ while(n>0){ n--; } return n; } D)
int fact(int n){ return n!; } Answer: A Explanation: The method includes a base case (n==0) and the recursive call fact(n-1). Question 41. Which of the following statements about the java.io.File class is true? A) It can read the contents of a file directly. B) It represents an abstract pathname and provides methods to query file attributes.
C) It automatically creates a file on the filesystem when instantiated. D) It implements the Serializable interface. Answer: B Explanation: File is a representation of a pathname; it does not perform I/O itself. Question 42. Which of the following is the correct way to declare a generic method that swaps two elements in an array? A) public static void swap(T[] arr, int i, int j) B) public static <T> void swap(T[] arr, int i, int j) C) public static <T> T swap(T[] arr, int i, int j) D) public static void <T> swap(T[] arr, int i, int j) Answer: B Explanation: The generic type parameter <T> must appear before the return type. Question 43. Which of the following statements about the Thread class is FALSE? A) A thread can be started only once. B) The run() method contains the code that executes in the new thread. C) Calling start() directly executes the run() method in the current thread. D) Extending Thread is one way to create a new thread. Answer: C Explanation: start() creates a new thread and then invokes run(); it does not execute run() in the current thread. Question 44. Which of the following statements correctly creates an immutable String object from a character array char[] chars? A) String s = new String(chars);
Question 47. 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: Both the lambda expression (A) and the anonymous inner class (C) implement Runnable. Question 48. Which of the following statements about the java.time.LocalDate class is true? A) It stores date and time with timezone information. B) It is mutable. C) It represents a date without a time‑zone in the ISO‑8601 calendar system. D) It can be instantiated with new LocalDate(). Answer: C Explanation: LocalDate represents a date (year‑month‑day) without time‑zone or time components; it is immutable. Question 49. Which of the following statements about the assert keyword is correct? A) Assertions are enabled by default at runtime. B) An AssertionError is a checked exception. C) Assertions can be used to verify assumptions during development and testing. D) The assert keyword can only be used inside methods. Answer: C
Explanation: Assertions are typically disabled by default and must be enabled with the -ea flag; they help verify assumptions. Question 50. Which of the following statements correctly describes the relationship between ArrayList and List? A) ArrayList implements the List interface. B) ArrayList extends the List class. C) ArrayList and List are unrelated. D) List is a concrete class that can be instantiated. Answer: A Explanation: ArrayList is a concrete class that implements the List interface. Question 51. Which of the following statements about method overriding is false? A) The overriding method must have the same return type or a subtype (covariant return). B) The access level of the overriding method cannot be more restrictive than the overridden method. C) The overriding method can throw new checked exceptions that are not declared in the superclass method. D) The @Override annotation helps the compiler detect overriding errors. Answer: C Explanation: An overriding method may only throw checked exceptions that are the same or subclasses of those declared in the overridden method. Question 52. Which of the following statements correctly creates a Map that preserves insertion order? A) Map<Integer,String> map = new HashMap<>();