This set of Java Multiple Choice Questions & Answers (MCQs) on “HttpResponse & URLConnection Class”.
1. Which of these is a wrapper around everything associated with a reply from an http server? Answer: b 2. Which of these transfer protocol must be used so that URL can be accessed by URLConnection class object? Answer: a 3. Which of these methods is used to know when was the URL last modified? Answer: b 4. Which of these methods is used to know the type of content used in the URL? Answer: c 5. Which of these data member of HttpResponse class is used to store the response from an http server? Answer: d 6. What will be the output of the following Java program? Note: Host URL is written in html and simple text. 7. What will be the output of the following Java program? Note: Host URL is having length of content 127. 8. What will be the output of the following Java program? Note: Host URL was last modified on july 18 tuesday 2013 .
a) HTTP
b) HttpResponse
c) HttpRequest
d) httpserver
Clarification: HttpResponse is wrapper around everything associated with a reply from an http server.
a) http
b) https
c) Any Protocol can be used
d) None of the mentioned
Clarification: For a URL to be accessed from remote location http protocol must be used.
a) LastModified()
b) getLastModified()
c) GetLastModified()
d) getlastModified()()
Clarification: None.
a) ContentType()
b) contentType()
c) getContentType()
d) GetContentType()
Clarification: None.
a) status
b) address
c) statusResponse
d) statusCode
Clarification: When we send a request to an http server it responds with a status code this status code is stored in statusCode and a textual equivalent which is stored in reasonPhrase.
import java.net.*;
class networking
{
public static void main(String[] args) throws Exception
{
URL obj = new URL("javamcq");
URLConnection obj1 = obj.openConnection();
System.out.print(obj1.getContentType());
}
}
a) html
b) text
c) html/text
d) text/html
Clarification: None.
Output:
$ javac networking.java
$ java networking
text/html
import java.net.*;
class networking
{
public static void main(String[] args) throws Exception
{
URL obj = new URL("javamcq");
URLConnection obj1 = obj.openConnection();
int len = obj1.getContentLength();
System.out.print(len);
}
}
a) 126
b) 127
c) Compilation Error
d) Runtime Error
Clarification: None.
Output:
$ javac networking.java
$ java networking
127
import java.net.*;
class networking
{
public static void main(String[] args) throws Exception
{
URL obj = new URL("javamcq");
URLConnection obj1 = obj.openConnection();
System.out.print(obj1.getLastModified);
}
}
a) july
b) 18-6-2013
c) Tue 18 Jun 2013
d) Tue Jun 18 2013
Clarification: None.
Output:
$ javac networking.java
$ java networking
Tue Jun 18 2013
