Explore topic-wise MCQs in Java Programming.

This section includes 13 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the output of this program?
import java.lang.reflect.*;
public class Additional_packages_Example
{
public static void main(String args[])
{
try
{
Class obj = Class.forName("java.awt.Dimension");
Constructor constructors[] = obj.getConstructors();
for (int k = 0; k < constructors.length; k++)
System.out.println(constructors[k]);
}
catch (Exception e)
{
System.out.print("Exception");
}
}
}

A. Program prints all the possible constructors of class Class
B. Program prints all the constructors of java.awt.Dimension package
C. Runtime Error
D. Program prints Exception
E. None of these
Answer» C. Runtime Error
2.

What is the output of this program?
 import java.lang.reflect.*;
public class Additional_packages_Example
{
public static void main(String args[])
{
try
{
Class obj = Class.forName("java.awt.Dimension");
Field fields[] = obj.getFields();
for (int k = 0; k < fields.length; k++)
System.out.println(fields[k]);
}
catch (Exception e)
{
System.out.print("Exception");
}
}
}

A. Program prints all the methods of java.awt.Dimension package
B. Program prints all the data members of java.awt.Dimension package
C. program prints all the methods and data member of java.awt.Dimension package
D. Program prints all the constructors of java.awt.Dimension package
E. None of these
Answer» C. program prints all the methods and data member of java.awt.Dimension package
3.

What is the output of this program?
import java.net.*;
public class Networking_Example
{
public static void main(String[] args) throws MalformedURLException
{
URL object = new URL("https://www.interviewmania.com/programming");
System.out.print(object.toExternalForm());
}
}

A. www.interviewmania.com/programming
B. www.interviewmania.com
C. https://www.interviewmania.com/programming
D. https://www.interviewmania.
E. None of these
Answer» D. https://www.interviewmania.
4.

What is the output of this program?
import java.net.*;
public class Networking_Host
{
public static void main(String[] args) throws MalformedURLException
{
URL object = new URL("https://www.interviewmania.com/programming");
System.out.print(object.getHost());
}
}

A. www.interviewmania.com
B. https://www.interviewmania.com/programming
C. interviewmania.com/programming
D. https://www.interviewmania
E. None of these
Answer» B. https://www.interviewmania.com/programming
5.

What is the output of this program?
import java.net.*;
public class Networking_Port
{
public static void main(String[] args) throws MalformedURLException
{
URL object = new URL("https://www.interviewmania.com/programming");
System.out.print(object.getPort());
}
}

A. garbage value
B. 1
C. -1
D. 0
E. None of these
Answer» D. 0
6.

What is the output of this program?
 import java.net.*;
public class Networking_Protocol
{
public static void main(String[] args) throws MalformedURLException
{
URL object = new URL("https://www.interviewmania.com/programming");
System.out.print(object.getProtocol());
}
}

A. com
B. www
C. http
D. https
E. None of these
Answer» E. None of these
7.

What is the output of this program?
import java.net.*;
public class networking_Length
{
public static void main(String[] args) throws Exception
{
URL object = new URL("https://www.interviewmania.com/java/programming");
URLConnection object0 = object.openConnection();
int length = object0.getContentLength();
System.out.print(length);
}
}

Note: Host URL is having length of content 256.

A. 260
B. 256
C. Compilation Error
D. Runtime Error
E. None of these
Answer» C. Compilation Error
8.

What is the output of this program?
import java.net.*;
public class networking_
{
public static void main(String[] args) throws MalformedURLException
{
URL object = new URL("https://www.interviewmania.com/java/programming");
System.out.print(object.toExternalForm());
}
}

A. https://www.interviewmania.com/java/programming
B. www.interviewmania.com
C. interviewmania.com/java/programming
D. https://www.interviewmania.com
E. None of these
Answer» B. www.interviewmania.com
9.

What is the output of this program?
import java.net.*;
public class networking_Example
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress object = InetAddress.getByName("interviewmania.com");
System.out.print(object.getHostName());
}
}

A. Compile time error
B. Runtime error
C. Interviewmania
D. interviewmania.com
E. None of these
Answer» E. None of these
10.

What is the output of this program?
import java.net.*;
public class networking_Example
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress object1 = InetAddress.getByName("tcs.com");
InetAddress object2 = InetAddress.getByName("interviewmania.com");
boolean p = object1.equals(object2);
System.out.print(p);
}
}

A. 1
B. 0
C. true
D. false
E. None of these
Answer» E. None of these
11.

What is the output of this program?
import java.io.*; 
import java.net.*;
public class URL_Example
{
public static void main(String[] args)
{
try
{
URL url=new URL("https://www.interviewmania.com/programming");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
} catch(Exception e){System.out.println(e);}
}
}

A. Protocol: https
B. Host Name: www.interviewmania.com
C. Port Number: -1
D. All of above
E. None of these
Answer» E. None of these
12.

What is the output of this program?
import java.net.*;
public class networking_Example
{
public static void main(String[] args) throws UnknownHostException
{
InetAddress object0 = InetAddress.getByName("Interviewmania.com");
InetAddress object1 = InetAddress.getByName("Interviewmania.com");
boolean p = object0.equals(object1);
System.out.print(p);
}
}

A. false
B. true
C. Interviewmania
D. Interviewmania.com
E. None of these
Answer» C. Interviewmania
13.

Which of these exceptions is thrown by URL class s constructors?

A. URLNotFoundException
B. MalformedURLException
C. URLSourceNotFound
D. URLNotFound
E. None of these
Answer» C. URLSourceNotFound