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
Question
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
Solution
The output of the code snippet will be 10.
Here's the step by step explanation:
- A class named
MyClassis defined. - Inside
MyClass, a static integer variablevalueis declared and initialized with the value 10. - A non-static method
printValue()is defined which prints the value of the static variablevalue. - In the
mainmethod, an objectobjofMyClassis created. - Using the object
obj, the methodprintValue()is called. SinceprintValue()prints the value of the static variablevalue, andvalueis 10, 10 is printed.
So, the correct answer is b.10.
Similar Questions
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
What is the output of the following code?public class Test { int x = 10; public static void main(String args[]) { System.out.print(x); } static { System.out.println(x + 10); }}Options: Pick one correct answer from below10 20errorexceptionNone of the above
What is the output of the following code?public class Solution { public void display() { int x = 10; } public static void main(String args[]) { System.out.println(x); }}Options: Pick one correct answer from below100Compile time errorRuntime error
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 is the output of the following code snippet?class Parent { static void print() { System.out.println("Parent"); }}class Child extends Parent { static void print() { System.out.println("Child"); }}public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.print(); }}Question 2Answera.Parentb.Childc.Runtime errord.Compilation error
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.