Interview Questions and Answers - Order By Rating Q31. Which immutable classes have you worked with ? Can you name some immutable Collections ? How to make an immutable collection ? Core Java
Ans. string and wrapper class objects Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  immutability  immutable  immutability classes   immutable  immutability collections Asked in 3 Companies Q32. Is immutability an advantage with Wrapper classes over primitive types ? Core Java
Ans. There is no concept of de referencing with primitive types and hence they are implicitly immutable. Having wrapper classes as mutable offers disadvantages compared to primitive types. Wrapper classes being immutable offer similar advantage as primitive types.It actually overshadows the disadvantage wrapper class could have if they are immutable. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  immutable  immutability classes  wrapper classes Q33. Can we use primitive types with Collection classes ? Core Java
Ans. No, As collection classes involve use of Generics, they cannot accept primitive types. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collection classes  collections Q34. What are the benefits of using Wrapper classes over primitive types ? Core Java
Ans. With Collection classes , we cannot use primitive types. Moreover for any class using generic types, we cannot use primitive types.
They add more functionality by means of additional methods.
As their reference can be null , they offer consistent check for uninitialized state.
They facilitate caching and reuse by means of constant Pools. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  wrapper classes  benefits of wrapper classes over primitives Q35. Why can we have a static inner class but not a static Top level class in Java ? Core Java
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  static class  inner classes   nested classes Expert   rare Q36. Why do we use Thread Class as well as Runnable Interface for creating threads in Java ? Core Java
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  Thread class  runnable interface Q37. 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 Java
Ans. Abstract Classes provide default implementations of methods that are inherited by the classes that extend them, which was not the case for Interfaces. This changed in Java 8, where default implementations are provided for methods. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  abstract classes  interfaces  default method Asked in 1 Companies expert Q38. Does Wrapper classes produces immutable objects ? Core Java
Ans. Yes Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  wrapper classes  immutable  immutability Q39. Design a class diagram for a farm. Design
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  class diagram   design Q40. Can an interface be defined inside a class just like we can have inner classes inside a class ? Core Java
Ans. Yes , we can have an interface that is part of a class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes   inner interface  nested classes Q41. Can a source file contain more than one class declaration ? Core Java
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 Q42. Is this code legal in java i.e nested class within interface implementing the parent interface ?
public interface MyInterface {
public class MyClass implements MyInterface {
}
} Core Java
Ans. Yes that's legal in java Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested classes  sub class Basic Q43. Can an inner class be subclass of it's parent class ? Core Java
Ans. Yes that can be done
public class OuterClass {
public class InnerClass extends OuterClass {
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested classes  sub class Basic Q44. What is the difference between inner class and sub class ? Core Java
Ans. Inner Class is a class that is nested within another class whereas sub class is a class that extends or inherit another class.
Inner class can only be accessed using reference of outer class ( Class name , if inner class is static or object reference, if inner class is non static ) whereas Sub class is accessed directly.
In terms of memory, inner class is stored just like another class having it's own body whereas sub class carries the body of parent class as well as it's own fields in memory. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  nested classes  inner class vs sub class  nested class vs sub class Basic Q45. Explain OOPs
or
Explain OOPs Principles
or
Explain OOPs Concepts
or
Explain OOPs features
or
Tell me something about OOPs Core Java
Ans. OOPs or Object Oriented Programming is a Programming model which is organized around Objects instead of processes. Instead of a process calling series of processes, this model stresses on communication between objects. Objects that all self sustained, provide security by encapsulating it's members and providing abstracted interfaces over the functions it performs. OOP's facilitate the following features
1. Inheritance for Code Reuse
2. Abstraction for modularity, maintenance and agility
3. Encapsulation for security and protection
4. Polymorphism for flexibility and interfacing Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  oops  oops features Asked in 260 Companies basic   frequent Q46. How can we hide a class in Java ? Core Java
Ans. By encapsulating it within another class and declaring it private. In such a case, it will only be accessible through parent class or parent class object. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  private class   private inner class  class hiding  encapsulation  object oriented programming (oops)  oops concepts  inner classes   nested classes Q47. Can we declare a class private ? Core Java
Ans. We can declare an inner class as private and hence would be restricting it's access from outside. We cannot make outer class as private. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  private class   inner classes  nested classesAns. Boxing conversion converts expressions of primitive type to corresponding expressions of reference type.
For example
boolean to Boolean
long to Long
double to Double Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  data type  boxing  wrapper classesAns. 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 Q50. What is the difference between these two method declarations ?
private static void method(String[] arg)
and
private static void method(String... arg) Core Java
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 Q51. 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 Java
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 Q52. 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 Java
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 Q53. 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 Java
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 Q54. 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 Java
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 Q55. 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 Java
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 Q56. 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 Q57. 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 Q58. 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 Q59. What are var args ?
or
What is the use of var args ? Core Java
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 Asked in 2 Companies Q60. Make the following class immutable
public class Employee {
private long employeeId;
private String employeeName;
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
} Core Java
Ans. public class Employee {
private long employeeId;
private String employeeName;
public Employee(long employeeId,String employeeName){
this.employeeId = employeeId;
this.employeeName = employeeName;
}
public long getEmployeeId() {
return employeeId;
}
public long getEmployeeName() {
return employeeName;
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  immutable  immutability  immutable  immutability class