Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Declaration:
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
}
}
{
// 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
}
}
}
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:
}
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:….
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.
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.
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 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();
}
}
}
For example
package info.sumudu.totalBeginner;
private int x;
private int y;
package info.sumudu.totalBeginner;
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.