Java Questions & Answers – Core Java API Packages

This section of our 1000+ Java MCQs focuses core Java API packages in Java Programming Language.

1. Which of these package is used for graphical user interface?
a) java.applet
b) java.awt
c) java.awt.image
d) java.io
View Answer

Answer: b
Explanation: java.awt provides capabilities for graphical user interface.

2. Which of this package is used for analyzing code during run-time?
a) java.applet
b) java.awt
c) java.io
d) java.lang.reflect
View Answer

Answer: d
Explanation: Reflection is the ability of a software to analyze itself. This is provided by java.lang.reflect package.

3. Which of this package is used for handling security related issues in a program?
a) java.security
b) java.lang.security
c) java.awt.image
d) java.io.security
View Answer

Answer: a
Explanation: java.security handles certificates, keys, digests, signatures, and other security functions.
advertisement
advertisement

4. Which of these class allows us to get real time data about private and protected member of a class?
a) java.io
b) GetInformation
c) ReflectPermission
d) MembersPermission
View Answer

Answer: c
Explanation: The ReflectPermission class allows reflection of private or protected members of a class. This was added after java 2.0 .

5. Which of this package is used for invoking a method remotely?
a) java.rmi
b) java.awt
c) java.util
d) java.applet
View Answer

Answer: a
Explanation: java.rmi provides capabilities for remote method invocation.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

  1.     import java.lang.reflect.*;
  2.     class Additional_packages
  3.     {	 
  4.          public static void main(String args[])
  5.          {
  6. 	     try
  7.              {
  8. 	         Class c = Class.forName("java.awt.Dimension");
  9. 		 Constructor constructors[] = c.getConstructors();
  10. 		 for (int i = 0; i < constructors.length; i++)
  11. 		     System.out.println(constructors[i]);
  12. 	     }
  13. 	     catch (Exception e)
  14.              {
  15.                    System.out.print("Exception");
  16.              }
  17.         }
  18.     }

a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the possible constructors of class ‘Class’
c) Program prints “Exception”
d) Runtime Error
View Answer

Answer: a
Explanation: None.
Output:

advertisement
$ javac Additional_packages.java
$ java Additional_packages
public java.awt.Dimension(java.awt.Dimension)
public java.awt.Dimension()
public java.awt.Dimension(int,int)

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

advertisement
  1.     import java.lang.reflect.*;
  2.     class Additional_packages
  3.     {	 
  4.          public static void main(String args[]) 
  5.          {
  6. 	     try
  7.              {
  8. 	         Class c = Class.forName("java.awt.Dimension");
  9. 		 Field fields[] = c.getFields();
  10. 		 for (int i = 0; i < fields.length; i++)
  11. 		     System.out.println(fields[i]);
  12. 	     }
  13. 	     catch (Exception e)
  14.              {
  15.                    System.out.print("Exception");
  16.              }
  17.         }
  18.     }

a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the methods of ‘java.awt.Dimension’ package
c) Program prints all the data members of ‘java.awt.Dimension’ package
d) program prints all the methods and data member of ‘java.awt.Dimension’ package
View Answer

Answer: c
Explanation: None.
Output:

$ javac Additional_packages.java
$ java Additional_packages
public int java.awt.Dimension.width
public int java.awt.Dimension.height

8. What is the length of the application box made in the following Java program?

  1.     import java.awt.*;
  2.     import java.applet.*;
  3.     public class myapplet extends Applet
  4.     {
  5.         Graphic g;
  6.         g.drawString("A Simple Applet",20,20);    
  7.     }

a) 20
b) Default value
c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: To implement the method drawString we need first need to define abstract method of AWT that is paint() method. Without paint() method we cannot define and use drawString or any Graphic class methods.

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

  1.     import java.lang.reflect.*;
  2.     class Additional_packages 
  3.     {	 
  4.          public static void main(String args[]) 
  5.          {
  6. 	     try
  7.              {
  8. 	         Class c = Class.forName("java.awt.Dimension");
  9. 		 Method methods[] = c.getMethods();
  10. 		 for (int i = 0; i < methods.length; i++)
  11. 		     System.out.println(methods[i]);
  12. 	     }
  13. 	     catch (Exception e)
  14.              {
  15.                  System.out.print("Exception");
  16.              }
  17.         }
  18.     }

a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the methods of ‘java.awt.Dimension’ package
c) Program prints all the data members of ‘java.awt.Dimension’ package
d) program prints all the methods and data member of ‘java.awt.Dimension’ package
View Answer

Answer: b
Explanation: None.
Output:

$ javac Additional_packages.java
$ java Additional_packages
public int java.awt.Dimension.hashCode()
public boolean java.awt.Dimension.equals(java.lang.Object)
public java.lang.String java.awt.Dimension.toString()
public java.awt.Dimension java.awt.Dimension.getSize()
public void java.awt.Dimension.setSize(double,double)
public void java.awt.Dimension.setSize(int,int)
public void java.awt.Dimension.setSize(java.awt.Dimension)
public double java.awt.Dimension.getHeight()
public double java.awt.Dimension.getWidth()
public java.lang.Object java.awt.geom.Dimension2D.clone()
public void java.awt.geom.Dimension2D.setSize(java.awt.geom.Dimension2D)
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public final native void java.lang.Object.wait(long)
public final void java.lang.Object.wait(long,int)
public final void java.lang.Object.wait()

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.