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 these 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.reflevt package.

3. Which of these 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.

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 a private or protected members of a class. This was added after java 2.0 .

5. Which of these 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.

6. Which of these package is used for all the text related modifications?
a) java.text
b) java.awt
c) java.lang.text
d) java.text.mofify
View Answer

Answer: a
Explanation: java.text provides capabilities for formatting, searching and manipulating text.

7. What is the output of this program?

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

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:
$ javac Additional_packages.java
$ java Additional_packages
public java.awt.Dimension(java.awt.Dimension)
public java.awt.Dimension()
public java.awt.Dimension(int,int)

8. What is the output of this program?

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

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

9. What is the length of the application box made by this program?

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

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.

10. What is the output of this program?

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

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.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







1000 Java MCQs | Quiz

Integer and Floating Data Types
Character and Boolean Data Types
Literals & Variables
Type Conversions, Promotions and Castings
Arrays
Arithmetic Operators
Bitwise Operators
Relational Operators and Boolean Logic Operators
Assignment Operators and Operator Precedence
Control Statements
Class Fundamentals & Declaring objects
Introduction to Methods
Constructors & Garbage Collection
Overloading Methods & Argument Passing
Access Control
Arrays Revisited & Keyword static
String Class
Inheritance
Method overriding
The Object Class
String Handling Basics
Character Extraction
String Comparison
Searching & Modifying a String
StringBuffer class
StringBuffer Methods
Packages
Interfaces
Java.lang Introduction
Java.lang - Integer, Long And Character Wrappers
Java.lang - Void, Process and System Class
Java.lang - Object & Math Class
Java.io Introduction
Java.io Byte Streams
Java.io Character Streams
Serialization
Serialization & Deserialization
Networking Basics
URL class
HttpResponse & URLConnection Class
networking - Server, Sockets & httpd Class
networking - httpd.java Class
networking - Datagrams
Java.util - ArrayList Class
Java.util - LinkedList, HashSet & TreeSet Class
Java.util - Maps
Java.util - Array Class
Java.util - Vectors & Stack
Java.util - Dictionary, Hashtable & Properties
Java.util - BitSet & Date class
Methods Taking Parameters
Exceptional Handling Basics
Exceptions Types
Throw, Throws & Nested Try
isAlive(), Join() & Thread Synchronization
Implementing Runnable interface for Threads
Thread class
Multithreading Basics
Networking Basics
Finally & Built in Exceptions
Creating Threads
Input & Output Basics
Reading Console Input
Writing Console Output
Reading & Writing Files
Applets Fundamentals
Core Java API Packages
Remote Method Invocation (RMI)
Text Formatting
Event Handling Basics
ActionEvent & AdjustmentEvent Class
ComponentEvent, ContainerEvent & FocusEvent Class
MouseEvent, TextEvent & WindowEvent Class
Event Listeners Interfaces
Java.lang - Rounding Functions
Java.lang - Miscellaneous Math Methods & StrictMath Class
Java.lang - ThreadGroup class & Runnable Interface
Java.lang - System Class Advance
Java.lang - Double & Float Wrappers
Java.lang - Byte & Short Wrappers
Java.lang - Character Wrapper Advance
Java.lang - Boolean Wrapper Advance
Java.lang - Runtime & ClassLoader Classes
java.lang - Class
Collection Framework Overview
Collections Interface
Collection Algorithms
Iterators
Locale & Random Classes
Observable & Timer Class
Restrictions On Generics
Wildcards
Recursion
Command Line Arguments
Inheritance - Abstract Class and Super
Try & Catch
Creating Exceptions
Generics
Generic Methods
Type Interface
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry