0% found this document useful (0 votes)
15 views

Core Java Interview Questions

Uploaded by

adipro1001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views

Core Java Interview Questions

Uploaded by

adipro1001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
CORE JAVA INTERVIEW QUESTIONS YOU'LL MOST LIKELY BE ASKED JAVA COLLECTIONS Cheat Sheet arraytise O11) Of) 01 Oto} Olt) Arey 12) O(n) O(n) Olt) Unked Use Ofn) ofa) ‘Ofn) Of1} Array: Remove Contains Next Se 243) on Oth/n} {1} Hash Table on) 041) on) ou) (O{1) Hash Table + Linked List 9) 01) (1) (1) (1) Bit Vector flog n} Oflog n) —Oflogn) —_Oflogn) Of) __Redbiack tre JCopyOnWriteArraySet O(n) Oo) fn). ott) fa) Array [ConcurrentSkiplistSet _Ollog 9) Oilog 9} on) fn) Skip List Cm fh /n) Hash Table oft) 04) Olt) Hash Table + Linked List O14) Ot) Of Jn) Array O14) Ofh /n) Hash Table {1} Aeray. flog n) Redblack tree (fh /n) Hash Tables Ot) Skip Uist a) ) Of) Of) (2) oft) On) 1) what are static blocks and static initializers in Java ? Static blocks or static initializers are used to initialize static fields In Java. we deciare static blocks when we want to intialize static fields in our class. Static blocks gets executed exactly once when the class is loaded + Static blocks are executed even before the constructors are executed. 2) How to call one constructor from the other constructor ? ‘With in the same class if we want to call one constructor from other we use this() method. Based on the number of parameters we pass appropriate this() method is called. Restrictions for using this method : 1) this must be the first statement in the constructor 2)we cannot use two this() methods In the constructor 3) What Is method overriding in java ? If we have methods with same signature (same name, same signature, same return type) In super class land subclass then we say subclass method is overridden by superclass ‘When to use overriding in java If we want same method with different behaviour In superclass and subclass then we go for overriding. ‘When we call overridden method with subclass reference subclass method Is called hiding the superclass method 4) What is super keyword in java ? Variables and methods of super class can be overridden in subclass . In case of overriding , a subclass object call its own variables and methods. Subclass cannot access the variables and methods of ‘superclass because the overridden variables or methods hides the methods and variables of super class. But still java provides a way to access super class members even if its members are overridden. Super is Used to access superclass veriables, methods, constructors, Super can be used In two forms : 1) First form is for calling super class constructor. 2) Second one is to call super class variables,methods. Super if present must be the first statement. 5) Difference between method overloading and method overriding in java ? [Method Overloading (Method Overriding |) Method Overloading occurs with In the same Method Overriding occurs between lelass two classes superclass and subclass |2) since it Involves with only one class inheritance Since method overriding occurs between superclass is not involved. land subclass inheritance is involved. |5)in overioading return type need not be the same 3) In overriding return type must be same. |a) Parameters must be different when we do 4) Parameters must be same. loverioading 5) Static polymorphism can be achelved using | 5) Dynamic polymorphism can be acheived using |method overloading ‘methad overriding. 66) In overloading one method can’t hide the 66) In overriding subclass method hides that of the another ‘superclass method. Overloading Overriding lass D054 class Dogi Public vota (ark) )( public void bark} ( Syoten, out -BEETEtntwoot—*}+__ 5 ea: System.out.printIn(“woof ") Dirt Parone » Sone Method Nome — » ‘Sone poroneter se nam) ( clase Hound extends Dosi At Public void snift()( / Syaten.out .printin("woof °): eratek. one cpegetay zante. ‘ 7 public voial bark] ( systen. out .printin("bowl"); > 6) Difference between abstract class and interface ? [ Interface ‘Abstract Class 1) Interface contains only abstract methods 1) Abstract class can contain abstract methods, | ‘concrete methods or both (2) Access Specifiers for methods in interface 2) —_ Except private we can have any access |must be public Specifier for methods in abstract class. |5) Variables defined must be public, static, 3) Except private variables can have any access ‘tinal specifiers |4) Multiple inheritance in java is implemented |4)We cannot achieve multiple inheritance using lusing interface abstract class. 5) To implement an interface we use '5)To implement an interface we use implements Implements keyword keyword 7) _ Why java Is platform independent? ‘The most unique feature of java Is platform independent. In any programming language soruce code is complied in to executable code . This cannot be run across all platforms. When javac complles a java Program it generates an executable file called .class file. lass file contains byte codes. Byte codes are interpreted only by 3VM's . Since these JVM's are made available across all platforms by Sun Microsystems, we can execute this byte code in any platform. Byte code generated in windows environment can also be executed in linux environment. This makes java platform independent. 8) What is method overloading In java ? A class having two or more methods with same name but with different arguments then we say that those methods are overloaded. Static polymorphism is achieved In java using method overioading. Method overloading is used when we want the methods to perform similar tasks but with different Inputs for values. When an overoaded method Is invoked java first checks the method name, and the number of arguments ,type of arguments; based on this compller executes this method, ‘Compiler decides which method to call at compile time, By using overloading statle polymorphism or static binding can be achieved in jav Note : Return type Is not part of method signature, we may have methods with different return types but return type alone Is not sufficient to call a method in Java. What Is difference between c++ and Java 7 Java I C++ Java is platform independent Ce is plotform dependent. There are ne pointers in Java There are pointers In C++. There is ne operator overloading in java | C 4+ has operator overloading. There is garbage collection in java There is ne garbage collection Supports multithreading DDoesint support multithreading There are no templates In java “There are templates in java 7) There are ne global variabies in java ‘There are global variables in c++ 10) What Is IT compiler ? JIT compiler stands for Just in time compiler. JIT compiler compiles byte code In to executable cod IIT a part of IVM .2IT cannot convert complete java program in to executable code it converts as and when it Is needed during execution. 11) What is bytecode in java ? ‘When a javac compiler compiler compiles a class it generates .class file. This .class file contains set of Instructions called byte code, Byte code Is a machine Independent language and contains set of Instructions which are to be executed only by JVM. JVM can understand this byte codes. 12) _ Difference between this() and super() in java ? this() Is used to access one constructor from another with In the same class while super) is used to access superclass constructor. Either this() or super() exists it must be the first statement in the ‘constructor. 13) What isa class ? Classes are fundamental or basic unit in Object Oriented Programming .A class Is kind of bi template for objects. Class defines variables, methods. A class tells what type of objects we are creating. For example take Department class tells us we can create department type objects. We can create any number of department objects. All programming constructs in java reside in class. When JVM starts running It first looks for the class when we compile. Every Java application must have atleast one class and one main method. Class starts with class keyword. A class definition must be saved in class file that has same as class name. File name must end with .java extension, public class FirstClass {public static vold main(String] arcs) {System.out.printin(*My First class"); ) > If we see the above class when we compile VM loads the FirstClass and generates a .class file(FirstClass.class). When we run the program we are running the class and then executes the main method. 44) What is an object 7 ‘An Object is instance of class. A class defines type of object. Each object belongs to some class.€very ‘object contains state and behavior. State Is determined by value of attributes and behavior is called method. Objects are alos called as an instanc To instantiate the class we declare with the class type. public classFirstCiass {public static voldmain(string{ args) { FirstClass fnew FirstClass(); ‘System.out;printin("My First class”); } > ‘To instantiate the FirstClass we use this statement FirstClass f=new FirstClass(); fis used to refer FirstClass object. 15)What Is method in java ? It contains the executable body that can be applied to the specific object of the class. Method includes method name, parameters or arguments and return type and a body of executable code. ‘Syntax : type methodName(Argument List) { ex : public float add(int a, int b, inte) methods can have multiple arguments. Separate with commas when we have multiple arguments. 16) What Is encapsulation ? The process of wrapping or putting up of data in to a single unit class and keeps data safe from misuse is called encapsulation ‘Through encapsulation we can hide and protect the data stored in java objects.Java supports ‘encapsulation through access control. There are four access control modifiers in Java public , private protected and defautt level. For example take @ cer class , In car we have many parts which Is not required for driver to know what all it consists inside. He Is required to know only about how to start and stop the car. So we can expose what all are required and hide the rest by using encapsulation. 17) Why main() method is public, static and void in java ? public : “public” Is an access specifier which can be used outside the class. When main method Is declared Public It means it can be used outside class. ‘static : To call a method we require object. Sometimes it may be required to call a method without the help of object. Then we declare that method as static. VM calls the main() method without creating object by declaring keyword static. vvold : void return type Is used when a method does‘nt return any value . main() method does’nt return ‘any value, so main() Is declared as void. Signature : public static void main(Stringl] args) ¢ 18) Explain about main() method in java ? Main() method is starting point of execution for all java applications. publi static void main(String] args) { String aros|] are array of string objects we need to pass from command line arguments. Every Java application must have atleast one main method. 19)What is constructor in java ? A constructor is a special method used to initialize objects in java. ‘We use constructors to initialize all variables in the class when an object Is created. As and when an object Is created It Is initialized automatically with the help of constructor in java. ‘We have two types of constructors Default Constructor Parameterized Constructor Signature : public classname() 4 , Signature : public classname(parameters list) { > 20) What is difference between length and length() method in java ? length() : In String clase we have length() method which ie used to return the number of characters in string. Ex : String str = “Hello World"; System.out.printin(str.length()); ‘Strilength() will return 11 characters including space. length : we have length instance variable in arrays which will return the number of values or objects in array. For example String days{]=(" Sun","Mon","wed","thu", "fri","sat" Will return 6 since the number of values in days array Is 6. 21) What is ASCII Code? ASCII stands for American Standard code for Information Interchange. ASCII character range Is 0 to 255. We can’t add more characters to the ASCII Character set, ASCII character set supports only English. That

You might also like