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.Any valueb.0c.Compile time errord.1
Question
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.Any valueb.0c.Compile time errord.1
Solution
The correct answer is c. Compile time error.
The reason is that the variables studentId and average in the Student class are declared as private. Private variables are only accessible within the same class, they cannot be accessed directly from outside the class. In the Test class, we are trying to access and modify the studentId variable directly which is not allowed. Hence, it will throw a
Similar Questions
Select the correct answerWhat is the output of the following program?public class Score{ public static void main(String[] args) { double data = 222.423; int sum = 3; float value = 2.1f; System.out.println(data + sum + value); }}Options222222.423227.52299222.5
Select the correct answerWhat will be the output of the following Java code?class average { public static void main(String args[]) { double num[] = { 1.9 , 5.4 , 9.5 , 1.2, 5.2 ,0.5}; double result; result = 0; for (int i = 0; i < 6; ++i) result = result + num[i]; System.out.print(result/6); } }Options3.94999999999999973.9999999443.3943.4999999999
Determine the output:public class A { public static void main(String argv[]){ int ary[]=new int[]{1,2,3}; System.out.println(ary[1]); }}Select one:a.2b.1c.Compilation Error:incorrect syntax
ind the output of the below Java program?class Student { int id; Student(int id) { this.id = id; }}public class Test { public static void main(String[] args) { Student s1 = new Student(1001); System.out.println(s1.equals(null)); }}A) trueB) falseC) errorD) 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
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.