Search Placementyogi.com
 

Difficult Static Keyword in Java Multiple Choice Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

class MyClass 
{
	static int j=func1();
	static int i=10;
	static int func1()
	{
		return i;
	}
	public static void main(String[] args) 
	{
		System.out.println("i="+i);
		System.out.println("j="+j);
	}
}
A.

i=10

j=0

B.

i=10

j=10

C.

Compilation Error

D.

Run Time Error

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

This was a bit tricky!!

j=10 !! But Why?? Either it should have give a Compilation Error or printed j=10 !

This strange behavior is because a class is loaded in 2 phases:

1. In the first phase the static members are stored into the memory .i.e. only 

static int j;

static int i;

are executed.

In this phase i and j are initialized to 0.

 

2. In the second phase, the static members are initialized. i.e. the following statements are executed in the 2nd phase.

j=func1();

i=10;

 

Since in this case func1() was called in the 2nd phase and by that time i=10 had not executed So the value of i was 0 and this was copied to j.

 
 

Banner to LogicGuns.com

Hide Page Information

Java Difficult Static Keyword Multiple Choice Questions and Answers for Written Round Interview

Here you can find Java Difficult Static Keyword Multiple Choice Questions with answers and explanation. We have a vast collection of Multiple Choice Questions on Difficult Static Keyword in Java that will help you a lot!

Why learn Java Difficult Static Keyword?

Difficult Static Keyword are an integral part of Java and many Multiple Choice Questions in the written round are asked from this topic.In this section you can learn and practice Java Difficult Static Keyword (Multiple Choice Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get Java Difficult Static Keyword Multiple Choice Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Java Difficult Static Keyword Multiple Choice Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for Java Difficult Static Keyword, We have quiz Multiple Choice Questions with answers to help you test yourself..

\n

Where can I get Java Difficult Static Keyword Interview Multiple Choice Questions and Answers (objective type, multiple choice)?

Here you can find objective type Java Difficult Static Keyword Multiple Choice Questions and answers for interview and entrance examination.Multiple Answers and true or false type Multiple Choice Questions are also provided.

How to solve Java Difficult Static Keyword problems?

You can easily solve all kind of Multiple Choice Questions based on Java Difficult Static Keyword by practicing the exercises (including shortcut Difficult Static Keyword to solve problems) given below.