Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Java Unit-3 Assignment Answers

Uploaded by

karanwork167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Java Unit-3 Assignment Answers

Uploaded by

karanwork167
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA Unit-3 Assignment Answers

1. What is package? Explain types of package. Write steps


to create package and use the package.
Ans : A set of classes and interfaces grouped together are
known as Packages in JAVA. The name itself defines that
pack (group) of related types such as classes, sub-
packages, enumeration, annotations, and interfaces that
provide name-space management.
Types of Package :
1. Built-in Packages : The built-in packages are from the
Java API. The JAVA API is the library of pre-defined
classes available in the Java Development Environment.
Few built-in packages are below:
 Java.lang–Bundles the fundamental classes
 Java.io - Bundle of input and output function classes
 Java.awt–Bundle of abstract window toolkit classes
 Java.swing–Bundle of windows application GUI toolkit
classes
 Java.net–Bundle of network infrastructure classes
 Java.util–Bundle of collection framework classes
 Java.applet–Bundle of creating applet classes
 Java.sql -Bundle of related data processing classes

2. User-defined Packages: User-defined packages are


bundles of groups of classes or interfaces created by the
programmer for their purpose.
To Create and Use Packages :
 First create a directory within name of package.
 Create a java file in newly created directory.
 In this java file you must specify the package name with the help of
package keyword.
 Save this file with same name of public class
 Now you can use this package in your program with – import
PackageName.*

2. What is wrapper class? Explain with example.


A Wrapper class in Java is a class whose object wraps or
contains primitive data types. When we create an object to a
wrapper class, it contains a field and in this field, we can store
primitive data types. In other words, we can wrap a primitive
value into a wrapper class object.
For Example :
class Main {
public static void main(String[] args) {

// create primitive types


int a = 5;
double b = 5.65;

//converts into wrapper objects


Integer aObj = Integer.valueOf(a);
Double bObj = Double.valueOf(b);

if(aObj instanceof Integer) {


System.out.println("An object of Integer is created.");
}

if(bObj instanceof Double) {


System.out.println("An object of Double is created.");
}
}
}
3. Write a difference between String class and StringBuffer
class. And explain any five method of String class and
StringBuffer class.

No. String StringBuffer

1) The String class is immutable. The StringBuffer class is mutable.

2) String is slow and consumes more StringBuffer is fast and consumes less
memory when we concatenate too memory when we concatenate t strings.
many strings because every time it
creates new instance.

3) String class overrides the equals() StringBuffer class doesn't override the
method of Object class. So you can equals() method of Object class.
compare the contents of two strings
by equals() method.

4) String class is slower while performing StringBuffer class is faster while


concatenation operation. performing concatenation operation.

5) String class uses String constant pool. StringBuffer uses Heap memory

Five Methods of String Class :


toCharArray() Converts this string to a new character array char[]

toLowerCase() Converts a string to lower case letters String

toString() Returns the value of a String object String

toUpperCase() Converts a string to upper case letters String

trim() Removes whitespace from both ends of a string


Five Methods of StringBuffer Class :
 The initial capacity of a StringBuffer can be specified
when it is created, or it can be set later with the
ensureCapacity() method.
 The append() method is used to add characters,
strings, or other objects to the end of the buffer.
 The insert() method is used to insert characters, strings,
or other objects at a specified position in the buffer.
 The delete() method is used to remove characters from
the buffer.
 The reverse() method is used to reverse the order of
the characters in the buffer.

4. What is Exception? Explain Exception handling


mechanism.
An exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
The exceptions raised in your module are handled in a flow
starting with an Exception Handler element. In an action, you
can have more than one Exception Handler flow to handle
different types of exceptions.
Exception Handling Mechanism :
An exception can be raised by OutSystems or in your logic
at any point of your module. For typical UI requests, you can
handle the raised exceptions by:
 Adding an Exception Handler element and its logic in your

action's flow.
 Adding an On Exception action in your UI Flows.
 Let the Global Exception Handler of your module do the

work. By default, Global Exception Handler property of your


module is set to the On Exception action of the "Common"
UI Flow.
In action flows starting in Timers you can only handle the
raised exceptions by adding Exception Handler elements in
your logic, otherwise, the execution flow is interrupted and
the error is logged.
When an exception is raised, the current execution flow is
interrupted and the flow restarts in the first Exception
Handler element which handles that type of exception.

5. What is user defined exception? Explain with example.


User-defined exceptions are also referred to as custom
exceptions. The exceptions created per our use case and
thrown using the throw keyword are user-defined exceptions,
and such exceptions are derived classes of the Exception
class from the java.lang package.
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}

public class Main {


public static void main(String[] args) {
try {
throw new CustomException("This is a custom
exception.");
} catch (CustomException e) {
System.out.println(e.getMessage());
}
}
}

You might also like