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

Search Java Interview Questions


 2098 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 'Declaration' - 16 question(s) found - Order By Newest

 Q1. What do you mean by "Java is a statically typed language" ?Core Java
Admin
info@buggybread.com
Ans. It means that the type of variables are checked at compile time in Java.The main advantage here is that all kinds of checking can be done by the compiler and hence will reduce bugs.

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

   Like      Discuss      Correct / Improve     java   statically typed language   variable declaration      basic        frequent


 Q2. What is the difference between declaration, instantiation and initialization ?Core Java2016-05-26 14:17:48

Ans. Declaration is intimation to the compiler about the nature of Data a reference is going to hold.

For example - List myList;

Instantiation is reservation of memory.

For example

myList = new ArrayList();

Initialization or construction is setting the default values for member elements.

For example

myList = new ArrayList(mySet);

** Example 2nd is both for instantiation as well as initialization. The only difference is that 2nd will initialized the member elements to their default values whereas 3rd will initialized it with the elements from set.


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

   Like      Discuss      Correct / Improve     declaration   instantiation   initialization   construction      basic        frequent


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


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


 Q5. If everything is an object , Cant we declare every object as

Object obj = new String();
Core Java2016-12-02 14:45:05

Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.

We cannot access methods defined in String class or in any class in hierarchy between String and Object.

For example - we cannot do obj.append("abc") as it will now give compile time error.

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

   Like      Discuss      Correct / Improve     object initialization  object declaration   runtime polymorphism


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


 Q7. What is the difference between int[] x; and int x[]; ?Core Java
admin
info@buggybread.com
Ans. No Difference. Both are the acceptable ways to declare an array.

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

   Like      Discuss      Correct / Improve     java   array   arrays   array declaration   difference between     ABC Ltd   Infosys  InfoTrellis      basic


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


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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



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


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


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


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


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


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


 Q16. Can a source file contain more than one class declaration ?Core Java2017-06-23 08:12:07

Ans. Yes, but only one of them can be declared public.

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

   Like      Discuss      Correct / Improve     class declaration




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: