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.
advertisement
advertisement

4. Which of these access specifiers can be used for a class so that its 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 the 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.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which of the following is an 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.
advertisement

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

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

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:

advertisement
$ javac packages.java
$ java packages
2

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

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

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

Answer: c
Explanation: None.
Output:

$ javac output.java
$ java output
Hxllo

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

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

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.

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.