Core Java Interview Questions
Core Java Interview Questions
1. Overloading :
2. Overriding :-
3. interface :-
// Jayshree
interface EmployeeDAO
{
void saveEmployee(Employee employee);
boolean deleteEmployee(int eid);
// Madan
class EmployeeDAOImpl implements EmployeeDAO
{
void saveEmployee(Employee employee) { }
boolean deleteEmployee(int eid) { }
}
// classes ,
interface Session
{
void save(Object o);
boolean save(Object o);
load(-,-)
get(-,-)
}
4. Abstract class :-
5. Encapulsation :-
6. Inheritance :-
7. static
8. final
SessionFactory factory;
factory.openSession();
class Question
{
int qno ;
String questionText ;
String[] options;
String originalAnswer;
}
Answer :-
Variable is a container where we store some value .
e.g. int a=10;
[ 10 ] a
void m1()
{
int c=10;
System.out.println(a + " " + b); // a &
b are global variables
System.out.println(c); // this will
give error as c is local variable of method m1()
}
void m2()
{
System.out.println(a + " " + b); //
a & b are global variables
System.out.println(c); // this will
give error as c is local variable of method m1()
}
}
class Test
{
System.out.println("Hello World");
}
Question :- what is jar file ? How to use class from any external
jar file ?
Answer :- class name & interface name should begin with upper case
and every new word in it should begin with upper case .
e.g. class DataInputStream , class ObjectOutputStream ,
interface HttpSession
variable name & method name should begin with lower case
and every new word in it should begin with upper case .
e.g. int myAge ; // primitive variable
ArrayList arrayList ; // reference variable
void setMyAge(int a);
int getMyAge();
Answer :-
class A
{
public static void main(String[] a)
System.out.println(Arrays.toString(a));
}
}
class B
{
public static void main(String[] a)
{
String[] b = {"Java" , "By" ,
"Kiran"};
A.main(b);
}
}
Answer :-
public static void main(String[] a)
// some statements
Question :- what is difference between for each loop and for loop ?
Answer :- for each loop is used generally to iterate over array and
collection . for loop is general loop which can be used anywhere.
int[] a = {10,20,30,'a'};
for(int element : a )
{
System.out.println(element);
}
e.g. while(true) { }
while(10>0) { }
such loops are used when we are not sure how many iteration
loop will have and we want user to take control of
when to stop loop iteration .
eno = e;
salary = s;
}
// 2 types variables:-
// 1. Primitive variable :- it stores value
//
int a=10;
// 2. Reference Variable :- It stores address
// e1 & e2 are reference
variables because address is stored into them
Answer :-
constructor is a special method whose name is similar to
class name and it is used to initilize instance variables
constructor method does not have any return type
constructor constructs object
If no constructor is defined by programmer , compiler adds
default constructor
class String
{
/* default constructor will create String class
object with empty content */
String()
{
}
String(String s) { }
String(Char[] c) { }
Answer :- Class.forName("com.mysql.jdbc.Driver")
Answer :- as we can call methods from this class using any java
object
Answer :- false , we can call parent class methods only using parent
class reference .
Answer :- No
Answer :- one class can extends ONLY ONE class . One class can have
only one Parent class .
class A {} class B {}
class C extends A , B . Not possible . So let's declare
class B as interface B
interface B { }
class C extends A implements B
Answer :- yes . private methods are not inherited into child class .
Answer:- In place parent type return type , we can child type return
type in overriding , it is called co-varient return type .
class A
{
Object m1() { }
}
class B extends A
{
String m1() { }
}
void m1(int... a)
m1(10,20);
m1(10,20,30);
m1();
interface EmployeeDAO
{
void addEmployee(Employee employee);
Employee getEmployee(int empId);
Question :- can we use any other access modifier than public while
defining interface methods in a implementation class .
Answer:- Not possible as interface methods are public and if we use
any other access modifier then it will lower visibility of these
methods.
class School
{
Room room = new Room(); // here object is created .
It is Composition
}
class School
{
Teacher teacher; // here object is declared not
created . It is aggregation
}
int size=10;
size=20;
sop(a.length);
sop(size);
Question :- what is exception handling ? is it achieved using try
and catch block or throws keyword ?
}
}
class Test
{
void m1()
{
try
{
int a =
Integer.parseInt("10");
int d =
Integer.parseInt("ten");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Question :- Difference between checked and unchecked exception ?
Answer :-
catch(NullpointerException npe |
ArithmaticException ae | IOException ioe) { }
Question :- what is try with resource statement ?
Question :- Can we write try block only , and not catch block or
finally block ?
Answer :- No .
Answer :- No.
Answer :- false .
Question :- what is use of FileInpuStream and FileOutputStream
class ?
Answer:-
interface Collection
{
int size();
boolean clear();
boolean remove(Object o)
boolean add(Object o)
boolean addAll(Collection c)
boolean removeAll(Collection c)
boolean contains(Object o)
boolean isEmpty()
Iterator iterator()
Object[] toArray();
Answer :-
List accepts duplicates , Set do not.
List has index , Set do not have index.
List provides Random Access using index
List preserve insertion order but Set do not .
Answer :- NO
Answer :-
Answer :- These classes contains some utility methods for array and
collection . e.g. To sort an array , Arrays class contains sort()
method , To sort collection, Collections class contains sort()
method .
Answer :- keySet() gives Set object which contains all keys from the
map .
Answer :- get(Object key) accept key and gives value associated with
key .
Question :- which map should be used when you want to have sorted
entries ?
Answer :- Any class which extends Number class like Integer , Float
can be used here as a type parameter .
arrayList.add(new Integer(10))
arrayList.add(new Integer(20))
arrayList.add(new String("JBK")) // compile time error
List<String> lists=Arrays.asList("10","20","30");
Stream<Integer>
stream=lists.stream().map(Integer::valueOf);// Integer.valueOf("10")
Integer.valueOf("20") Integer.valueOf("30")
Answer :-Keep external jar file in a build path and then import
class and use it
e.g. In JDBC application , we keep mysql jar file in a build path
and use
Driver class from this jar file
Answer :-
Protected members are visible inside package anywhere and outside
package they are available
inside child classes only
default members are visible inside package ONLY
int a=10;
System.out.println(i);
System.out.println(i.toString());
System.out.println(b);
int a=10;
Calendar c = Calendar.getInstance();
out.println("java is easy");
Question :- Can we have different name for public class and file
name ?
Question :- In one java file how many public classes we can write ?
Answer :- Only One public class is allowd in one java file as public
class name and java file name must be same . However we can write
any number of non-public classes in one java file . writing many
classes in one java file is not recommended approach .
int[] a1={10,20}
int[] a2={10,20}
sop(a1==a2)
sop(a1.equals(a2))