[go: up one dir, main page]

0% found this document useful (0 votes)
73 views17 pages

Non-Static Programs

The document contains 26 code snippets with questions about their output. The code snippets demonstrate concepts like object initialization order, static initialization blocks, constructor calls, static vs non-static methods and variables, and class loading order in Java.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views17 pages

Non-Static Programs

The document contains 26 code snippets with questions about their output. The code snippets demonstrate concepts like object initialization order, static initialization blocks, constructor calls, static vs non-static methods and variables, and class loading order in Java.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.what is the output of following code?

class Test

int a ;

Test()

System.out.println("Constructor");

System.out.println("Non-static block");

public static void main(String[] args)

System.out.println("main method");

new Test();

2.what is the output of following code?

class Test

public static void main(String[] args)

{
Test.fun();

static void fun()

System.out.println("User method...");

3.what is the output of following code?

class Test

public static void main(String[] args)

new Test();

Test()

System.out.println("Constructor");

4.what is the output of following code?

class Test
{

static

System.out.println("Class loading...");

Test()

System.out.println("Object initialization process....");

System.out.println("Object creation process....");

public static void main(String[] args)

new Test();

new Test();

new Test();

5.what is the output of following code?

class Test

{
static

System.out.println(this);

System.out.println(this);

6.what is the output of following code?

class Test

Test( )

System.out.println(this);

System.out.println(this.hashCode());

public static void main(String args[ ])

new Test( );

7.what is the output of following code?


class Test

int a ;

public static void main(String args[ ])

new Test();

System.out.println(this.a);

this.a = 100 ;

Test()

System.out.println(this.a);

8.what is the output of following code?

class Test

Test( )

System.out.println("Object address inside constructor : "+this);

public static void main(String args[ ])

{
Test obj = new Test();

System.out.println("Object address inside main : "+obj);

9.what is the output of following code?

class Employee

int eno ;

String ename ;

float esal ;

public static void main(String[] args)

Employee e = new Employee();

System.out.println("Eno : "+e.eno);

System.out.println("Ename : "+e.ename);

System.out.println("Esal : "+e.esal);

10.what is the output of following code?

class Test

static

{
Test obj = new Test();

System.out.println(a);

public static void main(String[] args)

System.out.println(obj);

11.what is the output of following code?

class Test

static Test a ;

static

System.out.println(Test.a);

Test.a = new Test() ;

public static void main(String[] args)

System.out.println(Test.a);

12.what is the output of following code?


class Test

static Test a = new Test() ;

static

System.out.println(Test.a);

public static void main(String[] args)

System.out.println(Test.a);

13.what is the output of following code?

class Test

static Test a ;

static

System.out.println(Test.a);

Test.a = new Test() ;

public static void main(String[] args)

System.out.println(Test.a);

}
}

14.what is the output of following code?

class Test

static Test a ;

public static void main(String[] args)

System.out.println(Test.a);

Test.initialize();

System.out.println(Test.a);

static void initialize()

Test.a = new Test() ;

15.what is the output of following code?

class Test

static Test a ;

public static void main(String[] args)

System.out.println(Test.a);
Test.a = Test.initialize();

System.out.println(Test.a);

static Test initialize()

return new Test() ;

16.what is the output of following code?

class Test

static Test a ;

public static void main(String[] args)

System.out.println(Test.a);

Test.initialize(new Test());

System.out.println(Test.a);

static void initialize(Test x)

Test.a = x ;

17.what is the output of following code?


class Test

int a ;

public static void main(String[] args)

Test obj = new Test();

System.out.println("a value : "+obj.a);

18.what is the output of following code?

class Test

int a ;

Test(int a)

this.a = a ;

public static void main(String[] args)

Test obj = new Test(10);

System.out.println("a value : "+obj.a);

}
19.what is the output of following code?

class Test

int a ;

Test(int x)

a=x;

public static void main(String[] args)

Test obj = new Test(10);

System.out.println("a value : "+obj.a);

20.what is the output of following code?

class Test

int a ;

Test(int a)

a=a;

public static void main(String[] args)

{
Test obj = new Test(10);

System.out.println("a value : "+obj.a);

21.what is the output of following code?

class Test

public static void main(String[] args)

Test.fun();

this.fun();

Test obj = new Test();

obj.fun( );

void fun()

System.out.println("non-static user method....");

22.what is the output of following code?

class Test
{

static Test obj = new Test() ;

static

Test.obj.fun();

public static void main(String[] args)

Test.obj.fun();

void fun()

System.out.println("non-static user method....");

23.what is the output of following code?

class First

public static void main(String[] args)

Second addr = new Second();

addr.check();

class Second
{

void check()

System.out.println("Second class non-static method....");

24.what is the output of following code?

class First

static Second addr = new Second();

public static void main(String[] args)

First.addr.check();

class Second

void check()

System.out.println("Second class non-static method....");

25.what is the output of following code?


class PrintStream

void println(String s)

class System

static PrintStream out ;

class MainClass

public static void main(String[] args)

System.out.println();

26.what is the output of below program?

class Example

int x=10;

int y=20;

public static void main(String[] args)

{
System.out.println("main");

Example e1=new Example();

You might also like