What is the output for the below code ? public class A { int k; boolean istrue; static int p; public void printValue() { System.out.print(k); System.out.print(istrue); System.out.print(p); } } public class Test{ public static void main(String argv[]){ A a = new A(); a.printValue();

  1. 0 false 0

  2. 0 true 0

  3. 0 0 0

  4. Compile error - static variable must be initialized before use.


Correct Option: A
Explanation:

To solve this question, the user needs to know about Java classes, instance variables, static variables, and method invocation.

The given program has two classes: A and Test. The A class defines three instance variables: k (of type int), istrue (of type boolean), and a static variable p (of type int). Class A also defines a method named printValue() that prints the values of k, istrue, and p, in that order.

In the main() method of the Test class, an object of class A is created and its printValue() method is called.

Since int and boolean instance variables are initialized to their default values of 0 and false respectively, and the static variable p is also initialized to 0, the output of the program will be:

A. 0 false 0

Option A is correct because the program will print the values of k (0), istrue (false), and p (0) in that order.

Option B is incorrect because istrue is initialized to false, not true.

Option C is incorrect because k and istrue are both non-static instance variables, and they will be initialized to their default values of 0 and false, respectively. The static variable p will also be initialized to 0.

Option D is incorrect because there is no compile error in the code.

The Answer is: A. 0 false 0

Find more quizzes: