Core Java - Interview Questions and Answers for 'Ocjp' | 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 'Ocjp' - 9 question(s) found - Order By Newest

 Q1. 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 Java
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


 Q2. Can we overload method as following ?

void method(int... x){};
void method(int[] x){};
Core Java
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


 Q3. Which of the following syntax is correct ?import static java.lang.System.*;or static import java.lang.System.*;Core Java
Ans. import static java.lang.System.*;

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

   Like         Discuss         Correct / Improve     java   static import   generics   import   OCJP   SCJP   Oracle certified java developer     Asked in 1 Companies


 Q4. Which of the following code is correct ?

a.


FileWriter fileWriter = new FileWriter("../file.txt");
File file = new File(fileWriter );
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

b.

BufferedWriter bufferedOutputWriter = new BufferedWriter("../file.txt");
File file = new File(bufferedOutputWriter );
FileWriter fileWriter = new FileWriter(file);


c.

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

d.

File file = new File("../file.txt");
BufferedWriter bufferedOutputWriter = new BufferedWriter(file);
FileWriter fileWriter = new FileWriter(bufferedOutputWriter );
Core Java
Ans. c.

File file = new File("../file.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedOutputWriter = new BufferedWriter(fileWriter);

 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   filewriter   bufferedwriter   scjp   ocjp  file handling


 Q5. Which exceptions should be handled with the following code ?

FileOutputStream fileOutputStream = new FileOutputStream(new File("newFile.txt"));
Ans. FileNotFoundException

 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   scjp   ocjp   filenotfoundexception   fileoutputstream  file handling


 Q6. What will the following code print ?

java.util.Calendar c = java.util.Calendar.getInstance();
c.add(Calendar.MONTH, 5);
System.out.println(c.getTime());
Core Java
Ans. Date and Time after 5 months from now.

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

   Like         Discuss         Correct / Improve     java   date   calendar   scjp   ocjp   coding   code


 Q7. What will the following code print ?

public static void main(String[] args){
Integer i1 = new Integer("1");
Integer i2 = new Integer("2");
Integer i3 = Integer.valueOf("3");
int i4 = i1 + i2 + i3;
System.out.println(i4);
}
Core Java
Ans. 6

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

   Like         Discuss         Correct / Improve     java   int   integer   scjp   ocjp


 Q8. Why following method declarations are not valid ?

void method(int... x, int y){};
void method(int... x,int... y){};
Core Java
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


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

void method(int x ){};
void method(int... x){};
Core Java
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



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: