Java - Interview Questions and Answers for 'Methods' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Methods' - 102 question(s) found - Order By Newest

 Q1. How can we run a java program without making any object?Core Java
Anonymous
Ans. By putting code within static method. With Java 6 and earlier versions, even static block can be used.

  Sample Code for static block

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

   Like      Discuss      Correct / Improve     java   oops   static   static method   static block     American Express  Cognizant (CTS)      basic        frequent

Try 1 Question(s) Test


Frequently asked to fresh graduates and less experienced.
 Q2. Why do we write public static void main ? Can we use some other syntax too for main ?Core Java
Admin
info@buggybread.com
Ans.
1. public is the access modifier that makes the method accessible from anywhere, static is the keyword that makes it accessible even without creating any object and using class name only , void means it doesnt return anything , String args[] is the array of argument that this method receives.

2. If I use Main , it will compile correctly as Java will treat it as just another method but it wont be the method "main" which Java looks for when it looks to execute the class and hence will throw

Error: Main method not found in class , please define the main method as:
public static void main(String[] args)

3. Main is not a keyword but a special string that Java looks for while initiating the main thread.

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

   Like      Discuss      Correct / Improve     java   main method     Cognizant (CTS)  Computer Solutions & Software International (CSSI)  Telcoinabox  IBM Pune      basic        frequent

Try 1 Question(s) Test


 Q3. What are the advantages and disadvantages of static variables and static methods ?Core Java
Admin
info@buggybread.com
Ans. Advantages

Can do meta object operations ( like validating something before creating objects , keep count of number of objects )

Can do operations which have nothing to do with objects but still you want them to be tied to Class.

Disadvantages

Commonly used to static variables sometime leads to problems due to access by different objects.

Are not tied to objects so doesn't reflect pure Object Oriented approach.

Needs to be synchronized so as to avoid update conflicts by mutiple objects and threads.

Some limitation in testing as not all frameworks have facility to mock them. Powermock has but Mockito doesnt

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

   Like      Discuss      Correct / Improve     java   static   static variables   static methods


 Q4. Can we access instance variables within static methods ?Core Java
Anonymous
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     Oracle


 Q5. Similarity and Difference between static block and static method ?Core Java
Admin
info@buggybread.com
Ans. Both belong to the class as a whole and not to the individual objects. Static methods are explicitly called for execution whereas Static block gets executed when the Class gets loaded by the JVM.

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

   Like      Discuss      Correct / Improve     java   oops   static   static method   static block   difference between   similarity      basic        frequent

Try 1 Question(s) Test


 Q6. Can we override compareTo method for Enumerations ?Core Java
Admin
info@buggybread.com
Ans. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared.

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

   Like      Discuss      Correct / Improve     java   compareto   final methods   enum   enumeration      expert


 Q7. 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
Anonymous
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


 Q8. Difference between parameters and arguments ?Core Java
Admin
info@buggybread.com
Ans. Parameters are the variables that the method is expected to receive along with the method call. Arguments are the values which are passed on while calling the methods.

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

   Like      Discuss      Correct / Improve     parameter   argument  method   function  java      basic


 Q9. In which cases , moving methods to utility class could be useful ?Core Java

Ans. It could be worthy to move a method to util class if the method needs to be shared, doesn't require polymorphic behavior and need not be overridden in special cases.

Don't belong to one group through is-a relationship ( You can share through parent class method )

Don't implement a specific interface ( java 8 default methods )

Doesn't involve complex computing as you will be loosing the benefit of object state with just static method.

Doesn't require polymorphic behavior as static methods don't participate in runtime polymorphism.

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

   Like      Discuss      Correct / Improve     utility classes   util classes   static methods   application design        rare



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Can we have multiple main methods in a single class ?Core Java2017-01-12 11:00:02

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     HCL Technologies  SapientNitro


 Q11. Which of the following are valid declarations

1. void method(int... x){};
2. void method(int.. x){};
3. void method(int.. .x){};
4. void method(int ...x){};
5. void method(int... x){};
6. void method(int ... x){};
7. void method(int x, int... y){};
8. void method(int... x, int y){};
9. void method(int... x,int... y){};
Core Java2017-05-22 21:22:57

Ans. 1st is a valid and standard declaration.

2nd results in compilation error as only 2 dots are there.

3rd results in compilation error as three dots are not consecutive and broken.

4 through 6 may not be standard and ideal way of declarations but they are valid and will compile and work fine.

7 is valid declaration.

8 and 9 will result in compilation error as var args can only be provided to last argument.

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

   Like      Discuss      Correct / Improve     var args  methods  functions   method declaration  scjp  ocjp


 Q12. What is the difference between these two method declarations ?

private static void method(String[] arg)

and

private static void method(String... arg)
Core Java2017-05-23 08:41:54

Ans. First expects the argument as a string array whereas second expects variable number of string arguments or a string array.

So we can call both by providing string array as an argument but second can be called with 0 to n string arguments which cannot be done for first.

for example - We can call second method with any of following

method();
method("Hello");
method("Hello","World");
method(new String[4]);


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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q13. What is a final method ?Core Java
Anonymous
Ans. Its a method which cannot be overridden. Compiler throws an error if we try to override a method which has been declared final in the parent class.

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

   Like      Discuss      Correct / Improve     java   oops   final   final method     Oracle  Intertek  HCL Technologies  Tata Consultancy (TCS)  Accenture   IBM  Goldman Sachs   TrueCar   Intertek RedMane   Antra      basic        frequent

Try 1 Question(s) Test


 Q14. What is a Final Method ?Core Java
Anonymous
Ans. A Method that cannot be overriden in the sub class.

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

   Like      Discuss      Correct / Improve     java   oops   java keywords   final   final method   overriding   basic interview question      basic        frequent

Try 1 Question(s) Test


 Q15. Can we override static methods ? Why ?Core Java
Anonymous
Ans. No.

Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior.

A static method is not associated with any instance of a class so the concept of overriding for runtime polymorphism using static methods is not applicable.

 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   yes-no      intermediate        frequent


 Q16. How can we create objects if we make the constructor private ?Core Java
Anonymous
Ans. We can do so through a static public member method or static block.

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

   Like      Discuss      Correct / Improve     java   oops   static   constructor   static method   static block  private     Jabong.com


 Q17. What does String intern() method do?Core Java
Admin
info@buggybread.com
Ans. intern() method keeps the string in an internal cache that is usually not garbage collected.

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

   Like      Discuss      Correct / Improve     java   string class   string   intern method   garbage collection   advanced     Polaris  Netflix      expert


 Q18. Will the following program display ""Buggy Bread"" ?

class Test{
   static void display(){
      System.out.println(""Buggy Bread"");
   }
}

class Demo{
   public static void main(String... args){
      Test t = null;
      t.display();
   }
}
Core Java
Admin
info@buggybread.com
Ans.  Yes. static method is not accessed by the instance of class. Either you call it by the class name or the reference.

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

   Like      Discuss      Correct / Improve     java   static   static method   code


  Q19. Can we overload main method in Java ?Core Java
Admin
info@buggybread.com
Ans. Yes, but the overloaded main methods without single String[] argument doesn't get any special status by the JVM. They are just another methods that needs to be called explicitly.

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

   Like      Discuss      Correct / Improve     java   main method   overloading   yes-no     The Advisory Board Company  Thomson Reuters  ePace Technologies  PC Technology  PEOL  MilleniumSoft      intermediate        frequent



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. Can we use static method definitions in Interfaces ?Core Java
Admin
info@buggybread.com
Ans. Yes, Effective Java 8.

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

   Like      Discuss      Correct / Improve     java   java8   static interface methods   yes-no     HCL Technologies


 Q21. Why can't we use this in static context ?Core Java
Admin
info@buggybread.com
Ans. Static methods can be called using instance references wherein this would have made sense but static method can also be called using Class name wherein this would mean nothing and hence forbidden.

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

   Like      Discuss      Correct / Improve     java   static methods   this keyword

Try 2 Question(s) Test


 Q22. Can we overload method as following ?

void method(int... x){};
void method(int[] x){};
Core Java2017-05-22 21:27:42

Ans. No. Because java internally treats var args as arrays and hence both method declarations will generate the same byte code and hence would result in ambiguity while determining call binding.

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

   Like      Discuss      Correct / Improve     var args  methods  functions   method declaration  scjp  ocjp


 Q23. Why can't we have diamond problem with interfaces ?Core Java2017-07-30 08:15:33

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


 Q24. Can static method access instance variables ?Core Java
Anonymous
Ans. Though Static methods cannot access the instance variables directly, They can access them using instance handler.

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

   Like      Discuss      Correct / Improve     static   static method   java   oop   variables     Infoview Technologies      basic        frequent


 Q25. Does java allow overriding static methods ?Core Java
Anonymous
Ans. No. Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior.

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

   Like      Discuss      Correct / Improve     java   oops   static   static method   overriding      intermediate

Try 3 Question(s) Test


 Q26. Explain Method Local Inner Classes ?Core Java
Admin
info@buggybread.com
Ans. You can create a non-static local class inside a body of code. Interfaces cannot have local classes, and you cannot create local interfaces.

Local classes are accessible only from the body of the code in which the class is defined. The local classes are completely inaccessible outside the body of the code in which the class is defined.

You can extend a class or implement interfaces while defining a local class.

A local class can access all the variables available in the body of the code in which it is defined. You can pass only final variables to a local inner class.

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

   Like      Discuss      Correct / Improve     java   oops   inner classes   local classes   classes   method local inner classes     Cognizant (CTS)


 Q27. What will happen if static modifier is removed from the signature of the main method?
Admin
info@buggybread.com
Ans. Program throws "NoSuchMethodError" error at runtime .

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

   Like      Discuss      Correct / Improve     java   main method   main   static   static method   nosuchmethoderror


 Q28. what will be the output of this code ?

public static void main(String[] args){
   StringBuffer s1=new StringBuffer("Buggy");                       
   test(s1);                    
   System.out.println(s1);              
}       

private static void test(StringBuffer s){      
   s.append("Bread");   
}
Core Java
Admin
info@buggybread.com
Ans.  BuggyBread

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

   Like      Discuss      Correct / Improve     java   code   coding   stringbuffer   string   method calling   pass by reference


 Q29. what will be the output of this code ?

public static void main(String[] args)    {          
   String s1=new String("Buggy");                         
   test(s1);                    
   System.out.println(s1);              
}       

private static void test(StringBuffer s){      
   s.append("Bread");   
}
Core Java
Admin
info@buggybread.com
Ans.  Buggy

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

   Like      Discuss      Correct / Improve     java   code   coding   stringbuffer   string   method calling   pass by reference



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q30. what will be the output of this code ?

public static void main(String[] args)    {          
   StringBuffer s1=new StringBuffer("Buggy");                         
   test(s1);                    
   System.out.println(s1);              
}       

private static void test(StringBuffer s){      
   s=new StringBuffer("Bread");   
}
Core Java
Admin
info@buggybread.com
Ans.  Buggy

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

   Like      Discuss      Correct / Improve     java   code   coding   stringbuffer   string   method calling   pass by reference


 Q31. what will be the output ?

class Animal {
public void eat() throws Exception {
}
}

class Dog2 extends Animal {
public void eat(){}
public static void main(){
Animal an = new Dog2();
an.eat();
}
}
Core Java
Admin
info@buggybread.com
Ans. Compile Time Error: Unhandled exception type Exception

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

   Like      Discuss      Correct / Improve     java   code   coding   overridding   late binding   exception handling   abstract class   abstract methods


 Q32. If there is a conflict between Base Class Method definition and Interface Default method definition, Which definition is Picked ?
Admin
info@buggybread.com
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.
 Q33. Can we have a default method without a Body ?Core Java
Admin
info@buggybread.com
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


 Q34. Does java allow implementation of multiple interfaces having Default methods with Same name and Signature ?Core Java
Admin
info@buggybread.com
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     cognizant (CTS)      intermediate


  Q35. What are Default Methods ?Core Java
Admin
info@buggybread.com
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     DataArt  Expeditors  Wipro  GoodData  Infosys Technologies  sharp software development  Ensure support services      expert        frequent


Rarely asked as default methods have been introduced with Java 8.
 Q36. Can we have a default method definition in the interface without specifying the keyword "default" ? Core Java
Admin
info@buggybread.com
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


 Q37. 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();
}
}
Admin
info@buggybread.com
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


 Q38. 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();
}
}
Admin
info@buggybread.com
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


 Q39. 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");
}
}
Admin
info@buggybread.com
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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q40. 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 ?
Admin
info@buggybread.com
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


 Q41. 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 ?
Admin
info@buggybread.com
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


 Q42. 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 ?
Admin
info@buggybread.com
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


 Q43. Can we access Interface static method using Interface references ?
Admin
info@buggybread.com
Ans. No, only using Interface Name.

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

   Like      Discuss      Correct / Improve     java   java8   static interface methods


 Q44. Can we have default method with same name and signature in the derived Interface as the static method in base Interface and vice versa ?
Admin
info@buggybread.com
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.
 Q45. What is the @FunctionalInterface annotation ?Core Java
Admin
info@buggybread.com
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


 Q46. Will this code compile ?

public class BuggyBread1{
abstract public void test();
}
Core Java
Admin
info@buggybread.com
Ans. No. It will give the compilation error saying "The abstract method test in type BuggyBread1 can only be defined by an abstract class".

We need to declare the class abstract for it to have any abstract method.

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

   Like      Discuss      Correct / Improve     java   oops   abstract class   abstract methods   java compilation error


 Q47. Will this Code compile ?

abstract public class BuggyBread1{
abstract public void test(){};
}
Core Java
Admin
info@buggybread.com
Ans. No. This will give a compilation error saying "Abstract methods do not specify a body".

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

   Like      Discuss      Correct / Improve     java   abstract classes   abstract methods   java compilation error   java coding   java code   coding   yes-no


 Q48. Which Software Development methodology is being used in your current Job ?Process
Admin
info@buggybread.com
Ans. We are using Agile methodology. I attend daily stand up where the development leads takes the status of assigned stories, achievements, any bottlenecks or challenges. We follow iteration of 2 weeks.

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

   Like      Discuss      Correct / Improve     sdlc   agile methodology   software system analyst   software developer interview   development lead   project lead interview


 Q49. When are static and instance methods resolved ? During compile time or Runtime ?
Admin
info@buggybread.com
Ans. Static methods are resolved during compile time and hence they cannot participate in runtime polymorphism. Instance methods are resolved during runtime.

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

   Like      Discuss      Correct / Improve     java   static methods   methods   runtime polymorphism      intermediate



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q50. How can we execute a Java class independently if it doesn't have a static main method ?

a. By initiating the flow in any of the static method
b. By initiating the flow in any of static block
c. By initiating the flow in any other instance method named as main
d. By initiating the flow in any other instance method named as main and making it final
Anonymous
Ans. By initiating the flow in any of static block

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

   Like      Discuss      Correct / Improve     main method   java


 Q51. Explain TDD or Test Driven Design ?Design

Ans. TDD is a development process that involves short iterations: first an automated test case is written. Then, the code is written to pass that test, and finally one refactors the new code to acceptable standards.

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

   Like      Discuss      Correct / Improve     elsevier   test driven design ( TDD )   software development methodologies     Elsevier  Deegit  Cerner        frequent


 Q52. How are values passed in Java ? By value or reference ?

Ans. Java only provides pass by value.

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

   Like      Discuss      Correct / Improve     ebay   pass by value   pass by reference   method call   methods   functions   function call     Trustwave      basic        frequent


Recently asked in Capital One.
 Q53. What will happen if we call perform(null) ?

public void perform(Object obj) {
System.out.println("Object");
}

public void perform(Integer int) {
System.out.println("Integer");
}
Core Java

Ans. compiler will throws error as ambiguous methods

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

   Like      Discuss      Correct / Improve     overloading   method overloading     CapitalOne      intermediate


 Q54. What is the use of MethodHandle ?

Ans. MethodHandle is a modern, more flexible, more typesafe way of doing reflection.

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

   Like      Discuss      Correct / Improve     MethodHandle  Reflection


 Q55. What are instance methods ?2016-06-03 10:07:50

Ans. All methods defined in a class that are not marked static are actually instance methods.

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

   Like      Discuss      Correct / Improve     methods  functions


 Q56. Is a method name same as Class name permissible in Java ?2016-06-12 20:19:08

Ans. Yes, But the method should have some return type as otherwise it will be treated as a constructor.

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

   Like      Discuss      Correct / Improve     constructor  methods  method names

Try 1 Question(s) Test


 Q57. Can we declare a main method as abstract ?2016-06-21 13:11:31

Ans. No. Static methods cannot be overridden and hence make no sense to be declared abstract.

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

   Like      Discuss      Correct / Improve     main method   abstract

Try 1 Question(s) Test


 Q58. Can we send a request body with the Get Request ? If not, What is the alternate to pass message to the Get Request ?Web Service2016-06-22 09:18:14

Ans. Request Body in case of Get Request has no meaning and hence it's not parsed when the request is received. Alternatively Request Parameters are passed as either Path Params or Query Params.

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

   Like      Discuss      Correct / Improve     rest  web services  http  http methods  get request


 Q59. What was the driving force to introduce default methods in Interfaces wef from Java 8 ?Core Java2016-07-14 14:36:49

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     Infoview Technologies  Oracle  Oracle Polska  Infosys  Tata Consultancy (TCS)  CEBS Worldwide



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q60. Do all java classes need a main method?2016-07-20 15:17:32

Ans. No, Main method is the entry point into an application. An application usually contain multiple classes to perform a function.

Lets take an example of a House, House usually have only one external Door and you may have internal doors to move around within a house. Internal Doors are methods of classes whereas External Door is a special method called main method.

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

   Like      Discuss      Correct / Improve     main method


 Q61. Does default methods introduce multiple inheritance and the Diamond problem in Java 8 ?Core Java2016-07-20 17:16:28

Ans. Default methods results in multiple inheritance of behavior and not of state. In case we try to implement multiple interfaces with default method having same name and signature, and don't override it in implementation class, it will throw an error.

For example -

interface MyInterface {
public void default myMethod(){
}
}

interface MyInterface2 {
public void default myMethod(){
}
}

class MyClass implements MyInterface,MyInterface2 {
}

This code will compilation error "Duplicate Default Method"

if we specify the definition of myMethod() in myClass, compiler won't complain and there is no conflict and MyClass can use overridden definition. But if we don't override myMethod() in MyClass, Java would be in conflict as to what definition should be carried to MyClass and hence throws compilation error.

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

   Like      Discuss      Correct / Improve     default methods  java 8  multiple inheritance  diamond problem   interfaces


 Q62. Can we declare the main method as private ?Core Java2016-07-26 08:31:57

Ans. Yes, compiler won't complain but at runtime it will give an error saying "Error: Main method not found in class". Even though we can use this method as any other private method, it cannot be invocate by executing the class.

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

   Like      Discuss      Correct / Improve     main method   main method visibility      basic

Try 1 Question(s) Test


 Q63. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?Core Java2016-08-04 09:24:34

Ans. No, If both Parent and Derived are outer classes.

public class Vehicle {
   private static String manufacturingDate = "2016";
}

public class Car extends Vehicle{
   public static void main(String args[]){
      System.out.println(manufacturingDate); // error - The field Vehicle.manufacturingDate is not visible
   }
}

Yes, If derived is the inner class of Parent.

public class Vehicle {
   private static String manufacturingDate = "2016";
   static public class Car extends Vehicle{
      public static void main(String args[]){
         System.out.println(manufacturingDate); // no problem
      }
   }
}

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

   Like      Discuss      Correct / Improve     private members   private methods   private variables   inheritance   members visibility      basic        frequent


 Q64. What could be the problems due to the usage of static methods and elements ? Core Java2016-12-20 10:31:40

Ans. static methods and static elements are shared by all objects of a class and hence could create problem.

Static methods are not synchronized by default and hence could create problem when accessed through multiple threads. Static elements are shareable by class and hence state of one object could be altered by another object.

Some limitations with Unit testing as not all mocking framework facilitate mocking them. Power mock allows but Mockito doesn't

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

   Like      Discuss      Correct / Improve     static  static method  static variables


 Q65. Why main method is declared static ?Core Java2017-01-09 18:35:30

Ans. static is the keyword that makes it accessible even without creating any object and using class name only. Making it non static would like creation of object upfront before calling the method.

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

   Like      Discuss      Correct / Improve     main method  static     Adtech  Quark  PayPal        Basic


 Q66. Can an application have multiple main methods within different classes ? If yes, How will the app decide which one to be executed ?Core Java2017-01-12 10:56:40

Ans. Yes we can have a main method with string[] argument in every class of an application. When we execute an app we specify the starting point i.e the class that will get the control first and hence main method of that class gets executed.

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

   Like      Discuss      Correct / Improve     main method     HCL Technologies  Aspire Systems  SapientNitro


 Q67. Who invokes a thread's run method ?Core Java2017-01-12 11:23:15

Ans. After a thread is started using a call to start method, JVM invokes the thread’s run method when the thread is initially executed.

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

   Like      Discuss      Correct / Improve     threads  thread run method     HCL Technologies


 Q68. Can we override wait() or notify() methods?Core Java2017-01-19 13:59:12

Ans. wait and notify are declared final in object class and hence cannot be overridden.

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

   Like      Discuss      Correct / Improve     object class methods  wait  notify


 Q69. What is a method reference in Java ?Core Java2017-01-29 13:55:22

Ans. Introduced with java 8 , Method References help us to point to methods by their name.

A method references is described using :: symbol

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

   Like      Discuss      Correct / Improve     method reference  java 8  lambda expressions



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q70. Can we overload abstract methods ?Core Java2017-03-01 09:43:18

Ans. Yes

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

   Like      Discuss      Correct / Improve     overloading  abstract methods  overloading abstract methods


 Q71. Can we override abstract methods ?Core Java2017-03-01 09:45:08

Ans. Abstract methods are the methods that need to be overridden in the derived class ( either as implementing method or again abstract method ) so it's not only allowed but its required to override abstract method in the derived class.

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

   Like      Discuss      Correct / Improve     overriding  abstract methods  overriding abstract methods


 Q72. Can we override an abstract method with abstract method in the derived class ?Core Java2017-03-01 09:46:35

Ans. Yes, but in that case the derived class itself should be abstract. We cannot have an object of a class without definition for the method which has been declared abstract in the parent.

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

   Like      Discuss      Correct / Improve     overriding  overriding abstract methods  abstract methods


 Q73. What is a mutator in Java?Core Java2017-03-10 09:45:05

Ans. Mutator is another name for setter methods, i.e the method allows for mutating the property of an object and eventually the state of the object.

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

   Like      Discuss      Correct / Improve     setter method  mutator


 Q74. What are the advantages of using methods in Java ?Core Java2017-03-10 10:46:34

Ans. Likewise classes, methods also provide a way for code reuse and abstraction. Code is reused, clean and easy to understand if classified properly within classes and methods.

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

   Like      Discuss      Correct / Improve     methods  functions  advantages of methods in java


 Q75. Is it a good practice to override static methods ?Design2017-03-10 10:58:02

Ans. Though it's useful but it's not as useful as overriding member or object methods. We cannot achieve polymorphic behavior with static methods by overriding their definition in derived class.

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

   Like      Discuss      Correct / Improve     override static methods


 Q76. If we don't provide any argument to the executed program, What arguments will be passed to the main method ? Will the String array argument be null ?Core Java2017-04-04 14:38:48

Ans. No. It won't be null but an empty array with size 0

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

   Like      Discuss      Correct / Improve     main method


 Q77. In Java, if object A contains same the same data of object B, are they equal?Core Java2017-04-23 19:11:04

Ans. it depends on the implementation of equals method of the respective class. If no definition is provided and it uses the default definition of the object class, two references are equal only if they point to the same object.

We can have such an equality defined for a particular class objects if we provide appropriate implementation of equals method comparing all those fields.

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

   Like      Discuss      Correct / Improve     equals  equals method


 Q78. What are the trade offs between public constructor and static final method ?Core Java2017-05-07 19:26:45

Ans. Public constructor is simple and easy as it's the default way of object creation. So there are no additional coding overheads as compiler provides the default constructor if none is provided by coder.

With static final methods, it facilitates loose coupling by segregating the responsibility of object creation to a separate method. Validation can be done on the constructor arguments before calling it. Moreover if any adaption on the arguments is required that can achieved easily with factory method.On the flip side, there is coding overhead and additional method call.

  Sample Code for constructor

  Sample Code for factory

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

   Like      Discuss      Correct / Improve     constructor  factory design pattern  factory method


 Q79. What are the advantages and disadvantages of using lock classes from java.util.concurrent.locks vs. synchronized methods ?Core Java2017-05-07 19:30:42

 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     java.util.concurrent.locks  synchronization  synchronized methods



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q80. Does all methods need to have a return type ? Is it applicable to constructors too ?Core Java2017-05-13 19:59:44

Ans. All methods are not expected to return something but Yes, all methods are expected to have a return type. If a method returns nothing, it can be declared with the return type void.

Constructors are not expected to have any return types , not even void.

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

   Like      Discuss      Correct / Improve     return type   method  constructor


 Q81. What is the difference between the 2 methods in Java ?

Class BuggyBread {

BuggyBread(){
}
void BuggyBread(){
}
}
Core Java2017-05-13 20:02:27

Ans. BuggyBread method without any return type is the constructor which get's called upon object creation whereas BuggyBread method with return type of void is just another method that needs to be called explicitly for it's invocation.

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

   Like      Discuss      Correct / Improve     method  return type  constructor


 Q82. What are var args ?

or

What is the use of var args ?
Core Java2017-05-22 21:16:41

Ans. Var args is the feature of Java that allows a method to have variable number of arguments. Var args are used when we are not sure about the number of arguments a method may need. Internally java treats them as array.

Declarations of methods with var args

void method(int... x){};
void method(int x, int... y){};

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

   Like      Discuss      Correct / Improve     var args  methods  functions   method declaration     Hexaware  Bosch


 Q83. Why following method declarations are not valid ?

void method(int... x, int y){};
void method(int... x,int... y){};
Core Java2017-05-22 21:25:36

Ans. Because var args are not only allowed with the last argument in the method declaration.

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

   Like      Discuss      Correct / Improve     var args  methods  functions   method declaration  ocjp  scjp


 Q84. Which method will get called if we call it as method(1)

void method(int x ){};
void method(int... x){};
Core Java2017-05-22 21:32:17

Ans. Though the call an be bind to either of these but in this case, 1st method will get priority and hence will be called.

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

   Like      Discuss      Correct / Improve     var args  method  function  overloading  scjp  ocjp


 Q85. Do you see any problem with this code

public class BuggyBread {

   public static void main(String[] args) {
      method("Hello","World");
   }

   private static void method(String... args){
      for(String arg:args){
         System.out.println(arg);
      }
   }
}
Core Java2017-05-23 08:29:28

Ans. No, It will compile and execute perfectly fine. As we are using method with var args , we can call it with 0 to n arguments.

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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions


 Q86. Do you see any problem with this code

public class BuggyBread {

   public static void main(String[] args) {
      method();
   }

   private static void method(String... args){
      for(String arg:args){
         System.out.println(arg);
      }
   }
}
Core Java2017-05-23 08:30:09

Ans. This will compile fine as we can provide 0 arguments for a var arg but will print nothing upon execution.

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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions


 Q87. Do you see any problem with this code

public class BuggyBread {

   public static void main(String[] args) {
      method("Hello");
   }

   private static void method(String... args){
      for(String arg:args){
         System.out.println(arg);
      }
   }

   private static void method(String arg){
      System.out.println(arg);
   }
}
Core Java2017-05-23 08:32:25

Ans. It will compile fine and the method call will bind to method with specific argument and not with var arg

i.e private static void method(String arg)

It will print "Hello" upon execution

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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions


 Q88. Do you see any problem with this code

public class BuggyBread {

   public static void main(String[] args) {
      method("Hello");
   }

   private static void method(String... args){
      for(String arg:args){
         System.out.println(arg);
      }
   }

   private static void method(String[] arg){
      System.out.println(arg);
   }
}
Core Java2017-05-23 08:33:56

Ans. Yes, it will give compilation error and java will complain about duplicate method. Java treat var args internally as arrays and hence would result in same byte code for both method's syntax.

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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions


 Q89. Will this code work

public class BuggyBread {

   public static void main(String[] args) {
      method("Hello","World");
   }

   private static void method(String[] arg){
      System.out.println(arg);
   }
}

What all possible changes we can make to method signature to make it work ?
Core Java2017-05-23 08:36:19

Ans. It won't work as java won't find the method definition for method with 2 arguments.

We can either declare the method as

private static void method(String arg,String arg2)

or

private static void method(String... arg)

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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q90. Do you prefer using var args ?Core Java2017-05-23 09:04:40

Ans. Though var args are used rarely but they are pretty useful if a method is expected to receive variable number of arguments. For example - it's pretty useful for the main method as the caller has the flexibility to provide arguments in infinite ways.It provides a convenience and flexibility to the caller.

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

   Like      Discuss      Correct / Improve     var args  methods  method declarations  functions


 Q91. Can we compile and execute a Java class without main method ?Core Java2017-06-18 18:11:13

 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     jvm  compilation  main method      Basic


 Q92. Why do we pass an array of strings to main method ?Core Java2017-06-23 12:58:05

Ans. Array of strings in the main method are the list of arguments or parameters which are sent to the application / program.

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

   Like      Discuss      Correct / Improve     main method   main method string array argument


 Q93. Can we synchronize the run() method in Java?Core Java2017-07-23 20:08:58

 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     synchronization  multithreading  run method


 Q94. Is Runtime Polymorphism possible without Inheritance ?Core Java2017-08-06 19:12:26

Ans. Yes, Runtime Polymprohism requires either Class inheritance or Interface implementation.

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

   Like      Discuss      Correct / Improve     runtime polymorphism  method overriding  inheritance


 Q95. What are the advantage of Abstract classes over interfaces with respect to Java 7 ? and What changed in Java 8 to help facilitate that in Java 8 ?Core Java2017-08-17 19:28:33

 This question was recently asked at 'Proofpoint'.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     abstract classes  interfaces  default method     Proofpoint      expert


 Q96. Can we mock static methods ?Testing2017-08-17 19:32:01

Ans. Yes we can use PowerMock. With Mockito , we cannot mock static methods.

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

   Like      Discuss      Correct / Improve     mocking fraeworks  mocking static methods


 Q97. How can we ensure thread safety in static method ?Core Java2017-08-27 07:49:53

 This question was recently asked at 'Accenture'.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     threads  synchronization  static methods     Accenture


 Q98. Which of the following is false ?Core Java
a. A Class cannot override both hashcode and equals method.
b. A class can override both hashcode and equals method.
c. A Class must override hashCode method if its overridding equal method.
d. A Class can override hashCode even if its not overridding equals method.

Ans.a. A Class cannot override both hashcode and equals method.

 Q99. Which of the following is not true for abstract classes ?Core Java
a. Abstract Class is only meant to be sub classed and not supposed to be instantiated.
b. Abstract class handlers can be used to handle derived class objects.
c. We can't have an abstract class without abstract methods.
d. Abstract class has member elements.

Ans.c. We can't have an abstract class without abstract methods.

 Q100. Which of the following keyword is expected with main method declaration ?Core Java
a. Final
b. Static
c. Volatile
d. Transient

Ans.b. Static



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: