OJT Core Java Interview Questions Lyst8139
OJT Core Java Interview Questions Lyst8139
8888809416
www.jbktest.com 1|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 2|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
{
int c;
c=a+b;
return c;
}
a and b are called as arguments or parameters . we can write any no of arguments . Arguments
means input to method.method accepts this input , process it and generates output .
// return type of deposit(int amount) is void means it will not return any value .
void deposit(int amount)
{
balance=balance+amount;
}
// return type of withdraw(int amount) is int means it will return int value .
int withdraw(int amount)
{ balance=balance-amount;
return balance;
}
void m1()
www.jbktest.com 3|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
{
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()
}
}
10) Can we use return keyword in a method whose return type is void ?
Answer :- yes , we can use it to end method execution , but we can not return any value .
class Test
{
System.out.println("Hello World");
www.jbktest.com 4|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
11) If method returns value of type int , what type of variable we should declare to store that
value ? is this process compulsory ?
Answer :- int type variable should be declared to store this return value .
Here we have declared answer variable of type int as add(-,-) gave 30 value which is int . It is
NOT compulsory to store return value of a method in a variable .
System.out.println(add(20,30)); // we did not save return value here , just printed it .
12) If method returns object then why do we define a reference pointing to that object ?
Answer :- for calling methods from object we define reference .
www.jbktest.com 5|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
13) If method return type is java.lang.Object then what type of object this method may return
?
Answer:-It may return any type of object . e.g. ArrayList class has method get() whose syntax
is
when we call this method , we may get Student class object if Student objects are present in
ArrayList.
when we call this method , we may get Employee class object if Employee objects are
present in ArrayList.
interface List
{
Object get(int index);
}
www.jbktest.com 6|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
System.out.println(s.length());
15) What is variable argument method ? what is difference between method with array
argument and method with variable argument ?
Answer :- Variable argument method can be called by passing 0 or any no of arguments .
However a method is defined with array argument then compulsory we need to pass
array or null value while calling that method.
16) public static void main(String... a) is it write syntax for main method ?
Answer :- Yes . variable argument is accepted here .
17) void add(int a , int... b) is it right ?
Answer :- Yes . variable argumet cab be combined with normal arguments but in that case ,
we should write variable argument at the end
18) when jVM calls main(String[] args) , does it pass String[] while calling it ?
Answer :- Yes . If programmmer has pass any arguments from arguments tab of eclipse , JVM
creates String[] using those arguments and pass it to main(String[] a) while calling it .
19) when we do not pass any program argument , what is size of String[] which is passed to
main(String[] args)
Answer :- If no arguments are passed , Still JVM creates an String[] array , but it is empty
array . It's length is 0;
20) Can we write any statement after return statement ? why ?
Answer :- No , as return will end method execution . hence statements below return will not
execute ever .
www.jbktest.com 7|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
22) why main() is declared static ? what would have happened if it was declared as non-static
method ?
Answer :- main() is declared static so that it will be called by JVM using classname and
object is not required for calling it . we generally create object inside main() , and if main()
was NON-static , we would need object to call it . So it would have been impossible to call
main() .
23) How to call main() of one class into another class ?
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);
}
}
24) can we overload main() and if done which version of it will be called ?
Answer :- Yes , we can overload main() , However JVM will main(String[] a) . Other main()
methods we need to call manually .
25) Can we run class without main() ? When java progran execution gets over ?
www.jbktest.com 8|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- No we can not run class without main() . when main() execution gets finished , we
say java program execution is over .
26) what is use of System.exit(0) ?
Answer :- It is used to terminate java program .
27) what is method recursion ? which exception may occur if we don't have controlled over
recursion ?
Answer :- method recursion means calling method inside method defination . we should
control how many times method will be called recursively otherwise StackOverflowError
occurs .
28) How to perform main() recursion ?
Answer :-
public static void main(String[] a)
{
// some statements
outer:
for(;1<10;)
{
www.jbktest.com 9|Pa ge
https://thekiranacademy.com/ Mob No. 8888809416
int[] a = {10,20,30,'a'};
for(int element : a )
{
System.out.println(element);
}
www.jbktest.com 10 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
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 .
36) when else block and else if block is executed ?
Answer :- Else if block condition is checked only if condition in if block is false . Then one
by one all else if block condition is checked until any condition evaluates to true . However if
none of the condition is evaluated to true then else block is executed .
37) can we write default case as a first case in switch statement ?
Answer :- Yes , we can write default case anywhere in switch block .
38) when default case is executed ? is it compulsory to write it ?
Answer :- default case is executed only if none of the case is executed . It is not compulsory
to have default case in switch block.
39) which types of values are possible as a case value ?
Answer :- int , char , String , enum type of values are possible as case value .
40) what is an object ? what is state and behaviour of a object ? explain with one example
object is instance of a class . object is any real time entity .
Every object contains 2 things :-
1) state of object :- value of instance variables e.g. eno=1 salary=1000
2) behaviour of an object :- methods
public class Employee
{
int eno ;
int salary ;
eno = e;
salary = s;
}
www.jbktest.com 11 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
// 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
www.jbktest.com 12 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
/**
* Constructs a new object.
*/
43) How to call non-static methods using object reference and anonymous object ?
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
www.jbktest.com 13 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 14 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
class String
{
/* default constructor will create String class object with empty content */
String()
{
}
String(String s) { }
String(Char[] c) { }
51) which variable should be declared as non-static variable and which variable should be
decared as static variable . explain with example
Answer :- variables whose value depends on object should be declared as instance variable /
object variable / non-static variable . e.g. empId and salary of employees dependens on
employee . However company name is not dependent on object . It is common to all
employee objects . hence companyName should be declared as static variable
52) what is gloabl variable and tell me types of global variables ?
www.jbktest.com 15 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- global variable is available everywhere inside a class . global variables are
declared at the start of class defination . global variables are of 2 types :- static and non-
static .
53) what is local variables ? which modifier we can apply to local variable ?
Answer :- local variable is available only within a block where it is declared . local variables
are defined inside method or any other block like try block , catch block . for local variable
ONLY final modofier is applicable.
54) Difference between local and global variables ?
Answer :- global variable is available everywhere inside a class . local variable is available
only within a block where it is declared . for global variable we can use all
modifiers but for local variable ONLY final modofier is applicable .
55) what is use of static block ? How many static block we can write in a class ? when it is
executed ?
Answer :- static block is used to initilize static variables . we can write any no of static blocks
in a class . it is executed when class is loaded into memory . it is executed even
before main() method also.
56) Which method 's call prompts execution of static block ?
Answer :- Class.forName("com.mysql.jdbc.Driver")
57) what is instance block ? what is difference between instance block and constructor ?
Answer :- Constructor is used to initilize instance variables . instance block is used to
perform some task which we want to do after initilization . Constructor accepts arguments ,
instance block can not . Instance block executes before constructor .
www.jbktest.com 16 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- address stored in final reference variable can not be changed means this reference
can not point to any other new object .
final String s = new String("jbk"); // s(1000) ==>[jbk] String class object at address
1000
s=new String("java"); // it will give error as s is final variable
61) final Employee e1 = new Employee(1,1000);
e1.salary=2000; will this code run ?
Answer :- Yes . as we are changing value present inside object and not address stored in
reference varibale e1
62) Tell me important methods from java.lang.Object class
Answer :- equals(), toString() , hashCode() , notify(),notifyAll(),wait(),clone() etc
63) why java.lang.Object class's name is Object ?
Answer :- as we can call methods from this class using any java object
64) Object getInstance()
{
return ---;
}
www.jbktest.com 17 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- If constants are declared static means all objects will have same value for that final
variable . Hence compiler replaces name of static variable , whenever it is used , with it's
value . Hence class loading of class whose static variable it is , is not required , hence
memory is saved .
class A
{
public static final int x=10;
}
class B
{
void m1()
{
System.out.println(A.x);// this statement will be replaced by
System.out.println(10) by compiler
www.jbktest.com 18 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
74) which super class constructor is called by default from child class constructor , default or
parameterized constructor ?
Answer :- by defualt , default constructor of parent class is called from child class
constructor
75) Why multiple inheritance is not possible in java using classes ? How to achieve it ?
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
76) static members are inherited into child class . true / false.
Answer:- true
77) what is polymorphism ? Types of polymorphism ?
Answer:- polymorphism is an ability to exist in multiple forms .
There are 2 types of polymorphism :-
1) compile time polymorphism
2) run time polymorphism
78) why method overloading is called static binding ?
Answer :- having multiple methods with same name but different arguments is method
overloading .It is an compile time polymorphism/early binding/staic binding as
method call is bound with method definition based on arguments
79) why method overriding is called dynamic binding ?
Answer :- redefining method of parent class in a child class , is called method overriding
polymorphism :- ability to exist in multiple forms method overriding is run time
polymorphism because which method will be called is decided at runtime based on object.
www.jbktest.com 19 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
80) static method , final method , private method can not be overrident in child class .
true/false
Answer :- true . static method / final method / private method of parent class can not be
overriden .
81) can we have same private method in parent and child class ?
Answer :- yes . private methods are not inherited into child class .
82) what is method hiding ?
Answer :- In parent class and Child class , if we have static method with same signature it is
called method hiding .
83) what is method signature ?
Answer :- method signature means name of method and arguments . return type is not
considered in it .
84) difference between overloading and overriding ?
Answer:-overloading is associated with single class , whereas overriding is associated with
parent and child class . overloading is compile time polymorphism as method
call is bound with method defination at compile time based on arguments . whereas method
overriding is run time polymorphism because which method will called is decided at
runtime based on object. In orverloading method signture must be different whereas in
overriding method signature must be same . return type is not considered in overloading
whereas in overriding return type must be same or covarient .
class A
{
Object m1() { }
}
class B extends A
{
String m1() { }
}
www.jbktest.com 20 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
86) what is variable argument method ? can we have 2 variable arguments in one method ?
Answer:-Variable argument method can be called by passing 0 or any no of arguments . we
can't have method with 2 variable arguments.
void m1(int... a)
m1(10,20);
m1(10,20,30);
m1();
www.jbktest.com 21 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
}
90) why interface constants are declared as static final and not just final .
Answer:- So that they will be accessed by interface name . Declaring constants static also
save memory .
91) why interface methods are by default public and abstract ?
Answer:- interface methods are public so that anyone can implements it in any package . By
default interface methods are abstract because interface provides 100% abstraction by
providing all abstract methods whereas in abstract class , there are some defined methods also
.
92) Is it compulsory to define all methods from interface in implementation class ?
Answer:- Not compulsory but then we need to define implementation class as abstract class
as it will abstract method .
93) 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.
94) why do we declare interface reference and not reference of it's implementation class ?
Answer:- Because we know interface name but we do not know it's implementation class .
95) can we have 2 methods with same signature but different return type in 2 different
interfaces for which we are writing implementation class ?
Answer :- not possible as implementation class can not have 2 methods with same signature .
interface A { void m1() }
interface B { char m1() }
class C implements A,B not possible as it will contains 2 methods with same
signature which is not allowed .
96) what is abstract class ? why abstract class contains constructor ? when is it called ?
www.jbktest.com 22 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- Abstract class can contain abstract and concrete both methods. Concrete methods
have definition . abstract methods does not have definition . We can't create object of abstract
class . But we can create abstract class's subclass object . Abstract class's child class
constructor calls constructor of Parent abstract class .
97) what is difference between interface and abstract class in jdk1.8
Answer :- Subclasses of abstract class must define abstract method from abstract class .
When we have objects with some common behaviour and some uncommon
behaviour , go for abstract class .when we have only uncommon behaviour , go for interface .
interface does not have constructor but abstract class have constructor . interface
contains only constants but abstract class can contains constant and plain variables also .
98) Can we create object of abstract class and interface ? why ?
Answer :- No as their defination is not complete as it contains some abstract ( undefined )
methods .
99) Why do we declare reference of parent abstract class which points to it's child class object
, why not have child class reference
Answer :- because in real time , we know abstract class name but don't know it's child class
names .
www.jbktest.com 23 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
105) when we should write setter and getters methods in java class ?
Answer :- when class contains private variables , we should define public setter and getter
methods to access these private variables .
106) what is is-a and has-a relationship ?
Answer :- is-a means inheritance and has-a relationship is based on usage than inheritance .
when we want to access members of any class and if our class don't have any is-a relationship
with that class , we can use has-a relationship to define reference of that class in our class .
107) what is difference between aggregation and composition .
Answer :- In composition , if container object is destroyed then contained object is also
destroyed means it is tight coupling . e.g
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
}
If School object is destroyed then Teacher object will NOT be destroyed as it not
stored inside School object .
108) what is an array ? How to get/read any element from an array ?
Answer :- array is a special variable which can store multiple values in it . To read array
elements we can use any loop .
109) why array is called type safe ?
Answer :- as it contains elements of same type only .
www.jbktest.com 24 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 25 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- checked exception is that exception which must be handled using try and catch
block or else we get compile time error stating that you must handle this exception . When
you handle exception only then compiler let you go for execution . e.g.
ClassNotFoundException
unchecked exception handling is optional . e.g. NullPointerException .
119) difference between Exception class and Error class ?
Answer :- Exception occurs due to problem in programmer's logic . whereas Errors occurs
due to problem in system in which program is running .
e.g. NullPointerException occurs due to programmer's fault whereas OutOfMemory Error
will occur due to problem with JVM . Errors normaly can not be recovered but exception can
be handled .
www.jbktest.com 26 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 27 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 28 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
stored in some storage media like file , database , so that it can be read afterwards . such
objects are called persistent objects . Serilization , JDBC , Hibernate are different ways to
achieve it .
138) what is importance of serialVersionUID ?
Answer :- serialVersionUID must be declared as private static final long variable . If used ,
we don't get new uid each time we change serilized class defination .
139) Is constructor called in the process of Deserlization ?
Answer :- NO.
140) 1) long a=456L 2) long a=456l . which statement is correct ?
Answer :- Both are correct .
141) what is difference between float and double ?
Answer :- double is more precise than float , as float stores more decimal values than float .
double size is 8 byte whereas float size is 4 byte . by default every decimal value is
considered double in java . to make decimal value float , we must use letter f . e.g. float a =
3.2f; float b=4.5 will give error as 4.5 will be considred as double and without type casting it
is not possible to store double value in float variable .
142) why is it required to write f letter for floating values in case of float data type ?
Answer :- by default every decimal value is considered double in java . to make decimal
value float , we must use letter f . e.g. float a = 3.2f; float b=4.5 will give error as 4.5 will be
considred as double and without type casting it is not possible to store double value in float
variable .
www.jbktest.com 29 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
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();
www.jbktest.com 30 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 31 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
163) which map should be used when you want to preserve insrtion order based on keys ?
Answer :- LinkedHashMap should be used when you want to preserve insrtion order based on
keys .
164) Every class which is used as a Key in HashMap , must implements equals() and
hashcode(). why ?
Answer :- Key in hashmap should be unique . If two key object's contents are equals then
their hashcode must be eqaul so that hashmap will not accept duplicate keys . But for this
equals() and hashCode() must have been implemented by key .
165) What advatntages we get with generic collection ?
Answer :- Generic Collection allows only Homegenous ( same type ) objects . Hence we
don't require type casting as generic makes collection type safe .
166) ArrayList<int> will it work ?
Answer :- NO as type parameter only class name works but no primitives are supported .
167) ArrayList<? extends Number> which objects can be used as type parameter here ?
www.jbktest.com 32 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- Any class which extends Number class like Integer , Float can be used here as a
type parameter .168) Generic is a compile time feature or runtime feature ?
Answer :- Generic is a compile time feature . e.g.
ArrayList<Integer> arrayList = new ArrayList(); at right side of = symbol after constructor
name we don't need <Integer> as it is new ArrayList() will be evaluated at runtime , compiler
is not bothered about it .
Here we are informing compiler that we are going to add Integer class objects in ArrayList .
Hence compiler will allow only Integer type objects and not any other object.
arrayList.add(new Integer(10))
arrayList.add(new Integer(20))
arrayList.add(new String("JBK")) // compile time error
ArrayList<Object> arrayList = new ArrayList();
arrayList.add(new Integer(10))
arrayList.add(new Integer(20))
arrayList.add(new String("JBK")) // NO compile time error as all java classes are
child classes of Object class
169) when you write ArrayList<String> , what message you convey to compiler and which
restrictions are followed after this
Answer :- Here we are informing compiler that we are going to add String class objects in
ArrayList . Hence compiler will allow only String type objects and not any other object.
170) class MyClass<T> , class MyClass<P> which is correct way of defining Generic class
?
Answer :- Both as any Letter can be used as a Type Parameter while defining generic class .
171) what is generic method ?
Answer :- generic method is a method which is not bound to any type .
e.g. class ArrayList<T>
{
T get(int index);
}
here get(-) can return any type of object , hence it is generic method .
www.jbktest.com 33 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 34 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
// map() will valueOf() to each string from stream and convert String into Integer
List<String> lists=Arrays.asList("10","20","30");
www.jbktest.com 35 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Optional op=al4.stream().reduce((no1,no2)->no1+no2);
if(op.isPresent())
System.out.println(op.get()); // it will print 10 which is addition of
1,2,3,4
186) what is difference between map() and flatMap()?
Answer :- map() is used to apply some functionality to every member from collection .
FlatMap can be used to convert Collection<Collection<T>> to Collection<T> . It
means it is used to convert List of List to single List
List<String> lists=Arrays.asList("10","20","30");
Stream<Integer> stream1=lists.stream().map(Integer::valueOf);//
Integer.valueOf("10") Integer.valueOf("20") Integer.valueOf("30")
List<Integer> resultList=stream1.collect(Collectors.toList());
System.out.println(resultList);
List<Integer> list1=Arrays.asList(1,2,3);
List<Integer> list2=Arrays.asList(4,5,6);
List<Integer> list3=Arrays.asList(7,8,9);
List<List<Integer>> listOfList=Arrays.asList(list1,list2,list3);
System.out.println(listOfList);
List<Integer> flatResult=listOfList.stream().flatMap(list-
>list.stream()).collect(Collectors.toList());
System.out.println(flatResult);
www.jbktest.com 36 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
}
192) what is package in java ? what are advatages of packages ?
Answer :- package is a like folder of OS where we group .class files of classes & interfaces .
It java's way of grouping related classes together.
advatages of packages are :-
www.jbktest.com 37 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
196) Which access modifiers should be used if you want your class's members accessible
outside package also
Answer :- public and protected
197) why main() is declare public and not protected ? who call main() ?
Answer :- public methods can be called by any class from any package . main() is declared
public so that it will be called by JVM which may be in any package .
198) Object o = new Object() ; o.clone(); Is it correct ?
www.jbktest.com 38 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- No. Protected members are visible inside package anywhere and outside package
they are available inside child classes only . But we can not call them using object of Parent
class and we have to use object of Child class only .
199) What is clonnable interface ? why clone() is declared inside Object class ?
Answer :- clonnable interface must be implemented by a class whose object we want to clone
. clone() is defined in Object class so that it can be called by any java object who wants
clonning .
200) What is difference between default modifier and protected modifier ?
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
Answer :- when we define one class inside another class , it is nested class . when we define
one interface inside another class , it is nested interface .
e.g.
interface Map
{
interface Entry
{
www.jbktest.com 39 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
}
}
204) Diffference between implementing interface methods using anonymous class and
lambda expression
Answer :- lambda expression is used to define single abstract method from functional
interface ,whereas anonymous class can be used to define any no of methods from any
interface .
www.jbktest.com 40 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
int a=10;
Integer i = Integer.valueOf(a); // valueOf() will convert primitive int value to wrapper class
Integer object . It's boxing
System.out.println(i);
System.out.println(i.toString());
www.jbktest.com 41 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
www.jbktest.com 42 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
int value2();
Answer :- In SCP , Duplicates objects are not created . hence memory is saved .
www.jbktest.com 43 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- String object's content can not be modified means it is constant . Whereas
StringBuffer object's content can be modified .
means String object is immutable and StringBuffer object is mutable.
Answer :- static import allow us to import static members from class . Once static members
are imported , we can use them directly without using class name also .
out.println("java is easy");
Answer :- Thread is a part of a process . process means any program in execution . Java
program in execution is a process and it contains one thread called main . we can define more
threads using Thread class . Thread is a like worker . we submit job to worker and worker
perform that job . Similarly in java , we define job in run() method and submit that job to
thread and thread executes that job . Multiple threads executes same or different job
simultenously .
222) 2 different ways of defining Job in multithreading ?
www.jbktest.com 44 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- we can define job in run() of Thread class or we can define job in run() of runnable
interface .
Yes . but run() with no argument is called automatically . other methods we need to
call manually .
www.jbktest.com 45 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
Answer :- Synchronized objects are thread safe object because they give consistent result
even if multiple thread are there .
Answer:-sleep() method is used to pause execution of thread for particular time , wheras due
to wait() thread goes into waiting state and wait gets over when notify() is called . sleep()
does not release lock whereas wait() releases the lock .
Answer:- notify() is used to send notification to single waiting thread and notifyAll() is used
to send notification to all waiting threads.
www.jbktest.com 46 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
230) When we call sleep() inside run() , we have to write try and catch compulsory . Why
can't we delegate responsibility of exception handling
using throws in this case ?
Answer :- Because if write public void run() throws InterruptedException then compiler will
give error as while overriding a method we can't throws exception if method is not declared
with throws in parent type .
interface Runnable
{
void run();
}
public void run() throws InterruptedException // it will give compile time error
{
Thread.sleep(1000);
}
231) tell me about different rules regarding exception which we must follow in method
overridng ?
Answer :- overriden method may throws same exception , child exception or no exception but
parent exception not possile .
233) Can we have different name for public class and file name ?
www.jbktest.com 47 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
234) 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 .
www.jbktest.com 48 | P a g e
https://thekiranacademy.com/ Mob No. 8888809416
int[] a1={10,20}
int[] a2={10,20}
sop(a1==a2)
sop(a1.equals(a2))
Answer :- false as contents of array are not compared here , but address of array is stored
which is present in a1 & a2 .
www.jbktest.com 49 | P a g e