Java Package Interview MCQ Questions and Answers

Study and learn Interview MCQ Questions and Answers on Java Package Rules. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Packages before reading these objective questions.



1) What is a package in Java?
A) A Package is a collection of files of type Java Class, Interfaces, or Abstract Class
B) A Package is simply a Directory or Folder with Java Classes
C) A Package usually contains Java Classes written for a specific purpose or problem
D) All the above
Answer [=]
D
2) Choose the correct syntax of a Java Package below.
A)
package PACKAGE_NAME;
B)
package PACKAGE_NAME.*;
C)
pkg PACKAGE_NAME;
D)
pkg PACKAGE_NAME.*;
Answer [=]
A
Explanation:

A package declaration statement should end with a package name but not with *.

3) The keyword used to declare a Java package is ____.
A) pkg
B) package
C) pkge
D) None of the above
Answer [=]
B
Explanation:

package

4) What is the maximum number of Java Class files that can be kept inside a single Java Package?
A) 8
B) 64
C) 128
D) Unlimited
Answer [=]
D
Explanation:

There is no limit. A Java package can accommodate as many files as the disk can hold.

5) Can you compile a Java file kept inside a directory without mentioning the package name?
folder_1
     ----- SomeFile.java
.
.
//SomeFile.java
//no package declaration
//package folder_1; commented
public class SomeFile
{

}
A) Yes
B) No
C) -
D) -
Answer [=]
A
Explanation:

Yes. But this Java class can not be imported.

6) The name of a package is the name of the ___ in Java.
A) folder
B) All parent folders separated by DOT symbols
C) All parent packages separated by DOT symbols
D) All the above
Answer [=]
D
7) It is possible to declare a package and import another package within the same Java class file. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes. True.



8) The keyword used to import a package into Java class or Interface is ___.
A) import
B) download
C) use
D) None of the above
Answer [=]
A
Explanation:

import

9) Which is the correct syntax to import a Java package below?
A)
import PACKAGE1.*;
B)
import PACKAGE1.CLASS1;
C)
import PACKAGE1.PACKAGE2.PACKAGE3.*;
D) All the above
Answer [=]
D
10) Choose correct declaration and importing of packages in Java.
A)
package SOMEPACKAGE;
import PACKAGE_N.*;
B)
import PACKAGE_N.*;
package SOMEPACKAGE;
C)
import PACKAGE_M.*;
package SOMEPACKAGE;
import PACKAGE_N.*;
D) All the above
Answer [=]
A
Explanation:

There can be only one package statement per class file. The package declaration statement should precede an import statement.

11) You can use the same name for a Parent package and Child package in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
12) What is the maximum number of levels or depth up to which sub-packages can be defined in Java?
A) 8
B) 16
C) 32
D) There is no limit
Answer [=]
D
Explanation:

There is no limit.

13) Choose a correct statement below about importing packages into a class.
A) A Java class or interface can import any number of packages.
B) It is advised to import only the required Classes of a package to save memory.
C) Java packages are usually distributed in the form of JAR files.
D) All the above
Answer [=]
D
14) When importing a Package, the Class is actually importing ____.
A) Classes or Interfaces from the package
B) Constants
C) Methods
D) None of the above
Answer [=]
A


15) The package declaration statement should be the first statement in a Java file. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

TRUE

16) You can place a comment before the Package Declaration statement in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

True

17) How does JAVAC or JAVA (JVM) find the packages that will be used inside classes by an import statement?
A) If the packages are defined on the same root level as the compiling or running class file, Java knows it.
B) You should manually use the CLASSPATH or CP command to include the path of the package or single-class for compiling and running
C) You can copy the JAR files in LIB folder of Java inside Program Files
D) All the above
Answer [=]
D
18) Which is the default Java package that will be auto included (imported) in the classpath while Compiling and Running a Java program?
A) java.io
B) java.util
C) java.net
D) java.lang
Answer [=]
D
Explanation:

java.lang or simply LANG package is imported automatically by the compiler and JVM (Java Virtual Machine)

19) What are the popular Classes or Interfaces inside a Java Language Pack (java.lang)?
A) Byte, Character, Short, Integer, Float, Long, Number
B) Math, String, StringBuffer, StringBuilder
C) Thread, Throwable, Exception, Error
D) All the above
Answer [=]
D
20) Choose a correct way of importing all the classes in the below java program with packages.
//Cat.java
package animals;
public class Cat { }

//Dog.java
package animals;
public class Dog { }

//PackageTesting.java
//import statements
public class
{
  //new Cat();
  //new Dog();
}
A)
import animals.*;
B)
import animals.cat;
import animals.Dog;
C) Both A and B
D) None
Answer [=]
C
Explanation:

You can import all the classes without mentioning their names with a single STAR. You can also import individual classes using separate import statements.

21) Which is the symbol used to separate a super-package and a sub-package at the time of declaring or importing in a Java program?
A) Dollar ($)
B) Pound (#)
C) Period (.) or DOT
D) Arrow (->)
Answer [=]
C
Explanation:

You can use the notation like package1.package2.package3 to declare a package by name package3 using the keyword "package".



22) Choose a correct statement about using the classes or interfaces or abstract classes inside the packages in our Java program.
A) You can extend the class imported from the package.
B) You can implement the interfaces imported from the package.
C) You can extend the abstract classes imported from the package.
D) All the above
Answer [=]
D
23) Accessing the variables, constants, or methods of a class, imported from a package is subjective to access modifiers like PUBLIC, PRIVATE, PROTECTED and default. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

TRUE. As the classes are declared public by default in each .class file, you can import the class successfully. Some variables and methods may be available after subclassing and some are available directly (public variables and methods).

24) What are the uses of a Java Package?
A) A package contains ready-to-use classes written for a specific purpose.
B) Packages are easy to distribute your code. It is nothing but reusability. Instead of writing code afresh, you can take advantage of the existing classes of a package. Simply import it and use.
C) Packages help in maintaining the code easily. Each sub-package may be maintained for more specific purposes. You can reuse the class names in sub-packages or other packages without name clash.
D) All the above
Answer [=]
D
25) What is the output of the below Java program with packages?
//MathClas.java
package package1;
public class MathClass
{
  public static int getRandom()
  {
    //returns a random number from 0 to 999
    return ((int) (Math.random() * 1000)); 
  }
}

//PackageTest3.java
import package1.*;
public class PackageTest3
{
  public static void main(String[] args)
  {
    System.out.print(MathClass.getRandom());
  }
}
A) No output
B) Compiler error
C) 484 (some random number)
D) None
Answer [=]
C
Explanation:

You can import all classes of a given package using PACKAGE_NAME.* notation. So, MathClass class will be imported successfully.