This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “HttpResponse & URLConnection Class”.
1. Which of these is a wrapper around everything associated with a reply from an http server?
a) HTTP
b) HttpResponse
c) HttpRequest
d) httpserver
View Answer
Explanation: HttpResponse is wrapper around everything associated with a reply from an http server.
2. Which of these transfer protocol must be used so that URL can be accessed by URLConnection class object?
a) http
b) https
c) Any Protocol can be used
d) None of the mentioned
View Answer
Explanation: For a URL to be accessed from remote location http protocol must be used.
3. Which of these methods is used to know when was the URL last modified?
a) LastModified()
b) getLastModified()
c) GetLastModified()
d) getlastModified()()
View Answer
Explanation: None.
4. Which of these methods is used to know the type of content used in the URL?
a) ContentType()
b) contentType()
c) getContentType()
d) GetContentType()
View Answer
Explanation: None.
5. Which of these data member of HttpResponse class is used to store the response from an http server?
a) status
b) address
c) statusResponse
d) statusCode
View Answer
Explanation: 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.
6. What will be the output of the following Java program?
import java.net.*;
class networking
{
public static void main(String[] args) throws Exception
{
URL obj = new URL("https://www.sanfoundry.com/javamcq");
URLConnection obj1 = obj.openConnection();
System.out.print(obj1.getContentType());
}
}
Note: Host URL is written in html and simple text.
a) html
b) text
c) html/text
d) text/html
View Answer
Explanation: None.
Output:
$ javac networking.java $ java networking text/html
7. What will be the output of the following Java program?
import java.net.*;
class networking
{
public static void main(String[] args) throws Exception
{
URL obj = new URL("https://www.sanfoundry.com/javamcq");
URLConnection obj1 = obj.openConnection();
int len = obj1.getContentLength();
System.out.print(len);
}
}
Note: Host URL is having length of content 127.
a) 126
b) 127
c) Compilation Error
d) Runtime Error
View Answer
Explanation: None.
Output:
$ javac networking.java $ java networking 127
8. What will be the output of the following Java program?
import java.net.*;
class networking
{
public static void main(String[] args) throws Exception
{
URL obj = new URL("https://www.sanfoundry.com/javamcq");
URLConnection obj1 = obj.openConnection();
System.out.print(obj1.getLastModified);
}
}
Note: Host URL was last modified on june 18 tuesday 2013.
a) june
b) 18-6-2013
c) Tue 18 Jun 2013
d) Tue Jun 18 2013
View Answer
Explanation: None.
Output:
$ javac networking.java $ java networking Tue Jun 18 2013
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Apply for Java Internship
- Check Java Books