0% found this document useful (0 votes)
30 views3 pages

Java URL Class MCQs and Answers

This document contains 9 multiple choice questions about the Java URL class. It tests knowledge of what URL stands for (Uniform Resource Locator), which exception is thrown by URL constructors (MalformedURLException), and which methods can be used to get the host, protocol, port, and full URL from a URL object. The answers are provided along with short explanations. Sample code is given to demonstrate using various URL methods like getProtocol(), getPort(), getHost(), and toExternalForm().
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Java URL Class MCQs and Answers

This document contains 9 multiple choice questions about the Java URL class. It tests knowledge of what URL stands for (Uniform Resource Locator), which exception is thrown by URL constructors (MalformedURLException), and which methods can be used to get the host, protocol, port, and full URL from a URL object. The answers are provided along with short explanations. Sample code is given to demonstrate using various URL methods like getProtocol(), getPort(), getHost(), and toExternalForm().
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

ava Questions & Answers � URL Class

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on �URL Class�.

1. What does URL stands for?


a) Uniform Resource Locator
b) Uniform Resource Latch
c) Universal Resource Locator
d) Universal Resource Latch
View Answer

Answer: a
Explanation: URL is Uniform Resource Locator.

2. Which of these exceptions is thrown by URL class�s constructors?


a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) URLNotFoundException
View Answer

Answer: c
Explanation: None

3. Which of these methods is used to know host of an URL?


a) host()
b) getHost()
c) GetHost()
d) gethost()
View Answer

Answer: b
Explanation: None.

4. Which of these methods is used to know the full URL of an URL object?
a) fullHost()
b) getHost()
c) ExternalForm()
d) toExternalForm()
View Answer

Answer: d
Explanation: None.

5. Which of these class is used to access actual bits or content information of a


URL?
a) URL
b) URLDecoder
c) URLConnection
d) All of the mentioned
View Answer

Answer: d
Explanation: URL, URLDecoder and URLConnection all there are used to access
information stored in a URL.

6. What will be the output of the following Java code?

import [Link].*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("[Link]
[Link]([Link]());
}
}
a) http
b) https
c) www
d) com
View Answer

Answer: a
Explanation: [Link]() is used to know the protocol used by the host. http
stands for hypertext transfer protocol, usually 2 types of protocols are used http
and https, where s in https stands for secured.
Output:
$ javac [Link]
$ java networking
http

7. What will be the output of the following Java program?

import [Link].*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("[Link]
[Link]([Link]());
}
}
a) 1
b) 0
c) -1
d) garbage value
View Answer

Answer: c
Explanation: Since we have not explicitly set the port default value that is -1 is
printed.
Output:

8. What will be the output of the following Java program?

import [Link].*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("[Link]
[Link]([Link]());
}
}
a) sanfoundry
b) [Link]
c) [Link]
d) [Link]
View Answer

Answer: c
Explanation: None.
Output:
$ javac [Link]
$ java networking
[Link]

9. What will be the output of the following Java program?

import [Link].*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("[Link]
[Link]([Link]());
}
}
a) sanfoundry
b) [Link]
c) [Link]
d) [Link]
View Answer

Answer: d
Explanation: toExternalForm() is used to know the full URL of an URL object.
Output:
$ javac [Link]
$ java networking
[Link]

Common questions

Powered by AI

The toExternalForm() method is available in Java to obtain the full string representation of a URL object. This is useful for logging, debugging, or displaying the complete URL information to users .

URL stands for Uniform Resource Locator .

The URL class constructor throws a MalformedURLException. This exception is significant because it indicates that a URL string could not be parsed correctly, which developers need to handle to ensure robust network applications .

Not properly handling exceptions like MalformedURLException can lead to application crashes or failed network operations, undermining the application's reliability. This exception indicates that a URL is syntactically incorrect, and failing to address it can result in invalid network requests, security vulnerabilities, and degraded user experience .

Default URL constructors in Java handle encoding by assuming that supplied URLs are properly encoded. They do not automatically encode or decode data, which can lead to challenges when dealing with special characters or spaces that need URI encoding. Developers might face issues with malformed URLs if encoding is not handled manually .

Protocol selection in URLs is crucial because it determines how data is accessed over the network. The obj.getProtocol() method helps identify which protocol (http or https) the URL uses. HTTPS signifies a secure communication channel, vital for sensitive data, while HTTP suffices for less critical tasks. Proper protocol selection impacts network security, data integrity, and access permissions .

The getPort() method of a URL object would return -1 if no specific port has been set in the URL. This signifies that the default port for the protocol should be used .

The URL class represents a Uniform Resource Locator, identifying resources on the internet. URLDecoder is used to decode strings encoded in the application/x-www-form-urlencoded MIME format, which is often necessary when processing web forms. URLConnection provides a communication link between the application and a URL, enabling access to the actual resource or data the URL points to. Each plays distinct roles in handling URL content .

A Java programmer can use the getHost() method to retrieve the host name from a URL object .

The obj.getProtocol() method returns the protocol used by the URL, such as http or https, which is critical for determining data transmission methods and security. On the other hand, obj.getHost() retrieves the hostname, which identifies the server or domain. While getProtocol() is concerned with communication methods, getHost() deals with location within the network, both offering complementary insights for URL handling .

You might also like