What will be the output of the following Java code?
import java.net.*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("https://www.example.com/javamcq");
System.out.print(obj.getProtocol());
}
}
import java.net.*;
class networking
{
public static void main(String[] args) throws MalformedURLException
{
URL obj = new URL("https://www.example.com/javamcq");
System.out.print(obj.getProtocol());
}
}
A. http
B. https
C. www
D. com
Answer: Option B
Solution (By Examveda Team)
The correct answer is Option B: https.Let's break down why:
The code uses the java.net.URL class, which is designed to work with URLs (Uniform Resource Locators).
A URL is essentially an address for a resource on the internet, like a webpage.
The line URL obj = new URL("https://www.example.com/javamcq"); creates a new URL object named 'obj'. This object represents the URL "https://www.example.com/javamcq".
The obj.getProtocol() method is called. This method specifically extracts the protocol part of the URL.
The protocol is the first part of a URL that indicates how the data will be transferred. Common protocols are "http" and "https".
In the given URL "https://www.example.com/javamcq", the protocol is "https".
Therefore, obj.getProtocol() will return "https", which is then printed to the console using System.out.print().
Correct option is https