How does Java handle overriding methods that throw exceptions?
- The overridden method must declare exceptions
- The overriding method cannot declare exceptions
- The overriding method may declare exceptions
- The overriding method must declare exceptions
In Java, when a subclass overrides a method from its superclass, it must adhere to the method signature, including the exceptions it can throw. If the superclass method declares exceptions, the overriding method can declare the same exceptions or subtypes of those exceptions. This rule ensures that the subclass does not throw unexpected exceptions.
How does the use of synchronized methods or blocks affect the performance of a Java application and why?
- It has no impact on performance.
- It improves performance by speeding up method execution.
- It may degrade performance due to thread contention.
- It only affects memory management.
Synchronized methods or blocks can lead to performance degradation because they introduce thread contention. When multiple threads try to access synchronized code, they may block, waiting for access, which can lead to slowdowns. While synchronization is necessary for thread safety, it should be used judiciously, especially in high-throughput scenarios, to avoid excessive contention and performance issues.
What is the purpose of serialization in Java?
- To convert objects into a byte stream
- To create new objects
- To perform mathematical calculations on objects
- To sort objects
Serialization in Java is primarily used to convert objects into a byte stream so that they can be easily stored, transmitted over a network, or reconstructed at a later time. This is especially useful for saving object state, such as in file I/O or network communication.
Imagine a scenario where you need to send a text message over a network using Java. How would you utilize byte streams and character streams to ensure that the message is correctly received and encoded on the other side?
- Use DataOutputStream to convert the text message to bytes and send it over the network, and DataInputStream to read and decode the message.
- Use FileInputStream to send the message as bytes and FileReader to read and decode the message on the other side.
- Use ObjectOutputStream to serialize the message and ObjectInputStream to deserialize it for network transmission.
- Use PrintWriter to send the message as characters over the network, and Scanner to read and decode the message.
To send a text message over a network, you should use DataOutputStream to convert the message to bytes and DataInputStream to read and decode it. This ensures proper encoding and decoding of the message. PrintWriter and Scanner deal with characters, FileInputStream/FileReader with bytes, and ObjectOutputStream/ObjectInputStream with object serialization, which aren't ideal for text messages.
Can a constructor be private in Java?
- No, constructors can only be package-private
- No, constructors must always be public.
- Yes, constructors can be private in Java.
- Yes, constructors can only be protected.
Yes, constructors can be private in Java. A private constructor is often used in design patterns like Singleton to ensure that only one instance of a class can be created. It restricts external instantiation of the class.
Which of the following statements about the 'this' keyword is incorrect?
- 'this' can be used to call other class constructors from within a constructor.
- 'this' is used to reference the current instance of the class.
- 'this' is used to create a new object of the class.
- 'this' can be used to access instance variables and methods of the class.
The 'this' keyword in Java is primarily used to refer to the current instance of a class. It can be used to access instance variables and methods, and also to call other constructors of the same class. Option 3 is incorrect; 'this' does not create a new object but references the existing instance. Options 1, 2, and 4 are correct statements.
What does the substring method of the String class do?
- Checks if the string is empty.
- Converts the string to lowercase.
- Retrieves the string's length.
- Returns a new string with a portion of the original string.
The substring() method of the String class in Java returns a new string that is a subset of the original string. It takes two parameters, the starting index and the ending index, and extracts the characters within that range, creating a new string. The other options do not describe the functionality of the substring() method.
In which scenarios is it recommended to create a custom exception instead of using a standard Java exception?
- When you want to avoid using exceptions altogether.
- When you want to hide error details from the caller.
- When you want to make the exception unchecked.
- When you want to provide more specific information about the error.
It is recommended to create custom exceptions when you want to provide more specific information about the error. This helps in better error handling and debugging. Custom exceptions can also encapsulate application-specific logic related to the error.
How does Java manage the memory allocation of primitive and reference data types in the stack and heap?
- Primitive data types are always allocated on the stack, and reference data types are allocated on the heap.
- Both primitive and reference data types are always allocated on the stack.
- Primitive data types are allocated on the stack, and reference data types are allocated on the heap, but the exact allocation depends on the context.
- Primitive data types are always allocated on the heap, and reference data types are allocated on the stack.
In Java, primitive data types like int, char, and boolean are typically allocated on the stack because they have fixed sizes and are stored directly in the memory location of the variable. Reference data types, such as objects, are allocated on the heap because their sizes can vary, and they need to be dynamically managed. However, it's important to note that references to objects (not the objects themselves) can be stored on the stack. The allocation of memory depends on the context and whether the reference is local or part of an object.
In the context of garbage collection, what happens when a reference data type is set to null?
- The object is immediately removed from memory.
- The object is marked for garbage collection but not removed.
- Setting a reference to null has no impact on garbage collection.
- Garbage collection is triggered, but it doesn't remove the object.
Setting a reference to null in Java means that the object that was previously referenced by that variable becomes eligible for garbage collection. It is not immediately removed from memory, but it is marked as a candidate for garbage collection. When the Java garbage collector runs, it identifies objects with no active references (i.e., references set to null) and reclaims their memory. So, while setting a reference to null doesn't immediately remove the object, it initiates the process of cleaning up unreferenced objects.