Java Midterm Exam Questions & Answers
The use of '? extends Animal' in Java generics signifies a bounded wildcard, allowing a collection that can hold instances of Animal or its subclasses. This allows flexibility in dealing with methods that operate on class hierarchies, ensuring type safety while providing abstraction over permissible objects .
Java's try-catch block prioritizes execution by matching exceptions with catch clauses from the most specific to the most general. This hierarchy ensures that the most relevant exception handler is executed first, avoiding bypassing more specific handlers, which prevents masking detailed exception information .
Non-linear recursive methods are expensive due to the number of recursive calls they make. Each state duplicates the call stack multiple times, often leading to exponential growth in time and space complexity compared to linear recursion, which maintains a singular, direct recursion path .
A sequential search is inefficient comparative to algorithms like binary search because it checks each element sequentially. In the worst case, it examines every element once, requiring O(n) time complexity which is suboptimal for large data sets .
In Java, a LinkedList is a list of elements stored dynamically, allowing efficient insertion and removal operations. It is not inherently a Stack, which follows LIFO (Last-In, First-Out) structure, although a LinkedList can be used to implement stack functionalities by managing end insertions and deletions manually .
Using an unbounded wildcard, such as '?', looses concrete type information, making it difficult to enforce specific type operations. While it provides flexibility, accessing and manipulating elements becomes error-prone due to loss of compile-time checks, diminishing effective type safety in operations like adding elements .
In Java, string concatenation involving integers follows the left-to-right evaluation order. For 'System.out.println(1 + 2 + "java" + 3);', the expression '1 + 2' is evaluated first as an integer operation resulting in '3'. Then, the result is concatenated with "java", forming "3java", and finally '3' is appended, yielding '3java3' .
In object-oriented programming, object equivalence is determined by overriding the 'equals()' method rather than using '==', which tests for reference equality. Proper implementation of 'equals()' ensures logical equality by comparing object states .
The intern() method is significant because it provides a canonical representation of the string. It checks if the string is already in the pool and, if not, adds it. This makes reference comparisons possible with '==', as interned strings can be checked directly against each other, even if they were initially created as separate objects .
The output 'false true' demonstrates the difference between reference equality and content equality in Java. When 'String str1 = "java";' and 'String str2 = new String("java");' are compared using '==', it checks for reference equality, resulting in 'false' because they are different objects. However, 'str1' and 'str2.intern()' are content-equal because 'intern()' returns a canonical representation for string content, hence the output 'true' .
![1. What is the output from the following code snippet?
public class Test {
public static void main(String[] args) {
System](/p?url=https%3A%2F%2Fscreenshots.scribd.com%2FScribd%2F252_100_85%2F326%2F563780570%2F1.jpeg&__src=https%3A%2F%2Fwww.scribd.com%2Fdocument%2F563780570%2FJava-Programming-Midterm-Exam-1&__type=image)



