Core Java - Interview Questions and Answers for 'Default' | Search Interview Question - 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

   



Core Java - Interview Questions and Answers for 'Default' - 40 question(s) found - Order By Newest

next 30
Very Frequently asked across all type of companies and across all levels.
  Q1. Difference between Public, Private, Default and Protected ?Core Java
Ans. Private - Not accessible outside object scope.

Public - Accessible from anywhere.

Default - Accessible from anywhere within same package.

Protected - Accessible from object and the sub class objects.

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

   Like         Discuss         Correct / Improve     java   oop   access specifier   public   private   default   protected   public vs private vs default vs protected     Asked in 12 Companies      basic        frequent

Try 1 Question(s) Test


 Q2. 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


 Q3. Why Java provides default constructor ?Core Java
Ans. At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable. To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before it is used by any code.

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

   Like         Discuss         Correct / Improve     java   constructor   oops   default constructor   jvm      expert

Try 2 Question(s) Test


 Q4. What are the default or implicitly assigned values for data types in java ?
Ans. boolean ---> false
byte ----> 0
short ----> 0
int -----> 0
long ------> 0l
char -----> /u0000
float ------> 0.0f
double ----> 0.0d
any object reference ----> null

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

   Like         Discuss         Correct / Improve     java   data types   default values

Try 1 Question(s) Test


 Q5. Why do member variables have default values whereas local variables don't have any default value ?
Core Java
Ans. member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used.

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

   Like         Discuss         Correct / Improve     java   jvm   memory management   variables   stack memory   heap memory   default values      expert


 Q6. What will be the output of following code ?
class BuggyBread2 {       
   private static int counter = 0;     
   void BuggyBread2() {        
      counter = 5;    
   }     
   
   BuggyBread2(int x){
      counter = x;    
   }        

   public static void main(String[] args) {        
      BuggyBread2 bg = new BuggyBread2();        
      System.out.println(counter);    
   } 
}
Core Java
Ans.  Compile time error as it won't find the constructor matching BuggyBread2(). 
Compiler won't provide default no argument constructor as programmer has already defined one constructor. 
Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that. 

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

   Like         Discuss         Correct / Improve     java   code   coding   tricky questions   interesting questions   default constructor   constructor

Try 3 Question(s) Test


 Q7. Does java allow implementation of multiple interfaces having Default methods with Same name and Signature ?Core Java
Ans. No. Compilation error.

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

   Like         Discuss         Correct / Improve     java   java8   default method   yes-no     Asked in 1 Companies      intermediate


  Q8. What are Default Methods ?Core Java
Ans. With Java 8, We can provide method definitions in the Interfaces that gets carried down the classes implementing that interface in case they are not overridden by the Class. Keyword "default" is used to mark the default method.

  Sample Code for interface default

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

   Like         Discuss         Correct / Improve     java   java8   default methods     Asked in 7 Companies      expert        frequent


 Q9. What was the driving force to introduce default methods in Interfaces wef from Java 8 ?Core Java
Ans. Earlier any class implementing an interface was supposed to implement all methods declared in an interface. There was no place for optionally implementing all or subset of methods.Though we have abstract classes wherein we could have provided such a mechanism by declaring some methods as abstract while providing definition for some. But as Abstract classes have a body and are comparatively heavier than interfaces and interfaces associate closely to the concept of providing interfacing than abstract classes, Java might have though of providing optional implementation for default methods. This way same interface can be reused in variety of ways rather than making copies of an interface to suit different needs.

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

   Like         Discuss         Correct / Improve     java 8   java8  interface default methods  default methods     Asked in 6 Companies


 Q10. What access the variable gets if we don't provide any access specifier ?Core Java
Ans. It gets the default access i.e package protected.

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

   Like         Discuss         Correct / Improve     access specifiers  default Access specifier      Basic


 Q11. Can we have a private default method ?Core Java
Ans. No. The whole idea of default method is to provide a default definition in case the implementing class intend to provide definition for only some of the methods. Making any of the interface method private would restrict it from being implemented by the implementing class. So default method is an option to provide implementation and not restricting a new definition.

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

   Like         Discuss         Correct / Improve     java 9  default methods


Recently asked in Infoview Technologies.
 Q12. What is a Default Constructor ?Core Java
Ans. The no argument constructor provided by Java Compiler if no constructor is specified.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   default constructor     Asked in 1 Companies      Basic        frequent

Try 2 Question(s) Test


 Q13. Will Compiler creates a default no argument constructor if we specify only multi argument constructor ?
Ans. No, Compiler will create default constructor only if we don't specify any constructor.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   default constructor   yes-no      expert        rare

Try 1 Question(s) Test


 Q14. Difference between Class#getInstance() and new operator ?
Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor.

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

   Like         Discuss         Correct / Improve     java   constructor   object creation   default constructor   getinstance   new operator


 Q15. Can we create an object if a Class doesn't have any constructor ( not even the default provided by constructor ) ?
Ans. Yes , using Class.getInstance.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   default constructor   class.getinstance


 Q16. Does every class needs to have one non parameterized constructor ?Core Java
Ans. No. Every Class only needs to have one constructor - With parameters or without parameters. Compiler provides a default non parameterized constructor if no constructors is defined.

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

   Like         Discuss         Correct / Improve     java   oops   constructor   default constructor   yes no      intermediate

Try 3 Question(s) Test


 Q17. Variable of the boolean type is automatically initialized as?Core Java
Ans. The default value of the boolean type is false.

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

   Like         Discuss         Correct / Improve     java   data type   boolean   initialization   default object construction   default constructor   default value


 Q18. In a case where there are no instance variables what does the default constructor initialize?Core Java
Ans. Java expects the superclass ( Object Class ) constructor to be called while creation of any object. So super constructor is called in case there are no instance variables to initialize.

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

   Like         Discuss         Correct / Improve     java   constructor   object creation   default constructor   super

Try 1 Question(s) Test


 Q19. What will be the output of following code ?

public static void main(String[] args)
{
int x = 10;
int y;
if (x < 100) y = x / 0;
if (x >= 100) y = x * 0;
System.out.println("The value of y is: " + y);
}
Ans. The code will not compile raising an error that the local variable y might not have been initialized. Unlike member variables, local variables are not automatically initialized to the default values for their declared type.

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

   Like         Discuss         Correct / Improve     java   code   default value


 Q20. If there is a conflict between Base Class Method definition and Interface Default method definition, Which definition is Picked ?
Ans. Base Class Definition.

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

   Like         Discuss         Correct / Improve     java   java8   default methods


Rarely asked as default methods have been introduced with Java 8.
 Q21. Can we have a default method without a Body ?Core Java
Ans. No. Compiler will give error.

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

   Like         Discuss         Correct / Improve     java   java8   default method   yes-no        rare


Rarely asked as default methods have been introduced with Java 8.
 Q22. Can we have a default method definition in the interface without specifying the keyword "default" ? Core Java
Ans. No. Compiler complains that its an abstract method and hence shouldn't have the body.

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

   Like         Discuss         Correct / Improve     java   java8   default methods   default keyword   yes-no


 Q23. Can a class implement two Interfaces having default method with same name and signature ?

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}
}
Ans. No. Compiler gives error saying "Duplicate Default Methods"

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

   Like         Discuss         Correct / Improve     java   java8   default methods


 Q24. What If we make the method as abstract in another Interface ?

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}
}
Ans. Even then the Compiler will give error saying that there is a conflict.

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

   Like         Discuss         Correct / Improve     java   java8   default methods


 Q25. What if we override the conflicting method in the Class ?

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}

public void defaultMethod(){
System.out.println("HelloJava8");
}
}
Ans. There won't be any error and upon execution the overriding class method will be executed.

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

   Like         Discuss         Correct / Improve     java   java8   default methods


 Q26. What will happen if there is a default method conflict as mentioned above and we have specified the same signature method in the base class instead of overriding in the existing class ?
Ans. There won't be any problem as the Base class method will have precedence over the Interface Default methods.

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

   Like         Discuss         Correct / Improve     java   java8   default methods


 Q27. If a method definition has been specified in Class , its Base Class , and the interface which the class is implementing, Which definition will be picked if we try to access it using Interface Reference and Class object ?
Ans. Class method definition is overriding both the definitions and hence will be picked.

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

   Like         Discuss         Correct / Improve     java   java8   default methods


 Q28. If a method definition has been specified in the Base Class and the interface which the class is implementing, Which definition will be picked if we try to access it using Interface Reference and Class object ?
Ans. Base Class Definition will have precedence over the Interface Default method definition.

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

   Like         Discuss         Correct / Improve     java   java8   default methods


 Q29. Can we have default method with same name and signature in the derived Interface as the static method in base Interface and vice versa ?
Ans. Yes , we can do that as static methods are not accessible using references and hence cannot lead to conflict. We cannot do inverse as Default methods cannot be overridden with the static methods in derived interface.

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

   Like         Discuss         Correct / Improve     java   java8   default methods   static interface methods


Not frequently asked as it was introduced with Java 8.
 Q30. What is the @FunctionalInterface annotation ?Core Java
Ans. This is an informative annotation that specify that the interface is a functional interface. A Function Interface has only one abstract method and many default methods. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.

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

   Like         Discuss         Correct / Improve     java   java8   java 8   functional interface   default methods


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: