DRAG DROP
You are developing an application that includes a class named Kiosk. The Kiosk class
includes a static property named Catalog. The Kiosk class is defined by the following code
segment. (Line numbers are included for reference only.)
You have the following requirements:
Initialize the _catalog field to a Catalog instance.
Initialize the _catalog field only once.
Ensure that the application code acquires a lock only when the _catalog object must be
instantiated.
You need to meet the requirements.
Which three code segments should you insert in sequence at line 09? (To answer, move the
appropriate code segments from the list of code segments to the answer area and arrange
them in the correct order.)
Explanation:
Note:
* If the _catalog field is not instantiated (== null) then need to create a new instance
(_catalog = new Catalog();
* If the _catalog field is instantiated (not null/!= null) then lock it.
the explanation is correct but the 1st selected code segment is wrong,
1st code segment should be
if (_catalog == null) _catalog = new Catalog();
see similar question 41
you are correct else null will be never assigned
so ideally if :
if(_catalog==null) _catalog = new _catalog();
if(_catalog!=null)
{
lock(_lock)
…..
….
}
wrong : https://msdn.microsoft.com/en-us/library/ff650316.aspx
the answer is
if(_catalog == null)
lock(_lock)
if(catalog == null) _catalog = new Catalog();
It is said lock as to be acquiered when catalog must be instanciated, not when t is already instanciated, and the new catalog is called inside the lock, with another testto avoid any thread access issue
Correct!
Not a well framed question… better to skip it.
Not good questions but
the answer is:
if(_catalog == null)
lock(_lock)
if(catalog == null) _catalog = new Catalog();