Question 24Asked in DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 3 (5:00 PM)

class Person { private String name;

class Person { private String name; public Person(String name) { this.name = name; } private void displayName() { System.out.println("Name: " + name); } } public class Test { public static void main(String[] args) { Person p = new Person("John"); p.displayName(); } } What will happen when the above code is compiled and executed?
  1. ACompilation Error: The method displayName is not visible in Test.
  2. BRuntime Error: The method displayName cannot be called.
  3. CThe code will compile and display Name: John.
  4. DCompilation Error: The field name is not visible in Test.
Show answerHide answer

Correct answer: (A) Compilation Error: The method displayName is not visible in Test.

Explanation

displayName() is private in Person, so it cannot be called from class Test. The compiler reports that displayName() has private access in Person.

From DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 3 (5:00 PM)

This is question 24 of DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 3 (5:00 PM). See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.