Which three code segments should you insert in sequence at line 09?

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.)

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.)

Answer:

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.



Leave a Reply 7

Your email address will not be published. Required fields are marked *


Mediterrano

Mediterrano

the explanation is correct but the 1st selected code segment is wrong,
1st code segment should be
if (_catalog == null) _catalog = new Catalog();

Mediterrano

Mediterrano

see similar question 41

vivek

vivek

you are correct else null will be never assigned

so ideally if :

if(_catalog==null) _catalog = new _catalog();
if(_catalog!=null)
{
lock(_lock)
…..
….
}

My'nD

My'nD

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

Saikat

Saikat

Not a well framed question… better to skip it.

zen

zen

Not good questions but
the answer is:
if(_catalog == null)
lock(_lock)
if(catalog == null) _catalog = new Catalog();