Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views7 pages

Chapter 2-1

Uploaded by

Yash Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Chapter 2-1

Uploaded by

Yash Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 2

Define Constructor. List its types.


OR
Explain the types of constructors in Java with suitable example
Ans.
Constructor:
1. Constructors are used to initialize an object as soon as it is created.
2. Every time an object is created using the ‘new’ keyword, a constructor is invoked.
3. If no constructor is defined in a class, java compiler creates a default constructor.
4. Constructors are similar to methods but with to differences, constructor has the same
name as that of the class and it does not return any value.
5. The types of constructors are:
1. Default constructor
2. Constructor with no arguments
3. Parameterized constructor
4. Copy constructor
1. Default constructor:
Java automatically creates default constructor if there is no default or parameterized
constructor written by user.
Default constructor in Java initializes member data variable to default values (numeric values
are initialized as 0, Boolean is initialized as false and references are initialized as null).
2.Constructor with no arguments:
Such constructors does not have any parameters.
All the objects created using this type of constructors has the same values for its data
members.
3. Parametrized constructor:
Such constructor consists of parameters.
Such constructors can be used to create different objects with data members having
different values.
4.Copy Constructor :
A copy constructor is a constructor that creates a new object using an existing object of the
same class and initializes each instance variable of newly created object with corresponding
instance variables of the existing object passed as argument.
This constructor takes a single argument whose type is that of the class containing the
constructor.
2.Explain the command line arguments with suitable example.
Ans.
Java Command Line Argument:
1. The java command-line argument is an argument i.e. passed at the time of running
the java program.
2. The arguments passed from the console can be received in the java program and it
can be used as an input.
3. So, it provides a convenient way to check the behaviour of the program for the
different values.
4. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
5. Command Line Arguments can be used to specify configuration information while
launching your application.
6. There is no restriction on the number of java command line arguments.
7. You can specify any number of arguments Information is passed as Strings.
8. They are captured into the String args of your main method
Simple example of command-line argument in java In this example, we are receiving only
one argument and printing it. To run this java program, you must pass at least one argument
from the command prompt.
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}

3.Explain the four access specifiers in Java.


Ans.
There are 4 types of java access modifiers:
1. private
2. default
3. Protected
4. public
1.private access modifier:
The private access modifier is accessible only within class.
2.default access specifier:
If you don’t specify any access control specifier, it is default, i.e. it becomes implicit public
and it is accessible within the program.
3.protected access specifier:
The protected access specifier is accessible within package and outside the package but
through inheritance only.
4.public access specifier:
The public access specifier is accessible everywhere. It has the widest scope among all
other modifiers.

4.State use of finalize( ) method with its syntax.


Ans.
Use of finalize( ):
1. Sometimes an object will need to perform some action when it is destroyed.
2. 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.
3. To handle such situations java provide a mechanism called finalization.
4. In finalization, specific actions that are to be done when an object is garbage
collected can be defined.
5. To add finalizer to a class define the finalize() method.
6. The java run-time calls this method whenever it is about to recycle an object.
7. Syntax:
protected void finalize()
{
}

5.Describe the use of any methods of vector class with their syntax.
Ans.
1.boolean add(Object obj):
Appends the specified element to the end of this Vector
2.Boolean add(int index,Object obj):
Inserts the specified element at the specified position in this Vector.
3.void addElement(Object obj)
Adds the specified component to the end of this vector, increasing its size by one.
4.int capacity()
Returns the current capacity of this vector.
5.void clear()
Removes all of the elements from this vector.
6.boolean contains(Object element):
Tests if the specified object is a component in this vector.
7.void copyInto(Object[] anArray):
Copies the components of this vector into the specified array.
8.Object firstElement():
Returns the first component (the item at index 0) of this vector.
9.Object elementAt(int index):
Returns the component at the specified index.
10.int indexOf(Object elem):
Searches for the first occurence of the given argument, testing for equality using the equals
method.
11.Object lastElement():
Returns the last component of the vector.
12.Object insertElementAt(Object obj,int index):
Inserts the specified object as a component in this vector at the specified index.
13.Object remove(int index):
Removes the element at the specified position in this vector.
14.void removeAllElements():
R6emoves all components from this vector and sets its size to zero.

6.Name the wrapper class methods for the following:


1.To convert string objects to primitive int.
2.To convert primitive int to string objects.
Ans.
To convert string objects to primitive int:
String str=”5”;
int value = Integer.parseInt(str);
To convert primitive int to string objects:
int value=5;
String str=Integer.toString(value);
7.Define array. List its types.
Ans.
An array is a homogeneous data type where it can hold only objects of one data type.
Types of Array:
1.One-Dimensional
2.Two-Dimensional

8.Differentiate between array and vector.


Ans.

Sr.no. Array Vector

1. An array is a structure that holds The Vector is similar to array holds multiple
multiple values of the same type. objects and like an array; it contains
components that can be accessed using an
integer index

2. An array is a homogeneous data type Vectors are heterogeneous. You can have
where it can hold only objects of one objects of different data types inside a Vector
data type.
3. After creation, an array is a fixed-length The size of a Vector can grow or shrink as
structure needed to accommodate adding and removing
items after the Vector has been created.

4. Array can store primitive type data Vector are store nonprimitive type data
element. element.
5. Declaration of an array : int arr[] = new Declaration of Vector:
int [10]; Vector list = new Vector(3)
6. Array is the static memory allocation. Vector is the dynamic memory allocation.

9.List any four methods of string class and state the use of each
Ans.
1. The java.lang.String class provides a lot of methods to work on string.
2. By the help of these methods, We can perform operations on string such as
trimming, concatenating, converting, comparing, replacing strings etc.
1. to Lowercase ():
Converts all of the characters in this String to lower case.
Syntax:
s1.toLowerCase()
Example:
String s="Java";
System.out.println(s.toLowerCase());
Output:
java

2.to Uppercase():
Converts all of the characters in this String to upper case
Syntax:
s1.toUpperCase()
Example:
String s=Java";
System.out.println(s.toUpperCase());
Output: JAVA3) trim (): Returns a copy of the string, with leading and trailing
whitespace omitted. Syntax: s1.trim() Example: String s=" Sachin ";
System.out.println(s.trim()); Output:Sachin 4) replace ():Returns a new string resulting
from replacing all occurrences of old Char in this string with new Char. Syntax:
s1.replace(‘x’,’y’) Example: String s1="Java is a programming language. Java is a
platform."; String s2=s1.replace("Java","Kava"); //replaces all occurrences of "Java" to
"Kava" System.out.println(s2); Output: Kava is a programming language. Kava is a
platform.
Differentiate between String and String Buffer.
Ans.

Sr.no String String Buffer

1. String is a major class String Buffer is a peer class of


String
2. Length is fixed (immutable) Length is flexible (mutable)

3. Contents of object cannot be modified Contents of object can be modified

4. Object can be created by assigning Objects can be created by calling


String constants enclosed in double constructor of String Buffer class
quotes. using “new”

5. Ex: String s=”abc”; Ex:


StringBuffer s=new StringBuffer
(“MSBTE”)

You might also like