0% found this document useful (0 votes)
2 views56 pages

Java Interview

Java is a platform-independent language as it compiles programs into bytecode that can run on any system with a Java Virtual Machine (JVM). Key features of Java include simplicity, robustness, security, and support for object-oriented programming. The document also discusses the roles of JVM, JRE, JDK, class loaders, and the differences between Java and C++, along with data types and packages in Java.

Uploaded by

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

Java Interview

Java is a platform-independent language as it compiles programs into bytecode that can run on any system with a Java Virtual Machine (JVM). Key features of Java include simplicity, robustness, security, and support for object-oriented programming. The document also discusses the roles of JVM, JRE, JDK, class loaders, and the differences between Java and C++, along with data types and packages in Java.

Uploaded by

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

​1​

​1. Is Java Platform Independent if then how?​


​….​

​ es,​ ​Java​ ​is​ ​a​ ​Platform​ ​Independent​ ​language.​ ​Unlike​ ​many​ ​programming​ ​languages​ ​javac​ ​compiler​
Y
​compiles​ ​the​ ​program​ ​to​ ​form​ ​a​ ​bytecode​ ​or​ ​.class​ ​file.​ ​This​ ​file​ ​is​ ​independent​ ​of​ ​the​ ​software​ ​or​
​hardware​ ​running​ ​but​ ​needs​ ​a​ ​JVM(Java​ ​Virtual​ ​Machine)​ ​file​ ​preinstalled​ ​in​ ​the​ ​operating​ ​system​​for​
​further execution of the bytecode.​
​Although​​JVM​​is​​platform​​dependent,​​the​​bytecode​​can​​be​​created​​on​​any​​System​​and​​can​​be​​executed​
​in any other system despite hardware or software being used which makes Java platform independent.​
​….​

​2. What are the top Java Features?​


​ ava​​is​​one​​the​​most​​famous​​and​​most​​used​​language​​in​​the​​real​​world,​​there​​are​​many​​features​​in​​Java​
J
​that makes it better than any other language some of them are mentioned below:​

​​ S
● ​ imple:​​Java is quite simple to understand and the​​syntax​
​●​ ​Platform​ ​Independent:​ ​Java​ ​is​ ​platform​ ​independent​ ​means​ ​we​ ​can​ ​run​​the​​same​​program​​in​
​any software and hardware and will get the same result.​
​●​ ​Interpreted:​​Java is interpreted as well as a compiler-based​​language.​
​●​ ​Robust:​​features like Garbage collection, exception​​handling, etc that make the language robust.​
​●​ ​Object-Oriented:​ ​Java​ ​is​ ​an​ ​object-oriented​ ​language​ ​that​ ​supports​ ​the​ ​concepts​ ​of​ ​class,​
​objects, four pillars of OOPS, etc.​
​●​ ​Secured:​ ​As​ ​we​ ​can​ ​directly​ ​share​ ​an​ ​application​ ​with​ ​the​ ​user​ ​without​ ​sharing​ ​the​ ​actual​
​program makes Java a secure language.​
​●​ ​High Performance:​ ​faster than other traditional interpreted​​programming languages.​
​●​ ​Dynamic:​​supports​​dynamic​​loading​​of​​classes​​and​​interfaces.Distributed:​​feature​​of​​Java​​makes​
​us able to access files by calling the methods from any machine connected.​
​●​ ​Multithreaded:​​deal with multiple tasks at once by​​defining multiple threads​
​….​
​●​ ​Architecture Neutral:​​it is not dependent on the architecture.​
​3. What is JVM?​

​JVM​ ​stands​ ​for​ ​Java​ ​Virtual​​Machine​​it​​is​​a​​Java​​interpreter.​​It​​is​​responsible​​for​​loading,​​verifying,​​and​


​ xecuting the bytecode created in Java.​
e
​Although​ ​it​ ​is​ ​platform​ ​dependent​ ​which​ ​means​ ​the​ ​software​ ​of​​JVM​​is​​different​​for​​different​​Operating​
​Systems it plays a vital role in making Java platform Independent.​
​To know more about the topic refer to​​JVM in Java​​.​
​….​

​4. What is JIT?​

​ IT​ ​stands​ ​for​ ​(Just-in-Time)​​compiler​​is​​a​​part​​of​​JRE(Java​​Runtime​​Environment),​​it​​is​​used​​for​​better​


J
​performance​ ​of​ ​the​ ​Java​ ​applications​ ​during​ ​run-time.​ ​The​ ​use​ ​of​ ​JIT​ ​is​ ​mentioned​ ​in​ ​step​ ​by​ ​step​
​process mentioned below:​
​1.​ ​Source code is compiled with javac compiler to form bytecode​
​2​
​ .​ B
2 ​ ytecode is further passed on to JVM​
​3.​ ​JIT​ ​is​ ​a​ ​part​ ​of​ ​JVM,​ ​JIT​ ​is​ ​responsible​​for​​compiling​​bytecode​​into​​native​​machine​​code​​at​​run​
​time.​
​4.​ ​The​​JIT​​compiler​​is​​enabled​​throughout,​​while​​it​​gets​​activated​​when​​a​​method​​is​​invoked.​​For​​a​
​compiled method, the JVM directly calls the compiled code, instead of interpreting it.​
​5.​ ​As JVM calls the compiled code that increases the performance and speed of the execution.​
​To know more about the topic refer to​​JIT in Java​​.​
​….​

​5. What are Memory storages available with JVM?​

​JVM consists of a few memory storages as mentioned below:​


​1.​ C ​ lass(Method)​ ​Area:​ ​stores​ ​class-level​ ​data​ ​of​ ​every​ ​class​ ​such​ ​as​ ​the​ ​runtime​ ​constant​ ​pool,​
​field, and method data, and the code for methods.​
​2.​ ​Heap:​ ​Objects​​are​​created​​or​​objects​​are​​stored.​​It​​is​​used​​to​​allocate​​memory​​to​​objects​​during​
​run time.​
​3.​ ​Stack:​​stores​​data​​and​​partial​​results​​which​​will​​be​​needed​​while​​returning​​value​​for​​method​​and​
​performing dynamic linking​
​4.​ ​Program​ ​Counter​ ​Register:​ ​stores​ ​the​ ​address​ ​of​ ​the​ ​Java​ ​virtual​ ​machine​​instruction​​currently​
​being executed.​
​5.​ ​Native Method Stack: stores all the native methods used in the application.​
​To know more about the topic refer to​​JVM Memory Storages​​.​
​….​

​6. What is a classloader?​


​ lassloader​ ​is​ ​the​ ​part​ ​of​ ​JRE(Java​ ​Runtime​ ​Environment),​ ​during​ ​the​ ​execution​ ​of​ ​the​ ​bytecode​ ​or​
C
​created​ ​.class​ ​file​ ​classloader​ ​is​ ​responsible​ ​for​ ​dynamically​​loading​​the​​java​​classes​​and​​interfaces​​to​
​JVM(Java​​Virtual​​Machine).​​Because​​of​​classloaders​​Java​​run​​time​​system​​does​​not​​need​​to​​know​​about​
​files and file systems.​
​To know more about the topic refer to​​ClassLoader​​in Java.​
​….​

​7. Difference between JVM, JRE, and JDK.​


​ VM:​​JVM​​also​​known​​as​​Java​​Virtual​​Machine​​is​​a​​part​​of​​JRE.​​JVM​​is​​a​​type​​of​​interpreter​​responsible​
J
​for​​converting​​bytecode​​into​​machine-readable​​code.​​JVM​​itself​​is​​platform​​dependent​​but​​it​​interprets​​the​
​bytecode which is the platform-independent reason why Java is platform-independent.​
​JRE:​ ​JRE​ ​stands​ ​for​ ​Java​ ​Runtime​ ​Environment,​ ​it​ ​is​ ​an​ ​installation​ ​package​ ​that​ ​provides​ ​an​
​environment to run the Java program or application on any machine.​
​JDK:​​JDK​​stands​​for​​Java​​Development​​Kit​​which​​provides​​the​​environment​​to​​develop​​and​​execute​​Java​
​programs.​ ​JDK​ ​is​ ​a​ ​package​ ​that​ ​includes​​two​​things​​Development​​Tools​​to​​provide​​an​​environment​​to​
​develop your Java programs and, JRE to execute Java programs or applications.​
​To know more about the topic refer to the​​Differences​​between JVM, JRE, and JDK​​.​
​….​

​8. What are the differences between Java and C++?​


​C++​ ​Java​
​3​

​C++ is Platform Dependent​ ​Java is Platform Independent​

​C++ is mainly used for System Programming​ ​Java is Mainly used for Application Programming​

​C++ is nearer to hardware​ ​Java is not so interactive with hardware​

​C++ supports global and namespace scope.​ ​Java doesn’t support global scope.​

​ unctionality​ ​supported​ ​in​ ​C++​ ​but​ ​not​ ​in​ ​Java​


F
​are:​
​ unctionality​ ​supported​​in​​Java​​but​​not​​in​​C++​
F
​●​ ​goto​
​are:​
​●​ ​Pointers​
​●​ ​thread support​
​●​ ​Call by reference​
​●​ ​documentation comment​
​●​ ​Structures and Unions​
​●​ ​unsigned right shift(>>>)​
​●​ ​Multiple Inheritance​
​●​ ​Virtual Functions​

​ ava​ ​is​ ​also​ ​an​ ​object-oriented​ ​language.​ ​It​ ​is​ ​a​


J
​ ++​ ​is​ ​an​ ​object-oriented​​language.​​It​​is​​not​​a​
C
​single​ ​root​ ​hierarchy​ ​as​ ​everything​ ​gets​ ​derived​
​single root hierarchy .​
​from a single class (java.lang.Object).​

​ ava​​uses​​a​​Single​​inheritance​​tree​​as​​classes​​in​
J
​C++ always creates a new inheritance tree.​
​Java are the child of object classes in Java.​

​9. Explain public static void main(String args[]) in Java.​

​Unlike​ ​any​ ​other​ ​programming​ ​language​ ​like​ ​C,​ ​C++,​ ​etc.​​In​​Java,​​we​​declared​​the​​main​​function​​as​​a​


​ ublic static void main (String args[]). The meanings of the terms are mentioned below:​
p
​1.​ ​public:​​the​​public​​is​​the​​access​​modifier​​responsible​​for​​mentioning​​who​​can​​access​​the​​element​
​or​ ​the​ ​method​ ​and​ ​what​ ​is​ ​the​ ​limit.​ ​It​ ​is​ ​responsible​ ​for​ ​making​ ​the​ ​main​ ​function​ ​globally​
​available.​​It​​is​​made​​public​​so​​that​​JVM​​can​​invoke​​it​​from​​outside​​the​​class​​as​​it​​is​​not​​present​​in​
​the current class.​
​2.​ ​static:​​static​​is​​a​​keyword​​used​​so​​that​​we​​can​​use​​the​​element​​without​​initiating​​the​​class​​so​​to​
​avoid the unnecessary allocation of the memory.​
​3.​ ​void:​​void​​is​​a​​keyword​​and​​is​​used​​to​​specify​​that​​a​​method​​doesn’t​​return​​anything.​​As​​the​​main​
​function doesn’t return anything we use void.​
​4.​ ​main:​​main​​represents​​that​​the​​function​​declared​​is​​the​​main​​function.​​It​​helps​​JVM​​to​​identify​​that​
​the declared function is the main function.​
​5.​ ​String​ ​args[]:​ ​It​ ​stores​ ​Java​ ​command-line​ ​arguments​ ​and​ ​is​ ​an​ ​array​ ​of​ ​type​​java.lang.String​
​….​
​class.​
​4​
​10. What is Java String Pool?​
​ ​​Java​​String​​Pool​​is​​a​​place​​in​​heap​​memory​​where​​all​​the​​strings​​defined​​in​​the​​program​​are​​stored.​​A​
A
​separate​​place​​in​​a​​stack​​is​​there​​where​​the​​variable​​storing​​the​​string​​is​​stored.​​Whenever​​we​​create​​a​
​new​ ​string​ ​object,​ ​JVM​ ​checks​ ​for​​the​​presence​​of​​the​​object​​in​​the​​String​​pool,​​If​​String​​is​​available​​in​
​the pool, the same object reference is shared with the variable, else a new object is created.​

​ xample:​
E
​String str1="Hello";​
​// "Hello" will be stored in String Pool​
​// str1 will be stored in stack memory​
​11. What will happen if we declare don’t declare the main as static?​
​We​ ​can​ ​declare​ ​the​ ​main​ ​method​ ​without​ ​using​ ​static​ ​and​ ​without​ ​getting​ ​any​ ​errors.​ ​But,​ ​the​ ​main​
​ ethod will not be treated as the entry point to the application or the program.​
m
​….​

​12. What are Packages in Java?​


​ ackages​ ​in​ ​Java​ ​can​ ​be​ ​defined​ ​as​ ​the​​grouping​​of​​related​​types​​of​​classes,​​interfaces,​​etc​​providing​
P
​access to protection and namespace management.​
​….​

​13. Why Packages are used?​


​Packages​ ​are​ ​used​ ​in​ ​Java​ ​in​ ​order​ ​to​ ​prevent​ ​naming​ ​conflicts,​ ​control​ ​access,​ ​and​ ​make​
​ earching/locating and usage of classes, interfaces, etc easier.​
s
​….​

​14. What are the advantages of Packages in Java?​


​There are various advantages of defining packages in Java.​
​●​ ​Packages avoid name clashes.​
​●​ ​The Package provides easier access control.​
​●​ ​We can also have the hidden classes that are not visible outside and are used by the package.​
​●​ ​It is easier to locate the related classes.​
​15. How many types of packages are there in Java?​
​There are two types of packages in Java​
​●​ ​User-defined packages​
​●​ ​Build In packages​
​16. Explain different data types in Java.​
​There are 2 types of data types in Java as mentioned below:​
​1.​ ​Primitive Data Type​
​2.​ ​Non-Primitive Data Type or Object Data type​
​Primitive​​Data​​Type:​​Primitive​​data​​are​​single​​values​​with​​no​​special​​capabilities.​​There​​are​​8​​primitive​
​data types:​
​●​ ​boolean: stores value true or false​
​●​ ​byte: stores an 8-bit signed two’s complement integer​
​●​ ​char: stores a single 16-bit Unicode character​
​●​ ​short: stores a 16-bit signed two’s complement integer​
​●​ ​int: stores a 32-bit signed two’s complement integer​
​●​ ​long: stores a 64-bit two’s complement integer​
​●​ ​float: stores a single-precision 32-bit IEEE 754 floating-point​
​●​ ​double: stores a double-precision 64-bit IEEE 754 floating-point​
​Non-Primitive​ ​Data​ ​Type:​ ​Reference​ ​Data​ ​types​ ​will​ ​contain​ ​a​ ​memory​ ​address​ ​of​ ​the​ ​variable’s​
​values​ ​because​ ​it​ ​is​ ​not​ ​able​ ​to​ ​directly​ ​store​ ​the​ ​values​ ​in​ ​the​ ​memory.​ ​Types​ ​of​ ​Non-Primitive​ ​are​
​mentioned below:​
​5​
​​
● ​ trings​
S
​●​ ​Array​
​●​ ​Class​
​●​ ​Object​
​●​ ​Interface​
​17. When a byte datatype is used?​
​ ​ ​byte​ ​is​ ​an​ ​8-bit​ ​signed​ ​two-complement​ ​integer.​​The​​minimum​​value​​supported​​by​​bytes​​is​​-128​​and​
A
​127​ ​is​ ​the​ ​maximum​ ​value.​ ​It​ ​is​ ​used​ ​in​ ​conditions​ ​where​ ​we​ ​need​ ​to​ ​save​ ​memory​ ​and​ ​the​ ​limit​ ​of​
​numbers needed is between -128 to 127.​
​18. Can we declare Pointer in Java?​
​No,​ ​Java​ ​doesn’t​ ​provide​ ​the​ ​support​ ​of​ ​Pointer.​ ​As​ ​Java​ ​needed​ ​to​ ​be​ ​more​ ​secure​ ​because​ ​which​
f​eature of the pointer is not provided in Java.​
​19. What is the default value of byte datatype in Java?​
​The default value of the byte datatype in Java is 0.​
​20. What is the default value of float and double datatype in Java?​
​The default value of the float is 0.0f and of double is 0.0d in Java.​
​21. What is the Wrapper class in Java?​
​Wrapper,​ ​in​ ​general,​ ​is​ ​referred​ ​to​ ​a​ ​larger​ ​entity​ ​that​ ​encapsulates​ ​a​ ​smaller​​entity.​​Here​​in​​Java,​​the​
​ rapper class is an object class that encapsulates the primitive data types.​
w
​The​ ​primitive​ ​data​ ​types​ ​are​ ​the​ ​ones​ ​from​ ​which​ ​further​ ​data​ ​types​ ​could​ ​be​ ​created.​ ​For​ ​example,​
​integers​ ​can​ ​further​ ​lead​ ​to​ ​the​ ​construction​ ​of​ ​long,​ ​byte,​ ​short,​ ​etc.​ ​On​ ​the​ ​other​ ​hand,​ ​the​ ​string​
​cannot, hence it is not primitive.​
​Getting​ ​back​ ​to​ ​the​ ​wrapper​ ​class,​ ​Java​ ​contains​ ​8​ ​wrapper​ ​classes.​ ​They​ ​are​ ​Boolean,​ ​Byte,​ ​Short,​
​Integer,​ ​Character,​ ​Long,​ ​Float,​ ​and​ ​Double.​ ​Further,​ ​custom​ ​wrapper​ ​classes​ ​can​ ​also​ ​be​ ​created​ ​in​
​Java​ ​which​ ​is​ ​similar​ ​to​ ​the​ ​concept​ ​of​ ​Structure​ ​in​ ​the​ ​C​ ​programming​ ​language.​​We​​create​​our​​own​
​wrapper class with the required data types.​
​22. Why do we need wrapper classes?​
​The​ ​wrapper​ ​class​ ​is​ ​an​​object​​class​​that​​encapsulates​​the​​primitive​​data​​types,​​and​​we​​need​​them​​for​
t​he following reasons:​
​1.​ ​Wrapper classes are final and immutable​
​2.​ ​Provides methods like valueOf(), parseInt(), etc.​
​3.​ ​It provides the feature of autoboxing and unboxing.​
​23. Differentiate between instance and local variables.​
​Instance Variable​ ​Local Variable​

​ eclared​ ​outside​ ​the​ ​method,​ ​directly​ ​invoked​


D
​Declared within the method.​
​by the method.​

​Has a default value.​ ​No default value​

​It can be used throughout the class.​ ​The scope is limited to the method.​

​24. What are the default values assigned to variables and instances in Java?​
I​n​​Java​​When​​we​​haven’t​​initialized​​the​​instance​​variables​​then​​the​​compiler​​initializes​​them​​with​​default​
​values.​​The​​default​​values​​for​​instances​​and​​variables​​depend​​on​​their​​data​​types.​​Some​​common​​types​
​of default data types are:​
​●​ ​The default value for numeric types (byte, short, int, long, float, and double) is 0.​
​●​ ​The default value for the boolean type is false.​
​●​ ​The default value for object types (classes, interfaces, and arrays) is null.​
​●​ ​The null character, “u0000, ” is the default value for the char type.​
​25. What is a Class Variable?​
I​n​​Java,​​a​​class​​variable​​(also​​known​​as​​a​​static​​variable)​​is​​a​​variable​​that​​is​​declared​​within​​a​​class​​but​
​outside​ ​of​ ​any​ ​method,​ ​constructor,​​or​​block.​​Class​​variables​​are​​declared​​with​​the​​static​​keyword,​​and​
​​
6
​they​​are​​shared​​by​​all​​instances​​(objects)​​of​​the​​class​​as​​well​​as​​by​​the​​class​​itself.​​No​​matter​​how​​many​
​objects are derived from a class, each class variable would only exist once.​

/​/ Java program to demonstrate use of Clas Variable​


​class GFG {​
​public static int ctr = 0;​
​public GFG() { ctr++; }​
​public static void main(String[] args)​
​{​
​GFG obj1 = new GFG();​
​GFG obj2 = new GFG();​
​GFG obj3 = new GFG();​
​System.out.println("Number of objects created are "​
​+ GFG.ctr);​
​}​
​}​

​26. What is the default value stored in Local Variables?​


​There​​is​​no​​default​​value​​stored​​with​​local​​variables.​​Also,​​primitive​​variables​​and​​objects​​don’t​​have​​any​
​ efault values.​
d
​27. Explain the difference between instance variable and a class variable.​
I​nstance​ ​Variable:​ ​A​ ​class​ ​variable​ ​without​ ​a​ ​static​ ​modifier​ ​known​ ​as​ ​an​ ​instance​ ​variable​​is​​typically​
​shared​ ​by​ ​all​ ​instances​ ​of​ ​the​ ​class.​ ​These​ ​variables​ ​can​ ​have​ ​distinct​​values​​among​​several​​objects.​
​The​ ​contents​ ​of​ ​an​ ​instance​ ​variable​ ​are​ ​completely​ ​independent​ ​of​ ​one​ ​object​ ​instance​​from​​another​
​because they are related to a specific object instance of the class.​
​Example:​
​ ​Java​

/​/ Java Program to demonstrate Instance Variable​


​import java.io.*;​
​class GFG {​
​private String name;​
​public void setName(String name) { this.name = name; }​
​public String getName() { return name; }​
​public static void main(String[] args)​
​{​
​GFG obj = new GFG();​
​obj.setName("John");​
​System.out.println("Name " + obj.getName());​
​}​
​}​

​Class​ ​Variable:​ ​Class​ ​Variable​ ​variable​ ​can​ ​be​ ​declared​ ​anywhere​ ​at​ ​the​ ​class​ ​level​ ​using​ ​the​
​ eyword​ ​static.​ ​These​ ​variables​ ​can​ ​only​ ​have​ ​one​ ​value​ ​when​ ​applied​ ​to​ ​various​ ​objects.​ ​These​
k
​variables​​can​​be​​shared​​by​​all​​class​​members​​since​​they​​are​​not​​connected​​to​​any​​specific​​object​​of​​the​
​class.​

/​/ Java Program to demonstrate Class Variable​


​import java.io.*;​
​class GFG {​
​// class variable​
​private static final double PI = 3.14159;​
​private double radius;​
​public GFG(double radius) { this.radius = radius; }​
​public double getArea() { return PI * radius * radius; }​
​7​

​ ublic static void main(String[] args)​


p
​{​
​GFG obj = new GFG(5.0);​
​System.out.println("Area of circle: "​
​+ obj.getArea());​
​}​
​}​

​28. What is a static variable?​


​ he​ ​static​ ​keyword​ ​is​ ​used​​to​​share​​the​​same​​variable​​or​​method​​of​​a​​given​​class.​​Static​​variables​​are​
T
​the​ ​variables​ ​that​ ​once​ ​declared​ ​then​ ​a​ ​single​ ​copy​ ​of​ ​the​ ​variable​ ​is​ ​created​ ​and​ ​shared​ ​among​ ​all​
​objects at the class level.​
​29. What is the difference between System.out, System.err, and System.in?​
​ ystem.out​​–​​It​​is​​a​​PrintStream​​that​​is​​used​​for​​writing​​characters​​or​​can​​be​​said​​it​​can​​output​​the​​data​
S
​we want to write on the Command Line Interface console/terminal.​

/​/ Java Program to implement​


​// System.out​
​import java.io.*;​
​// Driver Class​
​class GFG {​
​// Main Function​
​public static void main(String[] args)​
​{​
​// Use of System.out​
​System.out.println("");​
​}​
​}​

​System.out​ ​System.err​

​It will print to the standard out of the system.​ ​It will print to the standard error.​

​It is mostly used to display results on the console.​ ​It is mostly used to output error texts.​

I​t​ ​gives​ ​output​ ​on​ ​the​ ​console​ ​with​ ​the​ I​t​ ​also​ ​gives​ ​output​ ​on​ ​the​ ​console​ ​but​ ​most​ ​of​
​default(black) color.​ ​the IDEs give it a red color to differentiate.​

​System.in​ ​–​ ​It​ ​is​ ​an​ ​InputStream​ ​used​ ​to​ ​read​ ​input​ ​from​ ​the​ ​terminal​ ​Window.​ ​We​ ​can’t​ ​use​ ​the​
​System.in directly so we use Scanner class for taking input with the system.in.​
​30. What do you understand by an IO stream?​

​ ava​ ​brings​ ​various​ ​Streams​ ​with​ ​its​ ​I/O​ ​package​ ​that​ ​helps​ ​the​ ​user​ ​to​ ​perform​ ​all​ ​the​ ​input-output​
J
​operations.​​These​​streams​​support​​all​​types​​of​​objects,​​data​​types,​​characters,​​files,​​etc​​to​​fully​​execute​
​the I/O operations.​
​ 1.​​What​​is​​the​​difference​​between​​the​​Reader/Writer​​class​​hierarchy​​and​​the​
3
​InputStream/OutputStream class hierarchy?​
​ he​ ​key​ ​difference​ ​between​ ​them​ ​is​ ​that​ ​byte​ ​stream​ ​data​ ​is​ ​read​ ​and​ ​written​ ​by​ ​input/output​ ​stream​
T
​classes.​​Characters​​are​​handled​​by​​the​​Reader​​and​​Writer​​classes.​​In​​contrast​​to​​Reader/Writer​​classes,​
​​
8
​which​​accept​​character​​arrays​​as​​parameters,​​input/output​​stream​​class​​methods​​accept​​byte​​arrays.​​In​
​comparison​ ​to​ ​input/output​ ​streams,​ ​the​ ​Reader/Writer​ ​classes​ ​are​ ​more​ ​efficient,​ ​handle​ ​all​ ​Unicode​
​characters,​​and​​are​​useful​​for​​internalization.​​Use​​Reader/Writer​​classes​​instead​​of​​binary​​data,​​such​​as​
​pictures, unless you do so.​

/​/ Java Program to demonstrate Reading Writing Binary Data​


​// with InputStream/OutputSream​
​import java.io.*;​
​class GFG {​
​public static void main(String[] args)​
​{​
​try {​
​// Writing binary data to a file using​
​// OutputStream​
​byte[] data = { 3, 334, 234, 234, 324, 234 };​
​OutputStream os​
​= new FileOutputStream("data.bin");​
​os.write(data);​
​os.close();​
​// Reading binary data from a file using​
​// InputStream​
​InputStream is​
​= new FileInputStream("data.bin");​
​byte[] Buffer = new byte[5];​
​is.read(buffer);​
​is.close();​
​// Printing the read data​
​for (byte b : Buffer) {​
​System.out.println(b);​
​}​
​}​
​catch (IOException e) {​
​e.printStackTrace();​
​}​
​}​
​}​

​32. What are the super most classes for all the streams?​
​ ll​ ​the​ ​stream​ ​classes​ ​can​ ​be​ ​divided​ ​into​ ​two​ ​types​ ​of​ ​classes​ ​that​ ​are​ ​ByteStream​ ​classes​ ​and​
A
​CharacterStream​ ​Classes.​ ​The​ ​ByteStream​ ​classes​ ​are​ ​further​ ​divided​ ​into​ ​InputStream​ ​classes​ ​and​
​OutputStream​ ​classes.​ ​CharacterStream​ ​classes​ ​are​ ​also​ ​divided​ ​into​ ​Reader​ ​classes​ ​and​ ​Writer​
​classes.​ ​The​ ​SuperMost​ ​classes​ ​for​ ​all​ ​the​ ​InputStream​ ​classes​ ​is​ ​java.io.InputStream​ ​and​ ​for​ ​all​ ​the​
​output​​stream​​classes​​is​​java.io.OutPutStream.​​Similarly,​​for​​all​​the​​reader​​classes,​​the​​super-most​​class​
​is java.io.Reader, and for all the writer classes, it is java.io.Writer.​
​33. What are the FileInputStream and FileOutputStream?​
​ o​ ​read​ ​and​ ​write​ ​data,​ ​Java​ ​offers​ ​I/O​ ​Streams.​ ​A​ ​Stream​ ​represents​ ​an​ ​input​ ​source​ ​or​ ​an​ ​output​
T
​destination,​​which​​could​​be​​a​​file,​​an​​i/o​​device,​​another​​program,​​etc.​​FileInputStream​​in​​Java​​is​​used​​to​
​read​ ​data​ ​from​ ​a​ ​file​ ​as​ ​a​ ​stream​ ​of​ ​bytes.​ ​It​ ​is​ ​mostly​ ​used​ ​for​ ​reading​ ​binary​ ​data​ ​such​ ​as​​images,​
​audio files, or serialized objects.​
​Example:​
​File file = new File("path_of_the_file");​
​FileInputStream inputStream = new FileInputStream(file);​
​ 4.​ ​What​ ​is​ ​the​ ​purpose​
3 ​of​ ​using​ ​BufferedInputStream​ ​and​
​BufferedOutputStream classes?​
​ hen​ ​we​ ​are​ ​working​ ​with​ ​the​ ​files​ ​or​ ​stream​ ​then​ ​to​ ​increase​ ​the​ ​Input/Output​ ​performance​ ​of​ ​the​
W
​program​ ​we​ ​need​ ​to​ ​use​ ​the​ ​BufferedInputStream​ ​and​ ​BufferedOutputStream​ ​classes.​ ​These​ ​both​
​classes​ ​provide​ ​the​ ​capability​ ​of​ ​buffering​ ​which​ ​means​ ​that​ ​the​ ​data​ ​will​ ​be​ ​stored​ ​in​ ​a​​buffer​​before​
​writing​​to​​a​​file​​or​​reading​​it​​from​​a​​stream.​​It​​also​​reduces​​the​​number​​of​​times​​our​​OS​​needs​​to​​interact​
​​
9
​with​​the​​network​​or​​the​​disk.​​Buffering​​allows​​programs​​to​​write​​a​​big​​amount​​of​​data​​instead​​of​​writing​​it​
​in small chunks. This also reduces the overhead of accessing the network or the disk.​
​35. What are FilterStreams?​
​ tream​​filter​​or​​Filter​​Streams​​returns​​a​​stream​​consisting​​of​​the​​elements​​of​​this​​stream​​that​​match​​the​
S
​given​ ​predicate.​ ​While​ ​working​ ​filter()​ ​it​ ​doesn’t​ ​actually​ ​perform​ ​filtering​ ​but​ ​instead​ ​creates​ ​a​ ​new​
​stream that, when traversed, contains the elements of initial streams that match the given predicate.​
​36. What is an I/O filter?​
​An​​I/O​​filter​​also​​defined​​as​​an​​Input​​Output​​filter​​is​​an​​object​​that​​reads​​from​​one​​stream​​and​​writes​​data​
t​o input and output sources. It used java.io package to use this filter.​
​37. How many ways you can take input from the console?​
​There are two methods to take input from the console in Java mentioned below:​
​1.​ ​Using Command line argument​
​2.​ ​Using Buffered Reader Class​
​3.​ ​Using Console Class​
​4.​ ​Using Scanner Class​
​The program demonstrating the use of each method is given below.​
​….​

​class GFG {​
​public static void main(String[] args)​
​{​
​// check if length of args array is​
​// greater than 0​
​if (args.length > 0) {​
​System.out.println(​
​"The command line arguments are:");​
​// iterating the args array and printing​
​// the command line arguments​
​for (String val : args)​
​System.out.println(val);​
​}​
​else​
​System.out.println("No command line "​
​+ "arguments found.");​
​}​
​}​
​….​

​….​

​….​

​class GFG {​
​public static void main(String[] args)​
​throws IOException​
​{​
​// Enter data using BufferReader​
​BufferedReader read = new BufferedReader(​
​new InputStreamReader(System.in));​
​// Reading data using readLine​
​String x = read.readLine();​
​// Printing the read line​
​System.out.println(x);​
​}​
​}​

​public class GfG {​


​public static void main(String[] args)​
​10​

​{​
/​/ Using Console to input data from user​
​String x = System.console().readLine();​
​System.out.println("You entered string " + x);​
​}​
​}​

​….​

i​mport java.util.Scanner;​
​class GfG {​
​public static void main(String args[])​
​{​
​// Using Scanner for Getting Input from User​
​Scanner in = new Scanner(System.in);​
​String str = in.nextLine();​
​System.out.println("You entered string " + str);​
​}​
​}​

​38. Difference in the use of print, println, and printf.​


​ rint,​ ​println,​ ​and​ ​printf​ ​all​ ​are​ ​used​ ​for​ ​printing​ ​the​ ​elements​ ​but​ ​print​ ​prints​ ​all​ ​the​ ​elements​ ​and​ ​the​
p
​cursor​ ​remains​ ​in​​the​​same​​line.​​println​​shifts​​the​​cursor​​to​​next​​line.​​And​​with​​printf​​we​​can​​use​​format​
​identifiers too.​
​39. What are operators?​
​ perators​ ​are​ ​the​ ​special​ ​types​ ​of​ ​symbols​ ​used​ ​for​ ​performing​ ​some​ ​operations​ ​over​ ​variables​ ​and​
O
​values.​
​41. Explain the difference between >> and >>> operators.​
​Operators​​like​​>>​​and​​>>>​​seem​​to​​be​​the​​same​​but​​act​​a​​bit​​differently.​​>>​​operator​​shifts​​the​​sign​​bits​
​ nd the >>> operator is used in shifting out the zero-filled bits.​
a
​….​

​class GFG {​
​public static void main(String[] args)​
​{​
​int a = -16, b = 1;​
​// Use of >>​
​System.out.println(a >> b);​
​a = -17;​
​b = 1;​
​// Use of >>>​
​System.out.println(a >>> b);​
​}​
​}​

​42. Which Java operator is right associative?​


​There is only one operator which is right associative which is = operator.​
​43. What is dot operator?​
​The​​Dot​​operator​​in​​Java​​is​​used​​to​​access​​the​​instance​​variables​​and​​methods​​of​​class​​objects.​​It​​is​​also​
​ sed to access classes and sub-packages from the package.​
u
​44. What is covariant return type?​
​ he​​covariant​​return​​type​​specifies​​that​​the​​return​​type​​may​​vary​​in​​the​​same​​direction​​as​​the​​subclass.​
T
​It’s​ ​possible​ ​to​ ​have​ ​different​ ​return​ ​types​ ​for​ ​an​ ​overriding​ ​method​ ​in​ ​the​ ​child​ ​class,​ ​but​ ​the​ ​child’s​
​return​ ​type​ ​should​ ​be​ ​a​ ​subtype​ ​of​ ​the​ ​parent’s​ ​return​ ​type​ ​and​ ​because​ ​of​ ​that​ ​overriding​ ​method​
​becomes variant with respect to the return type.​
​We use covariant return type because of the following reasons:​
​ 1​
1
​●​ ​Avoids​​confusing​​type​​casts​​present​​in​​the​​class​​hierarchy​​and​​makes​​the​​code​​readable,​​usable,​
​and maintainable.​
​ ​ ​Gives liberty to have more specific return types when overriding methods.​

​●​ ​Help in preventing run-time ClassCastExceptions on returns.​
​45. What is the transient keyword?​
​ he​​transient​​keyword​​is​​used​​at​​the​​time​​of​​serialization​​if​​we​​don’t​​want​​to​​save​​the​​value​​of​​a​​particular​
T
​variable​ ​in​ ​a​ ​file.​ ​When​ ​JVM​ ​comes​ ​across​ ​a​ ​transient​ ​keyword,​ ​it​ ​ignores​ ​the​ ​original​ ​value​ ​of​ ​the​
​variable and saves the default value of that variable data type.​
​46. What’s the difference between the methods sleep() and wait()?​
​Sleep()​ ​Wait()​

​The sleep() method belongs to the thread class.​ ​Wait() method belongs to the object class.​

​ leep​ ​does​ ​not​ ​release​ ​the​ ​lock​ ​that​​the​​current​


S ​ ait()​ ​release​ ​the​ ​lock​ ​which​ ​allows​ ​other​
w
​thread holds.​ ​threads to acquire it.​

​This method is a static method.​ ​This method is not a static method.​

I​nterruptedException​ ​is​ ​shown​ ​if​ ​the​ ​thread​ ​is​


​Sleep() does not throw an InterruptedException.​
​interrupted while waiting.​

​ ainly​ ​used​ ​to​ ​delay​ ​a​ ​thread​ ​for​ ​some​ ​specific​


M ​ ainly​ ​used​ ​to​ ​pause​ ​a​ ​thread​ ​until​ ​notified​​by​
M
​time duration.​ ​another thread.​

​Sleep() Has Two Overloaded Methods:​ ​Wait() Has Three Overloaded Methods:​
​●​ ​sleep(long millis)millis: milliseconds​ ​●​ ​wait()​
​●​ ​sleep(long​ ​millis,​ ​int​ ​nanos)​ ​nanos:​ ​●​ ​wait(long timeout)​
​Nanoseconds​ ​●​ ​wait(long timeout, int nanos)​

​47. What are the differences between String and StringBuffer?​


​String​ ​StringBuffer​

​Store of a sequence of characters.​ ​Provides functionality to work with the strings.​

I​t​ ​is​ ​mutable​ ​(can​ ​be​ ​modified​ ​and​ ​other​ ​string​ ​operations​
​It is immutable.​
​could be performed on them.)​

I​t​ ​is​ ​thread-safe​ ​(two​ ​threads​ ​can’t​ ​call​ ​the​ ​methods​ ​of​
​No thread operations in a string.​
​StringBuffer simultaneously)​

​48. What are the differences between StringBuffer and StringBuilder?​


​StringBuffer​ ​StringBuilder​

​ tringBuffer​ ​provides​ ​functionality​ ​to​ ​work​ ​with​


S ​ tringBuilder​ ​is​ ​a​ ​class​ ​used​ ​to​ ​build​ ​a​ ​mutable​
S
​the strings.​ ​string.​
​12​

I​t​ ​is​ ​thread-safe​ ​(two​ ​threads​ ​can’t​ ​call​ ​the​ I​t​ ​is​ ​not​ ​thread-safe​ ​(two​ ​threads​ ​can​ ​call​ ​the​
​methods of StringBuffer simultaneously)​ ​methods concurrently)​

​Comparatively slow as it is synchronized.​ ​Being non-synchronized, implementation is faster​

​ 9.​​Which​​among​​String​​or​​String​​Buffer​​should​​be​​preferred​​when​​there​​are​
4
​a lot of updates required to be done in the data?​
​The​​string​​is​​preferred​​over​​StringBuffer​​as​​StringBuilder​​is​​faster​​than​​StringBuffer,​​it​​has​​the​​capability​
t​o utilize it whenever possible.​
​50. Why is StringBuffer called mutable?​
​ tringBuffer​​class​​in​​Java​​is​​used​​to​​represent​​a​​changeable​​string​​of​​characters.​​It​​offers​​an​​alternative​
S
​to​ ​the​ ​immutable​ ​String​​class​​by​​enabling​​you​​to​​change​​a​​string’s​​contents​​without​​constantly​​creating​
​new​ ​objects.​ ​Mutable​ ​(modifiable)​ ​strings​ ​are​ ​created​ ​with​ ​the​ ​help​ ​of​ ​the​ ​StringBuffer​ ​class.​ ​The​
​StringBuffer class in Java is identical to the String class except that it is changeable.​
​Example:​
​ ​Java​

/​/ Java Program to demonstrate use of stringbuffer​


​public class StringBufferExample {​
​public static void main(String[] args)​
​{​
​StringBuffer s = new StringBuffer();​
​s.append("Geeks");​
​s.append("for");​
​s.append("Geeks");​
​String message = s.toString();​
​System.out.println(message);​
​}​
​}​

​51. How is the creation of a String using new() different from that of a literal?​
​ tring​​using​​new()​​is​​different​​from​​the​​literal​​as​​when​​we​​declare​​string​​it​​stores​​the​​elements​​inside​​the​
S
​stack​ ​memory​ ​whereas​ ​when​ ​it​ ​is​ ​declared​ ​using​ ​new()​ ​it​ ​allocates​ ​a​ ​dynamic​ ​memory​ ​in​ ​the​ ​heap​
​memory. The object gets created in the heap memory even if the same content object is present.​
​Syntax:​
​String x = new String("ABC");​

​57. How to copy an array in Java?​


​In Java there are multiple ways to copy an Array based on the requirements.​
​●​ ​clone()​​method​​in​​Java:​​This​​method​​in​​Java​​is​​used​​to​​create​​a​​shallow​​copy​​of​​the​​given​​array​
​which means that the new array will share the same memory as the original array.​
​int[] Arr = { 1, 2, 3, 5, 0};​
​int[] tempArr = Arr.clone();​
​13​

​●​ a ​ rraycopy()​​method:​​To​​create​​a​​deep​​copy​​of​​the​​array​​we​​can​​use​​this​​method​​which​​creates​​a​
​new array with the same values as the original array.​
​int[] Arr = {1, 2, 7, 9, 8};​
​int[] tempArr = new int[Arr.length];​
​System.arraycopy(Arr, 0, tempArr, 0, Arr.length);​
​●​ ​copyOf()​​method:​​This​​method​​is​​used​​to​​create​​a​​new​​array​​with​​a​​specific​​length​​and​​copies​​the​
​contents of the original array to the new array.​
​int[] Arr = {1, 2, 4, 8};​
​int[] tempArr = Arrays.copyOf(Arr, Arr.length);​
​●​ ​copyOfRange()​ ​method:​ ​This​ ​method​ ​is​ ​very​ ​similar​ ​to​ ​the​ ​copyOf()​ ​method​ ​in​ ​Java,​ ​but​ ​this​
​method also allows us to specify the range of the elements to copy from the original array.​
​int[] Arr = {1, 2, 4, 8};​
​int[] temArr = Arrays.copyOfRange(Arr, 0, Arr.length);​
​58. What do you understand by the jagged array?​
​ ​​jagged​​Array​​in​​Java​​is​​just​​a​​two-dimensional​​array​​in​​which​​each​​row​​of​​the​​array​​can​​have​​a​​different​
A
​length.​​Since​​all​​the​​rows​​in​​a​​2-d​​Array​​have​​the​​same​​length​​but​​a​​jagged​​array​​allows​​more​​flexibility​​in​
​the​​size​​of​​each​​row.​​This​​feature​​is​​very​​useful​​in​​conditions​​where​​the​​data​​has​​varying​​lengths​​or​​when​
​memory usage needs to be optimized.​
​Syntax:​
​int[][] Arr = new int[][] {​
​{1, 2, 8},​
​{7, 5},​
​{6, 7, 2, 6}​
​};​
​59. Is it possible to make an array volatile?​
I​n​​Java,​​it​​is​​not​​possible​​to​​make​​a​​volatile.​​Volatile​​keywords​​in​​Java​​can​​only​​be​​applied​​to​​individual​
​variables​​but​​not​​to​​arrays​​or​​collections.​​The​​value​​of​​the​​Variable​​is​​always​​read​​from​​and​​written​​to​​the​
​main​ ​memory​ ​when​ ​it​ ​is​ ​defined​ ​as​ ​volatile​ ​rather​ ​than​ ​being​ ​cached​​in​​a​​thread’s​​local​​memory.​​This​
​makes it easier to make sure that all threads that access the variable can see changes made to it.​
​60. What are the advantages and disadvantages of an array?​

​The advantages of Arrays are:​


​●​ D ​ irect​​and​​effective​​access​​to​​any​​element​​in​​the​​collection​​is​​made​​possible​​by​​arrays.​​An​​array’s​
​elements​ ​can​ ​be​ ​accessed​ ​using​ ​an​ ​O(1)​ ​operation,​ ​which​ ​means​ ​that​ ​the​ ​amount​ ​of​ ​time​
​needed to do so is constant and independent of the array’s size.​
​●​ ​Data​​can​​be​​stored​​effectively​​in​​memory​​using​​arrays.​​The​​size​​of​​an​​array​​is​​known​​at​​compile​
​time since its elements are stored in contiguous memory regions.​
​●​ ​Due​ ​to​ ​the​ ​fact​ ​that​ ​the​ ​data​ ​is​ ​stored​ ​in​ ​contiguous​ ​memory​ ​areas,​ ​arrays​ ​provide​ ​quick​​data​
​retrieval.​
​●​ ​Arrays​ ​are​ ​easy​ ​to​ ​implement​ ​and​ ​understand,​ ​making​ ​them​ ​an​ ​ideal​ ​choice​ ​for​ ​beginners​
​learning computer programming.​
​Disadvantages of Arrays are:​
​●​ A ​ rrays​ ​are​ ​created​ ​with​ ​a​​predetermined​​size​​that​​is​​chosen​​at​​that​​moment.​​This​​means​​that​​if​
​the​​array’s​​size​​needs​​to​​be​​extended,​​a​​new​​array​​will​​need​​to​​be​​made,​​and​​the​​data​​will​​need​
​to be copied from the old array to the new array, which can take a lot of time and memory.​
​●​ ​There​ ​may​ ​be​ ​unused​​memory​​space​​in​​an​​array’s​​memory​​space​​if​​the​​array​​is​​not​​completely​
​occupied. If you have poor recall, this can be a problem.​
​●​ ​Compared​ ​to​ ​other​ ​data​ ​structures​ ​like​ ​linked​ ​lists​ ​and​ ​trees,​ ​arrays​ ​might​​be​​rigid​​due​​to​​their​
​fixed size and limited support for sophisticated data types.​
​ 4​
1
​ ​ ​Because​​an​​array’s​​elements​​must​​all​​be​​of​​the​​same​​data​​type,​​it​​does​​not​​support​​complex​​data​

​types like objects and structures.​
​61. What is an object-oriented paradigm?​
​ aradigm​ ​literally​ ​means​ ​a​ ​pattern​ ​or​ ​a​ ​method.​ ​Programming​ ​paradigms​ ​are​ ​the​ ​methods​ ​to​ ​solve​ ​a​
P
​program​ ​that​ ​is​ ​of​ ​four​ ​types​ ​namely,​​Imperative,​​logical,​​functional,​​and​​object-oriented.​​When​​objects​
​are​ ​used​ ​as​ ​base​ ​entities​ ​upon​ ​which​ ​the​ ​methods​ ​are​ ​applied,​ ​encapsulation​ ​or​ ​inheritance​
​functionalities are performed, it is known as an object-oriented paradigm.​
​62. What are the main concepts of OOPs in Java?​
​The main concepts of OOPs in Java are mentioned below:​
​●​ ​Inheritance​
​●​ ​Polymorphism​
​●​ ​Abstraction​
​●​ ​Encapsulation​
​ 3.​ ​What​ ​is​ ​the​ ​difference​ ​between​ ​an​ ​object-oriented​ ​programming​
6
​language and an object-based programming language?​
​Object-Oriented Programming Language​ ​Object-Based Programming Language​

​ bject-oriented​ ​programming​ ​language​ ​covers​


O ​ he​ ​scope​ ​of​ ​object-based​ ​programming​ ​is​
T
​larger​ ​concepts​ ​like​ ​inheritance,​ ​polymorphism,​ ​limited​ ​to​ ​the​ ​usage​ ​of​ ​objects​ ​and​
​abstraction, etc.​ ​encapsulation.​

​It supports all the built-in objects​ ​It doesn’t support all the built-in objects​

​Examples: Java, C#, etc.​ ​Examples: Java script, visual basics, etc.​

​64. How is the ‘new’ operator different from the ‘newInstance()’ operator in Java?​
t​he​ ​new​ ​operator​ ​is​ ​used​​to​​create​​objects,​​but​​if​​we​​want​​to​​decide​​the​​type​​of​​object​​to​​be​​created​​at​
​runtime,​ ​there​ ​is​ ​no​ ​way​​we​​can​​use​​the​​new​​operator.​​In​​this​​case,​​we​​have​​to​​use​​the​​newInstance()​
​method​​.​
​65. What are Classes in Java?​
I​n​ ​Java,​ ​Classes​ ​are​ ​the​ ​collection​ ​of​ ​objects​ ​sharing​ ​similar​ ​characteristics​ ​and​ ​attributes.​ ​Classes​
​represent​ ​the​ ​blueprint​ ​or​ ​template​​from​​which​​objects​​are​​created.​ ​Classes​​are​​not​​real-world​​entities​
​but help us to create objects which are real-world entities.​
​66. What is the difference between static (class) method and instance method?​
​Static(Class) method​ ​Instance method​

​ tatic​ ​method​ ​is​ ​associated​ ​with​ ​a​ ​class​ ​rather​


S ​ he​ ​instance​ ​method​ ​is​ ​associated​ ​with​ ​an​
T
​than an object.​ ​object rather than a class.​

​ tatic​ ​methods​ ​can​ ​be​ ​called​ ​using​ ​the​ ​class​


S ​ he​​instance​​method​​can​​be​​called​​on​​a​​specific​
T
​name only without creating an instance of a class.​ ​instance of a class using the object reference.​

​ tatic​ ​methods​ ​do​ ​not​ ​have​ ​access​ ​to​ ​this​


S
​Instance methods have access to this keyword.​
​keyword.​

​ his​ ​method​ ​can​ ​access​ ​only​ ​static​ ​members​ ​of​


T ​ his​ ​method​ ​can​ ​access​ ​both​ ​static​ ​and​
T
​the class​ ​non-static methods of the class.​
​15​
​67. What is this keyword in Java?​

​‘this’ is a keyword used to reference a variable that refers to the current object.​
​68. What are Brief Access Specifiers and Types of Access Specifiers?​

​ ccess​ ​Specifiers​ ​in​ ​Java​ ​help​ ​to​ ​restrict​ ​the​ ​scope​ ​of​ ​a​ ​class,​ ​constructor,​ ​variable,​ ​method,​ ​or​ ​data​
A
​member. There are four types of Access Specifiers in Java mentioned below:​
​1.​ ​Public​
​2.​ ​Private​
​3.​ ​Protected​
​4.​ ​Default​
​69. What will be the initial value of an object reference which is defined as an instance variable?​
​The initial value of an object reference which is defined as an instance variable is a NULL value.​
​70. What is an object?​
​The​ ​object​ ​is​ ​a​ ​real-life​ ​entity​ ​that​ ​has​​certain​​properties​​and​​methods​​associated​​with​​it.​​The​​object​​is​
​ lso defined as the instance of a class. An object can be declared using a new keyword.​
a
​71. What are the different ways to create objects in Java?​
​Methods to create objects in Java are mentioned below:​
​1.​ ​Using new keyword​
​2.​ ​Using new instance​
​3.​ ​Using clone() method​
​4.​ ​Using deserialization​
​5.​ ​Using the newInstance() method of the Constructor class​
​To know more about methods to create objects in Java refer to​​this article​​.​
​72. What are the advantages and disadvantages of object cloning?​
​ here are many advantages and disadvantages of using object cloning as mentioned below:​
T
​Advantages:​
​●​ ​In​ ​Java,​ ​the​ ​‘=’​ ​assignment​ ​operator​ ​cannot​ ​be​ ​used​ ​for​ ​cloning​ ​as​ ​it​ ​simply​ ​creates​​a​​copy​​of​
​reference​ ​variables.​ ​To​ ​overcome​ ​such​ ​discrepancy​ ​the​ ​clone()​ ​method​ ​of​ ​Object​​class​​can​​be​
​used over the assignment operator.​
​●​ ​The​ ​clone()​ ​method​​is​​a​​protected​​method​​of​​class​​Object​​which​​means​​that​​only​​the​​Employee​
​class​ ​can​ ​clone​ ​Employee​ ​objects.​ ​This​ ​means​ ​no​ ​class​ ​other​ ​than​ ​Employee​ ​can​ ​clone​
​Employee objects since it does not know the Employee class’ attributes.​
​●​ ​Code size decreases as repetition decreases.​
​Disadvantages:​
​●​ ​As​​the​​Object.clone()​​method​​is​​protected,​​so​​need​​to​​provide​​our​​own​​clone()​​and​​indirectly​​call​
​Object.clone() from it.​
​●​ ​If​ ​we​ ​don’t​ ​have​ ​any​ ​methods​ ​then​ ​we​ ​need​ ​to​ ​provide​ ​a​ ​Cloneable​ ​interface​ ​as​ ​we​ ​need​ ​to​
​provide JVM information so that we can perform a clone() on our object.​
​ 3.​ ​What​ ​are​ ​the​ ​advantages​ ​of​​passing​​this​​into​​a​​method​​instead​​of​​the​​current​
7
​class object itself?​
​There​​are​​a​​few​​advantages​​of​​passing​​this​​into​​a​​method​​instead​​of​​the​​current​​class​​object​​itself​​these​
​ re:​
a
​●​ ​this​​is​​the​​final​​variable​​because​​of​​which​​this​​cannot​​be​​assigned​​to​​any​​new​​value​​whereas​​the​
​current class object might not be final and can be changed.​
​16​
​●​ ​this can be used in the synchronized block.​
​74. What is the constructor?​
​Constructor​ ​is​ ​a​ ​special​ ​method​ ​that​​is​​used​​to​​initialize​​objects.​​Constructor​​is​​called​​when​​a​​object​​is​
​ reated. The name of constructor is same as of the class.​
c
​Example:​
​// Class Created​
​class XYZ{​
​private int val;​

/​/ Constructor​
​XYZ(){​
​val=0;​
​}​
​};​
​75. What happens if you don’t provide a constructor in a class?​
​If​ ​you​ ​don’t​ ​provide​ ​a​ ​constructor​ ​in​ ​a​ ​class​ ​in​ ​Java,​ ​the​ ​compiler​ ​automatically​ ​generates​ ​a​ ​default​
​ onstructor with no arguments and no operation which is a default constructor.​
c
​76. How many types of constructors are used in Java?​
​There are two types of constructors in Java as mentioned below:​
​1.​ ​Default Constructor​
​2.​ ​Parameterized Constructor​
​Default​ ​Constructor:​ ​It​ ​is​ ​the​ ​type​​that​​does​​not​​accept​​any​​parameter​​value.​​It​​is​​used​​to​​set​​initial​
​values for object attributes.​
​class_Name();​
​// Default constructor called​
​Parameterized​ ​Constructor:​ ​It​ ​is​ ​the​ ​type​ ​of​ ​constructor​ ​that​ ​accepts​ ​parameters​ ​as​ ​arguments.​
​These are used to assign values to instance variables during the initialization of objects.​
​class_Name(parameter1, parameter2......);​
​// All the values passed as parameter will be​
​// allocated accordingly​
​77. What is the purpose of a default constructor?​
​ onstructors​​help​​to​​create​​instances​​of​​a​​class​​or​​can​​be​​said​​to​​create​​objects​​of​​a​​class.​​Constructor​
C
​is​ ​called​ ​during​ ​the​ ​initialization​ ​of​ ​objects.​ ​A​ ​default​ ​constructor​ ​is​ ​a​ ​type​ ​of​​constructor​​which​​do​​not​
​accept​​any​​parameter,​​So​​whatever​​value​​is​​assigned​​to​​properties​​of​​the​​objects​​are​​considered​​default​
​values.​
​78. What do you understand by copy constructor in Java?​
​ he​​copy​​constructor​​is​​the​​type​​of​​constructor​​in​​which​​we​​pass​​another​​object​​as​​a​​parameter​​because​
T
​which​​properties​​of​​both​​objects​​seem​​the​​same,​​that​​is​​why​​it​​seems​​as​​if​​constructors​​create​​a​​copy​​of​
​an object.​
​79. Where and how can you use a private constructor?​
​A​ ​private​ ​constructor​ ​is​ ​used​ ​if​ ​you​ ​don’t​ ​want​ ​any​ ​other​ ​class​ ​to​ ​instantiate​ ​the​ ​object​ ​to​ ​avoid​
​ ubclassing. The use private constructor can be seen as implemented in the example.​
s
​17​
​….​

i​mport java.io.*;​
​class GFG {​
​static GFG instance = null;​
​public int x = 10;​
​// private constructor can't be accessed outside the​
​// class​
​private GFG() {}​
​// Factory method to provide the users with instances​
​static public GFG getInstance()​
​{​
​if (instance == null)​
​instance = new GFG();​
​return instance;​
​}​
​}​
​// Driver Class​
​class Main {​
​public static void main(String args[])​
​{​
​GFG a = GFG.getInstance();​
​GFG b = GFG.getInstance();​
​a.x = a.x + 10;​
​System.out.println("Value of a.x = " + a.x);​
​System.out.println("Value of b.x = " + b.x);​
​}​
​}​

​ utput​
O
​Value of a.x = 20​
​Value of b.x = 20​
​80. What are the differences between the constructors and methods?​
​ ava​ ​constructors​ ​are​ ​used​ ​for​ ​initializing​ ​objects.​ ​During​ ​creation,​ ​constructors​ ​are​ ​called​ ​to​ ​set​
J
​attributes for objects apart from this few basic differences between them are:​
​1.​ ​Constructors​​are​​only​​called​​when​​the​​object​​is​​created​​but​​other​​methods​​can​​be​​called​​multiple​
​times during the life of an object.​
​2.​ ​Constructors do not return anything, whereas other methods can return anything.​
​3.​ ​Constructors​ ​are​ ​used​ ​to​ ​setting​ ​up​ ​the​ ​initial​ ​state​ ​but​ ​methods​ ​are​ ​used​ ​to​ ​perform​ ​specific​
​actions.​
​81. What is an Interface?​
​ n​​interface​​in​​Java​​is​​a​​collection​​of​​static​​final​​variables​​and​​abstract​​methods​​that​​define​​the​​contract​
A
​or​​agreement​​for​​a​​set​​of​​linked​​classes.​​Any​​class​​that​​implements​​an​​interface​​is​​required​​to​​implement​
​a​​specific​​set​​of​​methods.​​It​​specifies​​the​​behavior​​that​​a​​class​​must​​exhibit​​but​​not​​the​​specifics​​of​​how​​it​
​should be implemented.​
​Syntax:​
​interface​
​{​
​// constant fields​
​// methds that are abstract by default​
​}​
​18​
​….​

​interface Shape {​
​double getArea();​
​double getPerimeter();​
​}​
​class Circle implements Shape {​
​private double radius;​
​public Circle(double radius) { this.radius = radius; }​
​public double getArea()​
​{​
​return Math.PI * radius * radius;​
​}​
​public double getPerimeter()​
​{​
​return 2 * Math.PI * radius;​
​}​
​}​
​class GFG {​
​public static void main(String[] args)​
​{​
​Circle circle = new Circle(5.0);​
​System.out.println("Area of circle is "​
​+ circle.getArea());​
​System.out.println("Perimeter of circle is"​
​+ circle.getPerimeter());​
​}​
​}​

​ utput​
O
​Area of circle is 78.53981633974483​
​Perimeter of circle is31.41592653589793​
​82. Give some features of the Interface.​
​ n​​Interface​​in​​Java​​programming​​language​​is​​defined​​as​​an​​abstract​​type​​used​​to​​specify​​the​​behavior​
A
​of​​a​​class.​​An​​interface​​in​​Java​​is​​a​​blueprint​​of​​a​​behavior.​​A​​Java​​interface​​contains​​static​​constants​​and​
​abstract methods.​
​Features of the Interface are mentioned below:​
​●​ ​The interface can help to achieve total abstraction.​
​●​ ​Allows us to use multiple inheritances in Java.​
​●​ ​Any class can implement multiple interfaces even when one class can extend only one class.​
​●​ ​It is also used to achieve loose coupling.​
​83. What is a marker interface?​
​An​ ​Interface​ ​is​ ​recognized​ ​as​ ​an​ ​empty​ ​interface​ ​(no​ ​field​ ​or​ ​methods)​ ​it​ ​is​ ​called​ ​a​ ​marker​​interface.​
​ xamples of marker interfaces are Serializable, Cloneable, and Remote interface​
E
​84. What are the differences between abstract class and interface?​

​Abstract Class​ ​Interface Class​

​ oth​ ​abstract​ ​and​ ​non-abstract​ ​methods​ ​may​


B
​The interface contains only abstract methods.​
​be found in an abstract class.​
​19​

​Abstract Class supports Final methods.​ ​The interface class does not support Final methods.​

​ ultiple​ ​inheritance​ ​is​ ​not​ ​supported​ ​by​ ​the​


M ​ ultiple​ ​inheritances​ ​is​ ​supported​ ​by​ ​Interface​
M
​Abstract class.​ ​Class.​

​ bstract​ ​Keyword​ ​is​ ​used​ ​to​ ​declare​ ​Abstract​


A I​nterface​ ​Keyword​ ​is​ ​used​ ​to​ ​declare​ ​the​ ​interface​
​class.​ ​class.​

​ xtend​ ​keyword​ ​is​ ​used​ ​to​ ​extend​ ​an​​Abstract​


e i​mplements​ ​Keyword​ ​is​ ​used​ ​to​ ​implement​ ​the​
​Class.​ ​interface.​

​ bstract​ ​Class​ ​has​ ​members​ ​like​ ​protected,​


A
​All class members are public by default.​
​private, etc.​

​85. What do you mean by data encapsulation?​

​ ata​ ​Encapsulation​ ​is​ ​the​ ​concept​ ​of​ ​OOPS​ ​properties​ ​and​ ​characteristics​ ​of​ ​the​ ​classes​ ​that​ ​The​
D
​interface​ ​is​ ​binded​ ​together.​ ​Basically,​ ​it​ ​bundles​ ​data​ ​and​ ​methods​ ​that​ ​operate​ ​on​ ​that​​data​​within​​a​
​single​ ​unit.​ ​Encapsulation​ ​is​ ​achieved​ ​by​ ​declaring​ ​the​ ​instance​ ​variables​ ​of​ ​a​ ​class​ ​as​​private,​​which​
​means they can only be accessed within the class.​
​86. What are the advantages of Encapsulation in Java?​
​The advantages of Encapsulation in Java are mentioned below:​
​1.​ ​Data​ ​Hiding:​ ​it​ ​is​ ​a​ ​way​ ​of​ ​restricting​ ​the​ ​access​ ​of​ ​our​ ​data​ ​members​ ​by​ ​hiding​ ​the​
​implementation​​details.​​Encapsulation​​also​​provides​​a​​way​​for​​data​​hiding.​​The​​user​​will​​have​​no​
​idea about the inner implementation of the class.​
​2.​ ​Increased​ ​Flexibility:​ ​We​ ​can​ ​make​ ​the​ ​variables​​of​​the​​class​​read-only​​or​​write-only​​depending​
​on our requirements.​
​3.​ ​Reusability:​ ​Encapsulation​ ​also​ ​improves​ ​the​ ​re-usability​ ​and​ ​is​ ​easy​ ​to​ ​change​ ​with​ ​new​
​requirements.​
​4.​ ​Testing code is easy: Code is made easy to test for unit testing.​
​87. What is the primary benefit of Encapsulation?​
​ he​​main​​advantage​​of​​Encapsulation​​in​​Java​​is​​its​​ability​​to​​protect​​the​​internal​​state​​of​​an​​object​​from​
T
​external​ ​modification​ ​or​ ​access.​ ​It​ ​is​ ​the​ ​is​ ​a​ ​way​ ​of​ ​hiding​ ​the​ ​implementation​ ​details​ ​of​​a​​class​​from​
​outside​​access​​and​​only​​exposing​​a​​public​​interface​​that​​can​​be​​used​​to​​interact​​with​​the​​class.​​The​​main​
​benefit​ ​is​ ​of​ ​providing​ ​a​ ​way​ ​to​ ​control​ ​and​ ​manage​ ​the​ ​state​ ​and​ ​the​ ​behavior​ ​of​ ​an​ ​object​​and​​also​
​protecting it from modification and unauthorized access at the same time.​

/​/ Java Program to demonstrate use of Encapsulation​


​import java.io.*;​
​class Person {​
​private String Name;​
​private int age;​
​public String getName() { return Name; }​
​public void setName(String Name) { this.Name = Name; }​
​public int getAge() { return age; }​
​public void setAge(int age) { this.age = age; }​
​}​
​20​

/​/ Driver class​


​class GFG {​
​// main function​
​public static void main(String[] args)​
​{​
​Person p = new Person();​
​p.setName("Rohan");​
​p.setAge(29);​
​System.out.println("Name is " + p.getName());​
​System.out.println("Age is " + p.getAge());​
​}​
​}​

​ utput​
O
​Name is Rohan​
​Age is 29​
​88. What do you mean by aggregation?​

​ ggregation​ ​is​ ​a​ ​term​ ​related​ ​to​ ​the​ ​relationship​ ​between​ ​two​ ​classes​ ​best​ ​described​ ​as​ ​a​ ​“has-a”​
A
​relationship.​ ​This​ ​kind​ ​is​ ​the​ ​most​ ​specialized​ ​version​ ​of​ ​association.​ ​It​ ​is​ ​a​ ​unidirectional​ ​association​
​means​ ​it​ ​is​ ​a​ ​one-way​ ​relationship.​ ​It​ ​contains​ ​the​ ​reference​ ​to​ ​another​ ​class​ ​and​ ​is​ ​said​ ​to​ ​have​
​ownership of that class.​
​89. What is the ‘IS-A ‘ relationship in OOPs Java?​
​‘IS-A’ is a type of relationship in OOPs Java where one class inherits another class.​
​90. Define Inheritance.​
​ hen​ ​an​ ​object​ ​that​ ​belongs​ ​to​ ​a​ ​subclass​ ​acquires​ ​all​​the​​properties​​and​​behavior​​of​​a​​parent​​object​
W
​that​​is​​from​​the​​superclass,​​it​​is​​known​​as​​inheritance.​ ​A​​class​​within​​a​​class​​is​​called​​the​​subclass​​and​
​the​​latter​​is​​referred​​to​​as​​the​​superclass.​​Sub​​class​​or​​the​​child​​class​​is​​said​​to​​be​​specific​​whereas​​the​
​superclass or the parent class is generic. Inheritance provides code reusability.​
​91. What are the different types of inheritance in Java?​
​Inheritance​​is​​the​​method​​by​​which​​the​​Child​​class​​can​​inherit​​the​​features​​of​​the​​Super​​or​​Parent​​class.​
I​n Java, Inheritance is of four types:​
​●​ ​Single​ ​Inheritance:​ ​When​ ​a​ ​child​ ​or​ ​subclass​ ​extends​ ​only​ ​one​ ​superclass,​ ​it​ ​is​ ​known​ ​to​ ​be​
​single inheritance. Single-parent class properties are passed down to the child class.​
​●​ ​Multilevel​ ​Inheritance:​ ​When​ ​a​ ​child​ ​or​ ​subclass​ ​extends​ ​any​ ​other​ ​subclass​ ​a​ ​hierarchy​ ​of​
​inheritance​ ​is​ ​created​ ​which​ ​is​ ​known​ ​as​ ​multilevel​ ​inheritance.​ ​In​ ​other​ ​words,​ ​one​ ​subclass​
​becomes the parent class of another.​
​●​ ​Hierarchical​ ​Inheritance:​ ​When​​multiple​​subclasses​​derive​​from​​the​​same​​parent​​class​​is​​known​
​as​ ​Hierarchical​ ​Inheritance.​ ​In​ ​other​ ​words,​ ​a​ ​class​ ​that​ ​has​ ​a​ ​single​ ​parent​ ​has​ ​many​
​subclasses.​
​●​ ​Multiple​​Inheritance:​​When​​a​​child​​class​​inherits​​from​​multiple​​parent​​classes​​is​​known​​as​​Multiple​
​Inheritance. In Java, it only supports multiple inheritance of interfaces, not classes.​
​92. What is multiple inheritance? Is it supported by Java?​
​ ​ ​component​ ​of​ ​the​ ​object-oriented​ ​notion​ ​known​ ​as​ ​multiple​ ​inheritances​ ​allows​ ​a​ ​class​ ​to​ ​inherit​
A
​properties​ ​from​ ​many​ ​parent​ ​classes.​ ​When​ ​methods​ ​with​ ​the​ ​same​ ​signature​ ​are​ ​present​ ​in​ ​both​
​superclasses​​and​​subclasses,​​an​​issue​​arises.​​The​​method’s​​caller​​cannot​​specify​​to​​the​​compiler​​which​
​class method should be called or even which class method should be given precedence.​
​Note: Java doesn’t support Multiple Inheritance​
​21​

/​/ Java Program to show multiple Inheritance​


​import java.io.*;​
​interface Animal {​
​void eat();​
​}​
​interface Mammal {​
​void drink();​
​}​
​class Dog implements Animal, Mammal {​
​public void eat() { System.out.println("Eating"); }​
​public void drink() { System.out.println("Drinking"); }​
​void bark() { System.out.println("Barking"); }​
​}​
​class GFG {​
​public static void main(String[] args)​
​{​
​Dog d = new Dog();​
​d.eat();​
​d.drink();​
​d.bark();​
​}​
​}​

​ utput​
O
​Eating​
​Drinking​
​Barking​
​93. How is inheritance in C++ different from Java?​
​Inheritance in C++​ ​Inheritance in Java​

​C++ lets the user to inherit multiple classes.​ ​Java doesn’t support multiple inheritances.​

​ hen​ ​a​ ​class​ ​is​ ​created​ ​in​ ​C++,​ ​it​ ​doesn’t​


W ​ ava​​is​​always​​said​​to​​have​​a​​single​​inheritance​​as​​all​
J
​inherit​ ​from​ ​the​ ​object​ ​class,​ ​instead​ ​exists​ ​the​ ​classes​ ​inherit​ ​in​ ​one​ ​or​ ​the​ ​other​ ​way​ ​from​ ​the​
​on its own.​ ​object class.​

​94. Is there any limitation to using Inheritance?​


​ es,​ ​there​ ​is​ ​a​ ​limitation​ ​of​ ​using​ ​Inheritance​ ​in​ ​Java,​ ​as​ ​because​ ​of​ ​inheritance​ ​one​ ​can​ ​inherit​
Y
​everything​ ​from​ ​super​ ​class​ ​and​ ​interface​ ​because​ ​of​ ​which​ ​subclass​ ​is​ ​too​ ​clustered​ ​and​​sometimes​
​error-prone when dynamic overriding or dynamic overloading is done in certain situations.​
​95.​​Although​​inheritance​​is​​a​​popular​​OOPs​​concept,​​it​​is​​less​​advantageous​​than​
​ omposition. Explain.​
c
I​nheritance​​is​​a​​popular​​concept​​of​​Object-Oriented​​Programming​​(OOP),​​in​​which​​a​​class​​can​​inherit​​the​
​properties​ ​and​ ​methods​ ​from​ ​any​ ​other​ ​class,​ ​which​ ​is​ ​referred​ ​to​ ​as​ ​a​ ​Parent​ ​or​ ​superclass.​ ​On​ ​the​
​other​​hand​​in​​Composition,​​a​​class​​can​​contain​​an​​instance​​of​​another​​class​​as​​a​​member​​variable​​which​
​is​ ​often​ ​referred​ ​to​ ​as​ ​part​ ​or​ ​a​ ​component.​ ​Below​ ​are​ ​some​ ​reasons​ ​why​ ​composition​ ​is​ ​more​
​advantageous than inheritance:​
​●​ ​Tight​​Coupling:​ ​Whenever​​any​​changes​​are​​made​​to​​the​​superclass,​​these​​changes​​can​​affect​
​the​ ​behavior​ ​of​ ​all​ ​its​ ​child​ ​or​ ​Subclasses.​ ​This​ ​problem​ ​makes​ ​code​ ​less​ ​flexible​ ​and​ ​also​
​creates​ ​issues​ ​during​ ​maintenance.​ ​This​ ​problem​ ​also​ ​leads​ ​to​ ​the​ ​Tight​​coupling​​between​​the​
​classes.​
​●​ ​Fragile​​Base​​Class​​Problem:​​When​​the​​changes​​to​​the​​base​​class​​can​​break​​the​​functionality​
​of​ ​its​ ​derived​ ​classes.​ ​This​ ​problem​ ​can​ ​make​ ​it​ ​difficult​ ​to​ ​add​ ​new​ ​features​ ​or​ ​modify​ ​the​
​existing ones. This problem is known as the Fragile Base class problem.​
​ 2​
2
​ ​ ​Limited​ ​Reuse:​ ​Inheritance​ ​in​ ​Java​​can​​lead​​to​​limited​​code​​reuse​​and​​also​​code​​duplication.​

​As​​a​​subclass​​inherits​​all​​the​​properties​​and​​methods​​of​​its​​superclass,​​sometimes​​it​​may​​end​​up​
​with unnecessary code which is not needed. This leads to a less maintainable codebase.​

​The​ ​association​ ​is​ ​a​ ​relation​ ​between​ ​two​ ​separate​ ​classes​ ​established​ ​through​ ​their​ ​Objects.​ ​It​
r​ epresents Has-A’s relationship.​
​97. What do you mean by aggregation?​
​Composition​​is​​a​​restricted​​form​​of​​Aggregation​​in​​which​​two​​entities​​are​​highly​​dependent​​on​​each​​other.​
I​t represents part-of the relationship.​
​98. What is the composition of Java?​
​ omposition​​implies​​a​​relationship​​where​​the​​child​​cannot​​exist​​independently​​of​​the​​parent.​​For​​example​
C
​Human heart, the heart doesn’t exist separately from a Human.​
​99. State the difference between Composition and Aggregation.​
​Aggregation​ ​Composition​

I​t​ ​defines​ ​a​ ​“has​ ​a”​ ​relationship​ ​between​ ​the​


​It represents the part-of relationship​
​objects​

​Objects are independent of each other.​ ​Objects are dependent on each other.​

​Represent it by using the filled diamond.​ ​Represent it by using the empty diamond.​

​Child objects don’t have a lifetime.​ ​Child objects have a lifetime.​

​100. Can the constructor be inherited?​


​No, we can’t inherit a constructor.​
​101. What is Polymorphism?​
​ olymorphism​​is​​defined​​as​​the​​ability​​to​​take​​more​​than​​one​​form​​It​​is​​of​​two​​types​​namely,​​Compile​​time​
P
​polymorphism​ ​or​ ​method​ ​overloading-​ ​a​​function​​called​​during​​compile​​time.​​For​​instance,​​take​​a​​class​
​‘area’.​​Based​​on​​the​​number​​of​​parameters​​it​​may​​calculate​​the​​area​​of​​a​​square,​​triangle,​​or​​circle.​​Run​
​time​​polymorphism​​or​​method​​overriding-​​links​​during​​run​​time.​ ​The​​method​​inside​​a​​class​​overrides​​the​
​method of the parent class.​
​102. What is runtime polymorphism or dynamic method dispatch?​
​ ynamic​ ​method​ ​dispatch​ ​is​​a​​resolving​​mechanism​​for​​method​​overriding​​during​​the​​run​​time.​​Method​
D
​overriding​​is​​the​​one​​where​​the​​method​​in​​a​​subclass​​has​​the​​same​​name,​​parameters,​​and​​return​​type​
​as​ ​a​​method​​in​​the​​superclass.​​When​​the​​over-ridden​​method​​is​​called​​through​​a​​superclass​​reference,​
​java​​determines​​which​​version​​(superclass​​or​​subclass)​​of​​that​​method​​is​​to​​be​​executed​​based​​upon​​the​
​type​​of​​an​​object​​being​​referred​​to​​at​​the​​time​​the​​call​​occurs.​​Thus​​the​​decision​​is​​made​​at​​run​​time.​​This​
​is referred to as dynamic method dispatch.​
​103. What is method overriding?​
​ ethod​​overriding,​​also​​known​​as​​run​​time​​polymorphism​​is​​one​​where​​the​​child​​class​​contains​​the​​same​
M
​method​​as​​the​​parent​​class.​​For​​instance,​​we​​have​​a​​method​​named​​‘gfg()’​​in​​the​​parent​​class.​​A​​method​
​gfg()​​is​​again​​defined​​in​​the​​sub-class.​​Thus​​when​​gfg()​​is​​called​​in​​the​​subclass,​​the​​method​​within​​the​
​class id executed. Here, gfg() within the class overridden the method outside.​
​104. What is method overloading?​
​23​

​ ethod​​overriding​​is​​a​​method​​to​​achieve​​Run-time​​polymorphism​​in​​Java.​​Method​​overriding​​is​​a​​feature​
M
​that​​allows​​a​​child​​class​​to​​provide​​a​​specific​​implementation​​of​​a​​method​​that​​is​​already​​provided​​by​​one​
​of​ ​its​ ​parent​ ​classes.​ ​When​ ​a​ ​method​ ​in​ ​a​ ​child​ ​class​ ​has​ ​the​ ​same​ ​name,​ ​the​ ​same​ ​parameters​ ​or​
​signature,​​and​​the​​same​​return​​type(or​​sub-type)​​as​​a​​method​​in​​its​​parent​​class,​​then​​the​​method​​in​​the​
​subclass is said to override the method in the superclass.​
​105. Can we override the static method?​
​No, as static methods are part of the class rather than the object so we can’t override them.​
​106. Can we override the overloaded method?​
​Yes,​ ​since​ ​the​ ​overloaded​ ​method​ ​is​ ​a​ ​completely​ ​different​ ​method​ ​in​ ​the​ ​eyes​ ​of​ ​the​ ​compiler.​
​ verriding isn’t the same thing at all. The decision as to which method to call is deferred to runtime.​
O
​107. Can we overload the main() method?​
​Yes​ ​in​ ​Java​ ​we​ ​can​ ​overload​ ​the​ ​main​ ​method​ ​to​ ​call​ ​the​ ​main​​method​​with​​the​​help​​of​​its​​predefined​
​ alling method.​
c
​108. What are method overloading and method overriding?​
​Method​​Overloading​​:​​It​​is​​also​​known​​as​​Compile​​Time​​Polymorphism.​​In​​method​​overloading​​two​​or​
​more methods are shared in the same class with a different signature.​
​….​

​class GFG {​
​static int multiply(int a, int b) { return a * b; }​
​static int multiply(int a, int b, int c)​
​{​
​return a * b * c;​
​}​
​static int multiply(int a, int b, int c, int d)​
​{​
​return a * b * c * d;​
​}​
​public static void main(String[] args)​
​{​
​System.out.println("multiply() with 2 parameters");​
​System.out.println(multiply(4, 5));​
​System.out.println("multiply() with 3 parameters");​
​System.out.println(multiply(2, 3, 4));​
​System.out.println("multiply() with 4 parameters");​
​System.out.println(multiply(2, 3, 4, 1));​
​}​
​}​

​ utput​
O
​multiply() with 2 parameters​
​20​
​multiply() with 3 parameters​
​24​
​multiply() with 4 parameters​
​24​
​ 4​
2
​Method​​Overriding:​ ​Method​​Overriding​​occurs​​when​​a​​subclass​​can​​provide​​the​​implementation​​of​​a​
​method​​which​​is​​already​​defined​​in​​the​​parent​​class​​or​​superclass.​​The​​return​​type,​​name​​and​​arguments​
​must be similar to the methods in superclass.​
​Example:​
​ ​Java​

/​/ Java Program to demonstrate use of Method Overriding​


​import java.io.*;​
​class Vehicle {​
​void drive()​
​{​
​System.out.println("drive() method of base class");​
​System.out.println("driving the Car.");​
​}​
​}​
​class Car extends Vehicle {​
​void drive()​
​{​
​System.out.println(​
​"drive() method of derived class");​
​System.out.println("Car is driving.");​
​}​
​}​
​class GFG {​
​public static void main(String[] args)​
​{​
​Car c1 = new Car();​
​Vehicle v1 = new Vehicle();​
​c1.drive();​
​v1.drive();​
​Vehicle vehicle = new Car();​
​// drive() method of Vehicle class is overridden by​
​// Car class drive()​
​vehicle.drive();​
​}​
​}​

​ utput​
O
​drive() method of derived class​
​Car is driving.​
​drive() method of base class​
​driving the Car.​
​drive() method of derived class​
​Car is driving.​
​Method Overloading​ ​Method Overriding​

​ hen​ ​a​ ​subclass​ ​provides​ ​its​ ​own​


W
​ hen​ ​two​ ​or​ ​multiple​ ​methods​ ​are​ ​in​ ​the​ ​same​
W
​implementation​ ​of​ ​a​ ​method​ ​that​​is​​already​
​class with different parameters but the same name.​
​defined in the parent class.​
​25​

​ ethod​ ​overloading​ ​can​ ​only​ ​happen​ ​in​ ​the​ ​same​


M ​ ethod​ ​overriding​ ​can​ ​only​ ​happen​ ​in​
M
​class or between a subclass or parent class.​ ​Subclass.​

​ hen​ ​an​ ​error​ ​occurs​ ​it​ ​is​ ​caught​ ​at​ ​the​ ​compile​
W ​ hen​ ​an​ ​error​ ​occurs​ ​it​ ​is​ ​caught​ ​at​
W
​time of the program.​ ​Runtime of the program.​

​Example of Compile Time Polymorphism.​ ​Example of Run Time Polymorphism.​

​ ethod​ ​Overloading​ ​may​ ​or​ ​may​ ​not​ ​require​


M ​ ethod​
M ​overriding​ ​always​ ​needs​
​Inheritance.​ ​Inheritance.​

I​t​ ​is​ ​performed​ ​in​ ​two​ ​classes​ ​with​ ​an​


​It occurs within the class.​
​inheritance relationship.​

​109. Can we override the private methods?​


I​t​​is​​not​​possible​​to​​override​​the​​private​​methods​​in​​Java.​​Method​​overriding​​is​​where​​the​​method​​in​​the​
​subclass​ ​is​ ​implemented​ ​instead​ ​of​ ​the​ ​method​ ​from​ ​the​ ​parent​ ​class.​ ​The​ ​private​ ​methods​ ​are​
​accessible​​only​​within​​the​​class​​in​​which​​it​​is​​declared.​​Since​​this​​method​​is​​not​​visible​​to​​other​​classes​
​and cannot be accessed, it cannot be overridden.​
​110. Can we change the scope of the overridden method in the subclass?​
I​n​​Java,​​it​​is​​not​​possible​​to​​modify​​the​​overridden​​method’s​​scope.​​The​​subclass​​method’s​​scope​​must​
​be​ ​equal​​to​​or​​wider​​than​​the​​Superclass​​method’s​​overridden​​method’s​​scope.​​The​​overridden​​method​
​in​ ​the​ ​subclass,​ ​for​ ​instance,​ ​can​ ​have​ ​a​ ​public​ ​scope​ ​or​ ​a​ ​more​ ​accessible​ ​scope​ ​like​ ​protected​ ​or​
​default​​if​​the​​overridden​​method​​in​​the​​superclass​​has​​a​​public​​scope.​​It​​cannot,​​however,​​have​​a​​more​
​exclusive scope like private.​
​111.​​Can​​we​​modify​​the​​throws​​clause​​of​​the​​superclass​​method​​while​​overriding​​it​​in​​the​
​ ubclass?​
s
​ e​ ​can​ ​modify​ ​the​ ​throws​ ​clause​ ​of​ ​the​ ​Superclass​​method​​with​​some​​limitations,​​we​​can​​change​​the​
W
​throws​ ​clause​ ​of​ ​the​ ​superclass​ ​method​ ​while​ ​overriding​ ​it​ ​in​ ​the​ ​subclass.​ ​The​ ​subclass​ ​overridden​
​method​ ​can​ ​only​ ​specify​ ​unchecked​ ​exceptions​ ​if​ ​the​ ​superclass​ ​method​ ​does​ ​not​ ​declare​ ​any​
​exceptions.​​If​​the​​superclass​​method​​declares​​an​​exception,​​the​​subclass​​method​​can​​declare​​the​​same​
​exception,​​a​​subclass​​exception,​​or​​no​​exception​​at​​all.​​However,​​the​​subclass​​method​​cannot​​declare​​a​
​parent exception that is broader than the ones declared in the superclass method.​
​112. Can you have virtual functions in Java?​
​Yes,​​Java​​supports​​virtual​​functions.​​Functions​​are​​by​​default​​virtual​​and​​can​​be​​made​​non-virtual​​using​
t​he final keyword.​
​113. What is Abstraction?​
​ bstraction​​refers​​to​​the​​act​​of​​representing​​essential​​features​​without​​including​​background​​details.​​The​
A
​detailed​​information​​or​​the​​implementation​​is​​hidden.​​The​​most​​common​​example​​of​​abstraction​​is​​a​​car,​
​we​ ​know​ ​how​ ​to​ ​turn​ ​on​ ​the​ ​engine,​ ​accelerate​ ​and​ ​move,​ ​however,​ ​the​ ​way​ ​engine​ ​works,​ ​and​ ​its​
​internal​​components​​are​​complex​​logic​​hidden​​from​​the​​general​​users.​​This​​is​​usually​​done​​to​​handle​​the​
​complexity.​
​114. What is Abstract class?​
​ ​​class​​declared​​as​​abstract,​​cannot​​be​​instantiated​​i.e.,​​the​​object​​cannot​​be​​created.​​It​​may​​or​​may​​not​
A
​contain abstract methods but if a class has at least one abstract method, it must be declared abstract.​
​Example of an abstract class with abstract method:​

/​/ Java Program to implement​


​// abstract method​
​import java.io.*;​
​// Abstract class​
​abstract class Fruits {​
​26​

​abstract void run();​


}​ ​
​// Driver Class​
​class Apple extends Fruits {​
​void run()​
​{​
​System.out.println("Abstract class example");​
​}​
​// main method​
​public static void main(String args[])​
​{​
​Fruits obj = new Apple();​
​obj.run();​
​}​
​}​

​115. When Abstract methods are used?​


​An​ ​abstract​ ​method​ ​is​ ​used​ ​when​ ​we​ ​want​ ​to​ ​use​ ​a​ ​method​ ​but​ ​want​ ​to​ ​child​ ​classes​ ​to​ ​decide​ ​the​
i​mplementation in that case we use Abstract methods with the parent classes.​
​ 16.​ ​How​ ​can​ ​you​ ​avoid​ ​serialization​ ​in​ ​the​ ​child​ ​class​ ​if​ ​the​ ​base​ ​class​ ​is​
1
​implementing the Serializable interface?​
​Serialization​ ​in​ ​the​ ​child​ ​class​ ​if​ ​the​ ​base​ ​class​ ​is​​implementing​​the​​Serializable​​interface​​then​​we​​can​
​ void it by defining the writeObject() method and throwing NotSerializableException().​
a
​117. What is Collection Framework in Java?​
​ ollections​ ​are​ ​units​ ​of​ ​objects​ ​in​ ​Java.​ ​The​ ​collection​ ​framework​ ​is​ ​a​​set​​of​​interfaces​​and​​classes​​in​
C
​Java​​that​​are​​used​​to​​represent​​and​​manipulate​​collections​​of​​objects​​in​​a​​variety​​of​​ways.​​The​​collection​
​framework​​contains​​classes(ArrayList,​​Vector,​​LinkedList,​​PriorityQueue,​​TreeSet)​​and​​multiple​​interfaces​
​(Set, List, Queue, Deque) where every interface is used to store a specific type of data.​
​118. Explain various interfaces used in the Collection framework.​
​Collection framework implements​
​1.​ ​Collection Interface​
​2.​ ​List Interface​
​3.​ ​Set Interface​
​4.​ ​Queue Interface​
​5.​ ​Deque Interface​
​6.​ ​Map Interface​
​Collection​ ​interface:​ ​Collection​ ​is​ ​the​ ​primary​ ​interface​ ​available​ ​that​ ​can​ ​be​ ​imported​ ​using​
​java.util.Collection.​
​Syntax:​
​public interface Collection<E> extends iterable​
​119. How can you synchronize an ArrayList in Java?​
​An ArrayList can be synchronized using two methods mentioned below:​
​1.​ ​Using Collections.synchronizedList()​
​2.​ ​Using CopyOnWriteArrayList​
​Using Collections.synchronizedList():​
​public static List<T> synchronizedList(List<T> list)​
​Using CopyOnWriteArrayList:​
​1.​ ​Create an empty List.​
​2.​ ​It implements the List interface​
​3.​ ​It is a thread-safe variant of ArrayList​
​4.​ ​T represents generic​
​ 20.​ ​Why​​do​​we​​need​​a​​synchronized​​ArrayList​​when​​we​​have​​Vectors​​(which​​are​
1
​synchronized) in Java?​
​ArrayList is in need even when we have Vectors because of certain reasons:​
​1.​ ​ArrayList is faster than Vectors.​
​2.​ ​ArrayList supports multithreading whereas Vectors only supports single-thread use.​
​ 7​
2
​3.​ ​ArrayList​ ​is​ ​safer​ ​to​ ​use,​ ​as​ ​Vectors​ ​supports​ ​single​ ​threads​ ​and​ ​individual​​operations​​are​​less​
​safe and take longer to synchronize.​
​ .​ ​Vectors are considered outdated in Java because of their synchronized nature.​
4
​121. Why can’t we create a generic array?​
​ eneric​ ​arrays​ ​can’t​ ​be​ ​created​ ​because​ ​an​ ​array​ ​carries​ ​type​ ​information​ ​of​ ​its​ ​elements​ ​at​ ​runtime​
G
​because​​of​​which​​during​​runtime​​it​​throw​​‘ArrayStoreException’​​if​​the​​elements’​​type​​is​​not​​similar.​​Since​
​generics​ ​type​ ​information​ ​gets​ ​erased​ ​at​ ​compile​ ​time​ ​by​ ​Type​ ​Erasure,​ ​the​ ​array​ ​store​ ​check​ ​would​
​have been passed where it should have failed.​
​ 22.​ ​Contiguous​ ​memory​ ​locations​ ​are​ ​usually​ ​used​ ​for​ ​storing​ ​actual​
1
​values in an array but not in ArrayList. Explain.​
​ he​​elements​​of​​an​​array​​are​​stored​​in​​contiguous​​memory​​locations,​​which​​means​​that​​each​​element​​is​
T
​stored​​in​​a​​separate​​block​​based​​on​​it​​located​​within​​the​​array.​​Since​​the​​elements​​of​​the​​array​​are​​stored​
​in​ ​contiguous​ ​locations,​ ​it​ ​can​ ​be​ ​relatively​ ​easy​ ​to​ ​access​ ​any​ ​element​ ​by​ ​its​ ​index,​ ​as​ ​the​ ​element​
​address​ ​can​ ​be​ ​calculated​ ​based​ ​on​ ​the​ ​location​ ​of​ ​the​ ​element.​ ​But​ ​Java​ ​implements​ ​ArrayLists​ ​as​
​dynamic​ ​arrays,​ ​which​ ​means​ ​that​ ​the​ ​size​ ​can​ ​change​ ​as​ ​elements​ ​are​​removed​​or​​added.​​ArrayList​
​elements​ ​are​​not​​stored​​in​​contiguous​​memory​​locations​​in​​order​​to​​accommodate​​this​​dynamic​​nature.​
​Instead,​ ​the​ ​ArrayList​ ​makes​ ​use​ ​of​ ​a​ ​method​ ​known​ ​as​ ​an​ ​expandable​ ​array​​in​​which​​the​​underlying​
​array​ ​is​ ​expanded​ ​to​​a​​larger​​size​​as​​needed​​and​​the​​elements​​are​​then​​copied​​to​​the​​new​​location.​​In​
​contrast​ ​to​ ​an​ ​ArrayList,​ ​which​ ​has​ ​a​ ​dynamic​ ​size​ ​and​ ​does​ ​not​ ​store​ ​its​ ​elements​ ​in​ ​contiguous​
​memory locations, an array has a fixed size and its elements are stored there.​
​123. Explain the method to convert ArrayList to Array and Array to ArrayList.​
​ onversion of List to ArrayList​
C
​There are multiple methods to convert List into ArrayList​

​Programmers​ ​can​ ​convert​ ​an​ ​Array​ ​to​ ​ArrayList​​using​​asList()​​method​​of​​the​​Arrays​​class.​​It​​is​​a​​static​


​ ethod of the Arrays class that accepts the List object.​
m
​Syntax:​
​Arrays.asList(item)​

/​/ Java program to demonstrate conversion of​


​// Array to ArrayList of fixed-size.​
​import java.util.*;​
​// Driver Class​
​class GFG {​
​// Main Function​
​public static void main(String[] args)​
​{​
​String[] temp = { "Abc", "Def", "Ghi", "Jkl" };​
​// Conversion of array to ArrayList​
​// using Arrays.asList​
​List conv = Arrays.asList(temp);​
​System.out.println(conv);​
​}​
​}​

​ utput​
O
​[Abc, Def, Ghi, Jkl]​
​28​
​Conversion of ArrayList to Array​

​ ava programmers can convert ArrayList to​


J
​Syntax:​
​List_object.toArray(new String[List_object.size()])​

/​/ Java program to demonstrate working of​


​// Objectp[] toArray()​
​import java.io.*;​
​import java.util.List;​
​import java.util.ArrayList;​
​// Driver Class​
​class GFG {​
​// Main Function​
​public static void main(String[] args)​
​{​
​// List declared​
​List<Integer>​
​arr = new ArrayList<Integer>();​
​arr.add(1);​
​arr.add(2);​
​arr.add(3);​
​arr.add(2);​
​arr.add(1);​
​// Conversion​
​Object[] objects = arr.toArray();​
​// Printing array of objects​
​for (Object obj : objects)​
​System.out.print(obj + " ");​
​}​
​}​

​ utput​
O
​1 2 3 2 1 1​
​ 24.​​How​​does​​the​​size​​of​​ArrayList​​grow​​dynamically?​​And​​also​​state​​how​​it​
1
​is implemented internally.​
​ ue​​to​​ArrayLists​​array-based​​nature,​​it​​grows​​dynamically​​in​​size​​ensuring​​that​​there​​is​​always​​enough​
D
​room​ ​for​ ​elements.​ ​When​ ​an​ ​ArrayList​ ​element​ ​is​ ​first​ ​created,​ ​the​ ​default​ ​capacity​ ​is​ ​around​ ​10-16​
​elements​ ​which​ ​basically​ ​depends​ ​on​ ​the​ ​Java​ ​version.​ ​ArrayList​ ​elements​ ​are​ ​copied​ ​over​ ​from​ ​the​
​original​ ​array​ ​to​ ​the​ ​new​ ​array​ ​when​ ​the​ ​capacity​ ​of​ ​the​ ​original​ ​array​ ​is​ ​full.​ ​As​ ​the​ ​ArrayList​ ​size​
​increases​​dynamically,​​the​​class​​creates​​a​​new​​array​​of​​bigger​​sizes​​and​​it​​copies​​all​​the​​elements​​from​
​the​ ​old​ ​array​ ​to​ ​the​ ​new​ ​array.​ ​Now,​ ​the​ ​reference​ ​of​ ​the​​new​​array​​is​​used​​internally.​​This​​process​​of​
​dynamically growing an array is known as resizing.​
​125. What is a Vector in Java?​
​Vectors​ ​in​ ​Java​ ​are​ ​similar​ ​and​ ​can​ ​store​ ​multiple​ ​elements​ ​inside​ ​them.​ ​Vectors​ ​follow​ ​certain​ ​rules​
​ entioned below:​
m
​1.​ ​Vector can be imported using Java.util.Vector.​
​2.​ ​Vector​​is​​implemented​​using​​a​​dynamic​​array​​as​​the​​size​​of​​the​​vector​​increases​​and​​decreases​
​depending upon the elements inserted in it.​
​3.​ ​Elements of the Vector using index numbers.​
​ 9​
2
​4.​ ​Vectors​ ​are​ ​synchronized​ ​in​ ​nature​ ​means​​they​​only​​used​​a​​single​​thread​​(​​only​​one​​process​​is​
​performed at a particular time ).​
​5.​ ​The vector contains many methods that are not part of the collections framework.​
​Syntax:​
​Vector gfg = new Vector(size, increment);​
​126. How to make Java ArrayList Read-Only?​
​An​ ​ArrayList​ ​can​ ​be​ ​made​ ​ready​ ​only​ ​using​ ​the​ ​method​ ​provided​ ​by​ ​Collections​ ​using​ ​the​
​ ollections.unmodifiableList() method.​
C
​Syntax:​
​array_readonly = Collections.unmodifiableList(ArrayList);​

/​/ Java program to demonstrate​


​// unmodifiableList() method​
​import java.util.*;​
​public class Main {​
​public static void main(String[] argv) throws Exception​
​{​
​try {​
​// creating object of ArrayList​
​<Character> ArrayList<Character>​
​temp​
​= new ArrayList<Character>();​
​// populate the list​
​temp.add('X');​
​temp.add('Y');​
​temp.add('Z');​
​// printing the list​
​System.out.println("Initial list: " + temp);​
​// getting readonly list​
​// using unmodifiableList() method​
​List<Character>​
​new_array​
​= Collections.unmodifiableList(temp);​
​// printing the list​
​System.out.println("ReadOnly ArrayList: "​
​+ new_array);​
​// Adding element to new Collection​
​System.out.println("\nIf add element in "​
​+ " the ReadOnly ArrayList");​
​new_array.add('A');​
​}​
​catch (UnsupportedOperationException e) {​
​System.out.println("Exception is thrown : "​
​+ e);​
​}​
​}​
​}​

​ utput​
O
​Initial list: [X, Y, Z]​
​ReadOnly ArrayList: [X, Y, Z]​
​If add element in the ReadOnly ArrayList​
​Exception is thrown : java.lang.UnsupportedOperationException​
​30​
​127. What is a priority queue in Java?​

​ ​ ​priority​ ​queue​ ​is​ ​an​ ​abstract​ ​data​ ​type​ ​similar​ ​to​ ​a​ ​regular​ ​queue​ ​or​ ​stack​ ​data​ ​structure.​ ​Elements​
A
​stored​​in​​elements​​are​​depending​​upon​​the​​priority​​defined​​from​​low​​to​​high.​​The​​PriorityQueue​​is​​based​
​on the priority heap.​
​Syntax:​
​ ​Java​

/​/ Java program to demonstrate the​


​// working of PriorityQueue​
​import java.util.*;​
​class PriorityQueueDemo {​
​// Main Method​
​public static void main(String args[])​
​{​
​// Creating empty priority queue​
​PriorityQueue<Integer>​
​var1​
​= new PriorityQueue<Integer>();​
​// Adding items to the pQueue using add()​
​var1.add(10);​
​var1.add(20);​
​var1.add(15);​
​// Printing the top element of PriorityQueue​
​System.out.println(var1.peek());​
​}​
​}​

​ utput​
O
​10​
​128. Explain the LinkedList class.​
​LinkedList​​class​​is​​Java​​that​​uses​​a​​doubly​​linked​​list​​to​​store​​elements.​​It​​inherits​​the​​AbstractList​​class​
​ nd implements List and Deque interfaces. Properties of the LinkedList Class are mentioned below:​
a
​1.​ ​LinkedList classes are non-synchronized.​
​2.​ ​Maintains insertion order.​
​3.​ ​It can be used as a list, stack, or queue.​
​Syntax:​
​LinkedList<class> list_name=new LinkedList<class>();​

​129. What is the Stack class in Java and what are the various methods provided by it?​
​ ​​Stack​​class​​in​​Java​​is​​a​​LIFO​​data​​structure​​that​​implements​​the​​Last​​In​​First​​Out​​data​​structure.​​It​​is​
A
​derived​ ​from​ ​a​ ​Vector​ ​class​ ​but​ ​has​ ​functions​ ​specific​ ​to​ ​stacks.​ ​The​ ​Stack​ ​class​ ​in​​java​​provides​​the​
​following methods:​
​●​ ​peek(): returns the top item from the stack without removing it​
​●​ ​empty(): returns true if the stack is empty and false otherwise​
​●​ ​push(): pushes an item onto the top of the stack​
​31​
​​ p
● ​ op(): removes and returns the top item from the stack​
​●​ ​search():​​returns​​the​​1,​​based​​position​​of​​the​​object​​from​​the​​top​​of​​the​​stack.​​If​​the​​object​​is​​not​​in​
​the stack, it returns -1​
​ 30.​ ​What​ ​is​ ​Set​ ​in​ ​the​ ​Java​ ​Collections​ ​framework​ ​and​ ​list​ ​down​ ​its​ ​various​
1
​implementations?​
​Sets​​are​​collections​​that​​don’t​​store​​duplicate​​elements.​​They​​don’t​​keep​​any​​order​​of​​the​​elements.​​The​
​ ava Collections framework provides several implementations of the Set interface, including:​
J
​●​ ​HashSet:​​HashSet​​in​​Java,​​stores​​the​​elements​​in​​a​​has​​table​​which​​provides​​faster​​lookups​​and​
​faster insertion. HashSet is not ordered.​
​●​ ​LinkedHashSet:​ ​LinkedHashSet​ ​is​ ​an​ ​implementation​ ​of​ ​HashSet​ ​which​​maintains​​the​​insertion​
​order of the elements.​
​●​ ​TreeSet:​​TreeSet​​stores​​the​​elements​​in​​a​​sorted​​order​​that​​is​​determined​​by​​the​​natural​​ordering​
​of the elements or by a custom comparator provided at the time of creation.​
​131. What is the HashSet class in Java and how does it store elements?​
​ he​​HashSet​​class​​implements​​the​​Set​​interface​​in​​the​​Java​​Collections​​Framework​​and​​is​​a​​member​​of​
T
​the​ ​HashSet​ ​class.​ ​Unlike​ ​duplicate​ ​values,​ ​it​ ​stores​ ​a​ ​collection​ ​of​ ​distinct​ ​elements.​ ​In​ ​this​
​implementation,​​each​​element​​is​​mapped​​to​​an​​index​​in​​an​​array​​using​​a​​hash​​function,​​and​​the​​index​​is​
​used​​to​​quickly​​access​​the​​element.​​It​​produces​​an​​index​​for​​the​​element​​in​​the​​array​​where​​it​​is​​stored​
​based​ ​on​ ​the​ ​input​ ​element.​ ​Assuming​ ​the​ ​hash​ ​function​ ​distributes​ ​the​ ​elements​ ​among​ ​the​ ​buckets​
​appropriately,​​the​​HashSet​​class​​provides​​constant-time​​performance​​for​​basic​​operations​​(add,​​remove,​
​contain, and size).​
​132. What is LinkedHashSet in Java Collections Framework?​
​ he​ ​LinkedHashSet​ ​is​ ​an​ ​ordered​ ​version​ ​of​​Hashset​​maintained​​by​​a​​doubly-linked​​List​​across​​all​​the​
T
​elements.​​It​​is​​very​​helpful​​when​​iteration​​order​​is​​needed.​​During​​Iteration​​in​​LinkedHashSet,​​elements​
​are returned in the same order they are inserted.​
​Syntax:​
​LinkedHashSet<E> hs = new LinkedHashSet<E>();​

/​/ Java Program to implement​


​// LinkedHashSet​
​import java.io.*;​
​import java.util.*;​
​// Driver Class​
​class GFG {​
​// Main Function​
​public static void main(String[] args)​
​{​
​// LinkedHashSet declared​
​LinkedHashSet<Integer>​
​hs = new LinkedHashSet<Integer>();​
​// Add elements in HashSet​
​hs.add(1);​
​hs.add(2);​
​hs.add(5);​
​hs.add(3);​
​// Print values​
​System.out.println("Values:" + hs);​
​}​
​}​

​ utput​
O
​Values:[1, 2, 5, 3]​
​32​

​133. What is a Map interface in Java?​

​ he​ ​map​ ​interface​ ​is​ ​present​ ​in​ ​the​ ​Java​ ​collection​ ​and​ ​can​ ​be​ ​used​ ​with​ ​Java.util​ ​package.​ ​A​ ​map​
T
​interface​​is​​used​​for​​mapping​​values​​in​​the​​form​​of​​a​​key-value​​form.​​The​​map​​contains​​all​​unique​​keys.​
​Also, it provides methods associated with it like containsKey(), contains value (), etc.​
​There are multiple types of maps in the map interface as mentioned below:​
​1.​ ​SortedMap​
​2.​ ​TreeMap​
​3.​ ​HashMap​
​4.​ ​LinkedHashMap​
​134. Explain Treemap in Java​
​TreeMap​ ​is​ ​a​ ​type​ ​of​ ​map​ ​that​ ​stores​ ​data​ ​in​ ​the​ ​form​ ​of​ ​key-value​ ​pair.​ ​It​ ​is​ ​implemented​ ​using​ ​the​
r​ ed-black tree. Features of TreeMap are :​
​1.​ ​It contains only unique elements.​
​2.​ ​It cannot have a NULL key​
​3.​ ​It can have multiple NULL values.​
​4.​ ​It is non-synchronized.​
​5.​ ​It maintains ascending order.​
​135. What is EnumSet?​
​EnumSet​ ​is​ ​a​ ​specialized​ ​implementation​ ​of​ ​the​ ​Set​ ​interface​ ​for​ ​use​ ​with​ ​enumeration​ ​type.​ ​A​ ​few​
f​eatures of EnumSet are:​
​1.​ ​It is non-synchronized.​
​2.​ ​Faster than HashSet.​
​3.​ ​All of the elements in an EnumSet must come from a single enumeration type.​
​4.​ ​It doesn’t allow null Objects and throws NullPointerException for exceptions.​
​5.​ ​It uses a fail-safe iterator.​
​Syntax:​
​public abstract class EnumSet<E extends Enum<E>>​
​Parameter: E specifies the elements.​
​136. What is BlockingQueue?​
​ 3​
3
​A​​blocking​​queue​​is​​a​​Queue​​that​​supports​​the​​operations​​that​​wait​​for​​the​​queue​​to​​become​​non-empty​
​while​ ​retrieving​ ​and​ ​removing​ ​the​ ​element,​ ​and​ ​wait​ ​for​ ​space​​to​​become​​available​​in​​the​​queue​​while​
​adding the element.​
​Syntax:​
​public interface BlockingQueue<E> extends Queue<E>​
​Parameters: E is the type of elements stored in the Collection​
​137. What is the ConcurrentHashMap in Java and do you implement it?​
​ oncurrentHashMap is implemented using Hashtable.​
C
​Syntax:​
​public class ConcurrentHashMap<K, ​V>​
​extends AbstractMap<K, ​V>​
​implements ConcurrentMap<K, ​V>, Serializable​
​Parameters: K is the key Object type and V is the value Object type​
​138. Can you use any class as a Map key?​
​Yes, we can use any class as a Map Key if it follows certain predefined rules mentioned below:​
​1.​ ​The class overriding the equals() method must also override the hashCode() method​
​2.​ ​The concurrentHashMap class is thread-safe.​
​3.​ ​The default concurrency level of ConcurrentHashMap is 16.​
​4.​ ​Inserting null objects in ConcurrentHashMap is not possible as a key or as value.​
​139. What is an Iterator?​

​ he​​Iterator​​interface​​provides​​methods​​to​​iterate​​over​​any​​Collection​​in​​Java.​​Iterator​​is​​the​​replacement​
T
​of​​Enumeration​​in​​the​​Java​​Collections​​Framework.​​It​​can​​get​​an​​iterator​​instance​​from​​a​​Collection​​using​
​the​ ​_iterator()_​ ​method.​ ​It​ ​also​ ​allows​ ​the​ ​caller​ ​to​ ​remove​ ​elements​ ​from​ ​the​ ​underlying​ ​collection​
​during the iteration.​
​140. What is an enumeration?​
​ numeration​ ​is​ ​a​ ​user-defined​ ​data​ ​type.​ ​It​ ​is​ ​mainly​ ​used​ ​to​ ​assign​ ​names​ ​to​ ​integral​ ​constants,​ ​the​
E
​names​ ​make​ ​a​ ​program​ ​easy​ ​to​ ​read​ ​and​ ​maintain.​ ​The​ ​main​ ​objective​ ​of​ ​the​ ​enum​ ​is​ ​to​ ​define​
​user-defined data types.​
​Example:​
​// A simple enum example where enum is declared​
​// outside any class (Note enum keyword instead of​
​// class keyword)​
​enum Color​
​{​
​RED, GREEN, BLUE;​
​}​
​141. What is the difference between Collection and Collections?​
​Collection​ ​Collections​

​The Collection is an Interface.​ ​Collections is a class.​

I​t​ ​provides​ ​the​ ​standard​ ​functionality​ ​of​ ​data​ I​t​ ​is​ ​to​ ​sort​ ​and​ ​synchronize​ ​the​ ​collection​
​structure.​ ​elements.​
​34​

I​t​ ​provides​ ​the​ ​methods​ ​that​ ​can​ ​be​ ​used​ ​for​ I​t​ ​provides​ ​static​ ​methods​ ​that​ ​can​ ​be​ ​used​ ​for​
​the data structure.​ ​various operations.​

​142. Differentiate between Array and ArrayList in Java.​


​Array​ ​ArrayList​

​Single-dimensional or multidimensional​ ​Single-dimensional​

​For and for each used for iteration​ ​Here iterator is used to traverse riverArrayList​

​ ize()​ ​method​ ​is​ ​used​ ​to​ ​compute​ ​the​ ​size​ ​of​


s
​length keyword returns the size of the array.​
​ArrayList.​

​ rrayList​​size​​is​​dynamic​​and​​can​​be​​increased​
A
​The array has Fixed-size.​
​or decreased in size when required.​

I​t​ ​is​ ​relatively​ ​slower​ ​because​ ​of​ ​its​ ​dynamic​


​It is faster as above we see it of fixed size​
​nature​

​ rimitive​ ​data​ ​types​ ​are​ ​not​ ​directly​ ​added​ ​to​


P
​ rimitive​ ​data​ ​types​ ​can​ ​be​ ​stored​ ​directly​ ​in​
P
​unlikely​ ​arrays,​ ​they​ ​are​ ​added​ ​indirectly​ ​with​
​unlikely objects.​
​help of autoboxing and unboxing​

​ hey​​can​​not​​be​​added​​here​​hence​​the​​type​​is​​in​
T ​ hey​​can​​be​​added​​here​​hence​​makingArrayList​
T
​the unsafe.​ ​type-safe.​

​ ere​ ​a​ ​special​ ​method​​is​​used​​known​​as​​add()​


H
​The assignment operator only serves the purpose​
​method​

​143. What is the difference between Array and Collection in Java?​


​Array​ ​Collections​

​Array in Java has a fixed size.​ ​Collections in Java have dynamic sizes.​

I​n​ ​an​ ​Array,​ ​Elements​ ​are​ ​stored​ ​in​ ​contiguous​ I​n​ ​Collections,​ ​Elements​ ​are​ ​not​ ​necessarily​
​memory locations.​ ​stored in contiguous memory locations.​

​ bjects​ ​and​​primitive​​data​​types​​can​​be​​stored​​in​
O
​We can only store objects in collections.​
​an array.​
​35​

​ anual​ ​manipulation​ ​is​ ​required​ ​for​ ​resizing​ ​the​


M
​Resizing in collections is handled automatically.​
​array.​

​ ollections​ ​have​ ​advanced​


C ​methods​ ​for​
​The array has basic methods for manipulation.​
​manipulation and iteration.​

​The array is available since the beginning of Java.​ ​Collections were introduced in Java 1.2.​

​144. Difference between ArrayList and LinkedList.​


​ArrayList​ ​LinkedList​

​ArrayList is Implemented as an expandable Array.​ ​LinkedList is Implemented as a doubly-linked list.​

​ inkedList​​Elements​​are​​stored​​in​​non-contiguous​
L
I​n​ ​ArrayList,​ ​Elements​ ​are​ ​stored​ ​in​ ​contiguous​
​memory​ ​locations​ ​as​ ​each​ ​element​ ​has​ ​a​
​memory locations​
​reference to the next and previous elements.​

​ inkedLists​ ​are​ ​faster​ ​for​ ​insertion​ ​and​ ​deletion​


L
​ArrayLists are faster for random access.​
​operations​

​ArrayLists are more memory efficient.​ ​LinkedList is less memory efficient​

​ rrayLists​ ​Use​ ​more​ ​memory​ ​due​​to​​maintaining​


A ​ inkedList​ ​Uses​ ​less​ ​memory​ ​as​ ​it​ ​only​ ​has​
L
​the array size.​ ​references to elements​

​The search operation is faster in ArrayList.​ ​The search operation is slower in LinkedList​

​145. Differentiate between ArrayList and Vector in Java.​


​ArrayList​ ​Vector​

​ rrayLists​ ​are​ ​implemented​ ​as​ ​an​ ​expandable​


A
​Vector is Implemented as a growable array.​
​array.​

​ArrayList is not synchronized.​ ​The vector is synchronized.​

​ rrayLists​ ​are​
A ​Faster​ ​for​ ​non-concurrent​ ​ ector​ ​is​ ​Slower​ ​for​ ​non-concurrent​ ​operations​
V
​operations.​ ​due to added overhead of synchronization.​

​ArrayLists were Introduced in Java 1.2.​ ​Vector was Introduced in JDK 1.0.​
​36​

​ ecommended​ ​for​ ​use​ ​in​ ​a​ ​single-threaded​


R ​ ectors​ ​are​ ​Recommended​ ​for​ ​use​ ​in​ ​a​
V
​environment.​ ​multi-threaded environment.​

I​n​ ​Vectors,​ ​the​ ​default​ ​initial​ ​capacity​ ​is​ ​10​ ​but​


​The default initial capacity of ArrayLists is 10.​
​the default increment is twice the size.​

​ArrayList performance is high.​ ​Vector performance is low.​

​146. What is the difference between Iterator and ListIterator?​


​Iterator​ ​ListIterator​

​ an​​traverse​​elements​​present​​in​​Collection​​only​​in​
C ​ an​ ​traverse​ ​elements​ ​present​ ​in​ ​Collection​
C
​the forward direction.​ ​both in forward and backward directions.​

​Used to traverse Map, List, and Set.​ ​Can only traverse List and not the other two.​

I​t​ ​has​ ​methods​ ​like​ ​nextIndex()​ ​and​


​Indexes can’t be obtained using Iterator​ ​previousIndex()​ ​to​ ​obtain​ ​indexes​ ​of​ ​elements​
​at any time while traversing the List.​

​ an’t​ ​modify​ ​or​ ​replace​ ​elements​ ​present​ ​in​


C ​ an​ ​modify​ ​or​ ​replace​ ​elements​ ​with​ ​the​​help​
C
​Collection​ ​of set(E e)​

​ an’t​ ​add​ ​elements,​ ​and​


C ​also​ ​throws​ ​ an​​easily​​add​​elements​​to​​a​​collection​​at​​any​
C
​ConcurrentModificationException.​ ​time.​

​ ertain​ ​methods​ ​of​ ​Iterator​ ​are​ ​next(),​ ​remove(),​


C ​ ertain​ ​methods​ ​of​ ​ListIterator​ ​are​ ​next(),​
C
​and hasNext().​ ​previous(), hasNext(), hasPrevious(), add(E e).​

​147. Differentiate between HashMap and HashTable.​


​HashMap​ ​HashTable​

​HashMap is not synchronized​ ​HashTable is synchronized​

​One key can be a NULL value​ ​NULL values not allowed​

​The iterator is used to traverse HashMap.​ ​Both Iterator and Enumertar can be used​

​ ashTable​ ​is​ ​slower​ ​as​ ​compared​ ​to​


H
​HashMap is faster.​
​HashMap.​

​148. What is the difference between Iterator and Enumeration?​


​Iterator​ ​Enumeration​
​37​

​ he​​Iterator​​can​​traverse​​both​​legacies​​as​​well​​as​
T ​ numeration​
E ​can​ ​traverse​ ​only​ ​legacy​
​non-legacy elements.​ ​elements.​

​The Iterator is fail-fast.​ ​Enumeration is not fail-fast.​

​The Iterators are slower.​ ​Enumeration is faster.​

​ he​​Iterator​​can​​perform​​a​​remove​​operation​​while​
T ​ he​ ​Enumeration​ ​can​ ​perform​ ​only​ ​traverse​
T
​traversing the collection.​ ​operations on the collection.​

​149. What is the difference between Comparable and Comparator?​


​Comparable​ ​Comparator​

​The interface is present in java.lang package.​ ​The Interface is present in java.util package.​

​Provides compareTo() method to sort elements.​ ​Provides compare() method to sort elements.​

​It provides single sorting sequences.​ ​It provides multiple sorting sequences.​

​ he​ ​logic​ ​of​ ​sorting​ ​should​ ​be​ ​in​ ​a​ ​separate​


T
​ he​ ​logic​ ​of​ ​sorting​ ​must​ ​be​ ​in​ ​the​ ​same​ ​class​
T
​class​ ​to​ ​write​ ​different​ ​sorting​ ​based​ ​on​
​whose object you are going to sort.​
​different attributes of objects.​

​ ethod​ ​sorts​ ​the​ ​data​ ​according​ ​to​ ​fixed​ ​sorting​


M ​ ethod​ ​sorts​ ​the​ ​data​ ​according​ ​to​ ​the​
M
​order.​ ​customized sorting order.​

​It affects the original class.​ ​It doesn’t affect the original class.​

I​mplemented​ ​frequently​ ​in​ ​the​ ​API​ ​by​ ​Calendar,​ I​t​ ​is​ ​implemented​ ​to​ ​sort​ ​instances​ ​of​
​Wrapper classes, Date, and String.​ ​third-party classes.​

​150. What is the difference between Set and Map?​


​Set​ ​Map​

​ he​ ​Set​ ​interface​ ​is​ ​implemented​ ​using​ ​java.util​


T ​ he​ ​map​ ​is​ ​implemented​ ​using​ ​java.util​
T
​package.​ ​package.​

​It can extend the collection interface.​ ​It does not extend the collection interface.​

​It does not allow duplicate values.​ ​It allows duplicate values.​

​The set can sort only one null value.​ ​The map can sort multiple null values.​
​38​
​….​

​151. Explain the FailFast iterator and FailSafe iterator along with examples for each.​
​ ​ ​FailFast​ ​iterator​ ​is​ ​an​ ​iterator​ ​that​ ​throws​ ​a​ ​ConcurrentModificationException​ ​if​ ​it​ ​detects​ ​that​ ​the​
A
​underlying​ ​collection​ ​has​ ​been​ ​modified​ ​while​​the​​iterator​​is​​being​​used.​​This​​is​​the​​default​​behavior​​of​
​iterators in the Java Collections Framework. For example, the iterator for a HashMap is FailFast.​

/​/ Java Program to demonstrate FailFast iterator​


​import java.io.*;​
​import java.util.HashMap;​
​import java.util.Iterator;​
​import java.util.Map;​
​class GFG {​
​public static void main(String[] args)​
​{​
​HashMap<Integer, String> map = new HashMap<>();​
​map.put(1, "one");​
​map.put(2, "two");​
​Iterator<Map.Entry<Integer, String> > iterator​
​= map.entrySet().iterator();​
​while (iterator.hasNext()) {​
​Map.Entry<Integer, String> entry​
​= iterator.next();​
​// this will throw a​
​// ConcurrentModificationException​
​if (entry.getKey() == 1) {​
​map.remove(1);​
​}​
​}​
​}​
​}​

​ utput:​
O
​Exception in thread "main" java.util.ConcurrentModificationException​
​….​

​A​ ​FailSafe​ ​iterator​ ​does​ ​not​ ​throw​ ​a​ ​ConcurrentModificationException​ ​if​ ​the​ ​underlying​ ​collection​ ​is​
​modified​​while​​the​​iterator​​is​​being​​used.​​Alternatively,​​it​​creates​​a​​snapshot​​of​​the​​collection​​at​​the​​time​
​the​​iterator​​is​​created​​and​​iterates​​over​​the​​snapshot.​​For​​example,​​the​​iterator​​for​​a​​ConcurrentHashMap​
​is FailSafe.​

/​/ Java Program to demonstrate FailSafe​


​import java.io.*;​
​import java.util.Iterator;​
​import java.util.Map;​
​import java.util.concurrent.ConcurrentHashMap;​
​class GFG {​
​public static void main(String[] args)​
​{​
​ConcurrentHashMap<Integer, String> map​
​= new ConcurrentHashMap<>();​
​map.put(1, "one");​
​map.put(2, "two");​
​Iterator<Map.Entry<Integer, String> > iterator​
​= map.entrySet().iterator();​
​while (iterator.hasNext()) {​
​Map.Entry<Integer, String> entry = iterator.next();​
​// this will not throw an exception​
​if (entry.getKey() == 1) {​
​map.remove(1);​
​}​
​39​

​}​
​}​
​}​

​152. What is Exception Handling?​


​ n​​Exception​​is​​an​​Event​​that​​interrupts​​the​​normal​​flow​​of​​the​​program​​and​​requires​​special​​processing.​
A
​During​ ​the​ ​execution​ ​of​ ​a​ ​program,​ ​errors​ ​and​ ​unplanned​ ​occurrences​ ​can​ ​be​ ​dealt​ ​with​ ​by​​using​​the​
​Java Exception Handling mechanism. Below are some reasons why Exceptions occur in Java:​
​●​ ​Device failure​
​●​ ​Loss of Network Connection​
​●​ ​Code Errors​
​●​ ​Opening an Unavailable file​
​●​ ​Invalid User Input​
​●​ ​Physical Limitations (out of disk memory)​
​153. How many types of exceptions can occur in a Java program?​

​There are generally two types of exceptions in Java:​


​●​ ​Built-in​ ​Exceptions:​ ​Built-in​ ​exceptions​ ​in​ ​Java​ ​are​ ​provided​ ​by​ ​the​ ​Java​ ​Libraries.​ ​These​
​exceptions​ ​can​ ​be​ ​further​ ​divided​ ​into​ ​two​ ​subcategories​ ​i.e.,​ ​checked​ ​and​ ​unchecked​
​Exceptions. Below are some of the built-in exceptions in Java:​
​●​ ​ArrayIndexOutOfBoundsExceptions​
​●​ ​ClassNotFoundException​
​●​ ​FileNotFoundException​
​●​ ​IOException​
​●​ ​NullPointerException​
​●​ ​ArithmeticException​
​●​ ​InterruptedException​
​●​ ​RuntimeException​
​●​ ​User-Defined​ ​Exceptions​​:​ ​User-defined​ ​exceptions​ ​are​ ​defined​ ​by​ ​the​ ​programmers​
​themselves​ ​to​ ​handle​ ​some​ ​specific​ ​situations​ ​or​ ​errors​ ​which​ ​are​ ​not​ ​covered​ ​by​ ​built-in​
​exceptions.​​To​​define​​user-defined​​exceptions​​a​​new​​class​​that​​extends​​the​​appropriate​​exception​
​class​​must​​be​​defined.​​User-defined​​Exceptions​​in​​Java​​are​​used​​when​​the​​built-in​​exceptions​​are​
​in Java.​
​154. Difference between an Error and an Exception.​
​Errors​ ​Exceptions​

​ ecover​​from​​exceptions​​by​​either​​using​​a​​try-catch​
R
​Recovering from Errors is not possible.​
​block or throwing exceptions back to the caller.​

I​t​ ​includes​ ​both​ ​checked​ ​as​ ​well​ ​as​ ​unchecked​


​Errors are all unchecked types in Java.​
​types that occur.​

​ rrors​ ​are​ ​mostly​ ​caused​ ​by​ ​the​


E
​ he​ ​program​ ​is​ ​mostly​ ​responsible​ ​for​ ​causing​
T
​environment​ i​n​ ​which​ ​the​ ​program​ ​is​
​exceptions.​
​running.​
​40​

​ rrors​​can​​occur​​at​​compile​​time​​as​​well​​as​
E ​ ll​ ​exceptions​ ​occur​ ​at​ ​runtime​ ​but​ ​checked​
A
​run​ ​time.​ ​Compile​ ​Time:​ ​Syntax​ ​Error,​​Run​ ​exceptions​ ​are​ ​known​ ​to​ ​the​ ​compiler​ ​while​
​Time: Logical Error.​ ​unchecked are not.​

​ hey​ ​are​
T ​defined​ ​in​ ​java.lang.Error​
​They are defined in java.lang.Exception package​
​package.​

​ xamples:​ ​Checked​ ​Exceptions:​ ​SQLException,​


E
​ xamples:​ ​java.lang.StackOverflowError,​
E ​IOException​ ​Unchecked​ ​Exceptions:​
​java.lang.OutOfMemoryError​ ​ArrayIndexOutOfBoundException,​
​NullPointerException, ArithmeticException.​

​155. Explain the hierarchy of Java Exception classes.​

​ ll​ ​exception​ ​and​ ​error​​types​​in​​Java​​are​​subclasses​​of​​the​​class​​throwable,​​which​​is​​the​​base​​class​​of​


A
​the​ ​hierarchy.​ ​This​ ​class​ ​is​ ​then​ ​used​ ​for​ ​exceptional​ ​conditions​ ​that​ ​user​ ​programs​ ​should​ ​catch.​
​NullPointerException​ ​is​ ​an​ ​example​ ​of​ ​such​ ​an​ ​exception.​ ​Another​ ​branch,​ ​error​ ​is​ ​used​ ​by​ ​the​ ​Java​
​run-time​​system​​to​​indicate​​errors​​having​​to​​do​​with​​the​​JRE.​​StackOverflowError​​is​​an​​example​​of​​one​​of​
​such error.​
​156. Explain Runtime Exceptions.​
​ untime​ ​Exceptions​ ​are​ ​exceptions​ ​that​ ​occur​ ​during​ ​the​ ​execution​ ​of​ ​a​ ​code,​ ​as​ ​opposed​ ​to​
R
​compile-time​ ​exceptions​ ​that​ ​occur​ ​during​ ​compilation.​ ​Runtime​​exceptions​​are​​unchecked​​exceptions,​
​as they aren’t accounted for by the JVM.​
​Examples of runtime exceptions in Java include:​
​​ N
● ​ ullPointerException: This occurs when an application attempts to use a null object reference.​
​●​ ​ArrayIndexOutOfBoundsException:​​This​​occurs​​when​​an​​application​​attempts​​to​​access​​an​​array​
​index that is out of bounds.​
​●​ ​ArithmeticException: This occurs when an application attempts to divide by zero.​
​●​ ​IllegalArgumentException:​ ​This​ ​occurs​ ​when​ ​a​ ​method​ ​is​ ​passed​ ​on​ ​an​ ​illegal​ ​or​​inappropriate​
​argument.​
​Unlike​ ​checked​ ​exceptions,​ ​runtime​ ​exceptions​ ​do​ ​not​ ​require​ ​a​ ​declaration​ ​in​ ​the​ ​throws​ ​clause​ ​or​
​capture​ ​in​ ​a​ ​try-catch​ ​block.​ ​However,​ ​handling​ ​runtime​ ​exceptions​ ​is​ ​advisable​ ​in​ ​order​ ​to​ ​provide​
​meaningful​ ​error​ ​messages​ ​and​ ​prevent​ ​a​ ​system​ ​crash.​ ​Because​ ​runtime​ ​exceptions​ ​provide​ ​more​
​specific​ ​information​ ​about​ ​the​ ​problem​ ​than​​checked​​exceptions,​​they​​enable​​developers​​to​​detect​​and​
​correct programming errors more easily and quickly.​
​157. What is NullPointerException?​
I​t​ ​is​ ​a​ ​type​ ​of​ ​run-time​ ​exception​ ​that​​is​​thrown​​when​​the​​program​​attempts​​to​​use​​an​​object​​reference​
​that​​has​​a​​null​​value.​​The​​main​​use​​of​​NullPointerException​​is​​to​​indicate​​that​​no​​value​​is​​assigned​​to​​a​
​reference variable, also it is used for implementing data structures like linked lists and trees.​
​41​
​158. When is the ArrayStoreException thrown?​
​ArrayStoreException​​is​​thrown​​when​​an​​attempt​​is​​made​​to​​store​​the​​wrong​​type​​of​​object​​in​​an​​array​​of​
​ bjects.​
o

/​/ Java Program to implement​


​// ArrayStoreException​
​public class GFG {​
​public static void main(String args[])​
​{​
​// Since Double class extends Number class​
​// only Double type numbers​
​// can be stored in this array​
​Number[] a = new Double[2];​
​// Trying to store an integer value​
​// in this Double type array​
​a[0] = new Integer(4);​
​}​
​}​

​ xample:​
E
​Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer​
​at GFG.main(GFG.java:6)​
​….​

​ 59. What is the difference between Checked Exception and Unchecked Exception?​
1
​Checked Exception:​
​ hecked​ ​Exceptions​ ​are​ ​the​ ​exceptions​ ​that​ ​are​ ​checked​ ​during​ ​compile​ ​time​ ​of​ ​a​ ​program.​ ​In​ ​a​
C
​program,​​if​​some​​code​​within​​a​​method​​throws​​a​​checked​​exception,​​then​​the​​method​​must​​either​​handle​
​the exception or must specify the exception using the throws keyword.​
​Checked exceptions are of two types:​
​●​ ​Fully​ ​checked​ ​exceptions:​ ​all​ ​its​ ​child​ ​classes​ ​are​ ​also​ ​checked,​ ​like​ ​IOException,​ ​and​
​InterruptedException.​
​●​ ​Partially checked exceptions: some of its child classes are unchecked, like an Exception.​
​Unchecked Exception:​
​ nchecked​ ​are​ ​the​ ​exceptions​ ​that​ ​are​ ​not​ ​checked​ ​at​ ​compile​ ​time​ ​of​ ​a​ ​program.​ ​Exceptions​ ​under​
U
​Error​ ​and​ ​RuntimeException​ ​classes​ ​are​ ​unchecked​ ​exceptions,​ ​everything​ ​else​ ​under​ ​throwable​ ​is​
​checked.​
​160. What is the base class for Error and Exception?​
​ 2​
4
​Error​​is​​an​​illegal​​operation​​performed​​by​​the​​user​​which​​causes​​abnormality​​in​​the​​program.​​Exceptions​
​are​ ​the​ ​unexpected​ ​events​ ​or​ ​conditions​ ​that​​comes​​while​​running​​the​​program,​​exception​​disrupts​​the​
​normal flow of the program’s instructions.​
​Errors and Exceptions both have a common parent class which is java.lang.Throwable class.​
​161. Is it necessary that each try block must be followed by a catch block?​
​No,​​It​​is​​not​​necessary​​to​​use​​catch​​block​​after​​try​​block​​in​​Java​​as​​we​​can​​create​​another​​combination​
​ ith finally block. Finally is the block which runs despite the fact that the exception is thrown or not.​
w
​162. What is exception propagation?​
​ xception​​propagation​​is​​a​​process​​in​​which​​the​​exception​​is​​dropped​​from​​to​​the​​top​​to​​the​​bottom​​of​​the​
E
​stack.​​If​​not​​caught​​once,​​the​​exception​​again​​drops​​down​​to​​the​​previous​​method,​​and​​so​​on​​until​​it​​gets​
​caught or until it reaches the very bottom of the call stack.​
​ 63.​ ​What​ ​will​ ​happen​ ​if​ ​you​ ​put​ ​System.exit(0)​ ​on​ ​the​ ​try​ ​or​ ​catch​ ​block?​ ​Will​ ​finally​
1
​block execute?​
​ ystem.exit(int)​​has​​the​​capability​​to​​throw​​SecurityException.​​So,​​if​​in​​case​​of​​security,​​the​​exception​​is​
S
​thrown​ ​then​ ​finally​ ​block​ ​will​ ​be​ ​executed​ ​otherwise​ ​JVM​ ​will​ ​be​ ​closed​ ​while​ ​calling​ ​System.​ ​exit(0)​
​because of which finally block will not be executed.​
​ 64.​ ​What​ ​do​ ​you​ ​understand​ ​by​ ​Object​ ​Cloning​ ​and​ ​how​ ​do​ ​you​ ​achieve​ ​it​ ​in​
1
​Java?​
I​t​ ​is​ ​the​ ​process​ ​of​ ​creating​ ​an​ ​exact​ ​copy​ ​of​ ​any​ ​object.​ ​In​ ​order​ ​to​ ​support​ ​this,​ ​a​ ​java​​class​​has​​to​
​implement​​the​​Cloneable​​interface​​of​​java.lang​​package​​and​​override​​the​​clone()​​method​​provided​​by​​the​
​Object class the syntax of which is:​
​Protected​​Object​​clone()​​throws​​CloneNotSupportedException{​​return​​(Object)super.clone();}In​​case​​the​
​Cloneable​ ​interface​ ​is​ ​not​ ​implemented​ ​and​ ​just​ ​the​ ​method​ ​is​ ​overridden,​ ​it​ ​results​ ​in​
​CloneNotSupportedException in Java.​
​165. How do exceptions affect the program if it doesn’t handle them?​
​Exceptions​ ​are​ ​responsible​​for​​abruptly​​terminating​​the​​running​​of​​the​​program​​while​​executing​​and​​the​
​ ode written after the exception occurs is not executed.​
c
​166. What is the use of the final keyword?​
​The​ ​final​ ​keyword​ ​is​ ​used​ ​to​ ​make​ ​functions​ ​non-virtual.​ ​By​ ​default,​ ​all​ ​the​ ​functions​ ​are​ ​virtual​ ​so​ ​to​
​ ake it non-virtual we use the final keyword.​
m
​167. What purpose do the keywords final, finally, and finalize fulfill?​
​i). final:​
​final is a keyword is used with the variable, method, or class so that they can’t be overridden.​

/​/ Java Program to use final​


​// keyword​
​import java.io.*;​
​// Driver Class​
​class GFG {​
​// Main function​
​public static void main(String[] args)​
​{​
​final int x = 100;​
​x = 50;​
​}​
​}​

​ utput:​
O
​./GFG.java:6: error: cannot assign a value to final variable x​
​x=50;​
​^​
​1 error​
​ii). finally​
​finally​ ​is​ ​a​ ​block​ ​of​ ​code​ ​used​ ​with​ ​“try-catch”​ ​in​ ​exception​ ​handling.​ ​Code​​written​​in​​finally​​block​​runs​
​ espite the fact exception is thrown or not.​
d
​43​

/​/ Java Program to implement finally​


​import java.io.*;​
​// Driver class​
​class GFG {​
​// Main function​
​public static void main(String[] args)​
​{​
​int x = 10;​
​// try block​
​try {​
​System.out.println("Try block");​
​}​
​// finally block​
​finally {​
​System.out.println(​
​"Always runs even without exceptions");​
​}​
​}​
​}​

​ utput​
O
​Try block​
​Always runs even without exceptions​
​iii). finalize​
​It​ ​is​ ​a​ ​method​ ​that​ ​is​ ​called​ ​just​ ​before​ ​deleting/destructing​ ​the​ ​objects​ ​which​ ​are​​eligible​​for​​Garbage​
​ ollection to perform clean-up activity.​
c

/​*package whatever // do not write package name here */​


​import java.io.*;​
​class GFG {​
​public static void main(String[] args)​
​{​
​System.out.println("Main function running");​
​System.gc();​
​}​
​// Here overriding finalize method​
​public void finalize()​
​{​
​System.out.println("finalize method overridden");​
​}​
​}​

​ utput​
O
​Main function running​
​168. What is the difference between this() and super() in Java?​
​this( )​ ​super( )​

I​t​ ​represents​ ​the​ ​current​ ​instance​ ​of​ ​the​​parent​


​It represents the current instance of the class.​
​class.​

​Calls the default constructor of the same class.​ ​Calls the default constructor of the base class.​

​Access the methods of the same class.​ ​Access the methods of the parent class.​
​44​

​Points current class instance.​ ​Points the superclass instance.​

​169. What is multitasking?​


​ ultitasking​​in​​Java​​refers​​to​​a​​program’s​​capacity​​to​​carry​​out​​several​​tasks​​at​​once.​​Threads,​​which​​are​
M
​quick​ ​operations​ ​contained​ ​within​ ​a​ ​single​​program,​​can​​do​​this.​​Executing​​numerous​​things​​at​​once​​is​
​known as multitasking.​

/​/ Java program for multitasking​


​import java.io.*;​
​public class MyThread extends Thread {​
​public void run()​
​{​
​// Code to be executed in this thread​
​for (int i = 0; i < 10; i++) {​
​System.out.println(​
​"Thread " + Thread.currentThread().getId()​
​+ ": " + i);​
​}​
​}​
​}​
​public class GFG {​
​public static void main(String[] args)​
​{​
​MyThread thread1 = new MyThread();​
​MyThread thread2 = new MyThread();​
​// Start the threads​
​thread1.start();​
​thread2.start();​
​}​
​}​

​170. What do you mean by a Multithreaded program?​


​ ultithreaded​​programs​​in​​Java​​contain​​threads​​that​​run​​concurrently​​instead​​of​​running​​sequentially.​​A​
M
​computer​ ​can​​use​​its​​resources​​more​​efficiently​​by​​combining​​multiple​​tasks​​at​​once.​​Any​​program​​with​
​multithreading​ ​allows​ ​more​ ​than​ ​one​ ​user​ ​to​ ​simultaneously​ ​use​ ​the​ ​program​ ​without​ ​running​​multiple​
​copies.​ ​A​ ​multithreaded​ ​program​ ​is​ ​designed​ ​to​ ​run​ ​multiple​ ​processes​ ​at​ ​the​ ​same​ ​time​ ​which​ ​can​
​improve​ ​the​ ​performance​ ​of​ ​a​ ​program​ ​and​ ​allows​ ​the​ ​program​ ​to​ ​utilize​ ​multiple​ ​processors​ ​and​
​improves the overall throughput.​
​171. What are the advantages of multithreading?​
​There are multiple advantages of using multithreading which are as follows:​
​●​ ​Responsiveness:​​User​​Responsiveness​​increases​​because​​multithreading​​interactive​​application​
​allows running code even when the section is blocked or executes a lengthy process.​
​●​ ​Resource​ ​Sharing:​ ​The​ ​process​​can​​perform​​message​​passing​​and​​shared​​memory​​because​​of​
​multithreading.​
​●​ ​Economy: We are able to share memory because of which the processes are economical.​
​●​ ​Scalability: Multithreading on multiple CPU machines increases parallelism.​
​●​ ​Better Communication: Thread synchronization functions improves inter-process communication.​
​●​ ​Utilization of multiprocessor architecture​
​●​ ​Minimized system resource use​
​172. What are the two ways in which Thread can be created?​
​ ultithreading​ ​is​ ​a​ ​Java​​feature​​that​​allows​​concurrent​​execution​​of​​two​​or​​more​​parts​​of​​a​​program​​for​
M
​maximum​​utilization​​of​​the​​CPU.​​In​​general,​​threads​​are​​small,​​lightweight​​processes​​with​​separate​​paths​
​of​​execution.​​These​​threads​​use​​shared​​memory,​​but​​they​​act​​independently,​​thus​​if​​any​​one​​thread​​fails​
​it does not affect the other threads. There are two ways to create a thread:​
​●​ ​By extending the Thread class​
​●​ ​By implementing a Runnable interface.​
​By extending the Thread class​
​ 5​
4
​We​ ​create​ ​a​ ​class​ ​that​ ​extends​ ​the​ ​java.lang.Thread​ ​class.​ ​This​ ​class​ ​overrides​ ​the​ ​run()​ ​method​
​available in the Thread class. A thread begins its life inside run() method.​
​Syntax:​
​public class MyThread extends Thread {​
​public void run() {​
​// thread code goes here​
​}​
​}​
​By implementing the Runnable interface​
​We​ ​create​ ​a​ ​new​ ​class​ ​that​ ​implements​ ​java.lang.Runnable​​interface​​and​​override​​run()​​method.​​Then​
​ e instantiate a Thread object and call the start() method on this object.​
w
​Syntax:​
​public class MyRunnable implements Runnable {​
​public void run() {​
​// thread code goes here​
​}​
​}​
​173. What is a thread?​
​ hreads​ ​in​ ​Java​ ​are​ ​subprocess​ ​with​ ​lightweight​ ​with​ ​the​ ​smallest​ ​unit​ ​of​ ​processes​ ​and​ ​also​ ​has​
T
​separate​ ​paths​ ​of​ ​execution.​ ​These​ ​threads​ ​use​ ​shared​ ​memory​ ​but​ ​they​ ​act​ ​independently​ ​hence​ ​if​
​there​​is​​an​​exception​​in​​threads​​that​​do​​not​​affect​​the​​working​​of​​other​​threads​​despite​​them​​sharing​​the​
​same​​memory.​​A​​thread​​has​​its​​own​​program​​counter,​​execution​​stack,​​and​​local​​variables,​​but​​it​​shares​
​the​ ​same​ ​memory​ ​space​ ​with​ ​other​ ​threads​ ​in​ ​the​ ​same​ ​process.​ ​Java​ ​provides​ ​built-in​ ​support​ ​for​
​multithreading through the Runnable interface and the Thread class.​
​174. Differentiate between process and thread?​
​A​​process​​and​​a​​thread​​are​​both​​units​​of​​execution​​in​​a​​computer​​system,​​but​​they​​are​​different​​in​​several​
​ ays:​
w
​Process​ ​Thread​

​ ​ ​thread​ ​is​ ​a​ ​single​​sequence​​of​​instructions​


A
​A process is a program in execution.​
​within a process.​

​The process takes more time to terminate.​ ​The thread takes less time to terminate.​

​ he​ ​thread​ ​takes​ ​less​ ​time​ ​for​ ​context​


T
​The process takes more time for context switching.​
​switching.​

​ he​ ​process​ ​is​ ​less​ ​efficient​ ​in​ ​terms​ ​of​


T ​ hread​ ​is​ ​more​ ​efficient​ ​in​ ​terms​ ​of​
T
​communication.​ ​communication.​

​The process is isolated.​ ​Threads share memory.​

​ hread​ ​has​ ​Parents’​ P


T ​ CB,​ ​its​ ​own​ ​Thread​
​ he​ ​process​ ​has​ ​its​ ​own​ ​Process​ ​Control​ ​Block,​
T
​Control​ ​Block,​ ​and​ ​Stack​ ​and​ ​common​
​Stack, and Address Space.​
​Address space.​

​The process does not share data with each other.​ ​Threads share data with each other.​
​46​
​175. Describe the life cycle of the thread?​

​A​​thread​​in​​Java​​at​​any​​point​​in​​time​​exists​​in​​any​​one​​of​​the​​following​​states.​​A​​thread​​lies​​only​​in​​one​​of​
t​he shown states at any instant:​
​1.​ ​New: The thread has been created but has not yet started.​
​2.​ ​Runnable:​ ​The​ ​thread​ ​is​ ​running,​ ​executing​ ​its​ ​task,​ ​or​ ​is​ ​ready​ ​to​ ​run​ ​if​ ​there​ ​are​ ​no​ ​other​
​higher-priority threads.​
​3.​ ​Blocked: The thread is temporarily suspended, waiting for a resource or an event.​
​4.​ ​Waiting:​ ​The​ ​thread​ ​is​ ​waiting​ ​for​ ​another​ ​thread​​to​​perform​​a​​task​​or​​for​​a​​specified​​amount​​of​
​time to elapse.​
​5.​ ​Terminated: The thread has completed its task or been terminated by another thread.​
​176. Explain suspend() method under the Thread class.​
​ he​ ​suspend()​ ​method​ ​of​ ​the​ ​Thread​ ​class​ ​in​ ​Java​ ​temporarily​ ​suspends​ ​the​ ​execution​ ​of​ ​a​ ​thread.​
T
​When​​a​​thread​​is​​suspended​​it​​goes​​into​​a​​blocked​​state​​and​​it​​would​​not​​be​​scheduled​​by​​the​​operating​
​system​​which​​means​​that​​it​​will​​not​​be​​able​​to​​execute​​its​​task​​until​​it​​is​​resumed.​​There​​are​​more​​safer​
​and​ ​flexible​ ​alternatives​ ​to​ ​the​ ​suspend()​ ​methods​ ​in​ ​the​ ​modern​ ​java​ ​programming​ ​language.​ ​This​
​method does not return any value.​
​Syntax:​
​public final void suspend();​
​47​

/​/ Java program to show thread suspend() method​


​import java.io.*;​
​class MyThread extends Thread {​
​public void run()​
​{​
​for (int i = 0; i < 10; i++) {​
​System.out.println(" Running thread : " + i);​
​try {​
​Thread.sleep(1000);​
​}​
​catch (Interrupted_Exception e) {​
​e.printStackTrace();​
​}​
​}​
​}​
​}​
​class GFG {​
​public static void main(String[] args)​
​{​
​MyThread t1 = new MyThread();​
​t1.start();​
​try {​
​Thread.sleep(3000);​
​}​
​catch (InterruptedException e) {​
​e.printStackTrace();​
​}​
​// suspend the execution of the thread​
​t1.suspend();​
​System.out.println("Suspended thread ");​
​try {​
​Thread.sleep(3000);​
​}​
​catch (InterruptedException e) {​
​e.printStackTrace();​
​}​
​// resume the execution of the thread​
​t1.resume();​
​System.out.println("Resumed thread");​
​}​
​}​

​ utput:​
O
​Thread running: 0​
​Thread running: 1​
​Thread running: 2​
​Suspended thread​
​Resumed thread​
​Thread running: 3​
​Thread running: 4​
​Thread running: 5​
​Thread running: 6​
​Thread running: 7​
​Thread running: 8​
​Thread running: 9​
​177. Explain the main thread under Thread class execution.​
​ ava​​provides​​built-in​​support​​for​​multithreaded​​programming.​​The​​main​​thread​​is​​considered​​the​​parent​
J
​thread​ ​of​ ​all​ ​the​ ​other​ ​threads​ ​that​ ​are​ ​created​ ​during​ ​the​ ​program​ ​execution.​ ​The​ ​main​ ​thread​ ​is​
​ 8​
4
​automatically​ ​created​ ​when​ ​the​ ​program​ ​starts​ ​running.​ ​This​ ​thread​ ​executes​ ​the​ ​main​ ​method​ ​of​ ​the​
​program.​​It​​is​​responsible​​for​​executing​​the​​main​​logic​​of​​the​​Java​​program​​as​​well​​as​​handling​​the​​user​
​input​ ​operations.​ ​The​ ​main​ ​thread​ ​serves​ ​as​ ​the​ ​base​ ​thread​ ​from​ ​which​ ​all​ ​other​ ​child​ ​threads​ ​are​
​spawned.​

​178. What is a daemon thread?​


​ ​​daemon​​thread​​in​​Java​​is​​a​​low-priority​​thread​​that​​is​​used​​to​​perform​​background​​operations​​or​​tasks​
A
​which​​are​​used​​to​​perform​​continuously.​​such​​as​​Garbage​​collection,​​Signal​​dispatches,​​Action​​listeners,​
​etc.​​Daemon​​threads​​in​​Java​​have​​lower​​priority​​than​​user​​threads,​​which​​means​​they​​can​​only​​execute​
​when​ ​no​ ​user​ ​threads​ ​are​ ​running.​ ​Daemon​ ​threads​ ​in​ ​Java​ ​are​ ​useful​ ​features​ ​that​ ​are​ ​required​ ​for​
​background​ ​tasks​ ​that​ ​do​ ​not​ ​require​ ​explicit​ ​shutdown​ ​or​ ​finalization.​ ​It​ ​allows​ ​more​ ​efficient​ ​use​ ​of​
​system resource and are used to simplify resources and can simplify long-running tasks.​
​179. What are the ways in which a thread can enter the waiting state?​
​ hread​​is​​a​​lightweight​​process​​that​​runs​​concurrently​​with​​the​​other​​thread​​inside​​a​​single​​process.​​Each​
T
​thread​​can​​execute​​a​​different​​task​​and​​share​​the​​resources​​within​​a​​single​​process.​​Thread​​in​​Java​​can​
​enter the waiting state in many different ways:​
​●​ ​Sleep()​ ​method​ ​Call:​ ​The​ ​sleep​ ​()​ ​method​ ​is​ ​used​ ​to​ ​pause​ ​the​ ​execution​ ​of​ ​the​ ​thread​ ​for​​a​
​specific amount of time. While the thread is paused it goes into the waiting state.​
​●​ ​Wait()​​method:​​This​​method​​is​​used​​to​​wait​​a​​thread​​until​​the​​other​​thread​​signals​​it​​to​​wake​​up.​
​Thread goes into the waiting state until it receives a notification from another thread.​
​●​ ​Join()​ ​method:​ ​Join()​ ​method​ ​can​ ​be​ ​used​ ​to​ ​wait​ ​for​ ​thread​ ​to​ ​finish​ ​the​ ​execution.​ ​Calling​
​thread goes into the waiting state until the target thread is completed.​
​●​ ​Waiting​ ​for​ ​I/O​ ​operations:​ ​If​ ​the​ ​thread​ ​is​ ​waiting​ ​for​ ​Input/Output​ ​operation​ ​to​ ​complete,​ ​it​
​goes into the waiting state until the operation is finished.​
​●​ ​Synchronization​​Issues:​​If​​there​​are​​any​​synchronization​​issues​​in​​a​​multi-threaded​​application,​
​threads may go into the waiting state until the synchronization issues are resolved.​
​180. How does multi-threading take place on a computer with a single CPU?​
​ ava​ ​uses​ ​a​ ​technique​ ​called​ ​time-sharing,​ ​commonly​ ​referred​ ​to​ ​as​ ​time-slicing,​ ​to​ ​implement​
J
​multi-threading​​on​​computers​​with​​a​​single​​CPU.​​The​​appearance​​of​​parallel​​execution​​is​​created​​by​​the​
​CPU​​switching​​between​​active​​threads.​​The​​operating​​system​​is​​in​​charge​​of​​allocating​​CPU​​time​​to​​each​
​thread sequentially and scheduling the threads.​
​In​ ​order​ ​to​ ​stop​ ​threads​ ​from​ ​interacting​ ​with​​one​​another​​and​​creating​​race​​situations​​or​​other​​issues,​
​Java​​has​​a​​number​​of​​ways​​to​​govern​​the​​behavior​​of​​threads,​​including​​synchronization​​and​​locking.​​It​​is​
​feasible​​to​​create​​multi-threaded​​programmers​​that​​operate​​correctly​​and​​effectively​​on​​a​​machine​​with​​a​
​single​ ​CPU​ ​by​ ​regulating​ ​the​ ​interaction​ ​between​ ​threads​ ​and​ ​making​ ​sure​​that​​crucial​​code​​parts​​are​
​synchronized.​ ​In​ ​contrast​ ​to​ ​running​ ​the​ ​same​ ​program​ ​on​ ​a​ ​computer​ ​with​ ​multiple​ ​CPUs​ ​or​ ​cores,​
​multi-threading​ ​on​ ​a​ ​single​ ​CPU​ ​can​ ​only​ ​give​ ​the​ ​appearance​ ​of​ ​parallelism,​​and​​actual​​performance​
​ 9​
4
​gains​ ​may​ ​be​ ​modest.​ ​The​ ​operating​ ​system​ ​divides​ ​the​ ​CPU​ ​time​ ​that​ ​is​ ​available​ ​when​ ​numerous​
​threads​​are​​running​​on​​a​​single​​CPU​​into​​small​​time​​slices​​and​​gives​​each​​thread​​a​​time​​slice​​to​​execute.​
​Rapid​ ​switching​ ​between​ ​the​ ​threads​ ​by​ ​the​ ​operating​ ​system​ ​creates​ ​the​ ​appearance​ ​of​ ​parallel​
​execution.​ ​The​ ​switching​ ​between​ ​threads​ ​appears​ ​to​ ​be​ ​immediate​ ​because​​the​​time​​slices​​are​​often​
​very tiny, on the order of milliseconds or microseconds.​
​ 81.​ ​What​ ​are​ ​the​ ​different​ ​types​ ​of​ ​Thread​ ​Priorities​ ​in​ ​Java?​ ​And​ ​what​ ​is​ ​the​
1
​default priority of a thread assigned by JVM?​
​ riorities​​in​​threads​​is​​a​​concept​​where​​every​​thread​​is​​having​​a​​priority​​which​​in​​layman’s​​language​​one​
P
​can​​say​​every​​object​​is​​having​​priority​​here​​which​​is​​represented​​by​​numbers​​ranging​​from​​1​​to​​10.​​There​
​are different types of thread properties in Java mentioned below:​
​●​ ​MIN_PRIORITY​
​●​ ​MAX_PRIORITY​
​●​ ​NORM_PRIORITY​
​By default, the thread is assigned NORM_PRIORITY.​
​182. Why Garbage Collection is necessary in Java?​
​ or​​Java,​​Garbage​​collection​​is​​necessary​​to​​avoid​​memory​​leaks​​which​​can​​cause​​the​​program​​to​​crash​
F
​and​ ​become​ ​unstable.​ ​There​ ​is​ ​no​ ​way​ ​to​ ​avoid​ ​garbage​ ​collection​ ​in​ ​Java.​ ​Unlike​ ​C++,​ ​Garbage​
​collection​ ​in​ ​Java​ ​helps​ ​programmers​ ​to​ ​focus​ ​on​ ​the​ ​development​ ​of​ ​the​ ​application​ ​instead​ ​of​
​managing​ ​memory​ ​resources​ ​and​ ​worrying​ ​about​ ​memory​ ​leakage.​ ​Java​ ​Virtual​ ​Machine​ ​(JVM)​
​automatically​ ​manages​ ​the​ ​memory​ ​periodically​ ​by​ ​running​ ​a​ ​garbage​ ​collector​ ​which​ ​frees​ ​up​ ​the​
​unused​​memory​​in​​the​​application.​​Garbage​​collection​​makes​​Java​​memory​​efficient​​because​​it​​removes​
​unreferenced objects from the heap memory.​
​183. What is the drawback of Garbage Collection?​
​Apart from many advantages, Garbage Collector has certain drawbacks mentioned below:​
​1.​ ​The​ ​main​ ​drawback​ ​to​ ​Garbage​ ​collection​ ​is​ ​that​ ​it​ ​can​ ​cause​ ​pauses​ ​in​ ​an​ ​application’s​
​execution as it works to clear the memory which slows down the performance of the application.​
​2.​ ​The​ ​Process​ ​of​ ​Garbage​ ​collection​ ​is​ ​non-deterministic​ ​which​ ​makes​ ​it​ ​difficult​​to​​predict​​when​
​garbage​ ​collection​ ​occurs​ ​which​ ​causes​ ​unpredictable​ ​behavior​​in​​applications.​​For​​Example,​​if​
​we​​write​​any​​program​​then​​it​​is​​hard​​for​​programmers​​to​​decide​​if​​the​​issue​​is​​caused​​by​​garbage​
​collection or by any other factors in the program.​
​3.​ ​Garbage​​collection​​can​​also​​increase​​memory​​usage​​if​​the​​program​​creates​​and​​discards​​a​​lot​​of​
​short-lived objects.​
​184. Explain the difference between a minor, major, and full garbage collection.​
​ he​ ​Java​ ​Virtual​ ​Machine​ ​(JVM)​ ​removes​ ​objects​ ​that​ ​are​ ​no​ ​longer​ ​in​ ​use​ ​using​ ​a​ ​garbage​​collector​
T
​which​​periodically​​checks​​and​​removes​​these​​objects.​​There​​are​​different​​types​​of​​garbage​​collection​​in​
​the​ ​JVM,​ ​each​ ​with​ ​different​ ​characteristics​ ​and​ ​performance​ ​implications.​ ​The​ ​main​​types​​of​​garbage​
​collection are:​
​●​ ​Minor​ ​garbage​ ​collection:​ ​Also​ ​known​ ​as​ ​young​ ​generation​ ​garbage​ ​collection,​ ​this​ ​type​ ​of​
​garbage​ ​collection​ ​is​ ​used​ ​to​ ​collect​ ​and​ ​reclaim​ ​memory​ ​that​ ​is​ ​used​ ​by​ ​short-lived​ ​objects​
​(objects that are quickly created and discarded).​
​●​ ​Major​​garbage​​collection:​​Also​​known​​as​​old-generation​​garbage​​collection,​​this​​type​​of​​garbage​
​collection​ ​is​ ​used​ ​to​ ​collect​ ​and​ ​reclaim​ ​memory​​that​​is​​used​​by​​long-lived​​objects​​(objects​​that​
​survive multiple minor garbage collections and are promoted to the old generation).​
​●​ ​Full​ ​garbage​ ​collection:​ ​During​ ​full​ ​garbage​ ​collection,​ ​memories​ ​from​ ​all​ ​generations​ ​are​
​collected​​and​​reclaimed,​​including​​memories​​of​​young​​and​​old.​​A​​full​​garbage​​collection​​normally​
​takes​ ​longer​ ​to​ ​complete​ ​than​ ​a​ ​minor​ ​or​ ​major​ ​garbage​ ​collection​ ​which​ ​causes​ ​that​ ​app​ ​to​
​pause temporarily.​
​185. How will you identify major and minor garbage collections in Java?​
​ ajor​ ​garbage​ ​collection​​works​​on​​the​​survivor​​space​​and​​Minor​​garbage​​collection​​works​​on​​the​​Eden​
M
​space​ ​to​ ​perform​ ​a​ ​mark-and-sweep​ ​routine.​ ​And​ ​we​ ​can​ ​identify​ ​both​ ​of​ ​them​ ​based​ ​on​ ​the​ ​output​
​where​​the​​minor​​collection​​prints​​“GC”,​​whereas​​the​​major​​collection​​prints​​“Full​​GC”​​for​​the​​case​​where​
​the garbage collection logging is enabled with “-XX:PrintGCDetails” or “verbose:gc”.​
​186. What is a memory leak, and how does it affect garbage collection?​
I​n​ ​Java​ ​Memory​ ​leaks​ ​can​ ​be​ ​caused​ ​by​ ​a​ ​variety​ ​of​ ​factors,​ ​such​ ​as​ ​not​ ​closing​ ​resources​​properly,​
​holding​​onto​​object​​references​​longer​​than​​necessary,​​or​​creating​​too​​many​​objects​​unnecessarily.​​There​
​are​​situations​​in​​which​​garbage​​collector​​does​​not​​collect​​objects​​because​​there​​is​​a​​reference​​to​​those​
​objects.​ ​In​ ​these​ ​situations​ ​where​ ​the​ ​application​ ​creates​ ​lots​ ​of​ ​objects​ ​and​ ​does​ ​not​ ​use​ ​them​ ​and​
​ 0​
5
​every​​object​​has​​some​​valid​​references,​​a​​Garbage​​collector​​in​​Java​​cannot​​destroy​​the​​objects.​​These​
​useless​ ​objects​ ​which​ ​do​ ​not​ ​provide​ ​any​ ​value​ ​to​ ​the​ ​program​ ​are​​known​​as​​Memory​​leaks.​​Memory​
​leaks​ ​can​ ​impact​ ​garbage​ ​collection​ ​negatively​ ​by​ ​preventing​ ​the​ ​garbage​ ​collector​ ​from​ ​reclaiming​
​unused​​memory.​​This​​behavior​​will​​lead​​to​​slow​​performance​​or​​sometimes​​system​​failure.​​In​​a​​program,​
​it is important to avoid memory leaks by managing resources and object references properly.​

/​/ Java Program to demonstrate memory leaks​


​import java.io.*;​
​import java.util.Vector;​
​class GFG {​
​public static void main(String[] args)​
​{​
​Vector a = new Vector(21312312);​
​Vector b = new Vector(2147412344);​
​Vector c = new Vector(219944);​
​System.out.println("Memory Leak in Java");​
​}​
​}​

​ utput:​
O
​Exception in thread "main" java.lang.OutOfMemoryError: Java heap space​
​at java.base/java.util.Vector.<init>(Vector.java:142)​
​at java.base/java.util.Vector.<init>(Vector.java:155)​
​at GFG.main(GFG.java:9)​
​187. Name some classes present in java.util.regex package.​
​Regular​​Expressions​​or​​Regex​​in​​Java​​is​​an​​API​​used​​for​​searching​​and​​manipulating​​of​​strings​​in​​Java.​
I​t creates String patterns that can extract the data needed from the strings or can generalize a pattern.​
​There are 3 Classes present in java.util.regex mentioned below:​
​●​ ​Pattern Class: Can define patterns​
​●​ ​Matcher Class: Can perform match operations on text using patterns​
​●​ ​PatternSyntaxException Class: Can indicate a syntax error in a regular expression pattern.​
​Also,​​apart​​from​​the​​3​​classes​​package​​consists​​of​​a​​single​​interface​​MatchResult​​Interface​​which​​can​​be​
​used for representing the result of a match operation.​
​188. Write a regular expression to validate a password.​
​A​​password​​must​​start​​with​​an​​alphabet​​and​​followed​​by​​alphanumeric​​characters;​​Its​​length​​must​​be​​in​
​ etween 8 to 20.​
b
​regex = “^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&-+=()])(?=\\S+$).{8, 20}$”​
​Explanation:​
​●​ ​^ used for starting character of the string.​
​●​ ​(?=.*[0-9]) used for a digit must occur at least once.​
​●​ ​(?=.*[a-z]) used for a lowercase alphabet must occur at least once.​
​●​ ​(?=.*[A-Z]) used for an upper case alphabet that must occur at least once in the substring.​
​●​ ​(?=.*[@#$%^&-+=()] used for a special character that must occur at least once.​
​●​ ​(?=\\S+$) white spaces don’t allow in the entire string.​
​●​ ​.{8, 20} used for at least 8 characters and at most 20 characters.​
​●​ ​$ used for the end of the string.​
​189. What is JDBC?​
​ DBC​​standard​​API​​is​​used​​to​​link​​Java​​applications​​and​​relational​​databases.​​It​​provides​​a​​collection​​of​
J
​classes​ ​and​ ​interfaces​ ​that​ ​let​ ​programmers​ ​to​ ​use​ ​the​ ​Java​ ​programming​ ​language​ ​to​ ​communicate​
​with​​the​​database.​​The​​classes​​and​​interface​​of​​JDBC​​allow​​the​​application​​to​​send​​requests​​which​​are​
​made​ ​by​ ​users​ ​to​ ​the​ ​specified​ ​database.​ ​There​ ​are​ ​generally​ ​four​ ​components​ ​of​ ​JDBC​ ​by​ ​which​ ​it​
​interacts with the database:​
​●​ ​JDBC API​
​●​ ​JDBC Driver manager​
​●​ ​JDBC Test Suite​
​●​ ​JDBC-ODBC Bridge Drivers​
​51​

​190. What is JDBC Driver?​


​ DBC​ ​Driver​ ​is​ ​a​ ​software​ ​component​ ​that​ ​is​ ​used​ ​to​ ​enable​ ​a​ ​Java​ ​application​ ​to​ ​interact​ ​with​ ​the​
J
​database.​ ​JDBC​ ​provides​ ​the​ ​implementation​ ​of​ ​the​ ​JDBC​ ​API​ ​for​ ​a​ ​specific​ ​database​ ​management​
​system,​ ​which​ ​allows​​it​​to​​connect​​the​​database,​​execute​​SQL​​statements​​and​​retrieve​​data.​​There​​are​
​four types of JDBC drivers:​
​●​ ​JDBC-ODBC Bridge driver​
​●​ ​Native-API driver​
​●​ ​Network Protocol driver​
​●​ ​Thin driver​

​191. What are the steps to connect to the database in Java?​


​There are certain steps to connect the database and Java Program as mentioned below:​
​●​ ​Import the Packages​
​●​ ​Load the drivers using the forName() method​
​●​ ​Register the drivers using DriverManager​
​●​ ​Establish a connection using the Connection class object​
​●​ ​Create a statement​
​●​ ​Execute the query​
​●​ ​Close the connections​
​192. What are the JDBC API components?​
​ DBC​ ​API​ ​components​ ​provide​ ​various​ ​methods​ ​and​ ​interfaces​ ​for​ ​easy​ ​communication​ ​with​ ​the​
J
​databases​​also​​it​​provides​​packages​​like​​java​​Se​​and​​java​​EE​​which​​provides​​the​​capability​​of​​write​​once​
​run anywhere (WORA).​
​Syntax:​
​java.sql.*;​
​52​
​193. What is JDBC Connection interface?​
​Java​ ​database​ ​connectivity​ ​interface​ ​(JDBC)​ ​is​ ​a​ ​software​ ​component​ ​that​​allows​​Java​​applications​​to​
i​nteract with databases. To enhance the connection, JDBC requires drivers for each database.​
​194. What does the JDBC ResultSet interface?​
​ DBC​ ​ResultSet​ ​interface​ ​is​ ​used​ ​to​​store​​the​​data​​from​​the​​database​​and​​use​​it​​in​​our​​Java​​Program.​
J
​We​​can​​also​​use​​ResultSet​​to​​update​​the​​data​​using​​updateXXX()​​methods.​​ResultSet​​object​​points​​the​
​cursor​ ​before​ ​the​ ​first​ ​row​ ​of​ ​the​ ​result​ ​data.​ ​Using​ ​the​ ​next()​ ​method,​ ​we​ ​can​ ​iterate​ ​through​ ​the​
​ResultSet.​
​195. What is the JDBC Rowset?​
​ ​​JDBC​​RowSet​​provides​​a​​way​​to​​store​​the​​data​​in​​tabular​​form.​​RowSet​​is​​an​​interface​​in​​java​​that​​can​
A
​be​​used​​within​​the​​java.sql​​package.​​The​​connection​​between​​the​​RowSet​​object​​and​​the​​data​​source​​is​
​maintained​​throughout​​its​​life​​cycle.​​RowSets​​are​​classified​​into​​five​​categories​​based​​on​​implementation​
​mentioned below:​
​1.​ ​JdbcRowSet​
​2.​ ​CachedRowSet​
​3.​ ​WebRowSet​
​4.​ ​FilteredRowSet​
​5.​ ​JoinRowSet​
​196. What is the role of the JDBC DriverManager class?​
​JDBC​ ​DriverManager​ ​class​ ​acts​ ​as​ ​an​ ​interface​ ​for​ ​users​ ​and​ ​Drivers.​ ​It​ ​is​ ​used​ ​in​ ​many​ ​ways​ ​as​
​ entioned below:​
m
​●​ ​It is used to create a connection between a Java application and the database.​
​●​ ​Helps to keep track of the drivers that are available.​
​●​ ​It can help to establish a connection between a database and the appropriate drivers.​
​●​ ​It contains all the methods that can register and deregister the database driver classes.​
​●​ ​DriverManager.registerDriver()​​method​​can​​maintain​​the​​list​​of​​Driver​​classes​​that​​have​​registered​
​themselves.​
​197. Differentiate between Iterable and Iterator.​
​Iterable​ ​Iterator​

I​terable​ ​provides​ ​a​ ​way​ ​to​ ​iterate​ ​over​ ​a​ I​terator​ ​helps​ ​in​ ​iterating​ ​over​ ​a​ ​collection​ ​of​
​sequence of elements.​ ​elements sequentially.​

​iterator() method returns an Iterator.​ ​hasNext() and next() methods are required.​

​remove() method is optional.​ ​remove() method is required in the iterator.​

​ xamples​ ​are​ ​ListIterator,​ ​Enumeration,​ ​and​


E
​Examples are List, Queue, and Set.​
​ArrayIterator.​

​198. Differentiate between List and Set.​


​Ordered​ ​Unordered​

​List allows duplicates.​ ​Set does not allow duplicate values.​

​List is accessed by index.​ ​Set is accessed by hashcode.​

​Multiple null elements can be stored.​ ​Null element can store only once.​
​53​

​ xamples​ ​are​ ​HashSet​


E ​and​ ​TreeSet.​
​Examples are ArrayList, LinkedList, etc.​
​LinkedHashSet etc.​

​199. Differentiate between List and Map.​


​List​ ​Map​

​List interface allows duplicate elements.​ ​Map does not allow duplicate elements.​

​The list maintains insertion order.​ ​The list maintains insertion order.​

​ he​​map​​allows​​a​​single​​null​​key​​at​​most​​and​
T
​Multiple null elements can be stored.​
​any number of null values.​

​ he​​list​​provides​​get()​​method​​to​​get​​the​​element​
T ​ he​ ​map​ ​does​ ​not​ ​provide​ ​a​ ​get​ ​method​ ​to​
T
​at a specified index.​ ​get the elements at a specified index.​

​ ap​ ​is​ ​Implemented​​by​​HashMap,​​TreeMap,​


M
​List is Implemented by ArrayList, etc.​
​LinkedHashMap​

​200. Differentiate between Queue and Stack.​


​Queue​ ​Stack​

​ ueue​ ​data​ ​structure​ ​is​ ​used​ ​to​ ​store​ ​elements,​


Q ​ tack​ ​data​ ​structure​ ​is​ ​used​ ​to​ ​store​
S
​and​ ​is​ ​used​ ​to​ ​perform​ ​operations​ ​like​ ​enqueue,​ ​elements,​ ​and​ ​is​ ​used​ ​to​​perform​​operations​
​dequeue from back or end of the queue.​ ​like push, pop from top of the stack.​

​Queue data structure Implements FIFO order.​ ​Stack data structure Implements LIFO order.​

I​nsertion​ ​and​ ​deletion​ ​in​ ​queues​​take​​place​​from​


​the​​opposite​​ends​​of​​the​​list.​​Deletion​​takes​​place​ I​nsertion​ ​and​ ​deletion​ ​in​ ​stacks​ ​take​ ​place​
​from​​the​​front​​of​​the​​list​​and​​insertion​​takes​​place​ ​only from one end of the list called the top.​
​at the rear of the list.​

​Insert operation is called enqueue operation.​ ​Insert operation is called Push operation.​

​ ueue​ ​is​ ​generally​ ​used​ ​to​ ​solve​ ​problems​


Q ​ tack​ ​is​ ​generally​ ​used​ ​to​ ​solve​ ​problems​
S
​related to sequential processing.​ ​related to recursion.​

​201. Differentiate between PriorityQueue and TreeSet.​


​Priority Queue​ ​TreeSet​

​It uses Queue as an underlying data structure.​ ​It uses a Set as an underlying data structure.​

​ his​ ​data​ ​structure​ ​does​ ​not​ ​allow​ ​duplicate​


T
​This data structure allows duplicate elements​
​elements​
​54​

​ riority​​Queue​​is​​Implemented​​by​​PriorityQueue​
P
​TreeSet is implemented by TreeSet class.​
​class.​

​PriorityQueue comes in JDK 1.5.​ ​TreeSet comes in JDK 1.4.​

​ riorityQueue<Integer>​
P ​pq​ ​=​ ​new​
​reeSet<Integer> ts = new TreeSet<>();​
​PriorityQueue<>();​

​202. Differentiate between the Singly Linked List and Doubly Linked List.​
​Singly Linked List​ ​Doubly Linked List​

​ ingly​​Linked​​List​​contain​​only​​two​​segments​​i.e,​
S ​ oubly​ ​Linked​ ​List​ ​contains​ ​three​ ​segments​
D
​Data and Link.​ ​i.e, Data, and two pointers.​

​ raversal​ ​in​ ​a​ ​doubly​ ​linked​ ​list​ ​is​ ​only​


T
​ raversal​​in​​a​​singly​​linked​​list​​is​​possible​​in​​only​
T
​possible​​in​​both​​directions​​forward​​as​​well​​as​
​a forward direction.​
​backward.​

I​t​ ​uses​ ​less​ ​memory​ ​as​ ​every​ ​single​ ​node​ ​has​ I​t​ ​requires​ ​more​​memory​​than​​a​​singly​​linked​
​only one pointer.​ ​list as each node has two pointers.​

​ asy​​to​​use​​and​​insert​​nodes​​at​​the​​beginning​​of​
E ​ lightly​ ​more​ ​complex​ ​to​ ​use​ ​and​ ​easy​ ​to​
S
​the list.​ ​insert at the end of the list.​

​ he​ ​time​ ​complexity​ ​of​ ​insertion​ ​and​ ​deletion​ ​is​


T ​ he​ ​time​​complexity​​of​​insertion​​and​​deletion​
T
​O(n).​ ​is O(1).​

​203. Differentiate between Failfast and Failsafe.​


​FailFast​ ​FailSafe​

​ ailsfast​ ​fails​ ​immediately​ ​when​ ​it​ ​detects​


F
​ ailsafe​ ​continues​​to​​iterate​​over​​the​​original​
F
​concurrent​ ​modification​ ​during​ ​the​ ​time​ ​of​
​collection and also creates a copy to modify.​
​iteration.​

​ ailfast​ ​is​ ​generally​ ​used​ ​in​ ​single-threaded​


F ​ ailsafe​ ​is​
F ​used​ ​in​ ​multithreaded​
​environments.​ ​environments.​
​55​

​ ailfast​ ​does​ ​not​ ​allow​ ​any​ ​modification​ ​while​


F ​ ailsafe​ ​allows​ ​modification​ ​during​ ​the​ ​time​
F
​iteration.​ ​of iteration.​

​ ailfast​​is​​fast​​compared​​to​​failsafe​​as​​it​​does​​not​
F ​ ailsafe​ ​is​ ​generally​ ​slow​ ​compared​ ​to​
F
​involve the copying of the collection.​ ​failfast.​

​ ailSafe​ ​does​ ​not​ ​throws​ ​any​ ​exception​ ​but​


F
​ ailFast​​throws​​ConcurrentModificationException​
F
​instead,​ ​it​ ​creates​​a​​copy​​of​​the​​collection​​to​
​if the collection is modified during iteration.​
​iterate.​

​204. Differentiate between HashMap and TreeMap.​


​HashMap​ ​TreeMap​

​ asmap​ ​uses​ ​a​ ​hashtable​ ​in​ ​order​ ​to​ ​store​


H ​ reemap​ ​uses​ ​Red-black​ ​trees​ ​to​ ​store​
T
​key-value pairs.​ ​key-value pair.​

​ ashmap​​does​​not​​maintain​​any​​specific​​order​​for​
H ​ reemap​ ​maintains​ ​a​ ​natural​ ​ordering​ ​based​
T
​key-value pairs.​ ​on the keys.​

​ rder​ ​of​ ​iteration​ ​is​ ​not​ ​guaranteed​ ​in​ ​the​


O
​Iteration is of sorted order based on keys.​
​hashmap.​

​ ashmaps​ ​are​ ​faster​ ​for​ ​retrieval​ ​compared​ ​to​


H ​ etrieval​​in​​Treemap​​is​​slower​​as​​it​​uses​​tree​
R
​Treemap.​ ​traversal to find keys.​

​ ashmap​ ​is​ ​implemented​ ​by​ ​using​ ​an​ ​Array​ ​of​


H ​ reeMap​ ​is​ ​implemented​ ​using​ ​a​ ​Red-black​
T
​linked list.​ ​Tree.​

​ ashmap​​uses​​the​​equals()​​method​​of​​the​​Object​
H ​ reeMap​ ​uses​ ​compareTo()​ ​method​ ​to​
T
​class to compare keys.​ ​compare keys.​

​205. Differentiate between Queue and Deque.​


​Queue​ ​Deque​

​ eque​ ​also​ ​known​ ​as​ ​a​ ​Double-ended​ ​queue​


D
​ he​ ​queue​ ​is​ ​a​ ​linear​ ​Data​ ​structure​​that​​is​​used​
T ​is​ ​also​ ​a​ ​linear​ ​data​ ​structure​ ​that​ ​stores​ ​a​
​to store a collection of elements.​ ​collection​ ​of​ ​elements​ ​with​ ​operations​ ​to​
​remove and add from both ends.​

​ lements​​in​​the​​queue​​can​​only​​be​​inserted​​at​​the​
E ​ lements​ ​can​ ​be​ ​inserted​ ​from​ ​both​ ​ends​ ​of​
E
​end of the data structure.​ ​the data structure.​

​ ueue​​can​​be​​implemented​​using​​Array​​or​​Linked​
Q ​ equeue​ ​can​ ​be​ ​implemented​ ​using​ ​Circular​
D
​List.​ ​Array or Doubly Linked List.​
​56​

​ ueues​​are​​generally​​used​​to​​implement​​a​​waiting​
Q ​ eque​ ​is​ ​used​ ​to​ ​implement​ ​a​ ​stack​ ​or​
D
​list or task queue.​ ​dequeuing elements from both ends.​

​206. Differentiate between HashSet and TreeSet.​


​HashSet​ ​TreeSet​

​HashSet is unordered.​ ​TreeSet is based on natural ordering.​

​HashSet allows null elements.​ ​TreeSet does not allow null elements.​

​HashSet is Implemented by the HashSet class.​ ​TreeSet is Implemented by TreeSet class.​

​HashSet<String> hs = new HashSet<>();​ ​TreeSet<String> ts = new TreeSet<>();​

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy