nerdexam
Oracle

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.

Java Basics

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)
  • A
    2% (1)
  • B
    2% (1)
  • C
    93% (52)
  • D
    4% (2)

Why each option

This question tests the correct syntax for creating a new object instance of a class in Java.

AStudent student1;

`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.

BStudent student1 = Student.new();

`Student.new()` is not a valid Java syntax for object instantiation. The `new` keyword is used directly before the constructor call.

CStudent student1 = new Student();Correct

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.

DStudent student1 = Student();

`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

#object instantiation#new keyword#constructors

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice