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

Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
3
packages and access modifiers default and protected:
what is wrong with below code?

package com.skillgun.p1;
public class ClassA{
   private int i=10;
   int j=20;
   protected int k=20;
   public int l=30;

   public void disp(){
     System.out.println("i val.."+i);
   }
}

class ClassB{
   public void fun(){
     ClassA c = new ClassA();
     System.out.println("j val.."+c.j);
   }
}
----

package com.skillgun.p2;

import com.skillgun.p1.*;

public class ClassC{
  public static void main(String[] args){
    ClassA a = new ClassA();
    System.out.println("j val is.."+a.j);
    System.out.println("k val is.."+a.k);
    System.out.println("l val is.."+a.l);
  }
}
  1. Nothing wrong
  2. in package 2, ClassC in main function you are trying to access a.j variable which you can't access because j is a package private variable, which can be accessible only in the same package.
  3. in package 2, ClassC in main function you are trying to access a.k variable which you can't access because j is a protected variable, which can be accessible only in the sub classes but not in other classes of different packages.
  4. both option 2 and 3 are right
Show Answer
Share Facebook gmail Twitter
4
packages and default access modifier:
what is wrong with below code?

package com.skillgun.p1;
public class ClassA{
   private int i=10;
   int j=20;
   protected int k=20;
   public int l=30;

   public void disp(){
     System.out.println("i val.."+i);
   }
}

class ClassB{
   public void fun(){
     ClassA c = new ClassA();
     System.out.println("j val.."+c.j);
   }
}
----

package com.skillgun.p2;

import com.skillgun.p1.*;

public class ClassC{
  public static void main(String[] args){
    ClassB a = new ClassB();
    
  }
}
  1. this program works perfectly fine.
  2. In package 2, in main function you are trying to create object for ClassB which is not possible because ClassB is not a public class, so you can't access it in a different package.
  3. option 2 is wrong because since we have imported complete package p1.*, we can access all the classes present in it.
  4. none
Show Answer
Share Facebook gmail Twitter
5
packages and protected access modifier:
what is wrong with below code?

package com.skillgun.p1;
public class ClassA{
   private int i=10;
   int j=20;
   protected int k=20;
   public int l=30;

   public void disp(){
     System.out.println("i val.."+i);
   }
}

----

package com.skillgun.p2;

import com.skillgun.p1.*;

public class ClassC extends ClassA{
  public static void main(String[] args){
    ClassA a = new ClassA();
    System.out.println("k value is.."+a.k);
    System.out.println("k value is.."+k);    
  }
}
  1. you can't extend ClassA in ClassC because both are in different packages.
  2. in main function of ClassC in package p2, you are trying to access a.k variable which will give compile time error.
  3. There is no problem with this program.
  4. in main function of ClassC in package p2, you are trying to access k variable in second System.out.println which will give compile time error.
Show Answer
Share Facebook gmail Twitter
6
Which of the below statements are false about access modifiers?
  1. at top level you can have only 2 access modifiers public or package private (default)
  2. at member level you can have 4 access modifiers, private or package private or protected or public.
  3. you can have private outer classes also.
  4. all are true
Show Answer
Share Facebook gmail Twitter
7
What is a package?
  1. A package is a namespace that organizes a set of related classes and interfaces. Package can be used like a container which contains similar related classes together in a folder.
  2. Package allows to define two classes with same name in two different packages. So package avoid naming collision.
  3. Package a mechanism to achieve security, by making a class as default in the package. Classes of other packages can't access default classes.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
8
How to create a package in Java?
  1. Open eclipse-> open the project where you want to create package-> right click on src folder-> new-> package-> give package name. This will create the package with the given name.
  2. Generally package name should be the reverse domain name of your company and your project name. Eg: if you are working on facebook application in skillgun company, then the package name for your application will be com.skillgun.facebook; Package names generally starts with small letters.
  3. package statement should be the first line of the java file, else it gives compile time error. There can be only one package for a java file.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
9
What are the package naming conventions in Java?
  1. Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
  2. Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.
  3. both are true
  4. none
Show Answer
Share Facebook gmail Twitter
10
What is import in java?
  1. import keyword is used to import built-in packages and user defined packages into a Java source file. So that your class can refer to a class that is in different package. This is similar to #include statements in c language which includes header files into a c file.
  2. In order to use a class of one package in other package, programmer has to import either that specific class or entier package by using import statement. Though we can use a member of one package in other package directly by using  complete package name qualifier along with that member, it is not recommended as such kind of code is not readable.
  3. By using import statements we can import static variables and nested classes also.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Read more papers :