Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers | ||||
| ||||
Ans. NullPointerException at line: "File file = new File("/folder", name);" | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   io   file   fileio   coding   code   file handling | ||||
| ||||
Ans. @Entity @Table @Id @Column @Temporal @Basic @Enumerated @Access @Embeddable @Lob @AttributeOverride @Embedded @GeneratedValue @ElementCollection @JoinTable @JoinColumn @CollectionId @GenericGenerator @OneToOne @OneToMany @ManyToOne @ManyToMany @NotFound | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  hibernate   annotations | ||||
Not frequently asked as it was introduced with Java 8. | ||||
| ||||
Ans. StringJoiner is a util method to construct a string with desired delimiter. This has been introduced with wef from Java 8. Sample Code StringJoiner strJoiner = new StringJoiner("."); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java8   java 8   string   stringjoiner | ||||
| ||||
Ans. public or default | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  access specifier   oops   java   class Asked in 2 Companies | ||||
| ||||
Ans. Methods , Variables , Initialization Blocks and nested Classes | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  oops   java   static   nested classes   static nested classes | ||||
Try 2 Question(s) Test | ||||
| ||||
Ans. We can override main method | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   main method | ||||
| ||||
Ans. In first case we are trying to initialize Inner class object using the instance of Outer Class whereas in second case we are trying to initialize the Inner class object directly using the Outer class name. In second case , Inner class is "static inner class" as we cannot access "non static inner class" using Classname alone. In first case, the inner class could be either "static inner class" or "non static inner class". | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  inner classes  inner class   static inner class   new operator | ||||
| ||||
Ans. 1. Regulation / Enforcement / Policy / Contract , It's primary use 2. Use as a collection of utility methods(since java 8 through default methods) 3. Collecting constants together, As Interface are lighter, it makes sense to use them instead of classes if no getters and setters required. 4. Creation of Custom Annotations 5. Special services like marker interface | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  interfaces   uses of interfaces in java Asked in 1 Companies | ||||
Frequently asked in high end product companies. Frequently asked in Deloitte. | ||||
| ||||
Ans. https://medium.com/javarevisited/internal-working-of-hashmap-in-java-97aeac3c7beb#:~:text=HashMap%20internally%20uses%20HashTable%20implementation,the%20entries%20into%20the%20map. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  hashmap  collections  hashmap internal implementation Asked in 20 Companies expert   frequent | ||||
| ||||
Ans. Modularity - First sign of good code is whether it has been segregated into methods and classes appropriately. I dont mind it in excess because I believe that is forward looking strategy as applications tends to expand and eventually become hard to read. Self Explanatory - Variables and methods should be named in a way that the code should be self explanatory even without comments. Use of Constant variables to explain use of literal. Proper Code Reuse - If there is anything being reused , it should be moved to parent classes / methods. Proper composition calls - Composed hierarchy should not be access in just single line. One or two levels is ok but having multiple levels make it hard to read and debug. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  code review  clean code Asked in 1 Companies intermediate   frequent | ||||
| ||||
Ans. We can overload the main method by specifying different argument types. For example - 2 main methods with different arguments is perfectly legal public static void main(); public static void main(String[] args); The following are not legal as compiler will complain of duplicate methods public static void main(String[] args); public static void main(String[] args); Even The following are not legal as we cannot overload on return types public static String main(String[] args); public static void main(String[] args); | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  main method Asked in 2 Companies | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node{ Integer body; Node nextNode; Node(Integer value){ body = value; } } private void insertInMiddle(Integer value){ head = start; if(start == null) { start = new Node(value); head = start; head.nextNode = null; return; } while(head.body < value){ if(head.nextNode == null || head.nextNode.body >= value){ Node newNode = new Node(value); newNode.nextNode = head.nextNode; head.nextNode = newNode; break; } head = head.nextNode; } } private void traverse(){ head = start; while(head != null){ System.out.println(head.body); head = head.nextNode; } } public static void main(String[] args){ LinkedList ll = new LinkedList(); ll.insertInMiddle(5); ll.insertInMiddle(10); ll.insertInMiddle(15); ll.insertInMiddle(7); ll.traverse(); } } | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  LinkedList  Data structures  Algorithm | ||||
| ||||
Ans. Interfaces don't have member elements and method definitions that could cause diamond problem. With Java 8, Interfaces have default method definitions. This could have created diamond problem but Java introduced a compile time check for "duplicate default methods" in case same method is derived from multiple interfaces and no definition is overridden by the class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  diamond problem  interfaces  java 8  default methods | ||||
| ||||
Ans. Step 1 - We can create a Registry Class having Map of all created objects as key and References list as value. Step 2 - Whenever we create an object , we should update the Registry in the constructor to add the new object. Step 3 - Whenever we assign a new reference to the object , we need to update the entry in Map. Similarly if the reference get's removed ( end of scope etc ), we need to remove the entry of reference from the list. Step 4 - We can have threaded code to monitor the Map to see if any object looses all it's references and should call the method to destroy object and clean the memory. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  garbage collection | ||||
| ||||
Ans. AMI is an Amazon Machine Image. It contains the configuration to enable to boot up an EC2 instance with said configuration whereas Cloud formation is a templating language that allows to describe how to build a VPC and also allows you to create AWS services AMI is templating specific to instances whereas the scope of CloudFormation templating is much bigger. CloudFormation could use AMI for launching instances along with other services. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  aws cloud formation  aws ami  amazon ami  aws ami  amazon ami vs aws cloud formation | ||||
| ||||
This question was recently asked at 'Barclays'.This question is still unanswered. Can you please provide an answer. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve   Asked in 1 Companies | ||||
| ||||
Ans. boolean - java.lang.Boolean byte - java.lang.Byte char - java.lang.Character double - java.lang.Double float - java.lang.Float int - java.lang.Integer long - java.lang.Long short - java.lang.Short void - java.lang.Void | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   java5   data types   wrapper classes   adapter design pattern   rare | ||||
| ||||
Ans. java.lang.StringBuffer. | ||||
Sample Code for StringBuffer | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   object class   stringbuffer expert   rare | ||||
| ||||
Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   hashcode   map   hashmap   object reference   advanced Asked in 1 Companies | ||||
Frequently asked to fresh graduates and less experienced developers. | ||||
| ||||
Ans. 1. Multithreading provides better interaction with the user by distribution of task 2. Threads in Java appear to run concurrently, so it provides simulation for simultaneous activities.The processor runs each thread for a short time and switches among the threads to simulate sim-ultaneous execution (context-switching) and it make appears that each thread has its own processor.By using this feature, users can make it appear as if multiple tasks are occurring simultaneously when, in fact, each is running for only a brief time before the context is switched to the next thread. 3. We can do other things while waiting for slow I/O operations.In the java.iopackage, the class InputStreamhas a method, read(), that blocks until a byte is read from the stream or until an IOExceptionis thrown. The thread that executes this method cannot do anything elsewhile awaiting the arrival of another byte on the stream. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   threads   multi threading  concurrency   multithreading Asked in 5 Companies intermediate   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. They are wrappers to primitive data types. They allow us to access primitives as objects. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   data types   wrapper classes Asked in 3 Companies basic   frequent | ||||
| ||||
Ans. Cookie and Session are used to store the user information. Cookie stores user information on client side and Session does it on server side. Primarily, Cookies and Session are used for authentication, user preferences, and carrying information across multiple requests. Session is meant for the same purpose as the cookie does. Session does it on server side and Cookie does it on client side. One more thing that quite differentiates between Cookie and Session. Cookie is used only for storing the textual information. Session can be used to store both textual information and objects. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  session   session management   java   cookies   authentication   web application   ebay intermediate | ||||
Very frequently asked. Usually asked in different variants like Diff between StringBuffer , String Builder ; Difference between StringBuilder and String class; Choice between these classes etc. | ||||
| ||||
Ans. A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. The String class represents character strings. All string literals in Java programs, such as "abc" are constant and implemented as instances of this class; their values cannot be changed after they are created. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   string   stringbuffer   frequently asked Asked in 18 Companies basic   frequent | ||||
Try 1 Question(s) Test | ||||
| ||||
Ans. The JDBC is a pure Java API used to execute SQL statements. It provides a set of classes and interfaces that can be used by developers to write database applications. The steps needed to execute a SQL query using JDBC: 1. Open a connection to the database. 2. Execute a SQL statement. 3. Process th results. 4. Close the connection to the database. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   jdbc   db connectivity Asked in 2 Companies basic   frequent | ||||
| ||||
Ans. We can make a class immutable by 1. Making all methods and variables as private. 2. Setting variables within constructor. Public Class ImmutableClass{ private int member; ImmutableClass(int var){ member=var; } } and then we can initialize the object of the class as ImmutableClass immutableObject = new ImmutableClass(5); Now all members being private , you cant change the state of the object. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   immutable  immutability   immutable  immutability class   technical lead Asked in 5 Companies intermediate   frequent | ||||
Try 2 Question(s) Test | ||||
| ||||
Ans. Yes.we cannot access them directly but we can access them using object reference.Static methods belong to a class and not objects whereas non static members are tied to an instance. Accessing instance variables without the instance handler would mean an ambiguity regarding which instance the method is referring to and hence its prohibited. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   oops   static   static methods   java keywords Asked in 1 Companies | ||||
Frequently asked to fresh graduates. | ||||
| ||||
Ans. When two threads are waiting each other and cant precede the program is said to be deadlock. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   threads   multi threading   operating system   deadlock  concurrency Asked in 23 Companies basic   frequent | ||||
| ||||
Ans. Java does not support pointers. Java does not support multiple inheritances. Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework. C++ compiles to machine language , when Java compiles to byte code . In C++ the programmer needs to worry about freeing the allocated memory , where in Java the Garbage Collector takes care of the the unneeded / unused variables. Java is platform independent language but c++ is depends upon operating system. Java uses compiler and interpreter both and in c++ their is only compiler. C++ supports operator overloading whereas Java doesn't. Internet support is built-in Java but not in C++. However c++ has support for socket programming which can be used. Java does not support header file, include library files just like C++ .Java use import to include different Classes and methods. There is no goto statement in Java. There is no scope resolution operator :: in Java. It has . using which we can qualify classes with the namespace they came from. Java is pass by value whereas C++ is both pass by value and pass by reference. Java Enums are objects instead of int values in C++ C++ programs runs as native executable machine code for the target and hence more near to hardware whereas Java program runs in a virtual machine. C++ was designed mainly for systems programming, extending the C programming language whereas Java was created initially to support network computing. C++ allows low-level addressing of data. You can manipulate machine addresses to look at anything you want. Java access is controlled. C++ has several addressing operators . * & -> where Java has only one: the . We can create our own package in Java(set of classes) but not in c and c++. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   c++   difference between java and c++   programming concepts   programming languages   architecture   technical architect   technical lead Asked in 1 Companies | ||||
| ||||
Ans. Struts 1 actions are singleton. So all threads operates on the single action object and hence makes it thread unsafe. Struts 2 actions are not singleton and a new action object copy is created each time a new action request is made and hence its thread safe. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   j2ee   mvc frameworks   struts   struts1   struts2   struts 1   struts 2   architecture | ||||
Try 1 Question(s) Test | ||||
Frequently asked to fresh graduates. | ||||
| ||||
Ans. Runnable - waiting for its turn to be picked for execution by the thread schedular based on thread priorities. Running - The processor is actively executing the thread code. It runs until it becomes blocked, or voluntarily gives up its turn. Waiting: A thread is in a blocked state while it waits for some external processing such as file I/O to finish. Sleeping - Java threads are forcibly put to sleep (suspended) with Thread.sleep. they can resume using Thread.resume method. Blocked on I/O - Will move to runnable after I/O condition like reading bytes of data etc changes. Blocked on synchronization - Will move to Runnable when a lock is acquired. Dead - The thread is finished working. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  java   threads   multi threading   scheduling   thread states   basic interview question Asked in 1 Companies basic   frequent | ||||
Try 2 Question(s) Test | ||||