You are on page 1of 11

Certificate

Assignment No.: 01 Course


Code: CAP 310

Course Instructor: Ms. Anupama Course Tutor: N.A.

DOA: 20-01-11 DOS: 02-02-11


Student’s Roll No: RD3912A18 Section No.:
D3912

Declaration:

I declare that this assignment is my individual work. I have not


copied from any other student’s work or from any other source
except where due to acknowledgement is made explicitly in the
text, nor has any part been written for me by another person.

Student’s Signature:
Amar singh

Evaluator’s Comments:

________________________________________________________________
Marks Obtained: ____ out
of______________________________________

PART A

Q1. What is relation between Class and Object. What is the size of object for
an class. How can we use Instance Variable? Explain with suitable example.

Ans. OBJECT :-An Object on the other hand has a limited lifespan.
 Objects are created and eventually destroyed.
 Also during that lifetime, the attributes of the object may undergo significant
change
 Objects are members of a class.
 Object can't define classes
 Object properties can access by class instance on which that object is
residing
 Should be belonging to any of the class
 Object - Can created and destroyed as your necessity.
CLASS:-

 A Class is static.
 All of the attributes of a class are fixed before, during, and after the
execution of a program.
 The attributes of a class don't change.
 The class to which an object belongs is also (usually) static.
 If a particular object belongs to a certain class at the time that it is created
then it almost certainly will still belong to that class right up until the time
that it is destroyed.
 A Class is a blueprint of an object or we can say that its a
template of an object.
 A class describes all the attributes of object.
 Classes(s) defines object
 Class-One class can define any no of Object

For example,

Big Class requires 4 more bytes than Small Class because of the s2 String.
A reference is 4 bytes on my Linux 32-bit box running Java 5.

If you run a "size of" on Small Class and Big Class you might see that they are the
same size, but that's because the VM has 8 byte alignment.

Add another reference to BigClass (String s3, for example) and you'll see the
size jump 8 bytes.

small class

private void nonworking() {


// no-op
big class

Private String s1;

}
}

private String s1;


private String s2;

private void working()

{
// a bunch of bytecode that eats up the perm gen heap
int i = 10;
for (int j =0 ; j < i; j++)

{
// a group of code
}
}
}

Q 2. What is meant by garbage collection? How is it done in Java?

ANs. Since objects are dynamically allocated by using the new operator. In some
languages, such as C++, dynamically allocated objects must be manually released
by use of a delete operator. Java takes a different approach; it handles deallocation
for user automatically. The technique that accomplishes this is called garbage
collection.
It works like this:
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. There is no
explicit need to destroy objects as in C++. Garbage collection only occurs
sporadically (if at all) during the execution of user program.
 It will not occur simply because one or more objects exist that are no longer
used. Furthermore, different Java run-time implementations will take
varying approaches to garbage collection, but for the most part, user should
not have to think about it while writing user programs.
The finalize( ) Method
Sometimes an object will need to perform some action when it is destroyed.
For example, if an object is holding some non-Java resource such as a file
handle or window character font, then user might want to make sure these
resources are freed before an object is destroyed. To handle such situations,
Java provides a mechanism called finalization.
 By using finalization, user can define specific actions that will occur when
an object is just about to be reclaimed by the garbage collector.
To add a finalizer to a class, user simply define the finalize( ) method. The Java
run time calls that method whenever it is about to recycle an object of that class.
Inside the finalize( ) method user will specify those actions that must be
performed before an object is destroyed.
 The garbage collector runs periodically, checking for objects that are no
longer referenced by any running state or indirectly through other referenced
objects. Right before an asset is freed, the Java run time calls
the finalize() method on the object. The finalize( ) method has this general form:

Protected void finalize( )


{
// finalization code here
}
Here, the keyword protected is a specifier that prevents access to finalize( ) by
code defined outside its class. It is important to understand that finalize( ) is
only called just prior to garbage collection.
Q 3. What is the significance of StringBuffer class? How it is differ from
String Class. Create an array of string class and implement both string &
stringBuffer to that array.

Ans. StringBuffer is a peer class of String that provides much of the


functionality of the strings. String represents fixed-length, immutable
character sequences.

 In contrast StringBuffer represents growable and writable


character sequences.
 The StringBuffer provides 3 constructors which create, initialize
and set the initial capacity of StringBuffer objects.
 This class provides many methods.
 For example
the length() method gives the current length i.e. how
many characters are there in the string, while the total allocated
capacity can be found by the capacity() method.
public class StringBufferDemo {

public static void main(String[] args) {

StringBuffer sb =new StringBuffer("Hello");


System.out.println("buffer= " +sb);
System.out.println("length= " +sb.length());
System.out.println("capacity= " +sb.capacity());

//appending and inserting into StringBuffer.


String s;
int a = 42;
StringBuffer sb1= new StringBuffer(40);
s= sb1.append("a=").append(a).append("!").toString();
System.out.println(s);
StringBuffer sb2 = new StringBuffer("WELCOME TO ");
sb2.insert(2, "JAVA!");
System.out.println(sb2);
}

}
Output
buffer= Hello
length= 5
capacity= 24
a=42!
WELCOME TO JAVA!

Q4. What is the difference between the Boolean & operator and the &&
operator?

Ans:….

1. The bitwise AND operator ( & )

(Boolean expression1) & (Boolean expression2)

to evaluate the above expression, Java first evaluates both Boolean expression1
and Boolean expression2 hence only if both Boolean expression1 and Boolean
expression2 evaluate to true, the whole expression evaluates to true.

2. The conditional AND operator ( && )

( Boolean expression1 ) && ( Boolean expression2 )

Here Java first evaluates Boolean expression1, only if it evaluates to true, Boolean
expression2 is evaluated. Hence Boolean expression2 is not evaluated if Boolean
expression1 evaluates to false.

The conditional AND operator, sometimes called the short-circuit operator is more
efficient that the bitwise AND operator. As it saves the processing of expression2
by first evaluating expression1 and ascertaining that the final result will be false.

PART B
Q 5 Write a java program to multiply 2*2 array.

Ans. Import java.io.*;

class MatrixMultiply{
public static void main(String[] args) {
int array[][] = {{5,6,7},{4,8,9}};
int array1[][] = {{6,4},{5,7},{1,1}};
int array2[][] = new int[3][3];
int x= array.length;
System.out.println("Matrix 1 : ");
for(int i = 0; i < x; i++) {
for(int j = 0; j <= x; j++) {
System.out.print(" "+ array[i][j]);
}
System.out.println();
}
int y= array1.length;
System.out.println("Matrix 2 : ");
for(int i = 0; i < y; i++) {
for(int j = 0; j < y-1; j++) {
System.out.print(" "+array1[i][j]);
}
System.out.println();
}

for(int i = 0; i < x; i++) {


for(int j = 0; j < y-1; j++)

{
for(int k = 0; k < y; k++)

{
array2[i][j] += array[i][k]*array1[k][j];
}
}
}
System.out.println("Multiply of both matrix : ");
for(int i = 0; i < x; i++)

{
for(int j = 0; j < y-1; j++)

{
System.out.print(" "+array2[i][j]);
}
System.out.println();
}
}
}

Q 6 Can we initialize parent class instance variable through child class


constructor. How? Give an example.

Ans. We usually put code to initialize an instance variable in a constructor.


There are two alternatives to using a constructor to initialize instance
variables: initialize blocks and final methods.

The superclass (parent) constructor


object has the fields of its own class plus all fields of its parent class,
grandparent class, all the way up to the root class Object. It's necessary to
initialize all fields, therefore all constructors must be called! The Java
compiler automatically inserts the necessary constructor calls in the
process of constructor chaining, or you can do it explicitly.
The Java compiler inserts a call to the parent constructor (super) if you
don't have a constructor call as the first statement of you constructor.
public Point(int x, int y) {
super(); // Automatically done if you don't call constructor here.
m_x = x;
m_y = y;
}

For example

package info.sumudu.totalBeginner;

public class Parent {

private int x;
private int y;

public Parent(int x, int y)


{
this.x = x;
this.y =y;
}

package info.sumudu.totalBeginner;

public class Child extends Parent {


private String name;
private int birthYear;
public Child(String name,int year)
{
this.name = name;
this.birthYear = year;
}

public static void main(String ds[])


{
Child baby = new Child("Sumudu",1980);
System.out.println("child name is" + baby.name + "Born in " +
baby.birthYear);
}
}

Q 7. What is meant by final class, method, and variable.

Ans. Final as name suggest its the final value i.e its final nothing to be done over it
if say for variable-then we cant change its value. If for method-then we can’t
override or overload it .Similar to it is-Finalize method-which is used as a
must block to be invoked before program termination. Either their is any
error or program. terminates abnormally if finalize method is their then be
sure that without doing work of finalize the program. Can’t terminate. So
finalize method is used to free the resources so that whatever happen to the
program. execution resources must be freed before termination.

• when user declare a class as final, that class cannot be extended. In java
"String" class is final. final class is the one which can't be sub classed. we
can't inherit that class.

• when user declare the variable as final then we can Initialize it only once.
the one which acts like a constant. we can't change the value of that var.

• final method: when declare the method as final we cannot override the
method.

You might also like