AP Computer Science A • Unit 1.1

Introduction to Algorithms, Programming, and Compilers

Detailed answer explanations for the Unit 1.1 multiple-choice practice questions.

Q1B
Q2C
Q3B
Q4C
Q5D
Q6D
Q7A
Q8C
Q9A
Q10A
Q11B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

The following statement is intended to print the circumference of a circle with radius 4, using 3.14 for pi. What kind of error does this code most clearly demonstrate?

System.out.println(3.14 * 4);
ASyntax error
BLogic error
CRun-time error
DNo error; the statement prints 25.12.
Correct answer: B. The statement runs without crashing, but it does not produce the intended circumference.
Explanation

A logic error occurs when the program runs without crashing but produces the wrong result. This statement follows Java's grammar and completes, so the problem is in what it calculates, not in whether it can run.

  • A is not correct: a syntax error prevents compilation, and nothing here breaks Java's grammar.
  • C is not correct: a run-time error interrupts a program during execution; this statement is not interrupted.
  • D is not correct: the statement does not print 25.12, so there is an error.
Q2

A Java compiler reports the following message. Which of the following is the best first step?

error: unclosed string literal
System.out.println("Welcome);
                     ^
AChange the class name to Welcome.
BRemove the call to println.
CAdd the missing quotation mark after Welcome.
DKeep changing the line until the error message disappears.
Correct answer: C. The message describes the error: the string opens with a quotation mark but never closes.
Explanation

A compiler message usually contains a description of the error, the line where the compiler noticed it, and sometimes a caret symbol pointing near the problem. Read the whole message before you touch anything: the caret marks where the compiler gave up, which is not always where you went wrong. Here the description names an unclosed string literal, so closing the string is the first step.

  • A is not correct: the message describes an unclosed string literal, not a class-name problem.
  • B is not correct: removing the call deletes the statement instead of correcting the unclosed string literal.
  • D is not correct: debugging should be a controlled investigation, not a series of random changes.
Q3

Consider the following code segment. Which statement best describes this code segment?

System.out.println(42 / 0);
AThe code fails to compile because division by zero is a syntax error.
BThe code compiles, but it fails during execution when the division is evaluated.
CThe code runs and prints 0 because the denominator is 0.
DThe code has a logic error because the program runs but produces the wrong result.
Correct answer: B. The code compiles, but it fails during execution because Java cannot evaluate integer division by zero.
Explanation

A run-time error occurs when a program starts but fails during execution. Nothing here breaks Java's grammar, so the compiler can create the bytecode; the failure appears when the division is evaluated.

  • A is not correct: a syntax error is an error that breaks Java's grammar and prevents compilation. Nothing in this statement breaks Java's grammar, so the compiler can create the bytecode; the failure appears only when the JVM evaluates the division.
  • C is not correct: Java cannot evaluate integer division by zero, so no value is printed.
  • D is not correct: a logic error means the program runs without crashing but produces the wrong result; here execution is interrupted.
Q4

Consider these two declarations, each stored in a matching file. Which statement about the declarations is correct?

public class ScoreBoard { }
public class scoreBoard { }
ABoth declarations name the same class because Java ignores letter case.
BOnly scoreBoard is legal because class names must begin with lowercase letters.
CScoreBoard and scoreBoard are different identifiers.
DBoth declarations produce a run-time error.
Correct answer: C. Java is case-sensitive, so changing a letter's case changes the identifier.
Explanation

Identifiers are names chosen for parts of the program. Both declarations are legal when each is stored in the matching file, but they name two different classes.

  • A is not correct: Java is case-sensitive, so changing a letter's case changes the identifier. ScoreBoard and scoreBoard differ by one letter's case, and that is enough to make them two separate names.
  • B is not correct: beginning with an uppercase letter is the standard naming convention for Java class names, not a rule that makes the other declaration illegal. Both declarations are legal when each is stored in the matching file.
  • D is not correct: a run-time error interrupts a program during execution; a declaration stored in its matching file is legal.
Q5

Which of the following best describes the role of the main method in a Java program?

AIt translates Java source code into bytecode.
BIt stores all variables used by the program.
CIt prevents syntax errors from occurring.
DIt is where program execution begins.
Correct answer: D. The main method is the entry point of a Java program.
Explanation

When a complete Java program runs, execution begins in the main method. The statements inside main are the statements that run first.

  • A is not correct: the compiler translates source code into bytecode, not the main method.
  • B is not correct: variables may be declared in many different scopes, not stored by main as a special container.
  • C is not correct: syntax errors are detected by the compiler; main does not prevent them.
Q6

Which of the following best describes what happens when a Java program is compiled?

AThe Java Virtual Machine runs the program and produces its output.
BThe program is tested with sample input and checked for logic errors.
CThe source file is renamed so that it matches the public class name.
DThe source code is checked for syntax errors and translated into bytecode.
Correct answer: D. Compilation checks Java source code and translates it into bytecode.
Explanation

During compilation, Java source code is checked for syntax errors. If compilation succeeds, the source code is translated into bytecode that can be run by the Java Virtual Machine.

  • A is not correct: running a Java program happens in two stages. First the compiler checks the source code and prepares it; then the JVM runs the prepared code. This choice describes the second stage, not compilation.
  • B is not correct: the compiler does not test the program with sample input or guarantee that the logic is correct.
  • C is not correct: the source file already has to match the public class: because the public class is named MyFirstClass, the source file must be named MyFirstClass.java. Compilation does not change that name; it translates the source code into bytecode.
Q7

Which of the following best describes an algorithm?

AA step-by-step procedure for solving a problem
BA Java program that must contain a main method
CAn error message produced by the compiler
DA file created after Java source code is compiled
Correct answer: A. An algorithm is a precise sequence of steps used to solve a problem.
Explanation

An algorithm does not have to be written in Java. It can be described in plain English, pseudocode, diagrams, or numbered steps, as long as the steps are clear enough to follow.

  • B is not correct: a Java program may implement an algorithm, but the algorithm itself is not necessarily a Java program.
  • C is not correct: a compiler message reports a problem in the source code. It usually contains a description of the error, the line where the compiler noticed it, and sometimes a caret symbol pointing near the problem. That is evidence for debugging, not a procedure for solving a problem.
  • D is not correct: compilation produces bytecode, an instruction format that the JVM can understand. An algorithm is an idea, not a programming language, and it can be described before any code is written.
Q8

Which statement about pseudocode is most accurate?

AIt must follow exact Java syntax.
BIt is used only after a program has compiled successfully.
CIt helps describe the logic of an algorithm before writing code.
DIt is the bytecode executed by the Java Virtual Machine.
Correct answer: C. Pseudocode helps plan the logic before writing real Java code.
Explanation

Pseudocode is language-independent. Its purpose is to describe the intended steps clearly without requiring exact Java syntax.

  • A is not correct: pseudocode is a compact, language-independent way to write an algorithm. It uses familiar words to express the intended actions without requiring the punctuation or exact keywords of a programming language.
  • B is not correct: pseudocode is a way to plan logic before writing Java syntax, so it does its work before compilation rather than after it.
  • D is not correct: bytecode is the instruction format the compiler produces for the JVM. Pseudocode is written for people, so that the actions and their order are clear enough that the plan can later be translated into code.
Q9

Which of the following is a literal?

A"Ready!"
BMyFirstClass
Cmain
Dpublic
Correct answer: A. A literal is a fixed value written directly in source code rather than a name that refers to a value.
Explanation

"Ready!" is a String literal. The quotation marks show where the text begins and ends; they are part of the source code but are not printed as part of the output.

  • B is not correct: MyFirstClass is an identifier, a name chosen for part of the program.
  • C is not correct: main is an identifier, not a value written directly in the source code.
  • D is not correct: public is a reserved keyword; Java assigns it a fixed meaning.
Q10

Why is the order of steps important in an algorithm?

AChanging the order can change the result produced by the algorithm.
BEvery algorithm must use Java syntax.
CThe compiler automatically rearranges steps into the correct order.
DSteps are executed alphabetically by their names.
Correct answer: A. Sequencing matters because later steps can depend on the results of earlier steps.
Explanation

An algorithm is an ordered procedure. Moving a calculation, decision, or output step can change the result or make the procedure invalid.

  • B is not correct: an algorithm is an idea, not a programming language. You can describe one in everyday language, with a diagram, as numbered steps, or in pseudocode.
  • C is not correct: a compiler follows the written program; it does not repair the algorithm's order.
  • D is not correct: sequencing means the instructions are arranged in a deliberate order. An algorithm is read from top to bottom, and each step is carried out in the order shown.
Q11

After a Java source file is compiled successfully, what happens when the program is executed?

AThe compiler converts bytecode into pseudocode.
BThe JVM runs the compiled bytecode.
CThe source file is renamed as a .class file.
DThe Java Virtual Machine checks for logic errors.
Correct answer: B. During execution, the Java Virtual Machine runs the bytecode produced by compilation.
Explanation

The source file remains a .java file. Successful compilation creates a separate compiled .class file containing bytecode for the JVM to run.

  • A is not correct: the compiler works in the other direction: it checks the source code and translates it into bytecode. Pseudocode is a way to plan logic before writing Java syntax, so it is not something the two stages of running a program produce.
  • C is not correct: compilation creates a new class file rather than renaming the source file.
  • D is not correct: the JVM executes the program; it does not guarantee that its logic is correct.