Interview Questions and Answers for 'Private' - 16 question(s) found - Order By Newest 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. Can we declare interface methods as private ?
Ans. With Java 9 we can declare interface methods as private Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   interfaces   yes-no   private Q3. How can we create objects if we make the constructor private ? Core Java
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 Asked in 1 Companies Q4. Can we instantiate the object of derived class if parent constructor is protected ? Core Java
Ans. Yes, as protected constructor is accessible in sub class. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   constructor   access specifier   protected   yes-no  private constructor Q5. Can we declare an abstract method private ?
Ans. No Abstract methods can only be declared protected or public. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   abstract   oops   access specifier   private   yes-no intermediate   rare Q6. Write a Program to print factorial of a number using recursion Core Java
Ans. public class BuggyBread {
public static void main(String args[]) {
System.out.println(factorial(5));
}
private static int factorial(int number){
if(number == 1){
return 1;
} else {
return number * factorial(number - 1);
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  recursion Asked in 25 Companies Q7. What will happen if we make the constructor private ? Core Java
Ans. We can't create the objects directly by invoking new operator. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   oops   constructor   access specifier   private Asked in 1 Companies basic Try 2 Question(s) TestFrequently asked in face to face interviews. Q8. Write a program to print fibonacci series. Core Java
Ans. int count = 15;
int[] fibonacci = new int[count];
fibonacci[0] = 0;
fibonacci[1] = 1;
for(int x=2; x < count; x++){
fibonacci[x] = fibonacci[x-1] + fibonacci[x-2];
}
for(int x=0; x< count; x++){
System.out.print(fibonacci[x] + " ");
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  ebay   fibonacci series Asked in 66 Companies basic   frequent Q9. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ? Core Java
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  object oriented programming (oops)  oops concepts   members visibility   inner classes  nexted classes basic   frequent Q10. Have you ever felt the need of keeping the constructor private ? Design
Ans. Yes, When either we don't want an object to be created ( class having all static elements and hence not required ) or object to be created using a static method or static block.
One such example could be a situation when we would like app to load objects at start up and we would like to restrict new object creation by any request. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  private constructor  constructor Q11. Can a constructor have a private access specifier ? Core Java
Ans. Yes, declaring a constructor private means disabling the creation of object that way using new operator. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor  private constructor Q12. What happens to private variables during inheritance ? Core Java
Ans. Private variables are not inherited. They are simply ignored and not made part of the derived object body. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  private variables  inheritance  object oriented programming (oops)  oops concepts Q13. 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 classes Q14. Why can't we declare on outer class as private ? Core Java
Ans. Making an outer class as private will make it inaccessible from anywhere and would make it useless. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  private variables Q15. 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 Q16. Can we instantiate the object of derived class if parent constructor is private ? Core Java
Ans. No Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  constructor  private constructor Q17. How can we create objects if we make the constructor private ? Core Java
a. We can't create objects if constructor is private b. We can only create objects if we follow singleton pattern c. We can only create one object d. We can create new object through static method or static blockAns.d. We can create new object through static method or static block