A shop program contains the following fragment inside a Java method, with no other declarations using these names. Which statements about the fragment or the proposed changes are true? Consider each proposed change independently. Select all that apply.
int daily = 11; { int extra = 2; } { int extra = 6; }
The two declarations of extra are legal because their separate blocks give the variables non-overlapping scopes.
Adding System.out.println(extra); after both blocks is legal because Java retains the most recent declaration of that name.
Moving both extra declarations directly into the enclosing method block is illegal because the declarations would have overlapping scopes.
Adding int daily = 6; inside the second block is legal because opening another block permits redeclaring an enclosing local name.
Unlock full course notes by signing up
Track one count through separate blocks
A food bank tracks packed boxes using the following fragment inside a Java method. Find the number printed by the first print statement and the number printed by the second. Enter both counts in boxes as exact integers.
Put dependent declarations in a legal order
A packing program calculates how many boxes it can fill after reserving some items. Arrange these four Java statements in the only order that compiles when they appear consecutively inside the same method block. None of these variable names is declared elsewhere, and you may not change any statement.
Separate the desk count from the class count
A parcel desk uses the following Java class. The field named parcels belongs to the class; ParcelDesk.parcels explicitly accesses it. Assume report() is called once, with the field initially holding its declared value.
Enter the three printed counts: the saved starting count, the final local count, and the final field count. Give each as an exact integer number of parcels.
Repair a name inside its own initializer
A booking program should initialize a local seat count from the class's stored count, allowing for six unavailable seats. In this code, Hall.seats explicitly accesses the field declared in Hall.
Complete the diagnosis and repair. For the first gap, choose compilation or execution. For the second gap, choose local variable or class field. For the final gap, enter the field access using the class name, with no spaces.
The original program is rejected during ________ because the right-hand seats in the local declaration refers to the ________ before it has received a value. Keeping the local variable's name unchanged, replace only that right-hand seats with ________.