Assignment Statements and Input MCQ Practice

Question 1 of 10

A theater clerk updates a crate count. What text does this Java code display? Ignore a final trailing newline.

int crates = 19;
crates = crates - 7;
crates = crates + 3;
System.out.print(crates);
  1. A. 19
  2. B. 12
  3. C. 15
  4. D. 22

Question 2 of 10

A shuttle logs fuel and a separate reserve copy. What text does this Java code display? Ignore a final trailing newline.

int fuel = 14;
int reserve = fuel;
fuel = fuel - 5;
System.out.print(reserve);
  1. A. 14
  2. B. 9
  3. C. 5
  4. D. 0

Question 3 of 10

Trace these assignments in order. What text is displayed? Ignore a final trailing newline.

int left = 8;
int right = 3;
left = left - right;
right = left + right;
System.out.print(left);
System.out.print(" ");
System.out.print(right);
  1. A. 8 3
  2. B. 5 8
  3. C. 5 5
  4. D. 8 8

Question 4 of 10

A clock converts minutes into 20-minute blocks. What text does this Java code display? Ignore a final trailing newline.

int minutes = 47;
int hours = minutes / 20;
int leftover = minutes % 20;
System.out.print(hours);
System.out.print(":");
System.out.print(leftover);
  1. A. 2:7
  2. B. 2:27
  3. C. 47:20
  4. D. 2.35:7

Question 5 of 10

These variables have already been declared. Which assignment is compatible?

int seats;
double cost;
String label;
Choice Attempted assignment
A seats = 12.5;
B cost = 12;
C label = 12;
D 12 = seats;
  1. A. seats = 12.5;
  2. B. cost = 12;
  3. C. label = 12;
  4. D. 12 = seats;

Question 6 of 10

A visitor badge is copied, then the original reference is cleared. What text does this Java code display? Ignore a final trailing newline.

String badge = "visitor";
String extra = badge;
badge = null;
System.out.print(extra);
  1. A. null
  2. B. visitor
  3. C. visitornull
  4. D. (no characters)

Question 7 of 10

A locker kiosk interacts with a visitor. Which classification of the table is correct?

Event Information flow
The visitor types locker 14 enters the program
The kiosk prints Open locker 14 leaves the program
  1. A. Typing the locker number is input; printing the message is output.
  2. B. Both events are output because text appears on a screen at some point.
  3. C. Both events are input because a locker number is involved.
  4. D. Printing the message is input and typing the number is output.

Question 8 of 10

Contract: Input.readInt() reads the next integer the user types and returns it as int. Each call consumes one integer. Remembering Scanner methods is not required. The user types 11, then 4. What text does this code display?

int width = Input.readInt();
int height = Input.readInt();
int tiles = width * height;
System.out.print(tiles);
  1. A. 15
  2. B. 44
  3. C. 11
  4. D. 4

Question 9 of 10

A program prints Enter jersey number. A fan types 23. Contract: Input.readInt() reads the next integer the user types and returns it as int. Which statement is required before the program can compute 23 + 1?

  1. A. The prompt itself stores 23 in a variable.
  2. B. The typed 23 is automatically named jersey.
  3. C. The program must read the typed value, for example int jersey = Input.readInt();, so a compatible int is stored.
  4. D. The program must print 24 before any read occurs.

Question 10 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 5, then 5, then 2. What text does this code display?

int a = Input.readInt();
int b = Input.readInt();
a = Input.readInt();
System.out.print(a + b);
  1. A. 10
  2. B. 7
  3. C. 12
  4. D. 2