2nd-cs
2nd-cs
2. They get initialized only once in the entire life time of the program.
3. They cannot be called by the object of the class in which they are defied.
4. A static method can operate only on static variables without objects otherwise non static
variables cannot be handled by a static method without using their respective class object.
(c) Define a class and object. Write syntax to create class and
object with an example.
Ans:
Java is complete object oriented programming language. All program code and data reside in
object and class. Java classes create objects and objects will communicate between using
methods.
Class:
A „class‟ is a user defined data type.
Data and methods are encapsulated in
class.
1|Page
Object:
1. It is a basic unit of Object Oriented Programming and represents the real life entities.
2. A typical Java program creates many objects, which as you know, interact by invoking
methods.
3. An object consists of state ,behavior and identity.
4. Syntax:
5. Example:
class Student
System.out.println(s1.name);
2. Parameterized constructor
3. copy constructor
Default Constructor:
2|Page
A constructor is called "Default Constructor" when it doesn't have any parameter.
<class_name>()
Example:
class Bike1
Bike1()
System.out.println("Bike is created");
Parameterized Constructor:
1. A constructor which has a specific number of parameters is called parameterized
constructor.
2. Parameterized constructor is used to provide different values to the distinct objects.
Example:
class Student4
3|Page
int id;
String name;
Student4(int i,String n)
id = i;
name = n;
void display()
System.out.println(id+" "+name);
s1.display();
s2.display();
Copy Constructor:
A copy constructor is used for copying the values of one object to another object.
Example:
class Student6
int id;
4|Page
String name;
Student6(int i,String n)
id = i;
name = n;
Student6(Student6 s)
id = s.id;
name =s.name;
void display()
System.out.println(id+" "+name);}
s1.display();
s2.display();
Winter 2017
}
class Parrot extends Bird
{
Polymorphism:
1) It is a feature that allows one interface to be used for a general class of actions.
2) The specific action is determined by the exact nature of the situation.
3) By this concept it is possible to design a generic interface to a group of related activities.
For E.g:-
void add(int a, int b)
{
int sum = a+b;
System.out.println(sum);
}
void add(float a, float b)
{
float sum = a+b;
System.out.println(sum);
}
6|Page
(b) Write any two methods of array list class with their syntax.
Ans.
booleanadd(E e):
void clear():
Objectclone():
booleancontains(Object o):
Eget(int index):
intindexOf(Object o):
booleanisEmpty() :
intlastIndexOf(Object o):
Removes the first occurrence of the object from the list if it is present.
int size():
7|Page
(b) What is the multiple inheritance? Write a java program to
implement multiple inheritance.
Ans.:
Multiple inheritance:
It is a feature in which a class inherits characteristics and features from more than one super class
or parent class.
Java cannot have more than one super class. Therefore interface is used to support multiple
inheritance in java. Interface specifies what a class must do but not how it is done.
Eg:
interface MyInterface
int strength=60;
void method1();
class MyBaseClass
String str;
MyBaseClass(String str)
this.str = str;
8|Page
}
public void display() {
System.out.println("Class: "+str);
float total;
super(str);
total = t;
System.out.println("Avg is "+avg);
c.display();
c.method1();
9|Page
(a) Describe following string class method with example:
(i) compareTo( )
(ii) equalsIgnoreCase( )
Ans.:
(i) compareTo( ):
Syntax:
intcompareTo(Object o)
OR
intcompareTo(String anotherString)
There are two variants of this method. First method compares this String to another Object and
second method compares two strings lexicographically.
Eg.
System.out.println(result);
System.out.println(result);
(ii) equalsIgnoreCase( ):
public Boolean equalsIgnoreCase(String str)
This method compares the two given strings on the basis of content of the string irrespective of
case of the string.
10 | P a g e
Example:
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";
Example:
class Vehicle{
void run()
System.out.println("Vehicle is running");
11 | P a g e
}
void run()
obj.run();
The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version
of a superclass. Whenever a subclass needs to refer to its immediate superclass, it can do so by use
of the keyword super.
Super has two general forms. The first calls the super class constructor. The second is used to
access a member of the superclass that has been hidden by a member of a subclass.
super() is used to call base class constructer in derived class. Super is used to call overridden
method of base class or overridden data or evoked the overridden data in derived class.
12 | P a g e
E.g.: use of super()
Box()
void show()
//definition of show
BoxWeight()
13 | P a g e
{
this.width = w;
this.height = h;
this.depth =d;
this.width = width;
this.height = height;
this.depth = depth;
}
14 | P a g e
(a) What is the use of wrapper classes in Java? Explain float
wrapper with its methods.
Ans.:
Use :
Java provides several primitive data types. These include int (integer values), char (character),
double (doubles/decimal values), and byte (single-byte values). Sometimes the primitive data type
isn't enough and we may have to work with an integer object.
Wrapper class in java provides the mechanism to convert primitive into object and object into
primitive.
Methods :
1) floatValue( ) method:
It is used to return value of calling object as float.
2) isInfinite( ) method:
True if, value of calling object is infinite, otherwise false.
3) isNaN( ) method:
True if, value of calling object is not a number, otherwise false.
7) parseFloat( ) method:
15 | P a g e
It is used to return float of a number in a string in radix 10.
8) toString(float f ) method:
It is used to find the string equivalent of a calling object.
9) valueOf(String s) method:
It is used to return Float object that has value specified by str.
2. default
3. protected
4. public
16 | P a g e
Example:
class test
System.out.println("Hello java");
testobj=new test();
In this example, we have created two classes test and test1. Test class contains private data
member and private method. We are accessing these private members from outside the class, so
there is compile time error
17 | P a g e
Example :
class test
System.out.println("Hello java");
testobj=new test();
System.out.println(obj.data);
obj.show();
Example :
test.java
package mypack;
{
18 | P a g e
protected void show()
System.out.println("Hello java");
//test1.java
Import mypack.test;
test1obj=new test1();
obj.show();
Example :
test.java
package mypack;
System.out.println("Hello java");
19 | P a g e
}
//test1.java
import mypack.test;
test1obj=new test1();
obj.show();
Summer 2017
20 | P a g e
Example:
class SingleLevelInheritanceParent
int l;
SingleLevelInheritanceParent(int l)
this.l = l;
void area()
int a = l*l;
SingleLevelInheritanceChild(int l)
super(l);
void volume()
int v;
v= l*l*l;
21 | P a g e
}
void area()
int a;
a = 6*l*l;
class SingleLevelInheritance
SingleLevelInheritanceChild(2);
cube.volume();
cube.area();
(a) Explain the following methods of string class with syntax and
example:
(i) substring()
(ii)replace()
(Note: Any other example can be considered)
Ans.:
22 | P a g e
(i) substring():
Syntax:
String substring(intstartindex)
startindex specifies the index at which the substring will begin.It will returns a copy of the
substring that begins at startindex and runs to the end of the invoking string
(OR)
String substring(intstartindex,intendindex)
Here startindex specifies the beginning index,andendindex specifies the stopping point. The string
returned all the characters from the beginning index, upto, but not including,the ending index.
Example :
System.out.println(("Welcome”.substring(3)); //come
System.out.println(("Welcome”.substring(3,5));//co
(ii) replace():
This method returns a new string resulting from replacing all occurrences of oldChar in this
string with newChar.
Syntax:
Example:
23 | P a g e
To disallow a method to be overridden final can be used as a modifier at the start of declaration.
Methods written as final cannot be overridden.
e.g.
class test
System.out.println(“In superclass”);
voiddisp()
System.out.println("Illegal!");
2. Prevent inheritance:
Final can be used to even disallow the inheritance, to do this a class can be defined with final
modifier, declaring a class as final declares all its method as final
e.g.
final class test
void disp()
24 | P a g e
System.out.println(“In superclass”);
Example:
public class A
int p;
25 | P a g e
A()
p = 0;
class Test
the object.
General Form :
protected void finalize()
26 | P a g e
}
(b) What is the use of ArrayList class? State any two methods with their
use from ArrayList.
Ans.:
Use of ArrayList class:
1. ArrayList supports dynamic arrays that can grow as needed.
Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index
>size()).
booleanaddAll(Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they
are returned by the specified collection's iterator.
4. void clear():
Removes all of the elements from this list.
27 | P a g e
5. boolean contains(Object o)
Returns true if this list contains the specified element. More formally, returns true if and only if this
list contains at least one element e such that (o==null ? e==null : o.equals(e)).
6. void ensureCapacity(intminCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the
number of elements specified by the minimum capacity argument.
Throws IndexOutOfBoundsException
if the specified index is is out of range (index < 0 || index >= size()).
8. intindexOf(Object o):
Returns the index in this list of the first occurrence of the specified element, or -1 if the List does
not contain this element.
9. intlastIndexOf(Object o):
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not
contain this element.
IndexOutOfBoundsException
if the specified index is is out of range (index < 0 || index >= size()).
28 | P a g e
13. int size():
Returns the number of elements in this list.
16.void trimToSize()
Trims the capacity of this ArrayList instance to be the list's current size.
29 | P a g e
30 | P a g e
31 | P a g e