Assignment Statements and Input

Help Questions

AP Computer Science A › Assignment Statements and Input

Questions 1 - 9
1

What is the result of the following method call given the input deposit 50.0?

public class BankAccount {

private double balance;

public BankAccount(double startBalance) {

    balance = startBalance;

}

public void deposit(double amount) {

    balance = balance + amount;

}

public double getBalance() {

    return balance;

}

}

BankAccount account = new BankAccount(100.0);

account.deposit(50.0);

double newBalance = account.getBalance();

System.out.println(newBalance);

100.0

Compilation error: deposit must return double

50.0

150.0

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program requires students to trace through object state changes, illustrating how instance variables are modified through method calls. Choice C is correct because the account starts with balance 100.0, the deposit method adds 50.0 to make it 150.0, and getBalance() returns this updated value. Choice A is incorrect because it shows only the deposit amount rather than the total balance, demonstrating a misunderstanding of how the deposit method modifies the existing balance. To help students: Emphasize tracing object state through method calls and understanding how instance variables persist between method calls. Encourage drawing memory diagrams to visualize object state changes, and provide feedback on understanding encapsulation and state modification.

2

Identify the error in the following assignment statement and suggest a correction in this student record program.


Scanner input = new Scanner(System.in);

Student student = new Student("Ava");

System.out.print("Quiz score: ");

double quizScore;

quizScore == input.nextDouble();

student.addScore(quizScore);

Change to: quizScore = input.nextLine();

Change to: quizScore = input.nextDouble();

Change to: quizScore.equals(input.nextDouble());

Change to: student.addScore(input.nextDouble);

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program requires students to identify that the code uses == (equality operator) instead of = (assignment operator) to store the input value. Choice A is correct because it correctly uses the assignment operator = to store the result of input.nextDouble() into the quizScore variable. Choice C is incorrect because equals() is a method for comparing objects, not for assignment, demonstrating confusion between comparison and assignment operations. To help students: Emphasize the critical difference between = (assignment) and == (comparison) operators in Java. Encourage students to trace through their code mentally, asking "Am I storing a value or comparing values?" at each step.

3

Given the object and method below, what is the output when toFahrenheit is called with input 25.0?


public class TempConverter {

    public double toFahrenheit(double celsius) {

        return celsius * 9.0 / 5.0 + 32.0;

    }

}

Scanner input = new Scanner(System.in);

TempConverter converter = new TempConverter();

double celsius = 25.0;

double fahrenheit = converter.toFahrenheit(celsius);

System.out.println(fahrenheit);

25.0

57.0

77.0

298.0

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program requires students to trace through a temperature conversion calculation where 25.0 Celsius is converted to Fahrenheit using the formula (C × 9/5 + 32). Choice B is correct because the calculation yields 25.0 × 9.0 / 5.0 + 32.0 = 45.0 + 32.0 = 77.0 degrees Fahrenheit. Choice A (57.0) is incorrect because it appears to result from an arithmetic error, possibly calculating 25 + 32 without the multiplication factor. To help students: Emphasize the importance of carefully tracing through mathematical expressions step by step. Encourage students to verify calculations by substituting values and following the order of operations precisely.

4

Identify the error in the following assignment statement and suggest a correction in this bank account withdrawal.


Scanner input = new Scanner(System.in);

BankAccount account = new BankAccount(200.0);

System.out.print("Withdraw amount: ");

double amount = input.nextDouble();

account.withdraw = amount;

System.out.println(account.getBalance());

Change to: account.withdraw(amount);

Change to: withdraw(account, amount);

Change to: account.withdraw = input.nextDouble();

Change to: account.withdraw(amount, account);

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program incorrectly attempts to assign a value directly to a method name (account.withdraw = amount) instead of calling the withdraw method. Choice A is correct because it properly calls the withdraw method with parentheses and passes the amount as an argument, following correct Java method invocation syntax. Choice B is incorrect because it still attempts to use assignment syntax with a method, which is not valid in Java - methods must be called, not assigned to. To help students: Emphasize that methods represent actions and must be invoked with parentheses, while fields represent data and can be assigned values. Practice distinguishing between method calls (object.method()) and field access (object.field).

5

How would you modify the code to correctly store user input in a variable for this student average calculation?


Scanner input = new Scanner(System.in);

Student student = new Student("Mia");

System.out.print("Test 1 score: ");

double test1;

student.addScore(test1);

Assume the user enters a decimal number.

Insert: double test1 = student.addScore();

Insert: test1 = nextDouble; before addScore

Insert: test1 = input.nextLine(); before addScore

Insert: test1 = input.nextDouble(); before addScore

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program declares a variable test1 but never assigns it a value before using it in the addScore method call, which would cause a compilation error. Choice A is correct because it assigns the result of input.nextDouble() to test1 before the variable is used, properly initializing it with user input. Choice B is incorrect because nextLine() returns a String, not a double, causing a type mismatch when assigning to a double variable. To help students: Emphasize that all variables must be initialized before use in Java. Encourage students to trace variable declarations and ensure each variable receives a value before being used in method calls or expressions.

6

What is the result of the following method call given input deposit 50.0 in this bank account code?


public class BankAccount {

    private double balance;

    public BankAccount(double start) { balance = start; }

    public void deposit(double amount) { balance = balance + amount; }

    public double getBalance() { return balance; }

}

BankAccount account = new BankAccount(100.0);

double depositAmount = 50.0;

account.deposit(depositAmount);

System.out.println(account.getBalance());

50.0

100.0

150.0

5000.0

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program creates a BankAccount with initial balance 100.0, deposits 50.0, and then prints the balance. Choice C is correct because the deposit method adds the amount to the existing balance: 100.0 + 50.0 = 150.0. Choice D (5000.0) is incorrect because it suggests multiplication rather than addition, demonstrating a misunderstanding of how the deposit method updates the balance. To help students: Emphasize tracing through object state changes step by step. Encourage students to mentally track instance variables as methods modify them, understanding that balance = balance + amount means adding to the existing value.

7

Identify the error in the following assignment statement and suggest a correction in this library book checkout.


Scanner input = new Scanner(System.in);

Book book = new Book("1984", 4);

System.out.print("Copies to checkout: ");

int count = input.nextInt();

int remaining = book.checkout(count);

System.out.println("Remaining: " + remaining);

Assume checkout is declared as public void checkout(int n).

Change to: void remaining = book.checkout(count);

Change to: int remaining = book.checkout();

Change to: book.checkout(count); and print book.getCopies()

Change to: int remaining = checkout(book, count);

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the code attempts to assign the result of a void method to an int variable, which is a type mismatch error since void methods don't return values. Choice A is correct because it calls the void checkout method without trying to capture a return value, then separately calls getCopies() to get the remaining count for printing. Choice D is incorrect because it attempts to declare a variable of type void, which is not allowed in Java - void is only used to indicate a method returns nothing. To help students: Emphasize understanding method signatures and return types before using methods. Practice identifying when methods return values versus when they perform actions without returning data.

8

How would you modify the code to correctly store user input in a variable for this temperature converter?


Scanner input = new Scanner(System.in);

TempConverter converter = new TempConverter();

System.out.print("Celsius: ");

double celsius = input.nextInt();

double fahrenheit = converter.toFahrenheit(celsius);

System.out.println(fahrenheit);

Assume the user may enter 18.5.

Change to: double celsius = input.nextDouble();

Change to: double celsius = input.nextDouble;

Change to: double celsius = input.nextLine();

Change to: int celsius = input.nextInt();

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program uses nextInt() to read user input but needs to handle a decimal value (18.5), which would cause an InputMismatchException. Choice A is correct because it uses nextDouble() to properly read decimal input, allowing the program to handle values like 18.5 correctly. Choice D is incorrect because it's missing parentheses after nextDouble, which is a syntax error - all method calls in Java require parentheses even when no arguments are passed. To help students: Emphasize matching Scanner methods to expected input types. Practice identifying potential runtime errors from type mismatches and choosing appropriate input methods based on the data being processed.

9

Given the object and method below, what is the output when getFullTitle is called with input "The"?


public class Book {

    private String title;

    public Book(String t) { title = t; }

    public String getFullTitle(String prefix) {

        return prefix + " " + title;

    }

}

Book book = new Book("Hobbit");

String result = book.getFullTitle("The");

System.out.println(result);

Hobbit The

The Hobbit

TheHobbit

The Hobbit

Explanation

This question tests AP Computer Science A skills in using assignment statements and handling input within the context of objects and methods. Assignment statements in Java store values in variables, and input handling involves reading and processing user input to perform operations with objects and methods. In this scenario, the program creates a Book object with title "Hobbit" and calls getFullTitle with prefix "The", which concatenates the prefix, a space, and the title. Choice B is correct because the method returns "The" + " " + "Hobbit" = "The Hobbit" with exactly one space between the words. Choice A is incorrect because it shows no space between the words, suggesting the student missed the space character in the concatenation expression. To help students: Emphasize careful attention to string concatenation details, including spaces and punctuation. Encourage students to trace through string operations character by character to avoid missing subtle elements like spaces.