class Sample{ private double num = 100; private int square(int a){ return a*a; }} public class Test{ public static void main(String args[]){ Sample obj = new Sample(); System.out.println(obj.num); System.out.println(obj.square(10)); } }Select one:a.Compile time errorb.100c.Run time errord.Executes but no output
Question
class Sample{ private double num = 100; private int square(int a){ return a*a; }} public class Test{ public static void main(String args[]){ Sample obj = new Sample(); System.out.println(obj.num); System.out.println(obj.square(10)); } }Select one:a.Compile time errorb.100c.Run time errord.Executes but no output
Solution
The correct answer is a. Compile time error.
The reason for this is that the variables and method in the Sample class are declared as private. Private variables and methods are only accessible within the class they are declared. In the Test class, you are trying to access these private members of the Sample class, which is not allowed. This will cause a compile time error.
Similar Questions
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
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.Runtime Exceptionb.a=10c.Compile Time Error
Determine the outputpublic class Trial{public static void main(String[] args) { int arr[4]={}; System.out.print(arr[0]); }}Select one:a.Garbage errorb.Compile time errorc.Runtime errord.0
Consider the below code snippet and determine the output.class Student{ private int studentId; private float average;}class Test{ public static void main(String a[]) { Student s=new Student(); s.studentId=123; System.out.println(s.studentId); }}Select one:a.Compile time errorb.1c.0d.Any value
Analyze the following code: public class Test { public static void main(String[] args) { int n = 2; square(n); } void square(int n) { System.out.print(n + " square is " + n * n); }}The program has a compile error because method square(int n) does not return a value.The program has a compile error because method square(int n) is not declared static.The program runs fine.The program prints2 square is 4
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.