Facebook

adsense

Tuesday, 31 March 2015

TEST YOUR JAVA SKILLS


1) Which is true about Static Initialization Block?

A. We can use only static members of a class inside the Static Initialization Block.
B. Static Initialization Blocks are mainly used to initialize static fields of a class.
C. Static Initialization Block is the first block to be executed after class is loaded in the memory.
D. All of the above.


2) Where the static initialization blocks are stored in the memory?


3) What will be the output of this program?

class A
{
static int i;

static
{
System.out.println(1);

i = 100;
}
}

public class StaticInitializationBlock
{
static
{
System.out.println(2);
}

public static void main(String[] args)
{
System.out.println(3);

System.out.println(A.i);
}
}

4) What happens when you compile the below class?

class A
{
int i;

static
{
System.out.println(i);
}
}

5) Is the below code written correctly?

class A
{
static
{
static
{
System.out.println(1);
}
}
}
6) How many static initialization blocks are there in the below Class A?

class A
{
static int a = 50;

static
{
a = 50;
}

static
{
a = 50;
}
}

7) What will be the outcome of the following program?

public class A
{
static
{
System.out.println(1);
}

static
{
System.out.println(2);
}

static
{
System.out.println(3);
}

public static void main(String[] args)
{
A a;
}
}

8) What will be the output of this program?

class A
{
static int first;

static String second;

static
{
System.out.println(1);

first = 100;
}

static
{
System.out.println(2);

second = "SECOND";
}
}

public class StaticInitializationBlock
{
static
{
System.out.println(3);
}

public static void main(String[] args)
{
System.out.println(4);

System.out.println(A.first);

System.out.println(A.second);
}
}

9) What will be the output of the below program?

class A
{
static int i;

static
{
i = 100;

System.out.println(1);
}

static void staticMethod()
{
System.out.println(i);

System.out.println(2);
}
}

public class B
{
static
{
System.out.println(3);
}

public static void main(String[] args)
{
System.out.println(4);

System.out.println(A.i);

A.staticMethod();
}
}

10) What is the difference between SIB and IIB?

11) Can we invoke static method inside the SIB – Static Initialization Block?


12) What will be the output of following program?

class A
{
{
System.out.println(1);
}

public A()
{
System.out.println(2);
}

public static void main(String[] args)
{
System.out.println(3);

A a = new A();
}
}

13) What will be the output of the below program?

public class A
{
{
System.out.println("First");
}

{
System.out.println("Second");
}

{
System.out.println("Third");
}

public A()
{
System.out.println("Fourth");
}

public static void main(String[] args)
{
System.out.println("Fifth");

A a = new A();
}
}

14) Which one of these are executed first while creating an object to the class?

a) Statements of IIB block
b) Statements of constructor


15) What will be the outcome of the below program?

public class A
{
int i;

{
System.out.println("IIB-1");

i = 100;
}

{
System.out.println("IIB-2");

System.out.println(i);

i = 200;
}

public static void main(String[] args)
{
System.out.println("main");

A a = new A();

System.out.println(a.i);
}
}

       

Try to solve these conceptual question of java to know how much java do you know, and write your answers in the comment below like, "Q no) answer" or click on answer link , I will post the answer after somedays if I will get a large number of clicks on answer links 

            


No comments:

Post a Comment