Assignment Statements and Input MCQ Practice

Question 1 of 10

A vending hopper updates a coin count. What text does this Java code display? Ignore a final trailing newline.

int coins = 31;
coins = coins + 9;
coins = coins / 4;
System.out.print(coins);
  1. A. 10
  2. B. 40
  3. C. 7
  4. D. 10.0

Question 2 of 10

A weather station copies a high temperature, then both variables change. What text does this Java code display? Ignore a final trailing newline.

int high = 16;
int low = high;
high = 21;
low = low + 2;
System.out.print(high);
System.out.print(",");
System.out.print(low);
  1. A. 21,18
  2. B. 21,16
  3. C. 16,18
  4. D. 23,18

Question 3 of 10

A student tries to rotate three values without an extra variable. What text does this Java code display? Ignore a final trailing newline.

int a = 4;
int b = 9;
int c = 15;
a = b;
b = c;
c = a;
System.out.print(a);
System.out.print(" ");
System.out.print(b);
System.out.print(" ");
System.out.print(c);
  1. A. 9 15 4
  2. B. 4 9 15
  3. C. 9 15 9
  4. D. 15 4 9

Question 4 of 10

A garden plot stores an area, then a side length is changed. What text does this Java code display? Ignore a final trailing newline.

double length = 3.0;
double width = 2.5;
double area = length * width;
length = 10.0;
System.out.print(area);
  1. A. 25.0
  2. B. 7.5
  3. C. 30.0
  4. D. 3.0

Question 5 of 10

Assume boolean open has been declared. Which assignment is compatible?

  1. A. open = "true";
  2. B. open = 1;
  3. C. open = true;
  4. D. true = open;

Question 6 of 10

Two String variables start by referring to the same text, then one is reassigned. What text does this Java code display? Ignore a final trailing newline.

String lane = "east";
String alias = lane;
alias = "west";
System.out.print(lane);
  1. A. west
  2. B. east
  3. C. eastwest
  4. D. null

Question 7 of 10

Which event supplies input to a greenhouse controller?

  1. A. The controller prints a humidity warning on a panel.
  2. B. A sensor sends a humidity reading that the program reads and stores.
  3. C. The controller draws a line graph of earlier readings.
  4. D. A speaker plays a recorded reminder to water plants.

Question 8 of 10

Contract: Input.readInt() reads the next integer the user types and returns it as int. Each call consumes one integer. The user types 18. What text does this code display?

int raw = Input.readInt();
int packed = raw / 5;
int spare = raw % 5;
System.out.print(packed);
System.out.print(" leftover ");
System.out.print(spare);
  1. A. 3 leftover 3
  2. B. 3 leftover 15
  3. C. 18 leftover 5
  4. D. 3.6 leftover 3

Question 9 of 10

The same compiled program is run twice. Each run calls Input.readInt() once, stores the result in tickets, and prints tickets * 2. Why can the two runs display different numbers?

  1. A. Assignment always reverses direction on the second run.
  2. B. The two runs may receive different typed integers, so tickets can store different values.
  3. C. int variables randomly change type between executions.
  4. D. System.out.print stores a new input value each time it is used.

Question 10 of 10

Contract: Input.readWord() reads the next whitespace-delimited word the user types and returns it as String. Each call consumes one word. The user types maple. What text does this code display?

String tree = Input.readWord();
String saved = tree;
tree = "pine";
System.out.print(saved);
  1. A. pine
  2. B. maple
  3. C. maplepine
  4. D. null