Interview Question and Answers | Search Coding Interview Questions - javasearch.buggybread.com
Javasearch.buggybread.com

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.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

   next 30
 Q71. Does Constructor creates the object ?Core Java
Ans. New operator in Java creates objects. Constructor is the later step in object creation. Constructor's job is to initialize the members after the object has reserved memory for itself.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   constructor   object creation      intermediate        rare


 Q72. What will this code print ?

String a = new String ("TEST");
String b = new String ("TEST");
if(a == b) {
System.out.println ("TRUE");
} else {
System.out.println ("FALSE");
}
Core Java
Ans. FALSE. == operator compares object references, a and b are references to two different objects, hence the FALSE. .equals method is used to compare string object content.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     string   string class   java   ==   object references   coding     Asked in 2 Companies      basic        frequent

Try 1 Question(s) Test


Frequently asked question for intermediate developers. Frequently asked in HCL Technologies and EPAM.
  Q73. What is Volatile keyword used for ?Core Java
Ans. Volatile is a declaration that a variable can be accessed by multiple threads and hence shouldnt be cached.

  Sample Code for volatile

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   synchronization   volatile   java keywords     Asked in 42 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q74. Will this code give error if i try to add two heterogeneous elements in the arraylist. ? and Why ?

List list1 = new ArrayList<>();
list1.add(5);
list1.add("5");
Ans. If we don't declare the list to be of specific type, it treats it as list of objects.

int 1 is auto boxed to Integer and "1" is String and hence both are objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   arraylist   list   autoboxing   wrapper classes      expert        rare


Advanced level question. Recently asked in few Indian service companies ( Based on 3 inputs )
 Q75. What are various types of Class loaders used by JVM ?Core Java
Ans. Bootstrap - Loads JDK internal classes, java.* packages.

Extensions - Loads jar files from JDK extensions directory - usually lib/ext directory of the JRE

System - Loads classes from system classpath.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   jvm   memory management   class loaders   bootstrap   extensions   system  classloaders   advanced   technical lead   technical architect     Asked in 7 Companies


  Q76. Difference between SAX and DOM Parser ?Xml
Ans. A DOM (Document Object Model) parser creates a tree structure in memory from an input document whereas A SAX (Simple API for XML) parser does not create any internal structure.

A SAX parser serves the client application always only with pieces of the document at any given time whereas A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.

A SAX parser, however, is much more space efficient in case of a big input document whereas DOM parser is rich in functionality.

Use a DOM Parser if you need to refer to different document areas before giving back the information. Use SAX is you just need unrelated nuclear information from different areas.

Xerces, Crimson are SAX Parsers whereas XercesDOM, SunDOM, OracleDOM are DOM parsers.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   xml   parsers   sax   dom parser   difference   architecture   technical lead   technical architect  markup language   sax vs dom     Asked in 14 Companies        frequent

Try 1 Question(s) Test


Very frequently asked.Usually among first few questions.
  Q77. What is MVC ? Design
Ans. MVC is a Design Pattern that facilititates loose coupling by segregating responsibilities in a Web application

1. Controller receives the requests and handles overall control of the request
2. Model holds majority of the Business logic, and
3. View comprise of the view objects and GUI component

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     j2ee   mvc   mvc design pattern   design pattern   struts   spring   web application   web frameworks   ebay     Asked in 60 Companies      basic        frequent

Try 1 Question(s) Test


 Q78. How compiler handles the exceptions in overriding ?Core Java
Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.

2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception

3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   overriding   exceptions   inheritence   inheritance  object oriented programming (oops)  oops concepts   oops


 Q79. What things should be kept in mind while creating your own exceptions in Java?Core Java
Ans. All exceptions must be a child of Throwable.

If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.

You want to write a runtime exception, you need to extend the RuntimeException class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   user defined exceptions   throwable   architecture   library development   technical architect   technical lead


Frequently asked to fresh graduates and less experienced.
  Q80. Difference between Composition and Inheritance ?Core Java
Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects.

In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects.

From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   oops concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   difference between   basic interview question     Asked in 12 Companies      basic        frequent

Try 2 Question(s) Test


 Q81. What will be the output of following code ?

public static void main(String[] args){
String name = null;
File file = new File("/folder", name);
System.out.print(file.exists());
}
Core Java
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


 Q82. Which annotations are used in Hibernate ?Hibernate
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.
 Q83. What is StringJoiner ?Core Java
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(".");
strJoiner.add("Buggy").add("Bread");
System.out.println(strJoiner); // prints Buggy.Bread


 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


 Q84. Which access specifiers can be used with top level class ? a. public or default b. public or private c. public or protected d. protected or defaultCore Java
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


 Q85. Which of the following can be marked static ?

a. Methods , Variables and Initialization Blocks.
b. Methods , Variables , Initialization Blocks and Outer Classes and nested Classes.
c. Methods , Variables , Initialization Blocks and Outer Classes.
d. Methods , Variables , Initialization Blocks and nested Classes
Core Java
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


 Q86. Which of the following is false about main method ?

a. It should be declared public and static
b. it should have only 1 argument of type String array
c. We can override main method
d. We can overload main method
Core Java
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


 Q87. What is the difference between the following two code lines ?

1. new OuterClass().new InnerClass();

2. new OuterClass.InnerClass();
Core Java
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


 Q88. What are the different uses of Interfaces in Java ?Core Java
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


 Q89. What do you look for when you do code review ?Core Java
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


 Q90. Can we have multiple main methods in a single class ?Core Java
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


 Q91. In a Linked list with sorted numbers, insert a new numbers while maintaining the sort order.Algorithm
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


 Q92. Why can't we have diamond problem with interfaces ?Core Java
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


 Q93. Let's suppose we have an to destroy objects, Design a program to implement garbage collection ?Design
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


 Q94. Difference between AWS Cloud Formation and AWS AMI ?Amazon Web Services (AWS)
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


 Q95. What is JUnitRunner ?JUnit
 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


 Q96. What are the Wrapper classes available for primitive types ?Core Java
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


 Q97. Which String class does not override the equals() and hashCode() methods, inheriting them directly from class Object?Core Java
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


 Q98. There are two objects a and b with same hashcode. I am inserting these two objects inside a hashmap.

hMap.put(a,a);
hMap.put(b,b);

where a.hashCode()==b.hashCode()

Now tell me how many objects will be there inside the hashmap?
Core Java
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.
 Q99. Explain multithreading in Java ?Core Java
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


 Q100. What are wrapper classes ?Core Java
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


previous 30   next 30

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: