0% found this document useful (0 votes)
8 views

Unit 3

Uploaded by

Dr D Suneetha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 3

Uploaded by

Dr D Suneetha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Packages

Aims and Objectives

 Aim of Packages is to create containers for classes that are used to keep the class name space
compartmentalized.
 Packages are stored in a hierarchical manner and are explicitly imported into new class
definitions.

Objectives

1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

Introduction to Packages

One of the main features of the Object Oriented Programming language is the ability to reuse the
code that is already created. One way for achieving this is by extending the classes and
implementing the interfaces. Java provides a mechanism to partition the classes into smaller
chunks. This mechanism is called Package. The Package is container of classes and interfaces.
The class is the container of the data and code. The package also addresses the problem of name
space collision that occurs when we use same name for multiple classes. Java provides
convenient way to keep the same name for classes as long as their subdirectories are different.

Packages are of two types:

 Pre-defined packages(java API packages)


 User-defined packages

.
1. Predefined packages –Java API provides large number of classes grouped into different
Packages according to the functionality. Most of the time we use the package available with
JavaAPI.
Java

lang io awt net applet util

In the above figure java is root package in it has many sub packages like lang,io ect.
Java System Packages and their Classes

Package Name Description


java.lang Contains language support classes. The java
compiler automatically uses this package. This
includes the classes such as primitive data
types, String, StringBuffer, StringBuilde etc;
java.util Contains the language utility classes such asa
Vectors,Hash Table , Date, StringTokenizer
etc;
java.io Contains the classes that support input and
output classes
java.awt Contains the classes for implementing the
graphical user interfaces
Java.net Contains the classes for networking
Java.applet Contains the classes for creating and
implementing the applets.

Using the System Packages


Packages are organized in hierarchical structure, that means a package can contain another
package, which in turn contains several classes for performing different tasks. There are two
ways to access the classes of the packages.
i. fully qualified class name- this is done specifying package name containing the class and
appending the class to it with dot (.) operator.
Example: java.util.StringTokenizer(); Here, "java.util" is the package and
"StringTokenizer()" is the class in that package. This is used when we want to refer to only one
class in a package.
ii. import statement –this is used when we want to use a class or many classes in many places in
our program. Example: (1) import java.util.*;
(2) import java.util.StringTokenizer;

2. User Define Packages


Creating a package

 While creating a package, you should choose a name for the package
and include a package statement along with that name at the top of
every source file that contains the classes, interfaces, enumerations,
and annotation types that you want to include in the package.

Syntax:

Package packagename;

Example;

package mypack;

 We can also create sub packages as follows

package packagename.subpackagename.subpcakage….;

Example

package mypack.B;

in the above statement mypack is main package and B is sub package

 The package statement should be the first line in the source file.
There can be only one package statement in each source file, and it
applies to all types in the file.

i.e

package mypack;

class Demo()

code;

}
 If a package statement is not used then the class, interfaces,
enumerations, and annotation types will be placed in the current
default package.

To compile the Java programs with package statements, you have to use -d
option as shown below.

Syntax:

javac –d . Destination_folder file_name.java

In the above statement –d specifies the java compiler to create a separate sub directory and place
the . class file there. The dot (.) after - d indicates that the package should be created in .the
current directory

Naming a package
Packages can be named using the Java Standard naming rules.
 Packages begin with "lowercase" letters.
 It is easy for the user to distinguish it from the class names.
 All the class Name by convention begin with "upper case" letters.
Example:
double d= java.lang.Math.sqrt(3);

Here, "java.lang" is the package, and "Math" is the class name, and "sqrt()" is the method name.

Package members

The types that comprise a package are known as the package members.

To use a public package member from outside its package, you must do one of the following:

 Refer to the member by its fully qualified name


 Import the package member
 Import the member's entire package

Referring to a package member by name

we can use a package member's simple name if the code we are writing is in the same package as that
member or if that member has been imported.
However, if we are trying to use a member from a different package and that package has not been imported,
you must use the member's fully qualified name, which includes the package name. Here is the fully qualified
name for the Demo class declared in the mypack package

mypack.Demo
You could use this qualified name to create an instance of mypack.Demo

mypack.Demo a = new mypack.Demo();

Qualified names are all right for infrequent use. When a name is used repetitively, however, typing the name
repeatedly becomes tedious and the code becomes difficult to read. As an alternative, you can import the
member or its package and then use its simple name.

Importing a Package Member

To import a specific member into the current file, put an import statement at the beginning of the file before any
type definitions but after the package statement, if there is one. Here's how you would import the Demo class
from the mypack package created in the previous section.

Import mypack.Demo;

Now you can refer to the A class by its simple name.

Demo a = new Demo();

This approach works well if you use just a few members from the mypack package. But if you use many
types from a package, you should import the entire package.

Importing an Entire Package

To import all the types contained in a particular package, use the import statement with the
asterisk (*) wildcard character.

import mypack.*;

Now you can refer to any class or interface in the mypack package by its simple name.

Let us consider mypack package contains classes like Demo,Demo1…

Demo a = new Demo();


Demo1 b= new Demo1();

// creating a package
package mypack; //mypack is used defined package name
public class Mul
{
//i nstance variables
private double a,b;
public Mul(double x , double y)
{
}
a=x;
b=y;
//method to find mul of two numbers
public void mul()
{
System.out.println(“multiplication of two numbers is :“+a*b);
}
}

Comile the above program


C:/>javac –d . Mul.java

Now .Mul class has been created in package mypack and it can be imported in other classes
//importing user defined package mypack
import mypack.Mul ;
class Muldemo
{
public static void main(String args[ ])
{
//create Mul class object
Mul obj = new Mul(2.0,3.0);
//call the mul() method
obj . mul();

}
}
c:\> javac Muldemo.java
c:/>java Muldemo
multiplication of two numbers is :“6.0

<<< Accessing and using a package>>>>

The visibility of an element is specified by the access specifies:

 Public
 Protected
 Default
 Private

Class member Private Default Protected Public


Member Member Member Member
Visible within YES YES YES YES
same class
Visible within No YES YES YES
the same
package by
subclasses
Visible within No YES YES YES
same package
by nonsubclass
Visible within No No YES YES
different
packages by
subclasses
Visible within No No No YES
different
packages by
Nonsubclasses

Default Access Modifier:

When we do not mention any access modifier, it is called default access modifier. The scope of
this modifier is limited to the package only. This means that if we have a class with the default
access modifier in a package, only those classes that are in this package can access this class. No
other class outside this package can access this class. Similarly, if we have a default method or
data member in a class, it would not be visible in the class of another package. Lets see an
example to understand this

package abcpackage;

public class Addition {


/* Since we didn't mention any access modifier here, it would
* be considered as default.
*/
int addTwoNumbers(int a, int b){
return a+b;
}
}
Test.java

package xyzpackage;

/* We are importing the abcpackage

* but still we will get error because the

* class we are trying to use has default access

* modifier.

*/

import abcpackage.*;

public class Test {

public static void main(String args[]){

Addition obj = new Addition();

/* It will throw error because we are trying to access

* the default method in another package

*/

obj.addTwoNumbers(10, 21);

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

The method addTwoNumbers(int, int) from the type Addition is not visible

at xyzpackage.Test.main(Test.java:12)

Private access modifier

The scope of private modifier is limited to the class only.


Private Data members and methods are only accessible within the class

Class and Interface cannot be declared as private

If a class has private constructor then you cannot create the object of that class from outside of
the class.

Let’ o understand this example, you must have the knowledge of packages in java.

In this example we have two classes, Test class is trying to access the default method of Addition
class, since class Test belongs to a different package, this program would throw compilation
error, because the scope of default modifier is limited to the same package in which it is
declared.
Addition.java

Let us see an example to understand this:

class ABC{

private double num = 100;

private int square(int a){

return a*a;

public class Example{

public static void main(String args[]){

ABC obj = new ABC();

System.out.println(obj.num);

System.out.println(obj.square(10));

}
}

Output:

Compile - time error

3. Protected Access Modifier

Protected data member and method are only accessible by the classes of the same package and
the subclasses present in any package. You can also say that the protected access modifier is
similar to default access modifier with one exception that it has visibility in sub classes.
Classes cannot be declared protected. This access modifier is generally used in a parent child
relationship.

Protected access modifier example in Java

In this example the class Test which is present in another package is able to call
the addTwoNumbers() method, which is declared protected. This is because the Test class
extends class Addition and the protected modifier allows the access of protected members in
subclasses (in any packages).
Addition.java

package abcpackage;

public class Addition {

protected int addTwoNumbers(int a, int b){

return a+b;

subclassing an imported Class

Test.java

package xyzpackage;
import abcpackage.*;

class Test extends Addition{

public static void main(String args[]){

Test obj = new Test();

System.out.println(obj.addTwoNumbers(11, 22));

Output:

33

Note:

Subclass in another package cannot access protected access member of super class

Example:

package package1;

public class Class1 {


public void tryMePublic() {
}

protected void tryMeProtected() {


}
}

package package2;

import package1.Class1;

public class Class2 extends Class1 {


doNow() {
Class1 c = new Class1();
c.tryMeProtected(); // ERROR: tryMeProtected() has protected access in Class1
tryMeProtected(); // No error
}
}

4. Public access modifier

The members, methods and classes that are declared public can be accessed from anywhere. This
modifier doesn’t put any restriction on the access.

public access modifier example in java

Let’s take the same example that we have seen above but this time the method
addTwoNumbers() has public modifier and class Test is able to access this method without even
extending the Addition class. This is because public modifier has visibility everywhere
Addition.java

package abcpackage;

public class Addition {

public int addTwoNumbers(int a, int b){

return a+b;

Test.java

package xyzpackage;

import abcpackage.*;

class Test{

public static void main(String args[]){

Addition obj = new Addition();


System.out.println(obj.addTwoNumbers(100, 1));

Output:

101

Importing classes from other packages

We can also import classes from other packages


For example
package abcpackage;

public class Sub {

public int subTwoNumbers(int a, int b){

return a-b;

Test.java

package xyzpackage;

import abcpackage.Sub;

class Test{

public static void main(String args[]){

Sub obj = new Sub();


System.out.println(obj.addTwoNumbers(100, 1));

Output:

99

Benefits of packages:
1. The classes in the packages can be easily reused.
2. In the packages, classes are unique compared with other classes in the other packages.
3. Packages provide a way to hide classes thus prevent classes from accessing by other programs.
4. Packages also provide way to separate "design" from "coding".

PATH and CLASSPATH

This section explains how to use the PATH and CLASSPATH environment variables on Microsoft
Windows, Solaris, and Linux. Consult the installation instructions included with your installation
of the Java Development Kit (JDK) software bundle for current information.

After installing the software, the Java folder has been created in particular path we have selected
in that we have JDK directory will have the structure shown below.

JDK version

lib bin …….

he bin directory contains both the compiler and the launcher.


Update the PATH Environment Variable (Microsoft Windows)

You can run Java applications just fine without setting the PATH environment variable. Or, you
can optionally set it as a convenience.

Set the PATH environment variable if you want to be able to conveniently run the executables
(javac.exe, java.exe, javadoc.exe, and so on) from any directory without having to type the
full path of the command. If you do not set the PATH variable, you need to specify the full path to
the executable every time you run it, such as:

C:\Java\jdk1.7.0\bin\javac MyClass.java

The PATH environment variable is a series of directories separated by semicolons ( ;). Microsoft
Windows looks for programs in the PATH directories in order, from left to right. You should have
only one bin directory for the JDK in the path at a time (those following the first are ignored), so
if one is already present, you can update that particular entry.

The following is an example of a PATH environment variable:

C:\Java\jdk1.7.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem

It is useful to set the PATH environment variable permanently so it will persist after rebooting. To
make a permanent change to the PATH variable, use the System icon in the Control Panel. The
precise procedure varies depending on the version of Windows:

Windows XP

1. Select Start, select Control Panel. double click System, and select the Advanced tab.
2. Click Environment Variables. In the section System Variables, find
the PATH environment variable and select it. Click Edit. If the PATH environment
variable does not exist, click New.
3. In the Edit System Variable (or New System Variable) window, specify the value of
the PATH environment variable. Click OK. Close all remaining windows by
clicking OK.

Windows Vista:

1. From the desktop, right click the My Computer icon.


2. Choose Properties from the context menu.
3. Click the Advanced tab (Advanced system settings link in Vista).
4. Click Environment Variables. In the section System Variables, find
the PATH environment variable and select it. Click Edit. If the PATH environment
variable does not exist, click New.
5. In the Edit System Variable (or New System Variable) window, specify the value of
the PATH environment variable. Click OK. Close all remaining windows by
clicking OK.
Windows 7:

1. From the desktop, right click the Computer icon.


2. Choose Properties from the context menu.
3. Click the Advanced system settings link.
4. Click Environment Variables. In the section System Variables, find
the PATH environment variable and select it. Click Edit. If the PATH environment
variable does not exist, click New.
5. In the Edit System Variable (or New System Variable) window, specify the value of
the PATH environment variable. Click OK. Close all remaining windows by
clicking OK.

Note: You may see a PATH environment variable similar to the following when editing it from
the Control Panel:

%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\
Wbem
Variables enclosed in percentage signs (%) are existing environment variables. If one of these
variables is listed in the Environment Variables window from the Control Panel (such
as JAVA_HOME), then you can edit its value. If it does not appear, then it is a special
environment variable that the operating system has defined. For example, SystemRoot is the
location of the Microsoft Windows system folder. To obtain the value of a environment variable,
enter the following at a command prompt. (This example obtains the value of
the SystemRoot environment variable):

echo %SystemRoot%
Update the PATH Variable (Solaris and Linux)

You can run the JDK just fine without setting the PATH variable, or you can optionally set it as a
convenience. However, you should set the path variable if you want to be able to run the
executables (javac, java, javadoc, and so on) from any directory without having to type the full
path of the command. If you do not set the PATH variable, you need to specify the full path to
the executable every time you run it, such as:

% /usr/local/jdk1.7.0/bin/javac MyClass.java

To find out if the path is properly set, execute:

% java -version

This will print the version of the java tool, if it can find it. If the version is old or you get the
error java: Command not found, then the path is not properly set.
To set the path permanently, set the path in your startup file.

For C shell (csh), edit the startup file (~/.cshrc):

set path=(/usr/local/jdk1.7.0/bin $path)

For bash, edit the startup file (~/.bashrc):

PATH=/usr/local/jdk1.7.0/bin:$PATH
export PATH

For ksh, the startup file is named by the environment variable, ENV. To set the path:

PATH=/usr/local/jdk1.7.0/bin:$PATH
export PATH

For sh, edit the profile file (~/.profile):

PATH=/usr/local/jdk1.7.0/bin:$PATH
export PATH

Then load the startup file and verify that the path is set by repeating the java command:

For C shell (csh):

% source ~/.cshrc
% java -version

For ksh, bash, or sh:

% . /.profile
% java -version
Checking the CLASSPATH variable (All platforms)

The CLASSPATH variable is one way to tell applications, including the JDK tools, where to
look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be
defined through other means, such as the bootstrap class path or the extensions directory.)

The preferred way to specify the class path is by using the -cp command line switch. This allows
the CLASSPATH to be set individually for each application without affecting other
applications. Setting the CLASSPATH can be tricky and should be performed with care.

The default value of the class path is ".", meaning that only the current directory is searched.
Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.
To check whether CLASSPATH is set on Microsoft Windows NT/2000/XP, execute the
following:

C:> echo %CLASSPATH%

On Solaris or Linux, execute the following:

% echo $CLASSPATH

If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or
Linux) or simply %CLASSPATH% (Microsoft Windows NT/2000/XP).

To modify the CLASSPATH, use the same procedure you used for the PATH variable.

Difference between PATH and CLASSPATH

The common difference between path and classpath is:

1)The main difference between PATH and CLASSPATH is that PATH is an environment
variable which is used to locate "java" or "javac" command used to run java program and
compile java source file. On the other hand, CLASSPATH, an environment variable is used by
System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class
file.

2) In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH
environment variable while in order to set CLASSPATH in Java you need to include all those
directories where you have put either your .class file or JAR file which is required by your Java
application.

3) Another significant difference between PATH and CLASSPATH is that PATH cannot be
overridden by any Java settings but CLASSPATH can be overridden by providing command line
option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in
Manifest file inside JAR archive.

4) PATH environment variable is used by operating system to find any binary or command typed
in the shell, this is true for both Windows and Linux environment while CLASSPATH is only
used by Java ClassLoaders to load class files.

Important Packages

1. java.lang: This package got primary classes and interfaces essential for Java program. It
consists of wrapper classes (wrapper classes can be used to convert ordinary data into objects),
Strings, Threads etc.
2. java.util: This package contains useful classes and interfaces like Stack, LinkedList, Arrays,
ArrayList, List etc...

3. java.io: This package handles files and input output related tasks.

4. java.awt: This package helps to develop GUI. It consists of an important sub package
java.awt.event to handle the events for GUI elements.

5. java.swing: This package helps to develop GUI like java.awt. In fact, this an extension to
java.awt.

6. java.net: Client - server programming can be done using this package. This is a very
important package to develop any web program like web browse or web server.

7. java.applet: Applets are programs which come from a server into a client and get executed on
the client machine. This package was very well known to everyone before the development of
servlets.

8. java.sql: This package helps us to connect to database like Oracle.


9.Java.security:This package has several classes that allow users to encrypt a file using a user-
provided key. It supports a cryptograph and digital signature

Summary
------------------------
In this unit we learnt about java packages, packages are directories which store classes and
interfaces. Programmers create their own packages and store their classes and interfaces

Review Questions
-------------------------
1. What is the importance of CLASSPATH.
2. What is a package? How to define it and access it?
3. Explain the various access specifiers of packages used in java.
4. Expalin user defined packages
5. Explain pre defined packages
Programs related to the topics discussed in this unit>>>>>
---------------------------------------------------------------------------
1. With sample program explain the creation of packages. Accessing a package and hiding
classes with packages.
Multiple Choice Questions
-----------------------------------
1. Which of the following is/are true about

packages in Java?

1) Every class is part of some package.

2) All classes in a file are part of the same

package.

3) If no package is specified, the classes in the

file

go into a special unnamed package

4) If no package is specified, a new package is

created with folder name of class and the

class is put in this package.

a)only 1,2,and 3

b)only 1,2,and 4

c)only 4

d)only 1 and 3

2.Which of the following is/are advantages

of packages?

a)Packages avoid name clashes

b)Classes, even though they are visible


outside their package, can have fields

visible to packages only

c)We can have hidden classes that are

used by the packages, but not visible

outside.

d)All of the above

3. File class is included in which package?

(a) java.io package (b) java.lang package

(c) java.awt package (d) java.net.package

4. A package is a collection of

(a) Classes (b) Interfaces (c) Editing tools

(d) Classes and interfaces

5.) A method within a class is only

accessible by classes that are defined

within the same package as the class of the

method. Which one of the following is used

to enforce such restriction?

(a) Declare the method with the keyword

public

(b) Declare the method with the keyword

private

(c) Declare the method with the keyword


protected

(d) Do not declare the method with any

accessibility modifiers

6. Basic Java language functions are stored

in which of the following java package?

(a) java.lang (b) java.io (c) java.net (d)

java.util (e) java.awt

7. Which of these keywords is used to

define packages in Java?

a) pkg

b) Pkg

c) package

d) Package

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?

a) Public

b) Protected

c) No Modifier

d) All of the mentioned

9. 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.*

10. Which of the following package stores

all the standard java classes?

a) lang

b) java

c) util

d) java.packages

11.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);

a) xello

b) xxxxx

c) Hxllo

d) Hexlo

12.Path is an environment variable which is

used by to find the executables.

a)linux

b)operating system

c)unix

d)none

13.Classpath is an environment variable

which is used by the to find


the path, of classes.

a)linux

b)java compiler

c)operating system

d)none

14.The value of classpath or -cp

command-line option, which overrides

a) the default value

b) the CLASSPATH value.

c)both a and b

d)only b

15.what are the different types of packages?

a)Built-in Package

b)User-defined-package

c)both a and b

d)only a

16.which keyword is used to import built-in

and user-defined packages?

a)this

b)static

c)import

d)final

17.Classpath is a parameter in the

a)Java Virtual Machine


b)Java compiler

c)a or b

d)a and b

18.how many ways to set the path in Java?

a)2

b)3

c)1

d)4

19.what are the Advantage of Java

Packages?

a) Java package is used to categorize the

classes and interfaces so that they can be

easily maintained.

b) Java package provides access

protection.

c) Java package removes naming collision.

d)all the above

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

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

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

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

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

a) HelloGoodWorld

b) HellGoodoWorld

c) Compilation error

d) Runtime error

25. Which of these packages contain

classes and interfaces used for input &

output operations of a program?

a) java.util

b) java.lang

c) java.io

d) All of the mentioned

26.Which of these class is not a member


class of java.io package?

a) String

b) StringReader

c) Writer

d) File

27. Which of these interface is not a

member of java.io package?

a) DataInput

b) ObjectInput

c) ObjectFilter

d) FileFilter

28.what are the different Overriding

methods from different packages in Java?

a)Private method overriding

b) Public method overriding

c) Default method overriding

d)all the above

29.By convention, java packages begin

with ................... letters while all the class

names begin with an ..................... letter.

A) uppercase, uppercase

B) lowercase, lowercase
C) uppercase, lowercase

D) lowercase, uppercase

30.Which of the following classes are

included in java.awt package.

i) Font ii) Frame

iii) Float iv) File

A) i and ii only

B) ii and iii only

C) iii and iv only

D) i and iv only

31.The Date class includes within

....................... package.

A) java.io

B) java.awt

C) java.net

D) java.util

32.. Package P1 contains the following

code

package P1;

public class student {Body of Student}

Class Test {Body of Test}

Now consider the following code;


import P1.*;

Class Result{

student S1;

Test t1;

This code compile because

A) Class result should be declared public.

B) Student class is not available

C) Test class is not available

D) Result body is not fully defined

33. State whether the following statements

about the advantages of organizing

classes into packages are True or False.

i) Two classes in two different packages

can not have the same name.

ii) The classes contained in the packages

of other programs can be easily reused.

A) True, False

B) False, True

C) True, True

D) False, False

34.Which keyword can protect a class in a


package from accessibility by the classes

outside the package.

i) private ii) protected

iii) final

A) i only

B) ii only

C) iii only

D) None of the above

35.Which of the following is a member of

the java.lang package?

(a) List (b) Queue (c) Math (d) Stack (e)

Process.

36.File class is included in which package?

(a) java.io package (b) java.lang package

(c) java.awt package (d) java.net.package

(e) java.util.package.

37.If a variable is declared as private , then

it can be used in _______

A. Any class of any package


B. Any class of same package

C. Only in the same class

D. Only subclass in that package

38.A package is container of _____

A. Methods

B. Objects

C. Classes

D. Variables

39.Wrapper classes are found in _____

package

A. java.lang

B. java.util

C. java.io

D. java.net

40.The random class is in ........package

A.java.io
B.java.util

C.java.lang

D.java.applet

answers:

1)a 11)c 21)b 31)d

2)d 12)b 22)a 32)c

3)a 13)b 23)d 33)b

4)d 14)c 24)d 34)d

5)d 15)c 25)c 35)b

6)a 16)c 26)a 36)a

7)c 17)c 27)c 37)c

8)d 18)a 28)d 38)c

9)c1 9)d 29)d 39)a

10)b 20)d 30)a 40)b

You might also like