Java Questions & Answers – Packages

This section of our 1000+ Java MCQs focuses on packages of Java Programming Language.

1. Which of these keywords is used to define packages in Java?
a) pkg
b) Pkg
c) package
d) Package
View Answer

Answer: c
Explanation: None.

2. Which of these is a mechanism for naming and visibility control of a class and its content?
a) Object
b) Packages
c) Interfaces
d) None of the Mentioned.
View Answer

Answer: b
Explanation: Packages are both naming and visibility control mechanism. We can define a class inside a package which is not accessible by code outside the package.

3. Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned
View Answer

Answer: d
Explanation: Either we can use public, protected or we can name the class without any specifier.

4. Which of these access specifiers can be used for a class so that it’s members can be accessed by a different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier
View Answer

Answer: a
Explanation: None.

5. Which of the following is correct way of importing an entire package ‘pkg’?
a) import pkg.
b) Import pkg.
c) import pkg.*
d) Import pkg.*
View Answer

Answer: c
Explanation: Operator * is used to import the entire package.

6. Which of the following is incorrect statement about packages?
a) Package defines a namespace in which classes are stored.
b) A package can contain other package within it.
c) Java uses file system directories to store packages.
d) A package can be renamed without renaming the directory in which the classes are stored.
View Answer

Answer: d
Explanation: A package can be renamed only after renaming the directory in which the classes are stored.

7. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
View Answer

Answer: b
Explanation: None.

8. What is the output of this program?

  1.     package pkg;
  2.     class display {
  3.         int x;
  4.         void show() {
  5.             if (x > 1)
  6.                 System.out.print(x + " ");
  7.         }
  8.     }
  9.     class packages {
  10.         public static void main(String args[]) {
  11.             display arr[] = new display[3];
  12.             arr[0].x = 0;      
  13.             arr[1].x = 1;
  14.             arr[2].x = 2;
  15.             for (int i = 0; i < 3; ++i)
  16.                 arr[i].show();
  17.          }
  18.     }

Note : packages.class file is in directory pkg;
a) 0
b) 1
c) 2
d) 0 1 2
View Answer

Answer: c
Explanation: None.
Output:
$ javac packages.java
$ java packages
2

9. What is the output of this program?

  1.     package pkg;
  2.     class output {
  3.         public static void main(String args[])
  4.         { 
  5.             StringBuffer s1 = new StringBuffer("Hello");
  6.             s1.setCharAt(1, x);
  7.             System.out.println(s1);
  8.         }
  9.     }

a) xello
b) xxxxx
c) Hxllo
d) Hexlo
View Answer

Answer: c
Explanation: None.
Output:
$ javac output.java
$ java output
Hxllo

10. What is the output of this program?

  1.     package pkg;
  2.     class output {
  3.         public static void main(String args[])
  4.         {
  5.             StringBuffer s1 = new StringBuffer("Hello World");
  6.             s1.insert(6 , "Good ");
  7.             System.out.println(s1);
  8.         }
  9.    }

Note : Output.class file is not in directory pkg.
a) HelloGoodWorld
b) HellGoodoWorld
c) Compilation error
d) Runtime error
View Answer

Answer: d
Explanation: Since output.class file is not in the directory pkg in which class output is defined, program will not be able to run.
output:
$ javac output.java
$ java output
can not find file output.class

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