1Z0-803 · Question #78
View the exhibit: public class Student { public String name = ""; public int age = 0; public String major = "Undeclared"; public boolean fulltime = true; public void display() { System.out.println("Na
The correct answer is C. Student student1 = new Student();. This question tests the correct syntax for creating a new object instance of a class in Java.
Question
View the exhibit:
public class Student { public String name = ""; public int age = 0; public String major = "Undeclared"; public boolean fulltime = true; public void display() { System.out.println("Name: " + name + " Major: " + major); } public boolean isFullTime() { return fulltime; } } Which line of code initializes a student instance?
Options
- AStudent student1;
- BStudent student1 = Student.new();
- CStudent student1 = new Student();
- DStudent student1 = Student();
How the community answered
(56 responses)- A2% (1)
- B2% (1)
- C93% (52)
- D4% (2)
Why each option
This question tests the correct syntax for creating a new object instance of a class in Java.
`Student student1;` declares a reference variable but does not create or initialize an object. The variable `student1` would hold a `null` value at this point.
`Student.new()` is not a valid Java syntax for object instantiation. The `new` keyword is used directly before the constructor call.
In Java, an object is initialized using the `new` keyword followed by the class constructor. `new Student()` calls the default constructor for the `Student` class, which allocates memory for the new object and returns a reference to it, which is then assigned to the `student1` reference variable.
`Student()` without the `new` keyword is not sufficient for creating an object; it resembles a method call or a constructor call missing the object creation operator.
Concept tested: Java object instantiation and constructors
Topics
Community Discussion
No community discussion yet for this question.