Java MCQs on packages of Java Programming Language.
1. Which of these keywords is used to define packages in Java? Answer: c 2. Which of these is a mechanism for naming and visibility control of a class and its content? Answer: b 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? Answer: d 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? Answer: a 5. Which of the following is the correct way of importing an entire package ‘pkg’? Answer: c 6. Which of the following is an incorrect statement about packages? Answer: d 7. Which of the following package stores all the standard java classes? Answer: b 8. What will be the output of the following Java program? Note : packages.class file is in directory pkg; 9. What will be the output of the following Java program? a) xello 10. What will be the output of the following Java program? Note : Output.class file is not in directory pkg.
a) pkg
b) Pkg
c) package
d) Package
Clarification: None.
a) Object
b) Packages
c) Interfaces
d) None of the Mentioned.
Clarification: 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.
a) Public
b) Protected
c) No Modifier
d) All of the mentioned
Clarification: Either we can use public, protected or we can name the class without any specifier.
a) Public
b) Protected
c) Private
d) No Modifier
Clarification: None.
a) import pkg.
b) Import pkg.
c) import pkg.*
d) Import pkg.*
Clarification: Operator * is used to import the entire package.
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
Clarification: A package can be renamed only after renaming the directory in which the classes are stored.
a) lang
b) java
c) util
d) java.packages
Clarification: None.
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<3;i++)
arr[i]=new display();
arr[0].x = 0;
arr[1].x = 1;
arr[2].x = 2;
for (int i = 0; i < 3; ++i)
arr[i].show();
}
}
a) 0
b) 1
c) 2
d) 0 1 2
Clarification: None.
Output:
$ javac packages.java
$ java packages
2
package pkg;
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1, x);
System.out.println(s1);
}
}
b) xxxxx
c) Hxllo
d) Hexlo
Clarification: None.
Output:
$ javac output.java
$ java output
Hxllo
package pkg;
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello World");
s1.insert(6 , "Good ");
System.out.println(s1);
}
}
a) HelloGoodWorld
b) HellGoodoWorld
c) Compilation error
d) Runtime error
Clarification: 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