Search Placementyogi.com
 

Java Static Keyword Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

class MyClass 
{
	static char i;
	static int j;

	public static void main(String[] args) 
	{
		System.out.println("i="+i+",j="+j);
	}
}
A.

i=0,j=0

B.

i=null,j=0

C.

i= ,j=0

D.

Compilation Error

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

i= j=0

The default value of char is " " and for integer is 0.

 
2

What is the output of the following program?

class MyClass 
{
	int i;
	float j;

	public static void main(String[] args) 
	{
		System.out.println("i="+i+"j="+j);
	}
}
A.

i=0 j=0.0

B.

i=0 j=0.000000

C.

Compile Time Error

D.

None of the Above

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

main is a static method and so it can not access non static member variables

 
3

What is the output of the following program?

class MyClass 
{
	static int myStaticVar;
	static void func1()
	{
		myStaticVar++;
		System.out.println("Inside func1");
	}
	static void func2()
	{
		myStaticVar++;
		System.out.println("Inside func1");		
	}
	public static void main(String[] args) 
	{
		System.out.println(myStaticVar);
		func1();
		System.out.println(myStaticVar);
		func2();
		System.out.println(myStaticVar);
	}	
}
A.

0

Inside func1

1

Inside func1

2

B.

0

Inside func1

0

Inside func1

0

C.

Compilation Error

D.

None of the Above

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

All the functions have access to the static variable myStaticVar and can change it directly.

0

Inside func1

1

Inside func1

2

 
4

What is the output of the following program?

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

10

20

B.

20

20

C.

Compilation Error

D.

None of the Above

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

You can not re-declare a variable in Java .

C:\java_practice\src\static\MyClass.java:4: error: variable i is already defined

 in class MyClass

        int i=20;

            ^

1 error

 
5

What is the output of the following program?

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

20

20

Because changing local variable changes the value of static variable

B.

20

10

Because the local i is local to main() and static i is still 10

C.

Compilation Error

Because You can not re-declare the same variable

D.

10

10

Because both the "i" in println refers to the static member variable i.

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

20

10

Because the local i is local to main() and static i is still 10.

The static variable can be accessed by using the class name directly in this case.

Syntax :

ClassName.variableName

 
 

Banner to LogicGuns.com

Hide Page Information

Java Static Keyword Objective Questions and Answers for Written Round Interview

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

Why learn Java Static Keyword?

Static Keyword are an integral part of Java and many Objective Questions in the written round are asked from this topic.In this section you can learn and practice Java Static Keyword (Objective 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 Static Keyword Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Java Static Keyword Objective 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 Static Keyword, We have quiz Objective Questions with answers to help you test yourself..

\n

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

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

How to solve Java Static Keyword problems?

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