nerdexam
Oracle

1Z0-803 · Question #75

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 D. A compile time error is generated.. Attempting to access and assign a value to a field that is not defined within a class will cause a compile-time error.

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; } } Given:

Public class TestStudent { public static void main(String[] args) { Student bob = new Student (); bob.name = "Bob"; bob.age = 18; bob.year = 1982; } } What is the result?

Options

  • Ayear is set to 1982.
  • Bbob.year is set to 1982
  • CA runtime error is generated.
  • DA compile time error is generated.

How the community answered

(30 responses)
  • A
    3% (1)
  • C
    3% (1)
  • D
    93% (28)

Why each option

Attempting to access and assign a value to a field that is not defined within a class will cause a compile-time error.

Ayear is set to 1982.

This statement describes a successful assignment, which cannot happen as the field `year` does not exist in the `Student` class, preventing successful compilation.

Bbob.year is set to 1982

This statement also describes a successful assignment to a non-existent field, which will cause a compilation error, not a successful assignment.

CA runtime error is generated.

The error occurs during compilation, not at runtime, because Java's strong type checking identifies the undefined field `year` before the code can ever be executed.

DA compile time error is generated.Correct

The `Student` class definition does not include a member variable named `year`. Therefore, attempting to assign a value to `bob.year` in the `TestStudent` class will cause a compile-time error because the Java compiler cannot find a matching field for `year` on the `Student` object.

Concept tested: Java class members and compile-time errors

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Topics

#compilation error#object fields#class members

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice