Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.
15 / 15 published solutions
Question 2 MSQ · 4.0 marks
Which of the following statement(s) is/are true regarding activation records used during function
calls?
6406537022708.
An activation record is removed from the stack immediately after a local
variable is declared.
6406537022709.
Local variables of a function are typically stored inside its activation record.
6406537022710.
A return address is stored in the activation record so that control can return to
the caller after the execution of the called function.
6406537022711.
All activation records in the stack correspond only to recursive function calls.
6406537022712.
When a function calls another function, the control link in the callee's
activation record points to the activation record of the caller.
Published solution
**1. Identify the frame:** Each active function invocation normally has an activation record holding its local execution state, including local variables and information needed to return. Ordinary and recursive calls both use frames.
**2. Follow a nested call:** A new frame is associated with the callee, while its control or dynamic link identifies the caller’s frame. The callee frame remains active until the call returns; declaring a local variable does not remove it.
**Answer: B, C, E — Local variables, return address and caller control link.**
**Convention note:** This is the conceptual activation-record model; a JVM may optimize physical frame layout and represent return information internally.
A: **Incorrect:** False: a frame is not popped when a local variable is declared.
B: **Correct:** True: local variables are typically part of the invocation’s frame.
C: **Correct:** True: return information resumes execution in the caller.
D: **Incorrect:** False: nonrecursive calls also have activation records.
E: **Correct:** True: the control link points to the caller’s activation record.
**1. Match the method signature:** Object.toString() is public, takes no arguments and returns String. A must override that signature and return the required formatted text.
**2. Trace both prints:** Explicit l1.toString() returns “Dell : 16GB”. Printing l1 as an Object also uses its toString() representation, so the same line appears twice.
**Answer: A — public String toString() returning brand + " : " + ram + "GB".**
A: **Correct:** The public no-argument String-returning override supplies the required representation.
B: **Incorrect:** A void return type is incompatible with Object.toString(), and a void call cannot be printed as a value.
C: **Incorrect:** This overload does not override toString(); moreover Object has no brand or ram fields.
D: **Incorrect:** Package-private access would weaken the inherited public method; super.toString() also gives the default representation.
generates compilation error because a variable of type
cannot refer to an object of type
.
6406537022724.
generates compilation error because a variable of type
cannot refer to an object of type
.
Published solution
**1. Check each declaration:** LINE 1 tries to override a final method; LINE 2 accesses a private superclass field; LINE 3 assigns a superclass object to a subclass reference without a valid conversion. Each is a compilation problem.
**2. Identify the false claim:** LINE 4 assigns new MergeSort(200) to a SortingAlgorithm reference, a valid widening reference conversion. Therefore the claim that LINE 4 is an error is false.
**Answer: D — The assertion that LINE 4 is a compilation error is false.**
A: **Incorrect:** True statement, so do not select: final timeComplexity() cannot be overridden.
B: **Incorrect:** True statement, so do not select: private complexityFactor is inaccessible directly in MergeSort.
C: **Incorrect:** True statement, so do not select: a general SortingAlgorithm object is not a MergeSort.
D: **Correct:** False statement, so select: a SortingAlgorithm variable may refer to a MergeSort object.
**1. Check the reference type:** SmartLight can implement both interfaces and inherit off() from SwitchOff. However, s1 is declared as SwitchOn, which exposes on() but not off().
**2. Resolve the call:** s1.on() is legal and would dispatch to SmartLight.on(). The later expression s1.off() fails at compile time because member lookup uses the declared type. The program therefore produces no normal output.
**Answer: B — Compile-time error at LINE 2: SwitchOn has no off() method.**
A: **Incorrect:** SmartLight needs no abstract modifier because both interfaces supply default bodies and on() is overridden.
B: **Correct:** SwitchOn does not declare or inherit off(), so LINE 2 fails.
C: **Incorrect:** These lines would require a suitable type or cast for the off() call first.
D: **Incorrect:** The program fails to compile, and on() would dispatch to the override rather than the interface default.
**1. First outer iteration:** Starting at i=0, print “1 2”. The inner do-while prints “3 4” at i=0 and i=1, leaving i=2.
**2. Second outer iteration:** Since 2<3, print “1 2” again. The inner do-while executes once even though i is already 2, prints “3 4” and increments i to 3. Both loops then end.
**Answer: C — Lines: 1 2; 3 4; 3 4; 1 2; 3 4.**
A: **Incorrect:** This omits the second inner iteration of the first outer pass.
B: **Incorrect:** The outer loop must run again when i reaches 2.
C: **Correct:** The five output lines match the two inner iterations followed by one.
D: **Incorrect:** This places the extra inner iteration in the wrong outer pass.
**1. Track the array object:** Passing a to update copies its reference, not the array. Updating x changes the shared array to [5,7,9,11], and the returned reference assigned to b points to that same array.
**2. Evaluate the expression:** Both a[2] and b[2] read 9. Their sum is 18.
**Answer: B — 18.**
A: **Incorrect:** This incorrectly leaves both values at their original 6.
B: **Correct:** Both references read the updated value 9, giving 18.
C: **Incorrect:** Nine is one element, not the sum of two element reads.
D: **Incorrect:** This incorrectly treats one reference as retaining the old value 6.
Choose the correct option regarding the output of the code.
6406537022745.
This code generates output:
6406537022746.
This code generates output:
6406537022747.
This code generates output:
6406537022748.
This code generates a compile time error.
Published solution
**1. Check abstract obligations:** Sensor declares abstract calibrate(). TemperatureSensor overrides showData() but never implements calibrate(), and it is not declared abstract.
**2. Conclude before tracing output:** This makes TemperatureSensor an invalid concrete class. Compilation fails before the call to showData() can execute.
**Answer: D — Compile-time error: TemperatureSensor does not implement calibrate().**
A: **Incorrect:** The abstract-method error prevents this output.
B: **Incorrect:** The abstract-method error prevents this output.
C: **Incorrect:** These two lines describe showData() only after the missing calibrate() implementation is repaired.
D: **Correct:** A concrete subclass must implement every inherited abstract method.
Identify the correct option for the code given above.
6406537022749.
This code generates output:
6406537022750.
This code generates output:
6406537022751.
This code generates a compile time error, since the implementations of
from
and
are in conflict with each other.
6406537022752.
This code generates a compile time error since
is not implemented in
class
Published solution
**1. Resolve inherited methods:** Machine’s concrete start() takes precedence over Device’s default start(). Robot implements the required abstract process(); Device supplies default stop().
**2. Follow the calls:** r.start() prints “Machine starts”; r.process() prints “Robot processing”; r.stop() prints “cannot stop device”.
**Answer: A — Machine starts; Robot processing; cannot stop device.**
A: **Correct:** This is the class method, Robot override and interface default in call order.
B: **Incorrect:** The interface default start() loses to the inherited concrete class method.
C: **Incorrect:** There is no conflict requiring an override because a concrete superclass method wins.
D: **Incorrect:** Robot inherits a complete default implementation of stop().
**1. Distinguish object copying from reference copying:** d2 is a new Dataset with its own name field, but its features field points to the same array as d1.features. d3 is simply another reference to d1.
**2. Apply the changes:** Assigning d2.name changes only d2. Assigning d2.features[0] changes the shared array, so every Dataset reference reads “Salary” there. The three lines are “CustomerData, Salary”, “SalesData, Salary”, “CustomerData, Salary”.
**Answer: A — CustomerData, Salary; SalesData, Salary; CustomerData, Salary.**
A: **Correct:** This preserves separate name fields and the shared features array.
B: **Incorrect:** The copy constructor does not clone the array, so d1 and d3 also read Salary.
C: **Incorrect:** Changing d2.name does not change d1.name; the array is shared as well.
D: **Incorrect:** All names do not change together; d1 and d3 retain CustomerData.
followed by runtime error
Reason: A method inside class
is invoking method
inside class
,
whereas
is invoking another method in class
.
6406537022759.
This program generates compiler error at
Reason: A method inside class
is invoking method
inside class
,
whereas
is invoking another method in class
.
6406537022760.
This program generates the output:
Published solution
**1. Construct the callback:** Passenger implements RideCallback, so passing this into new Driver(this) is valid. Driver stores a reference to that Passenger through the interface type.
**2. Follow execution:** bookRide("Taxi") calls startRide(), which prints “Driver: Driving Taxi...” and “Driver: Taxi has arrived.” It then invokes the stored callback, printing “Passenger: Taxi arrived!”. Mutual references do not themselves cause an error.
**Answer: D — Driver: Driving Taxi...; Driver: Taxi has arrived.; Passenger: Taxi arrived!.**
A: **Incorrect:** Passenger is a RideCallback, so the constructor argument is compatible.
B: **Incorrect:** Calling a method back on the caller is legal and does not cause the proposed runtime error.
C: **Incorrect:** The interface declares onRideArrived(), so the callback invocation compiles.
D: **Correct:** All three messages print in the order shown.
generates compilation error because a variable of type
cannot refer to an object of type
.
6406537022718.
This code generates the below output followed by runtime error at
because there is ambiguity in which
method is being invoked.
6406537022719.
This code generates the output:
6406537022720.
This code generates the output:
Published solution
**1. Choose overloads:** processData() and processData(String) are distinct signatures. The zero-argument method is inherited unchanged, so it prints “Processing general research data”.
**2. Apply overriding:** The String overload is overridden by GenomeDataProcessor. Dynamic dispatch therefore prints “Analyzing genome sequence file: covid_sequence.txt” for the second call, even through a DataProcessor reference.
**Answer: D — Processing general research data; Analyzing genome sequence file: covid_sequence.txt.**
A: **Incorrect:** Assigning a subclass object to a superclass reference is legal.
B: **Incorrect:** Argument count and type identify the signatures; no ambiguity exists.
C: **Incorrect:** The String overload dispatches to the subclass override, not the superclass body.
D: **Correct:** The no-argument inherited method runs first, then the String override.
**1. Check and cast the argument:** equals receives Object, so its fields cannot be accessed as Module fields until it is cast. D first tests instanceof Module and then explicitly casts obj to Module.
**2. Evaluate this example:** Both module IDs are 101 and both names refer to the same interned literal “Authentication”. Thus the displayed == test is true for these inputs and the program prints “m1 and m2 are same”.
**Answer: D — instanceof check followed by Module m=(Module)obj.**
**Convention note:** The constructor is represented by a placeholder comment and is assumed to initialize both fields as stated. For a correct general value-based equals implementation, compare strings with moduleName.equals(m.moduleName) or Objects.equals, not ==; equal text constructed as separate String objects need not share a reference. A matching hashCode is also required when such objects are used as hash keys.
A: **Incorrect:** obj still has declared type Object, so its Module fields cannot be accessed directly.
B: **Incorrect:** Object does not declare moduleName or moduleId, so this does not compile.
C: **Incorrect:** Module m=obj needs an explicit narrowing cast even after the ordinary instanceof test.
D: **Correct:** The type test and cast permit field access, and the literal references match for this example.
**1. Recover an accessible type:** getNextBook() has declared return type Object, so read() is not available until the result is cast. Book is accessible outside Library and declares read(); private inner Novel is not accessible there.
**2. Dispatch the call:** Casting each returned Novel to Book is valid, and read() dynamically invokes Novel.read(). With an iterator advancing through the three entries, the output is “Reading: Book A”, then B, then C.
**Answer: A — ((Book)iter.getNextBook()).read();.**
**Convention note:** The constructor and iterator methods are shown as implementation placeholders. The intended answer assumes they initialize currentIndex, return whether another book exists, and return/advance to the next book; without those missing method bodies the literal listing is incomplete.
A: **Correct:** The accessible abstract superclass exposes read(), and the actual Novel supplies its body.
B: **Incorrect:** Novel is a private member of Library and cannot be named directly here.
C: **Incorrect:** Library is not the returned book type and does not declare read().
D: **Incorrect:** Object does not declare read(), so the uncast expression fails member lookup.
**1. Initialize the superclass state:** empid and name belong privately to Employee. Manager must pass the supplied values to Employee’s two-argument constructor using super(empid_,name_).
**2. Trace printing:** Employee stores 101 and Nutan, then Manager stores HR. Manager.print() calls super.print() and prints department, producing “101 : Nutan : HR”.
**Answer: C — super(empid_, name_);.**
**Convention note:** The multiple public top-level classes are treated as separate source files, as customary for the question; each public class must be stored in its correspondingly named file.
A: **Incorrect:** Constructors are not invoked by writing the superclass name as an ordinary method.
B: **Incorrect:** this(empid_,name_) would require a matching two-argument Manager constructor, which is absent.
C: **Correct:** super(...) invokes the desired Employee constructor.
D: **Incorrect:** Manager cannot assign Employee’s private fields directly.
**1. Separate lookup from dispatch:** m.displayType() is declared in Matrix and dispatches to DiagonalMatrix, printing “Diagonal Matrix”. But determinant() exists only in the subclass, so m needs a cast before that member can be called.
**2. Check the casts:** The actual object is a DiagonalMatrix. Both the instanceof-guarded cast in C and the direct cast in D safely call determinant(), printing “Determinant is product of diagonal elements”.
**Answer: C, D — C and D — guarded or direct cast to DiagonalMatrix.**
A: **Incorrect:** Matrix does not declare determinant(), so this call fails at compile time.
B: **Incorrect:** determinant() is an instance method and cannot be invoked through the class name alone.
C: **Correct:** The type test succeeds; the cast exposes the subclass method.
D: **Correct:** The direct downcast succeeds because the actual object has the required subclass type.