Which of the following classes are byte stream classes in Java?

  • FileInputStream and FileOutputStream
  • FileInputStream and Reader
  • FileReader and FileOutputStream
  • FileReader and Writer
Byte stream classes in Java are used for handling binary data. The correct options are FileInputStream and FileOutputStream, as they are used to read and write binary data to files. FileReader and Reader are character stream classes used for reading text data, not binary data.

What happens if a superclass is not serializable but its subclass is, and we serialize an object of the subclass?

  • Only the subclass fields will be serialized.
  • Serialization will fail.
  • Serialization will proceed without errors.
  • The superclass fields will be serialized, but not the subclass fields.
If a superclass is not serializable but its subclass is, attempting to serialize an object of the subclass will result in a java.io.NotSerializableException. This happens because the superclass needs to be serializable for the serialization process to work correctly. The other options are incorrect, as they imply successful serialization without issues.

What is the purpose of using URL Encoding in Java?

  • To encrypt URLs for secure communication
  • To ensure special characters are transmitted properly
  • To optimize URL routing for faster navigation
  • To shorten URLs for better performance
URL Encoding in Java is used to ensure that special characters, such as spaces or symbols, are properly transmitted in URLs. It replaces reserved characters with escape sequences to prevent issues in URL handling and parsing. The other options do not accurately describe the purpose of URL Encoding.

The method ________ of the RandomAccessFile class sets the file-pointer offset.

  • moveFilePointer()
  • seek()
  • setFilePointer()
  • setPointerOffset()
The method seek() of the RandomAccessFile class in Java is used to set the file-pointer offset to a specified location within the file. It allows you to navigate within the file and start reading or writing data from that position.

The operator ______ is used to invert the value of a boolean expression.

  • !
  • &&
  • +
  • ==
In Java, the ! (logical NOT) operator is used to invert or negate the value of a boolean expression. It changes true to false and vice versa, making it a fundamental operator for boolean logic in Java.

The InputStream and OutputStream classes are part of the ________ package.

  • java.io
  • java.lang
  • java.streams
  • java.util
The InputStream and OutputStream classes are part of the java.io package in Java. These classes are used for reading and writing data in a byte-oriented manner, making them essential for I/O operations.

When a thread tries to access a synchronized block of code in an object, it must first obtain the ________ on the object.

  • Lock
  • Monitor
  • Semaphore
  • Semaphore
When a thread tries to access a synchronized block in Java, it must first obtain the "Lock" on the object. This ensures that only one thread can execute the synchronized code block at a time, preventing race conditions.

The ________ method of URL class provides the port number of the URL.

  • fetchPortNumber()
  • getPort()
  • obtainPort()
  • retrievePort()
The correct answer is getPort(). In Java, the URL class provides the getPort() method, which allows you to obtain the port number of a URL. This is useful when working with network-related tasks where you need to know the port associated with a particular URL.

Which class is commonly used for reading characters from a file in Java?

  • FileInputReader
  • FileOutputReader
  • FileReader
  • FileWriter
In Java, the commonly used class for reading characters from a file is FileReader. It is part of the java.io package and is specifically designed for character input. Other options like FileWriter, FileInputReader, and FileOutputReader are not standard classes in Java for file reading.

To obtain a string representation of a primitive data type, you can use the static valueOf method of the ________ class.

  • Double
  • Float
  • Integer
  • String
In Java, you can use the static valueOf method of the Integer class to obtain a string representation of a primitive data type. For example, Integer.valueOf(42) will return the string "42". The other options do not provide this specific functionality.