JPR PIP On Chapter 2..
JPR PIP On Chapter 2..
JPR PIP On Chapter 2..
2 1 4 Compare string class and string buffer class with any 4 points
3 2 4 What is constructor? Demonstrate the use of parameterized constructor with an example COI401.2
4 2 4 What is garbage collection and finalize method in java
7 3 4 What are the access control parameters? Explain the concept with suitable example.
8 1,2 4 WAP to create a vector with seven elements as (10,30,50,20,40,10,20) remove element at 3rd
and 4th element position. Insert new element at 3rd position .display then original and
current size of vector
9 1,2 4 Define wrapper class? Give the following wrapper class method with syntax and use
A)to convert integer number into string
B) to convert string to integer number
C)to convert object number into primitive number using type value method
10 3 4 Write a program to accept a number as command line argument and print the number is
even or odd.
Marking Scheme 4m
Arrays can hold primitive data type & Vectors can hold only objects.
objects
All elements of array should be of the The objects in a vector need not have to be
same data type. i.e. it can contain only homogeneous.
homogeneous elements.
For accessing elements of an array no Vector class provides different methods for
special methods are available as it is not a
class, but derived type. accessing and managing Vector elements.
2 1 2 Compare string class and string buffer class with any 4 points
Marking Scheme 4m
String StringBuffer
Objects can be created by asigning String Objects can be created by calling constructor
constants enclosed in double quotes of StringBuffer class using ‘new’
Answer A constructor is a special member which initializes an object immediately upon creation It has the
same name as class name in which it resides and it is syntactically similar to any method • When a
constructor is not defined, java executes a default constructor which initializes all numeric
members to zero and other types to null or spaces. Once defined, constructor is automatically
called immediately after the object is created before new operator completes.
Parameterized constructor: When constructor method is defined with parameters inside it, different
value sets can be provided to different constructor with the same name.
Example:
ClassRect
{
int length, breadth;
Rect(int 1, int b) // parameterized constructor
{
length=1; breadth=b;
}
public static void main(String args[])
{
Rect r- new Rect(4.5); // constructor with parameters
Rect r1new Rect(6,7);
System.out.println("Area: "+r length*rbreadth)); // o/p Area: 20
System.out.println("Area:"+rl length r1.breadth)); //o/p Area: 42
}
}
Marking Scheme 4M
Answer Garbage collection-
The objects are dynamically allocated in java by the use of the new operator. The objects which
are no longer in use are to be destroyed and the memory should be released for later reallocation.
Java, unlike C++, handles deallocation of memory automatically. This technique is called garbage
collection. When no references to an object exist, that object is assumed to be no longer needed,
and the memory occupied by the object can be reclaimed. Garbage collection occurs sporadically
during the execution of programs. It will not occur just because one or more objects exist that are
no longer used. Different run-time implementations will take varying approaches to garbage
collection.
finalize() method:-
Sometimes an object will need to perform some action when it is destroyed. Eg. If an object
holding some non java resources such as file handle or window character font, then before the
object is garbage collected these resources should be freed. To handle such situations java
provide a mechanism called finalization. In finalization, specific actions that are to be done when
an object is garbage collected can be defined. To add finalizer to a class define the finalize()
method. The java run-time calls this method whenever it is about to recycle an object. Inside the
finalize() method, the actions that are to be performed before an object is to be destroyed, can be
defined. Before an object is freed, the java run-time calls the finalize() method on the object.
The general form of the finalize() method is:
protected void finalize()
{
//finalization code
}
Describe the following String class methods with examples
a) Length() b) Charato c) Compareto()
5 2 4
Marking Scheme 4m
a) Length():
Syntax: int length()
It is used to return length of given string in integer
Eg. String str "INDIA";
System.out.println(str); System.out.println(str.length());
ii) charAt():
Syntax: char charAt(int position)
4m
Marking Scheme
Answer 1) Object elementAt(int index)
Returns the component at the specified index.
Removes the first (lowest-indexed) occurrence of the argument from this vector.
What are the access control parameters? Explain the concept with suitable example.
7 2 4
Marking Scheme 4m
Answer Java provides a number of access modifiers to set access levels for classes, variables, methods
and constructors.
1) Default Access Modifier- No keyword:
A variable or method declared without any access control modifier is available to any other
class in the same package.
visible to all class in its package
e.g variables and methods can be declared without any modifiers,
String version=”1.5.1”;
Boolean processOrder()
{
return true;
}
Variables, methods and constructors which are declared protected in a super class can be
accessed only by the subclasses in other package or any class within the package of the
protected members' class. The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected, however methods and fields in a interface
cannot be declared protected. Protected access gives the subclass a chance to use the helper
method or variable, while preventing a nonrelated class from trying to use it.
E.g The following parent class uses protected access control, to allow its child class override
protected void show() { }
5) private protected: Variables, methods which are declared protected in a super class can be
accessed only by the subclasses in same package. It means visible to class and its subclasses.
WAP to create a vector with seven elements as (10,30,50,20,40,10,20) remove element at 3rd
and 4th element position. Insert new element at 3rd position .display then original and
8 1,2 4 current size of vector
Marking Scheme 4m
Answer
import java.util.";
public class Vector Demo
{
public static void main(String args[])
{
Vector v = new Vector();
v.addElement(new Integer(10));
v.addElement(newInteger(20));
v.addElement(newInteger(30)).
v.addElement(newInteger(10));
v.addElement(new Integer(20));
System.out println(v.size());// display original size
Marking Scheme 4m
Objects like vector cannot handle primitive data types like int, float, long char and double. Wrapper
classes are used to convert primitive data types into object types. Wrapper classes are contained
in the java.lang package. The some of the Wrapper class are:
Simple type Wrapper Class
boolean Boolean
Int Integer
Char Character
float Float
C)To convert object number into primitive number using type value method:
The method here is typeValue(), meaning that type can be the data type of number.
Write a program to accept a number as command line argument and print the number is
10 3 4 even or odd.
Marking Scheme 4m
public class oe
{
public static void main(String key[]) {
{
int x=Integer.parseInt(key[0]);
if (x%2==0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number"); }
}
}
}
class CommandLineExample
{
Marking Scheme
The this Keyword:
Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the
this keyword. this can be used inside any method to refer to the current object. That is, this is
always a reference to the object on which the method was invoked. You can use this anywhere a
reference to an object of the current class" type is permitted. To better understand what this refers
to, consider the following version of Box(): // A redundant use of this. Box(double w, double h,
double d) { this.width = w; this height = h; this.depth = d; }