Core Concept

1. How can u create the Object of class Without using “New” opertor?
Ans :- There are 4 ways  to create object:

1.using new operator
Employee obj=new Employee();

Except this one

2.using factory methods
NumberFormat obj=NumberFormat.getNumberInstance();

3.using newInstance() method
Class c=Class.forName(“Employee”);
Employee obj=(Employee)c.newInstance();

or we can write these two lines as a single line

Employee obj = (Employee)Class.forName(“Employee”).newInstance();

4.By cloning
Employee obj1=new Employee();
Employee obj2=(Employee)obj1.clone();

Also, when the class is declared as static we can create an object for the class without instantiating or without using new operator…we can access the variables or methods in the class by using the classname

 2. What is the default initialized value of String type variable?
Ans :- Null

3.What is actual difference between statement,prepared statement and callable statement and when we have to use it?
Ans :- Statement:When you use normal statements compilation of statement or parsing of statement will happen everytime. This is time cosuming when u have mutliple queries to execute.

Statement s = conn.createStatement();
s.executeQuery();

Prepared Statement: In prepared statement SQL query will be compiled or parsed for the very first time and kept in some kind of pool. When u execute one more time the same statement with different values it saves the parsing time. – Prepared statement is precompiled statement it is used when we want one query to be executed n no. of times.

SQL :- select * from employee where employeeID=?,empName=?;
PreparedStatement ps = conn.PreparedStatement();

ps.execute(1,aru);
ps.execute(2,arya);

Callable Statement: Callable statements are used to execute stored procedures similar to java functions.

4. What will be the output of the program?

public class Test 
{
	public static void main(String args[]) 
	{
		ArrayList<String> list = new ArrayList<String>();
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");
		System.out.println("size :"+list.size());
		for(int i=0;i<list.size();i++) 
		{
			list.remove(i);
		}
		System.out.println("size after:"+list.size());
	}
}
Ans: Step 1: The array size: 4 array is list[0]=2, list[1]=3, list[2]=4, list[3]=5;

i=0;i<4;i++
it removes the 0th element, means it removes 2.
now the array is list[0]=3, list[1]=4, list[2]=5

Step 2: Now the size of the array is 3
now i=1 (bcz i++)
i<3;i++

it removes the 1st element, means it removes 4 (Bcz i=1).
now the array is list[0]=3, list[1]=5

Step 3: Now the size of the array is 2
now i=2

the condition is fail in for loop (bcz i=2. means 2<2)
So it won't go inside the loop
So the size after:2

The final Output is size :4
Size after:2

5. Difference between the == operator and equals method.
For comparing equality of string ,We Use equals()Method. There are two ways of comparison in java. One is “==” operator and another “equals()” method .  “==” compares the reference value of string object whereas equals() method is present in the java.lang.Object class. This method compares content of the string object. .

CODE:

public class EqualDemo2
{
	public static void main(String[] args) {
		String s1 = "Hello";
		String s2 = "Hello";
		String s3 = new String("Hello");
		if (s1.equals(s2))
		{
			System.out.println("TRUE");
		}
		else
		{
			System.out.println("FALSE");
		}
		if (s1 == s2)
		{
			System.out.println("TRUE");
		}
		else
		{
			System.out.println("FALSE");
		}
		if (s1.equals(s3))
		{
			System.out.println("TRUE");
		}
		else
		{
			System.out.println("FALSE");
		}
		if (s1 == s3)
		{
			System.out.println("TRUE");
		}
		else
		{
		System.out.println("FALSE");
		}
	}
}

OUTPUT :- TRUE,TRUE,TRUE,FALSE.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s