0% found this document useful (0 votes)
45 views22 pages

Java Class Loader

The document discusses various topics related to Java class loaders, variables, and blocks. 1. It defines package-private access and describes the three types of Java class loaders - bootstrap, extension, and system class path loaders and their order of execution. 2. It explains the differences between ClassNotFoundException and NoClassDefFoundError exceptions. 3. It discusses where local variables, instance variables, static variables, and objects are stored in memory and restrictions on their usage.

Uploaded by

imdadareeph
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
45 views22 pages

Java Class Loader

The document discusses various topics related to Java class loaders, variables, and blocks. 1. It defines package-private access and describes the three types of Java class loaders - bootstrap, extension, and system class path loaders and their order of execution. 2. It explains the differences between ClassNotFoundException and NoClassDefFoundError exceptions. 3. It discusses where local variables, instance variables, static variables, and objects are stored in memory and restrictions on their usage.

Uploaded by

imdadareeph
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 22

Prepared by: Imdad Areeph

Java Class Loader ,Variables and Blocks Related Questions


1. What is package-private ? 
 Package-private is a different name of default access specifier.
2. What is Class Loader in java ?
Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine on demand .

There are three types of class loaders. There are

 Bootstrap class loader


             The bootstrap class loader loads the core Java libraries[5] located in the {JAVA_HOME}/lib directory
  Extensions class loader
             The extensions class loader loads the code in the extensions directories {JAVA_HOME}/lib/ext directory
   System Class  Path Loader
              The system class loader loads code found on java.class.path, which maps to the system CLASSPATH variable
3. What is the order of execution of Class Loader in java?
Java applications  involve three classloaders – Bootstrap, Extensions and System-Classpath classloaders
Bootstrap classloader is the parent of all classloaders ,extensions Classloader is the immediate child of Bootstrap classloader and System-
Classpath classloader is the immediate child of Extensions classloader. Generally  Order of execution happens from sub-sequent children to
parent. 
So order of execution is starts from System-classpath-loader then extension then Bootstrap Loader.
4. What is differenence between ClassNotFoundException and NoClassDefFoundError ?
ClassNotFoundException is generated by a call to Class.forName() with a String that contains a class not available on the bundle's class path.
For example when we are dynamically want to load the class using  Class.forName("oracle.jdbc.driver.OracleDriver"); if that class is not aviailable
in runtion, it'll throw ClassNotFoundException
NoClassDefFoundError is thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class and that class or
its dependancies are not available  in run time. For example if Class x extends Class y and Class y is extends  class Z .When class loader tries to
load class x if any of the depenacies is missing it'll give
5. If we are using importing a class e.g java.util.ArrayList , and we are not using that class in side our program, does class loader will
load the class?
No, it won't
6. Local variable, instance variable, method and object, amonng of these which one will store in heap and which one will store in
stack?
local variable and method will store in stack
instance variable and object will store in heap
7. Emp e=new Employee(), where the "e" and "new Employee" will store?
"e" will store in stack and Emploee() ( or the object )  will store in heap.
8. Where we can can declare local variable ,where instance variable and where static variable?
Local : inside mehod, constructor, and block
instance: outside method, constructor and block
static: outside method, constructor and block
9. Can I write like this?
void methodA()
{
int a;
System.out.println("I am in methodA");
}
yes, ( a stupid question for smart people )

1
Prepared by: Imdad Areeph

10. Can I write like this?


void methodA()
{
int a;
System.out.println("I am in methodA " + (a+5));
}
no, because as long as we are not using local variable it won't give any error, but we should initialise it before using it(using of a).

11. Can we declare a local variable with in static block, like below?
public class TestVariable {
        static{
         int i=9;
         System.out.println("print  "+i);
    }
yes

12. Can we use an instance variable with in static block ?


public class TestVariable {
    int i=9;
        static{
             System.out.println("print  "+i);
    }

No, because instance variable alwasys accessable wth a reference type.

13. Can we write like this?


public class TestVariable {
                static{
         static int i=9;
         System.out.println("print  "+i);
    }
no, static variable always declares out side of block?

14. Can I write like this?

public class TestVariable {


    int y=9;
    static{
         System.out.println("print  "+new TestVariable().y);
    }
}
yes

2
Prepared by: Imdad Areeph

15. If I am writing static and non static block in my program like below, what is output?
public class TestBlock {

    static{

        System.out.println("I am in static block");


    }

    {
        System.out.println("I am in non static block");
    }
    public static void main(String a[]){
        TestBlock block=new TestBlock();

    }
}
Ans: I am in static block
I am in non static block

16. What will be output for this program?


public class TestBlock {

    static{

        System.out.println("I am in static1 block");


    }

    {
        System.out.println("I am in non static block");
    }

    static{

        System.out.println("I am in static2 block");


    }
    public static void main(String a[]){
        TestBlock block=new TestBlock();
 }
}
Ans
I am in static1 block
I am in static2 block
I am in non static block

3
Prepared by: Imdad Areeph

17. What will be the out put for folowing code?

public class TestBlock {

    static{

        System.out.println("I am in static1 block");


    }

    {
        System.out.println("I am in non static block");
    }

    public TestBlock() {
        System.out.println("I am in non argumented constructor");
    }
    
    public TestBlock(String s) {
        System.out.println("I am in argumented constructor  "+s);
    }

    public static void main(String a[]){


        TestBlock block=new TestBlock("hello");

    }
}
I am in static1 block
I am in non static block
I am in argumented constructor  hello
18. Can I use instance variable in static variable like below?
public class TestBlock {
    int z=98;
    static{
        System.out.println("I am in static block"+new TestBlock().z);
    }
    public static void main(String a[]){
        TestBlock block=new TestBlock();
    }
}
yes, you can execute this program, out put will
I am in static block98

4
Prepared by: Imdad Areeph

Variable and TypeCasting  Related Questions


1. What are the default value of these variable?
Object reference, byte, short,int, boolean,char
Object reference null (not referencing any object)
byte, short, int, long 0
float, double 0.0
boolean false
char '\u0000'
2.  what is upcasting and downcasting in java ?
Upcasting means An object of sub class can be refferered by object of super class
like
int x=9;
float y=x;
int x value will automatically asign to y value and compile perfectly.
downcasting:
Downcasting means opposite is not true, mean object of superclass  can not  reffered to subclass, we need to do typecast.
    int x=9;
    byte b=x;
It will give compile time error. We have to write like this
byte b=(byte)x;
3. What will be the output for following program?
public class TypeCast {
    public static void main(String [] args) {
        long l = 129L;
        byte b = (byte)l;
        System.out.println("The byte is " + b);
        }
}
Ans The byte is -127
byte range is -128 to 127. After 127 it'll calculate  form -128
These are two classes
public class A  {
    public void testA(){
        System.out.println("class A");
    }

}
public class B extends A{
public void testB(){
    System.out.println("Class B");
}
}
What will be output for following questions

5
Prepared by: Imdad Areeph

4. What is out put of this one


public class Test {
    public static void main(String args[]){
        A a=new A();
        B b= (B) a;
        b.testA();
    
    }

}
Ans run time error. A can not cast to B. Super class object can not cast to sub class.
5. In the above example can we write like this
public class Test {
    public static void main(String args[]){
        B b= new B();
        A a =(A)b;
        a.testA();
}
}
Ans yes out put will "Class A"
6. In the above example can I write like this
public class Test {
    public static void main(String args[]){
         A a =new B();
        a.testA();
            }}
yes, Super class reference can point to subclass Object.
7. In the above example can I write like this
public class Test {
    public static void main(String args[]){
               
        B b = new A();
        b.testB();
       
    }

}
no
8. In the above example can I write like this?

public class Test {


    public static void main(String args[]){
        A a= new B();
        a.testB();
       
    }

6
Prepared by: Imdad Areeph
}
no

9. Can I write like this?


public class Test {
    public static void main(String args[]){  
        B b= (B) new A();
        b.testB(); 
    }
}
no, run time error
10. There are three classes A,B,C like this
public class A  {
    public void testA(){
        System.out.println("class A");
    }
}
public class B extends A{
public void testB(){
    System.out.println("Class B");
}
}
public class C extends B{
public void tectC(){
    System.out.println("class c");
}
}
In this blelow class which type cast is wrong and which one is currect?
public class Test {
    public static void main(String args[]){
A a = new A();
        B b= new B();
        C c=new C();
        c=(C)a;
        c=a;
        c=(C)b;
        c=b
        b=(B)a;
        b=a;
        b=(C)c;
        b=c;
        a=(C)c;
        a=c;
        a=(B)b;
        a=b;
           
    }
}

7
Prepared by: Imdad Areeph

Ans
public class Test {
    public static void main(String args[]){

        A a = new A();


        B b= new B();
        C c=new C();
        c=(C)a;//w
        c=a;//w
        c=(C)b;//w
        c=b;w
        b=(B)a;//w
        b=a;//w
        b=(C)c;//c
        b=c;//c
        a=(C)c;//c
        a=c;//c
        a=(B)b;//c
        a=b; //c
        c.testA();

    }

}
11. There are four classes A,B,C,D. B extends A and D extends C. Like this
public class A  {
    public void testA(){
        System.out.println("class A");
    }

}
public class B extends A{
public void testB(){
    System.out.println("Class B");
}
}

public class C {
public void tectC(){
    System.out.println("class c");
}
}

public class D extends C {


    public void testD(){
        System.out.println("test D");
    }

8
Prepared by: Imdad Areeph

Can we typecast like below?


public class Test {
public static void main(String ar[]){
A a=new A();
B b = new B();
C c =new C();
D d =new D();
Object o=(A)a;
c=(C)o;
c.tectC();
}
}
no, its run time error. Two sibling can't be casted

Overloading and Overriding Related Questions


1. What is difference between overloading and over ridding?

Over Loading Over Ridding


It happens in same class It happens in case of inheritance

Method name same and argument different Method name and arguments same

Return type doesn't consider Return type considers

It is known as compile time and static binding Its known as runtime or dynamic binding

It can have any access level But here same access level or widder. It can't be narrow scope

2.
Bellow we have given some examples for better understanding of overloading and overridding
3. What is out put of this program?
public class OverLoading {
    public int methodA(){
        return 5;
       
    }
    public void methodA(){
        System.out.println("I am in void return tpye method");
    }
    public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
        int x=loading.methodA();
        System.out.println(x);
        loading.methodA();
    }

}
Ans compile time error, because both methodA(), consider as one method. Overloading doesn't consider return type

9
Prepared by: Imdad Areeph

4. What is out put of this program?


public class OverLoading {
    public int methodA(){
        return 5;
    
    }
    public void methodA(String str){
        System.out.println(str);
    }
    public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
        int x=loading.methodA();
        System.out.println(x);
        loading.methodA("hello");
    }

}
Ans
 5
hello
5. What is out put of this program?

public class OverLoading {


    public void methodA(int x){
        System.out.println(x);
       
    }
    public void methodA(String str){
        System.out.println(str);
    }
    public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
        loading.methodA(4.0);
        loading.methodA("hello");
    }
}

Ans
compile time error, argument mismatch
6. What is out put of this program?
public class OverLoading {
    public void methodA(float x){
        System.out.println(x);
        }

10
Prepared by: Imdad Areeph
    public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
        loading.methodA(4);
        loading.methodA(2.0f);
    }
}

Ans
4.0
2.0
7. What is out put of this program?
public class OverLoading {
    public void methodA(float x){
        System.out.println(x);
   
    }
        public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
        loading.methodA(4);
        loading.methodA(2.0f);
        loading.methodA(4.8d);
       
    }
}

Ans
compile time error. Because 4.8d can't implecitily type cast to float
8. What is out put of this program?
public class OverLoading {
    public void methodA(float x){
        System.out.println(x);
       
    }
    public void methodA(Object x){
        System.out.println("object"+x);
       
    }
        public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
        loading.methodA(4);
        loading.methodA(2.0f);
        loading.methodA(4.8d);
        }
}
Ans

11
Prepared by: Imdad Areeph
4.0
2.0
object4.8(because 4.8d can type cast to object)
9. What is out put of this program?
public class OverLoading {
    
    public void methodA(Object x){
        System.out.println("object "+x);
       
    }
    public void methodA(String x){
        System.out.println("string "+x);
       
    }
        public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
       
        loading.methodA("hello");
        loading.methodA(3);
       
    }
}

Ans
string hello
object 3
10. What is out put of this program?
public class OverLoading {
       public void methodA(Object x){
        System.out.println("object "+x);
        }
    public void methodA(int x){
        System.out.println("string "+x);
         }
        public static void main(String a[])
    {
        OverLoading loading= new OverLoading();
              loading.methodA("hello");
              loading.methodA(loading);
              loading.methodA(6.0f);
         }
}

Ans
object hello
object OverLoading@1bc4459
object 6.0

12
Prepared by: Imdad Areeph
11. What is out put of this program?
public class A  {
    public void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


public void testA(){
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();
    b.testA();
    A a = new B();
    a.testA();
    B b1=new A();
    b1.testA();
}
}
Ans
Compile time error in B b1=new A();
sub class reference can not refer super class object
12. What is out put of this program?
public class A  {
    public void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


public void testA(){
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();
    b.testA();
    A a = new B();
    a.testA();
    }
}

13
Prepared by: Imdad Areeph
Ans
Class B
Class B
13. What is out put of this program?
public class A  {
    public void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


public void testA(String srr){
    System.out.println(srr);
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();
    b.testA();
    A a = new B();
    a.testA();
    }
}

Ans
class A
class A
14. What is out put of this program?
public class A  {
    public void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


 void testA(){
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();
    b.testA();
    

14
Prepared by: Imdad Areeph
    }
}

Ans
Compilation error in class B,becaue over ridding method can't be narrow scope that described in super class
15. What is out put of this program?
public class A  {
    private void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


 public void testA(){
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();
    b.testA();
    
    }
}

Ans
Class B
Because private method can't override. The method testA(), we are declaring in B class, its itself one new method
16. What is out put of this program?
public class A  {
    final void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


 public void testA(){
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();

15
Prepared by: Imdad Areeph
    b.testA();
    
    }
}

Ans
Final method can't override, so it'll give compilation error in class B
17. What is out put of this program?
public class A  {
    public void testA(){
        System.out.println("class A");
    }
}

public class B extends A{


 public static void testA(){
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]){
    B b= new B();
    b.testA();
    
    }
}

Ans
Static method can't hide, again it'll give compilation error in class B
18. What is out put of this program?
public class A  {
    public void testA() throws Exception{
        System.out.println("class A");
    }
}
public class B extends A{
 public void testA(){
    System.out.println("Class B");
}
}
public class OverRiddingInJava {
public static void main(String ar[]){
    B b= new B();
    b.testA();
  A a=new B(); a.testA(); 

16
Prepared by: Imdad Areeph
    }
}

Ans
Compilation error : Unhandled exception type Exception , in a.testA()
19. What is out put of this program?
public class A  {
    public  void testA()throws Exception{
        System.out.println("class A");
    }
}

public class B extends A{


public void testA()throws ClassNotFoundException{
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]) throws Exception{
    B b= new B();
    b.testA();
    A a= new B();
    b.testA();
        }
}

Ans:
Class B
Class B
20. What is out put of this program?
public class A  {
    public  void testA()throws IOException{
        System.out.println("class A");
    }
}

public class B extends A{


public void testA()throws ClassNotFoundException{
    System.out.println("Class B");
}
}

public class OverRiddingInJava {


public static void main(String ar[]) throws Exception{
    B b= new B();
    b.testA();
    A a= new B();

17
Prepared by: Imdad Areeph
    b.testA();
        }
}

Ans: Compilation erro in class B. Because overridding method should throw, same exception or subclass excetion or no exception
21. What is dynamic method ditchpatch?
Ans:This is occours in case of over-ridding and super class and subclass having same method. Super class reference refers to sub class object
and calls the subclass method.
Its  runtime binding or late binding.
Example
package com;
public class A {
public void test(){
    System.out.println(" I am in Class A");
}
}
package com;
public class B extends A{
public void test()
{
    System.out.println("I am in class B");
}
}
package com;
public class TestDynamicMethodDispatch {
    public static void main(String ar[])
    {
        A a= new B();
        a.test();
    }
}

O/P
I am in class B

Modifiers Related Questions


1. Which class declaration is wrong, for top level class?
public class A
private class A
class A
static class A
final class A
abstract class A
transient class A
volatile class A
Ans: Top level class only can public, final and abstact, so others are wrong.
2.  What are wrong about variable declaration?
      a transient int i;

18
Prepared by: Imdad Areeph
      b volatile int j;
     c private int x;
     d public int y;
     e final int z;
     f abstact int a;
Ans: a,b,c,d correct, e and f wrong because final variable always initalised so we have to write final int z=7;
abstact modifier can not asign to variable.
3.  What are the wrong statement about method declaration?
public void methodA()
private void methodA()
final void methodA()
static void methodA()
volatile void methodA()
transient void methodA()
abstact void methodA();
Ans: Except volatile and transient other are correct, but if we are declaring method as abstact that class must be abstact
4. Can I write main method as  private static void main(String a[])?
Ans: You won't get any compilation error, but JVM can not consider as main method. It will be a new method.
5. Can we write main method like this?
 public static final void main(String ar[])
Ans: yes we can
6.  Can we wtire a calss like?
public final abstract class A
Ans :no
Because, class is final means we can't extend that calss else where, but abstract means, it needs to be subclassed
7.  Can I write one method like this
 final static void methodA()
Ans: we can
8. What is difference between interface and abstact class?

Interface Abstract Class


Interface is by default abtract in nature, means all the methods declare Here we have to explicitily
in interface is implicitily abstact, no nead to write abstact declare as abstact.

Contais all type of method


Contains only abstact methods and final variable
and and variable

We can extend oneabstact


We can implements no of interfaces in a class
class

If we are implementing interface we have to override all


Not necessary
implementation methods

We can keep onedefault


We can not keep constructor
constructor

9. What are similarities between abstact class and interface?


Ans Both we can't instantiate.
10.  In which case we should use interface and which case abstact class?
11. Which lines are wrong?
 final static void methodA(){
         public int i=8;//1
         private int j=8;//2
         int x=6;//3
         static int y=9;//4

19
Prepared by: Imdad Areeph
         final int z=8;//5
    }
Ans: only 3 and 5 are true and rest of lines is now allowed inside method.
12. What is the use of volatile modifier?
Ans:The volatile modifier applies only to instance variables .
13. What is transient modifiier in java?
Ans:Transient variable can not be serialized.
14. What is Java Serialization?
Ans:Serialization is a process to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again
For that we have to implements Serializable interface.
15. What is marker interface?
Ans:Interface which doesn't contain any method or variable called as marker interface. Exemple Serializable

String Related Questions


1. What is immutable class?
Ans:Immutable class is class, whose object's state cannot be modified after it is created.Like in java String is an immutable class. After creating its
object, it  can't be modified.
For example
String str=new String("xyz");
str=str.concat("abc");
When we are tring to concat "abc" with "xyz", its crating a new object altogether, its no more pointing preveous object.
2. Write piece of code for creating an  immutable class?
Ans:There are various way to create immutable class, this is one of them
final class ImmutableClass {
    private  String name;
20
Prepared by: Imdad Areeph
    public String getName() {
        return this.name;
    }
    public ImmutableClass(String name) {
       // do something here , to initialize the String name
    this.name=name;
}
}
3. What is difference between
String s="abc" and String s= new String("abc");?
Ans:String s="abc"; means its creating one reference not an object and it will store in stack.
String s=new String("abc"); means its creating one string object and it'll store in heap.
4. String s= new String("abc");
How many objects will be creating here?
Ans:Two objects. One is through new String() another when we are passing "abc" to string object.
5. What is output of this program?
package com;
public class TestString {
    public static void main(String a[]){
        String str="abc";
        String str1=new String("abc");
        System.out.println(str==str1);
       
    }
 
}
Ans: false
6. What is output of this program?
package com;
public class TestString {
    public static void main(String a[]){
        String str="abc";
        String str1=new String("abc");
        System.out.println(str.equals(str1));
        }
 }
Ans: true
7. What is output of following program?
package com;
public class TestString {
    public static void main(String a[]){
        String str1=new String("abc");
        String str2=new String("abc");
        System.out.println(str1==str2);
        }
 }
Ans: false

21
Prepared by: Imdad Areeph
8. What is output of following program?
package com;
public class TestString {
    public static void main(String a[]){
        String str1="abc";
        String str2="abc";
        System.out.println(str1==str2);
        }
 }
Ans: true
9. What is difference between "==" and "equals()" methods?
Ans: equal() method checks weather two String contains same content or not.
Like String s1=new Sting("xyz")
          String s2 ="xyz"
When I'll write s1.equals(s2), answer is true.
The operator "==", compares  the references, whether two reference pointing to same object or not.
In above example If I'll write
s1==s2
Answer is false;
If I'll write like
String s="asd";
String s1="asd";
In this case ,s==s1 is true.
10. What is difference between String Buffer , String Builder and String Buffer?
Ans: String: String is immutable in nature. We can't modify String object after creating.
 String Builder: Its mutable and  synchronized (which means it is thread safe and it can be used in multi threaded envirnoment). So its
perpermance is slower than String Buffer.
 String Buffer: Its also mutable and not synchronized. So perfermance wise its better that String Bulider.
11. When We should use String and when StringBuffer?
When the requirement like what ever is there in object should not cheange that case we should go for String.
But when we have to change frequently in object, in that case , we should go for StringBuffer.
For example String s="some";
                s= s.concat("com");
In this case two object will unnecessarily create, and it will take more memory space as well.
Bu in case of StingButter, when we'll write
               StringBuffer sb= new StringBuffer("some");
                sb=sb.append("come");
It's all together one object.

22

You might also like