Given the code fragment:
Which code fragment, when inserted at line n1, enables the code to print Java Programming:1?
Given the code fragment:
Which code fragment, when inserted at line n1, enables the code to print Java Programming:1?
The correct code fragment to enable the code to print 'Java Programming:1' is 'System.out.println(c.name + “:” + Course.count);'. In this code, 'c.name' accesses the instance variable 'name' of the object 'c', which was assigned the value 'Java Programming'. 'Course.count' accesses the static variable 'count' of the class 'Course'. Since 'count' is incremented in the constructor when 'new Course("Java Programming")' is called, 'count' will be 1. This results in the output 'Java Programming:1'.
Since count is a static variable of Course class, can be used using class name
C is the correct answer. "count" is a class property, and can be accesed directly with the Class if needed. The other solutions are not valid