nerdexam
Oracle

1Z0-808 · Question #63

Given the exhibit: public class Student { public String name = ""; public int age = 0; public String major = "Undeclared"; public boolean fulltime = true; public void display() {…

The correct answer is C. Student student1 = new Student(). Option C uses the correct Java syntax for instantiating an object: the new keyword followed by the class constructor call Student(), which allocates memory and initializes the object's fields to their default values. A is wrong because Student student1; only declares a…

Java Basics

Question

Given 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

(59 responses)
  • A
    2% (1)
  • B
    2% (1)
  • C
    90% (53)
  • D
    7% (4)

Explanation

Option C uses the correct Java syntax for instantiating an object: the new keyword followed by the class constructor call Student(), which allocates memory and initializes the object's fields to their default values.

  • A is wrong because Student student1; only declares a reference variable - it doesn't allocate any object, leaving student1 as null.
  • B is wrong because Student.new() is not valid Java syntax; new is a keyword, not a method you call on a class.
  • D is wrong because Student() without new reads like a method call, not object construction - Java requires new to invoke a constructor.

Memory tip: Think of new as placing an order - you must say new ClassName() to create something new, otherwise you're just holding an empty label (declaration) or writing nonsense syntax.

Topics

#object instantiation#new keyword#constructor#class declaration

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice