Java Interview Questions
Java Interview Questions
Answer :- JDK (Java Development Kit) contains JRE and Some development tools like
javac.exe (Java Compiler), java.exe , native2ascii.exe
JRE ( Java Runtime Enviroment ) contains jVM ( java virtual machine ) and
some jars having some predefined classes and interfaces.
JVM (Java Virtual Machine) is responsible for running our java program . It
is different for different OS.
JDK = JRE + some development tools like javac ( Java Compiler ) , java
JRE = JVM + Predefined classes and interfaces ( library )
JVM = A special program responsible for running our java program
3) what is jar file ? How to use class from any external jar file ?
Answer :- Jar file is a like zip file of Operating System . It contains packages
having .class files of classes and interfaces .
Zip file ==> Folders ==> files
Jar file ==> Packages ==> files (.class files of classes and interfaces).
It means , Jar file is container where we group different packages having
different classes & interfaces .
To use any external jar file which is not part of JRE , we must keep such
jar file in a build path .For that we need to
right click on project name , then select build path , then select
configure build path , then select libries tab , then
select external jar file and click on browse to select jar file.
If external jar file option is not visble , click on classpath .
Answer :- Yes . JVM is different for different OS . when we submit .class file to
JVM , JVM generates machine code specific to
current machine .
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();
7) what is variable ?
Answer :-
Variable is a container where we store some value .
e.g. int a=10;
[ 10 ] a
Variable cab be thought as name of memory block where value will be saved .
// return type of deposit(int amount) is void means it will not return any
value .
void deposit(int amount)
{
balance=balance+amount;
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()
}
}
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
{
static void m1(int a)
{
if(a<0)
{
return; // ok
// return 20 ; // NOT ok
}
System.out.println("Hello World");
}
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 .
12) If method returns object then why do we define a reference pointing to that
object ?
get(0) will give String object having content JBK and some methods . To
call these method we need to declare reference
pointing to object . s is reference .
without defining reference also we can call method ,however we can call
only one method in such case
arrayList.get(0).length();
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.
Answer:- Type casting is required so that we can call methods from child class .
Reference of Parent class can call
methods from parent class only and not child class methods .
To call child class methods we need reference of child class only .
interface List
{
Object get(int index);
}
System.out.println(s.length());
15) What is variable argument method ? what is difference between method with array
argument and method with variable argument ?
16) public static void main(String... a) is it write syntax for main method ?
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;
Answer :- No , as return will end method execution . hence statements below return
will not execute ever .
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 .
22) why main() is declared static ? what would have happened if it was declared as
non-static method ?
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 ?
Answer :- No we can not run class without main() . when main() execution gets
finished , we say java program execution is over .
27) what is method recursion ? which exception may occur if we don't have
controlled over recursion ?
Answer :-
public static void main(String[] a)
{
// some statements
Answer :- break is used to end execution of loop and return is used to end
execution of method.
Answer :- we can assign name to loop also just like we assign name to variable ,
method .
outer:
for(;1<10;)
{
}
31) What is difference between while and do-while loop ?
Answer :- while loop checks condition for every iteration . do while loop do not
check condition for first iteration .
32) 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 .
Answer :- default case is executed only if none of the case is executed . It is not
compulsory to have default case in switch block.
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
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 :- true . new keyword is used to allocate memory at runtime for object .
constructor constructs object by initializing variables from class . e.g.
1) String s = new String("JBK"); here using new and constructor String
class object is created .
/**
* 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
Answer :- Yes .
Answer :- NO . However we can receive ( get ) object from certain methods , means
method will create object and give it to us . e.g.
newInstance() will create object for us and then give it to us . However we
must remember that even newInstance() also require constructor for object
creation .
Answer :- There is only one way of creating java object and that is by using new
and constructor . However we can get ( receive ) object from different
ways :-
1) Class.forName("java.lang.String").newInstance()
2) In deserilization process , we get object .
3) We get object by clonning process also . Employee e =
(Employee)e1.clone()
4) factory methods are also give object . e.g. Calendar c =
Calendar.getInstance() . getInstance() will give object of a child class
of abstract class Calendar . we do not child class name . hence we have to create
reference of parent class Calendar .
5) In some cases , static block of a class also gives us object . e.g. In
JDBC , com.mysql.jdbc.Driver class has a static block which gives us
object of Driver class itself
class Driver
{
static Driver driver = null;
static
{
if(driver==null)
{
driver = new Driver();
DriverManager.registerDriver(driver);
}
}
}
This static block is executed when we call forName() and we get object
of Driver class .
Class.forName("com.mysql.jdbc.Driver");
Answer :- Having multiple constructor methods with same name but different
arguments is called Constructor overloading . e.g. String class has 15
constructor .
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
53) what is local variables ? which modifier we can apply to local variable ?
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.
Answer :- Class.forName("com.mysql.jdbc.Driver")
57) what is instance block ? what is difference between instance block and
constructor ?
59) which class other than String class is declared as final class ?
Answer :- In java String class , System class, all wrapper classes are declared as
final class .
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
Answer :- Yes . as we are changing value present inside object and not address
stored in reference varibale e1
Answer :- as we can call methods from this class using any java object
Answer :- class Object . by default every java class extends this class.
67) using reference of super class , we can call members of parent and child both .
true / false
Answer :- false , we can call parent class methods only using parent class
reference .
Answer :- No
Answer :- super keyword is used to access super class members inside child class .
super() is used to call super class ( parent class)
constructor inside child class. when super class and sub class both have
member of same name then use of super is compulsory to call super class
members.
Answer :- NO as this and super are references which points to object means it is
associated with object and static is associated with class
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
// Hence loading of class is not required
}
}
74) which super class constructor is called by default from child class constructor
, default or parameterized 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
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
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 .
Answer :- In parent class and Child class , if we have static method with same
signature it is called method hiding .
Answer :- method signature means name of method and arguments . return type is not
considered in it .
class A
{
Object m1() { }
}
class B extends A
{
String m1() { }
}
86) what is variable argument method ? can we have 2 variable arguments in one
method ?
void m1(int... a)
m1(10,20);
m1(10,20,30);
m1();
Answer:- abstraction means exposing only required things about abstract methods and
not showing implementation details of these methods . abstraction in java is
achieved by abstract class and interface .e.g. JDBC API contains certain interfaces
which are defined by Driver class . Programmer calls methods from these
interfaces . But they do not know implementation of these methods .e.g. we call
next() from ResultSet interface , but we don't know implementation of next () .
This is abstraction .Sun people exposed methods through interface but they didn't
reveal implementation of theses methods as we don't need to know it .
89) what is interface ? tell me where you have used it in your project .
interface EmployeeDAO
{
void addEmployee(Employee employee);
Employee getEmployee(int empId);
}
90) why interface constants are declared as static final and not just final .
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 .
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 ?
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 .
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 .
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 .
Answer :- When we want to have same hashcode for objects having same contents ,
then we must implement equals() and hashcode() both .
Answer :- when class contains private variables , we should define public setter
and getter methods to access these private variables .
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 .
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
}
111) How to find out class name of a class which is created internally for java
array ?
Answer :- capacity means how many objects will be saved in an array and size means
actual number of elements stored in an array .
Answer :- jagged array means no of rows fixed but no of columns not fixed .
e.g. int[] a = new int[3][];
116) Two dimensional array can be imagined as table having rows and columns and
each row from this table is an single dimensional array . true / false.
Answer :- true
117) what is exception handling ? is it achieved using try and catch block or
throws keyword ?
when exception occur in try block , program control is shifted to catch block
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 .
Answer :- At the end . Because if we write somewhere else we get compile time
error .
Answer :- From JDK 7 , java allows you to catch multiple type exceptions in a
single catch block. You can use vertical bar (|) to separate multiple exceptions
in catch block. It is alternative way to write multiple catch block to handle
multiple exceptions
BufferedReader is a resource that must be closed after the program is finished with
it:
Answer :- It occurs when we try to type cast parent object to child object . e.g.
131) File f= new File("abc.txt"); This statement will create abc.txt file in disk .
true/false.
Answer :- false .
133) why stream should be always closed in finaly block and not try block or catch
block ? is their any other way to do it ?
Answer :- There is no gurantee that all statements from try block will execute .
Hence it is suggested that we should not close stream in try block .
we should do it in finally block which executes always irrespective of exception
occurance . From JDK 7 , we can use try with resource statement , which
automatically closes resource (file) . No need to call close() .
Answer :- interface which does not have any method is called marker / tagging
interface . e.g. clonable , serilizable interfaces are marker interfaces.
Answer :- transient keyword is used for that variable whose value we do not want to
serilize .
Answer :- NO.
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 :- Java support Unicode which support many languages . Hence it needs more
memory . hence char data type size in java is 2 bytes , whereas language like C
support ASCII character set which support only English language .
Answer :- Collection is used to group objects in one container . It can group any
no of objects .
int[] a = {1,2,3.5f}; it will give error float value can't be stored in int array
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 :- Collection is for grouping single objects whereas Map is for grouping
pair of objects
Answer :- NO
Answer :- NON-Generic Collection is NOT type safe as it can accept any type of
objects.Hence type casting is required
Answer :- HashSet does not preserve insertion order whereas LinkedHashSet preserve
insertion order .
Answer :-
Comparable interface given natural sorting order and If we want custom sorting ,
then we need to use Comparator interface
Comparable interface gives SINGLE sorting sequence and Comparator gives multiple
sorting sequence
String class has already implemented Comparable interface to sort String objects
aplhabatically . default sorting of String objects is alphabatical sorting .
But if we want to sort String objects based on length of String then we need to
go for Comparator interface
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 :- values() gives Collection , which contains all values from map .
Answer :- get(Object key) accept key and gives value associated with key .
Answer :- remove(int index) will accept index of object which we want to remove
from ArrayList whereas remove(Object o) need object itself .
162) which map should be used when you want to have sorted entries ?
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 .
Answer :- Generic Collection allows only Homegenous ( same type ) objects . Hence
we don't require type casting as generic makes collection type safe .
167) ArrayList<? extends Number> which objects can be used as type parameter here ?
Answer :- Any class which extends Number class like Integer , Float can be used
here as a type parameter .
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
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 .
here get(-) can return any type of object , hence it is generic method .
Answer :- Arrays.copyOf(-) allows you to simply copy content of one array into
another array . new copied array can have greater or lower size than size of
original array .
int[] a = {10,20};
System.out.println(Arrays.toString(c));// [10]
Answer :- & checks both condition even if first condition is false whereas && will
check second condition only if first condition is true . Hence && is called logical
and operator . for and , we need both condition true for true result .
System.out.println("All is well");
Answer :- Properties file contains some configuration related data in the form of
key-value pair . e.g. we can specify database connection related details in
properties file . In java program , we use Properties class's load() to load these
properties in java program .
Answer :- HashTable , Vector are called as legacy classes because they are in JDK
since 1.1 version . Collection framework was introduced in 1.2 version . All legacy
classes were synchronized ( Thread Safe ) .
// map() will valueOf() to each string from stream and convert String into
Integer
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")
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
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);
List<Integer> al = Arrays.asList(1,2,3,4);
List<Integer> al = Arrays.asList(1,2,3,4);
Spliterator<Integer> splitIterator=al.spliterator();
splitIterator.forEachRemaining(System.out::println);
Iterator<Integer> iterator=al.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
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.
193) what is jar file ? Tell me command to create jar file and also to extract jar
file
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
195) How to use java.sql.Date and java.util.Date both classes in one java program ?
196) Which access modifiers should be used if you want your class's members
accessible outside package also
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 .
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 :-
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 :- The shallow copy of an object will have exact copy of all the fields of
original object. If original object has any references to other objects as fields,
then only references of those objects are copied into clone object, copy of those
objects are not created. That means any changes made to those objects through clone
object will be reflected in original object or vice-versa.
Deep copy of an object will have exact copy of all the fields of original object
just like shallow copy. But in additional, if original object has any references to
other objects as fields, then copy of those objects are also created by calling
clone() method on them. That means clone object and original object will be 100%
disjoint. They will be 100% independent of each other. Any changes made to clone
object will not be reflected in original object or vice-versa.
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
{
}
}
204) Diffference between implementing interface methods using anonymous class and
lambda expression
209) When is it compulsory to use wrapper class and not primitive type in
collection ?
int a=10;
System.out.println(i);
System.out.println(i.toString());
System.out.println(b);
int a=10;
Answer :- Optional class object is container for another object . before reading
object from this container , we call method isPresent() to ensure that object is
present inside optional object and it is not null . this way we avoid
NullPointerException .
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override
{
217) what is reflection ? which methods of Class class are used for it ?
218) what is difference between creating object using newInstance() and creating
object using new and constructor
Answer :- when we create object using new and constructor , we must know class name
at compile time
/* You can pass any class name here and you will get object of that class
*/
Object o= getObject(name);
Answer :- In SCP , Duplicates objects are not created . hence memory is saved .
Answer:- class whose only one instance is possible is called singleton object .
Calendar c = Calendar.getInstance();
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 :- using TimerTask class we define a task to schedule . Then we give this
task to Timer class object . Timer class contains schedule() method which accepts
taks object and Date object .
227) What is thread ?
Answer :- we can define job in run() of Thread class or we can define job in run()
of runnable interface .
236) 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 ?
interface Runnable
{
void run();
}
237) tell me about different rules regarding exception which we must follow in
method overridng ?
Answer :- Thread pool consist of group of threads . These threads are reused in
such way that many tasks are completed using less number of threads .
240) Can we have different name for public class and file name ?
Answer :- No . public class name and file name must be same.
241) 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 .
Answer :- select run configuration from run menu , inside arguments tab specify
arguments separated by space . using this program arguments , JVM creates
String[] , which it passes to main() method . If no arguments are passed , JVM
still create an array but it is empty array .
Answer :- bin folder contains all .class files of classes , interfaces , enums and
annonations.
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 .
Answer :- Yes as both will accept any type of object inside ArrayList
Answer :- object which is not pointed by any reference is eligible for garbage
collection.