nerdexam
Oracle

1Z0-811 · Question #47

Given the code of Student.java: class Course { String courseName; } public class Student { String stuName; public static void main(String[] args) { Student s = new Student(); s.stuName = args[0]…

The correct answer is A. The commands: javac Student.java java Student Richard William Java are used to print Richard William is studying Java. Note: The stated answer (A) appears to contain an error - option D is the logically correct answer based on how Java handles command-line arguments. Since Course is defined in the same file as Student, running javac Student.java compiles both classes - no separate javac…

Java Basics

Question

Given the code of Student.java: class Course { String courseName; } public class Student { String stuName; public static void main(String[] args) { Student s = new Student(); s.stuName = args[0]; Course c = new Course(); c.courseName = args[1]; System.out.println(s.stuName + " is studying "+ c.courseName); } } Which statement is true?

Options

  • AThe commands: javac Student.java java Student Richard William Java are used to print Richard William is studying Java.
  • BThe commands: javac Course.java javac Student.java java Course java Student "Richard William" Java are used to print Richard William is studying Java.
  • CThe commands: javac Student.java java Student Richard William Java are used to print Richard William is studying Java.
  • DThe commands: javac Student.java java Student "Richard William" Java are used to print Richard William is studying Java.

How the community answered

(51 responses)
  • A
    75% (38)
  • B
    4% (2)
  • C
    8% (4)
  • D
    14% (7)

Explanation

Note: The stated answer (A) appears to contain an error - option D is the logically correct answer based on how Java handles command-line arguments.

Since Course is defined in the same file as Student, running javac Student.java compiles both classes - no separate javac Course.java step is needed, which eliminates B. To assign "Richard William" to args[0] as a single value, the shell requires quotes around it; without quotes, Richard, William, and Java become args[0], args[1], and args[2] respectively, meaning A and C would print "Richard is studying William", not "Richard William is studying Java". D is the command that correctly produces the expected output: java Student "Richard William" Java sets args[0]="Richard William" and args[1]="Java". B also fails because Course has no main method, so java Course would throw a NoSuchMethodError.

Memory tip: Quotes "..." in the terminal = one argument. No quotes = each space creates a new args[n]. When a name has a space, always ask: "did they quote it?"

If your exam key truly marks A as correct, it may be a typo in the original question - A and C appear identical as written, and neither produces the stated output. Raise it with your instructor.

Topics

#compilation#command-line arguments#array indexing#JVM execution

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice