Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interview questions and answers

1
Example for accessing static methods in java, with object name:
what is the problem with below Java program?
class Base{
        public static void disp(){
                System.out.println("Base static disp");
        }
}
class Der extends Base{
        public static void disp(){
                System.out.println("Der static disp");
        }
}
public class HidingDemo {
        public static void main(String[] args) {
                Base b = new Der();
                b.disp();        
        }
}
  1. it gives compile time error as we are trying to access static method by object.
  2. there is no problem in this program.
  3. Here we are calling static (class) method by an object which is wrong. A class property has to be accessed with class name. So instead of b.disp(); we have to use Base.disp(). Though it won't give any error, but it shows a warning.
  4. none of the above.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
4
what does of below Java program represent?
class Base{
        int i = 10;
        public void fun(){
                System.out.println("Base fun.."+i);
        }
}
class Der extends Base{
        int i = 100;
        public void test(){
                System.out.println("Der test.."+i);
        }
}
public class HidingDemo {
        public static void main(String[] args) {
                Base b = new Der();
                b.fun();
        }
}
  1. we are using same variable i in both base and derived class, which gives compile time error.
  2. we are using same variable i in both base and derived class, since i is instance variable this is called as overriding.
  3. we are using same variable i in both base and derived class, this is called as hiding.
  4. it is neither hiding nor overriding. you can't do like this.
Show Answer
Share Facebook gmail Twitter
5
What is an immutable method?
  1. A method without any body.
  2. A method which is declared as final.
  3. A static method.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
8
Which of the below statements are true about final methods in java?
  1. final methods will not be inherited.
  2. final methods can be overridden.
  3. Object class uses many final methods.
  4. We can use private methods in place of final methods, as both are same.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
11
What is the output of below Java program?
class Base{
        public static final void fun(){
                System.out.println("Base fun..");
        }
}
class Der extends Base{
        public static final void fun(int i){
                System.out.println("Der fun..");
        }
}
public class NestedDemo {
        public static void main(String[] args) {
                Der.fun(10);
                Der.fun();
        }
}
  1. Compile time error, final methods can't be overridden and can't be hidden.
  2. Compile time error in main(), as we are trying to access fun() with out any parameters using Der class, but it is class method of Base class.
  3. Der fun..
    Base fun..
  4. all are false.
Show Answer
Share Facebook gmail Twitter
12
What is the output of below Java program?
class Base{
        private final void disp(){
                System.out.println("Base disp..");
        }
        public final void test(){
                System.out.println("Base test..");
        }
}
class Der extends Base{
        public final void fun(){
                disp();
                test();
                System.out.println("Der fun..");
        }
}
public class NestedDemo {
        public static void main(String[] args) {
                Der d = new Der();
                d.fun();
        }
}
  1. Compile time error in disp() of Base class, as it is not possible to give final for private methods.
  2. Compile time error in Der class fun() as we can't access disp() of Base class in Der class, as it is private method.
  3. Compile time error in Der class fun() in two places, as we can't access both disp() and test() of Base class, as final methods will not be inherited.
  4. Base disp..
    Base test..
    Der fun..
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
17
What is the output of below Java program?
class Base{
        public final void disp(){
                System.out.println("Base disp..");
        }
}
class Der extends Base{
        public void fun(){
                System.out.println("Der fun..");
        }
}
public class FinalDemo {
        public static void main(String[] args) {
                Der d = new Der();
                d.fun();
        }
}
  1. compile time error in Der class, as we can't extend Base class. Because all Base class methods are final, so Base class will become final class.
  2. it prints Der fun..
  3. Compile time error in Base class as we are trying to inherit Base class into Der class.
  4. none
Show Answer
Share Facebook gmail Twitter
18
Is it possible to make a class as both abstract and final in java:
What is the problem with below java program?
abstract final class Base{
        public final void disp(){
                System.out.println("Base disp..");
        }
}
  1. No problem
  2. it will give compile time error, because there is no main function.
  3. a class can be either abstract or final, not both. so it gives a compile time error.
  4. none
Show Answer
Share Facebook gmail Twitter
19
How to prevent a class from being inherited?
  1. Make your class as final
  2. Even if your class is not final, just make all the constructors of your class as private, so that others can't inherit your class.
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
20
Which of the below statements are wrong about an immutable?
  1. All immutable objects by default belongs to immutable classes only, always.
  2. all immutable objects by default contains only final variables.
  3. if a class contains all final methods then we can call that class as immutable or final class.
  4. none
Show Answer
Share Facebook gmail Twitter
Read more papers :