Packages(Computer Science > Java Program ) Questions and Answers

Question 1.


What is the output of this program?


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


Note : Output.class file is not in directory pkg.


  1.    HelloGoodWorld
  2.    HellGoodoWorld
  3.    Compilation error
  4.    Runtime error
Explanation:-
Answer: Option D. -> Runtime error

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



Question 2.


What is the output of this program?


package pkg;
class output {
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1, x);
System.out.println(s1);
}
}
  1.    xello
  2.    xxxxx
  3.    Hxllo
  4.    Hexlo
Explanation:-
Answer: Option C. -> Hxllo

None.
Output:
$ javac output.java
$ java output
Hxllo



Question 3.


What is the output of this program?


package pkg;
class display {
int x;
void show() {
if (x > 1)
System.out.print(x + " ");
}
}
class packages {
public static void main(String args[]) {
display[] arr=new display[3];
for(int i=0;i
  1.    0
  2.    1
  3.    2
  4.    0 1 2
Explanation:-
Answer: Option C. -> 2

None.
Output:
$ javac packages.java
$ java packages
2



Question 4.

Which of the following package stores all the standard java classes?


  1.    lang
  2.    java
  3.    util
  4.    java.packages
Explanation:-
Answer: Option B. -> java

None.



Question 5.

Which of the following is incorrect statement about packages?


  1.    Package defines a namespace in which classes are stored.
  2.    A package can contain other package within it.
  3.    Java uses file system directories to store packages.
  4.    A package can be renamed without renaming the directory in which the classes are stored.
Explanation:-
Answer: Option D. -> A package can be renamed without renaming the directory in which the classes are stored.

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



Question 6.

Which of the following is correct way of importing an entire package ‘pkg’?


  1.    import pkg
  2.    Import pkg.
  3.    import pkg.*
  4.    Import pkg.*
Explanation:-
Answer: Option C. -> import pkg.*

Operator * is used to import the entire package.



Question 7.

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?


  1.    Public
  2.    Protected
  3.    Private
  4.    No Modifier
Explanation:-
Answer: Option A. -> Public

None.



Question 8.

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?


  1.    Public
  2.    Protected
  3.    No Modifier
  4.    All of the mentioned
Explanation:-
Answer: Option D. -> All of the mentioned

Either we can use public, protected or we can name the class without any specifier.



Question 9.

Which of these keywords is used to define packages in Java?


  1.    pkg
  2.    Pkg
  3.    package
  4.    Package
Explanation:-
Answer: Option C. -> package

None.



Question 10.

Which of these is a mechanism for naming and visibility control of a class and its content?


  1.    Object
  2.    Packages
  3.    Interfaces
  4.    None of the Mentioned.
Explanation:-
Answer: Option B. -> Packages

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.