This set of Java Multiple Choice Questions & Answers (MCQs) on “URL Class”.
1. What does URL stands for? Answer: a 2. Which of these exceptions is thrown by URL class’s constructors? Answer: c 3. Which of these methods is used to know host of an URL? Answer: b 4. Which of these methods is used to know the full URL of an URL object? Answer: d 5. Which of these class is used to access actual bits or content information of a URL? Answer: d 6. What will be the output of the following Java code? a) http 7. What will be the output of the following Java program? a) 1 8. What will be the output of the following Java program? a) 9. What will be the output of the following Java program? a)
a) Uniform Resource Locator
b) Uniform Resource Latch
c) Universal Resource Locator
d) Universal Resource Latch
Clarification: URL is Uniform Resource Locator.
a) URLNotFound
b) URLSourceNotFound
c) MalformedURLException
d) URLNotFoundException
Clarification: None.
a) host()
b) getHost()
c) GetHost()
d) gethost()
Clarification: None.
a) fullHost()
b) getHost()
c) ExternalForm()
d) toExternalForm()
Clarification: None.
a) URL
b) URLDecoder
c) URLConnection
d) All of the mentioned
Clarification: URL, URLDecoder and URLConnection all there are used to access information stored in a URL.
import java.net.*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("javamcq");
System.out.print(obj.getProtocol());
}
}
b) https
c) www
d) com
Clarification: obj.getProtocol() 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 networking.java
$ java networking
http
import java.net.*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("javamcq");
System.out.print(obj.getPort());
}
}
b) 0
c) -1
d) garbage value
Clarification: Since we have not explicitly set the port default value that is -1 is printed.
Output:
$ javac networking.java
$ java networking
-1
import java.net.*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("javamcq");
System.out.print(obj.getHost());
}
}
b) .com
c) www..com
d) javamcq
Clarification: None.
Output:
$ javac networking.java
$ java networking
www..com
import java.net.*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("javamcq");
System.out.print(obj.toExternalForm());
}
}
b) .com
c) www..com
d) javamcq
Clarification: toExternalForm() is used to know the full URL of an URL object.
Output:
$ javac networking.java
$ java networking
https://www..com/javamcq