Find the output:class PrintNumber { public static void main(String args[]) { System.out.println(print()); } int print() { return 30; }}
Question
Find the output:class PrintNumber { public static void main(String args[]) { System.out.println(print()); } int print() { return 30; }}
Solution
The code you provided is written in Java. However, it will not compile because the print() method is not declared as static. In Java, a static method can only call other static methods directly. Non-static methods can only be called through an instance of the class.
Here is the corrected code:
class PrintNumber {
public static void main(String args[]) {
System.out.println(print());
}
static int print() {
return 30;
}
}
When you run this program, it will print 30 to the console. This is because the print() method returns the integer 30, and this value is then passed to System.out.println(), which prints it.
Similar Questions
Find the output of the following code.class Output { public static void main(String args[]) { int arr[] = {11, 21, 31, 41, 51}; for ( int i = 1; i < arr.length - 2; ++i) System.out.println(arr[i] + " "); } }*11 21 31 4111 21 3121 3131 41 51
e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}
Show what this program prints. Be exact and complete. Can you explain the behavior of each print statement?1234567public class Test { public static void main(String[] args) { System.out.println("39 + 3"); System.out.println(39 + 3); System.out.println("39" + 3); }}
What is the output of the following code?public class Solution { public static void main(String args[]) { int x = 7--2; System.out.println(x); }}
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
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.