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.

What does the Future interface represent in Java concurrency?

  • A class for creating threads in Java
  • A class for handling exceptions thrown by threads
  • A class for storing past execution results
  • A representation of a result that is yet to be computed
The Future interface in Java represents a result that is yet to be computed. It is used in concurrency to obtain the result of asynchronous operations. It allows you to check if the computation is complete, cancel the computation, and retrieve the result when it becomes available. This is commonly used with the Executor framework to manage concurrent tasks.