What is the output of the following code snippet?class MyClass { static int count = 0; public MyClass() { count++; } public static int getCount() { return count; }}public class Main { public static void main(String[] args) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(); System.out.println(MyClass.getCount()); }}Question 3Answera.0b.1c.2d.Compilation error
Question
What is the output of the following code snippet?class MyClass { static int count = 0; public MyClass() { count++; } public static int getCount() { return count; }}public class Main { public static void main(String[] args) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(); System.out.println(MyClass.getCount()); }}Question 3Answera.0b.1c.2d.Compilation error
Solution
The output of the code snippet will be 2.
Here's the step by step explanation:
-
The
MyClassclass has a static variablecountwhich is initialized to 0. Static variables are shared among all instances of a class, meaning all instances of the class have access to the same variable. -
The constructor of
MyClassincrements thecountvariable by 1 every time a new instance of the class is created. -
In the
mainmethod of theMainclass, two instances ofMyClassare created (obj1andobj2). Each time an instance is created, the constructor ofMyClassis called, which increments thecountvariable by 1. -
Therefore, after creating two instances, the
countvariable will be 2. -
The
getCountmethod is a static method that returns the current value of thecountvariable. -
So, when
System.out.println(MyClass.getCount());is executed, it prints the current value ofcount, which is 2.
So, the correct answer is c.2.
Similar Questions
What will be the output of the following code snippet?public class MyClass { static int value = 10; public void printValue() { System.out.println(value); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.printValue(); }}Question 4Answera.0b.10c.Compilation errord.Runtime exception
What is the Output of following Java Program?abstract class Demo{ public int a; Demo() { a = 10; } abstract public void set(); }class Test extends Demo{ final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }}Select one:a.Compile Time Errorb.a=10c.Runtime Exception
What's the output from the following statements?public class Foo { static int i = 0; static int j = 0; public static void main(String[] args) { int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); } k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); }}
Consider the following program.public class Main { public static void main(String args[]) { String[] names = new String[2]; names[0] = "Alice"; names[1] = "Bob"; names[2] = "Charlie"; System.out.println(names[2]); }}Which of the following is an output of the above program? a. Charlie b. Bob c. Alice d. Compilation Error
What is the output of the above program?class Num { Num(double x ){ System.out.println( x ) ; } }public class Test extends Num { public static void main(String[] args){ Num num = new Num( 2 ) ; } }02.0Compile time error None of the above
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.