Java Questions & Answers – HttpResponse & URLConnection Class

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

Answer: b
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

Answer: a
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

Answer: b
Explanation: None.
advertisement
advertisement

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

Answer: c
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

Answer: d
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

  1.     import java.net.*;
  2.     class networking 
  3.     {
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.             URL obj = new URL("https://www.sanfoundry.com/javamcq");
  7.             URLConnection obj1 = obj.openConnection();
  8.             System.out.print(obj1.getContentType());
  9.         }
  10.     }

Note: Host URL is written in html and simple text.
a) html
b) text
c) html/text
d) text/html
View Answer

Answer: d
Explanation: None.
Output:

advertisement
$ javac networking.java
$ java networking 
text/html

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

advertisement
  1.     import java.net.*;
  2.     class networking
  3.     {
  4.         public static void main(String[] args) throws Exception 
  5.         {
  6.             URL obj = new URL("https://www.sanfoundry.com/javamcq");
  7.             URLConnection obj1 = obj.openConnection();
  8.             int len = obj1.getContentLength();
  9.             System.out.print(len);
  10.         }
  11.     }

Note: Host URL is having length of content 127.
a) 126
b) 127
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: None.
Output:

$ javac networking.java
$ java networking 
127

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

  1.     import java.net.*;
  2.     class networking 
  3.     {
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.             URL obj = new URL("https://www.sanfoundry.com/javamcq");
  7.             URLConnection obj1 = obj.openConnection();
  8.             System.out.print(obj1.getLastModified);
  9.         }
  10.     }

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

Answer: d
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.